From 8b81208012c2d88df74e18579a8025c24305db8c Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 17 Jan 2019 23:19:51 -0500 Subject: [PATCH 1/3] Adding a test for checking if test files are missing. --- tests/missing-test-files.rs | 47 +++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 tests/missing-test-files.rs diff --git a/tests/missing-test-files.rs b/tests/missing-test-files.rs new file mode 100644 index 000000000..31d2dccff --- /dev/null +++ b/tests/missing-test-files.rs @@ -0,0 +1,47 @@ +use std::fs::{self, DirEntry}; +use std::io; +use std::path::Path; + +#[test] +fn test_missing_tests() { + explore_directory(Path::new("./tests")).unwrap(); +} + +/* +Test for missing files. + +Since rs files are alphabetically before stderr/stdout, we can sort by the full name +and iter in that order. If we've seen the file stem for the first time and it's not +a rust file, it means the rust file has to be missing. +*/ +fn explore_directory(dir: &Path) -> io::Result<()> { + let mut current_file = String::new(); + let mut files: Vec = fs::read_dir(dir)?.filter_map(Result::ok).collect(); + files.sort_by_key(|e| e.path()); + for entry in files.iter() { + let path = entry.path(); + if path.is_dir() { + explore_directory(&path)?; + } else { + let file_stem = path.file_stem().unwrap().to_str().unwrap().to_string(); + match path.extension() { + Some(ext) => { + match ext.to_str().unwrap() { + "rs" => current_file = file_stem.clone(), + "stderr" | "stdout" => { + assert_eq!( + file_stem, + current_file, + "{}", + format!("Didn't see a test file for {:}", path.to_str().unwrap()) + ); + }, + _ => continue, + }; + }, + None => {}, + } + } + } + Ok(()) +} From a3b3a54e930dec06935af37beae340a8f6a7b4ec Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 17 Jan 2019 23:50:30 -0500 Subject: [PATCH 2/3] Update to collect all the files then throw the error. --- tests/missing-test-files.rs | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/tests/missing-test-files.rs b/tests/missing-test-files.rs index 31d2dccff..f79abedc0 100644 --- a/tests/missing-test-files.rs +++ b/tests/missing-test-files.rs @@ -1,10 +1,22 @@ use std::fs::{self, DirEntry}; -use std::io; use std::path::Path; #[test] fn test_missing_tests() { - explore_directory(Path::new("./tests")).unwrap(); + let missing_files = explore_directory(Path::new("./tests")); + if missing_files.len() > 0 { + assert!( + false, + format!( + "Didn't see a test file for the following files:\n\n{}\n", + missing_files + .iter() + .map(|s| format!("\t{}", s)) + .collect::>() + .join("\n") + ) + ); + } } /* @@ -14,14 +26,15 @@ Since rs files are alphabetically before stderr/stdout, we can sort by the full and iter in that order. If we've seen the file stem for the first time and it's not a rust file, it means the rust file has to be missing. */ -fn explore_directory(dir: &Path) -> io::Result<()> { +fn explore_directory(dir: &Path) -> Vec { + let mut missing_files: Vec = Vec::new(); let mut current_file = String::new(); - let mut files: Vec = fs::read_dir(dir)?.filter_map(Result::ok).collect(); + let mut files: Vec = fs::read_dir(dir).unwrap().filter_map(Result::ok).collect(); files.sort_by_key(|e| e.path()); for entry in files.iter() { let path = entry.path(); if path.is_dir() { - explore_directory(&path)?; + missing_files.extend(explore_directory(&path)); } else { let file_stem = path.file_stem().unwrap().to_str().unwrap().to_string(); match path.extension() { @@ -29,12 +42,9 @@ fn explore_directory(dir: &Path) -> io::Result<()> { match ext.to_str().unwrap() { "rs" => current_file = file_stem.clone(), "stderr" | "stdout" => { - assert_eq!( - file_stem, - current_file, - "{}", - format!("Didn't see a test file for {:}", path.to_str().unwrap()) - ); + if file_stem != current_file { + missing_files.push(path.to_str().unwrap().to_string()); + } }, _ => continue, }; @@ -43,5 +53,5 @@ fn explore_directory(dir: &Path) -> io::Result<()> { } } } - Ok(()) + missing_files } From 38b3a4ec6309429a6ea4acced6dc2d5e1d745622 Mon Sep 17 00:00:00 2001 From: Unknown Date: Fri, 18 Jan 2019 00:12:35 -0500 Subject: [PATCH 3/3] Fixing issues pointed out by dogfood tests. --- tests/missing-test-files.rs | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/tests/missing-test-files.rs b/tests/missing-test-files.rs index f79abedc0..558e001d3 100644 --- a/tests/missing-test-files.rs +++ b/tests/missing-test-files.rs @@ -4,7 +4,7 @@ use std::path::Path; #[test] fn test_missing_tests() { let missing_files = explore_directory(Path::new("./tests")); - if missing_files.len() > 0 { + if !missing_files.is_empty() { assert!( false, format!( @@ -31,25 +31,22 @@ fn explore_directory(dir: &Path) -> Vec { let mut current_file = String::new(); let mut files: Vec = fs::read_dir(dir).unwrap().filter_map(Result::ok).collect(); files.sort_by_key(|e| e.path()); - for entry in files.iter() { + for entry in &files { let path = entry.path(); if path.is_dir() { missing_files.extend(explore_directory(&path)); } else { let file_stem = path.file_stem().unwrap().to_str().unwrap().to_string(); - match path.extension() { - Some(ext) => { - match ext.to_str().unwrap() { - "rs" => current_file = file_stem.clone(), - "stderr" | "stdout" => { - if file_stem != current_file { - missing_files.push(path.to_str().unwrap().to_string()); - } - }, - _ => continue, - }; - }, - None => {}, + if let Some(ext) = path.extension() { + match ext.to_str().unwrap() { + "rs" => current_file = file_stem.clone(), + "stderr" | "stdout" => { + if file_stem != current_file { + missing_files.push(path.to_str().unwrap().to_string()); + } + }, + _ => continue, + }; } } }