diff --git a/tests/coverage-source/comments.rs b/tests/coverage-source/comments.rs new file mode 100644 index 00000000000..379e8e5820e --- /dev/null +++ b/tests/coverage-source/comments.rs @@ -0,0 +1,6 @@ +/// Here's a doc comment! +fn main() { + // foo is bar + let foo = "bar"; + // loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong comment!!!!! +} diff --git a/tests/coverage-target/comments.rs b/tests/coverage-target/comments.rs new file mode 100644 index 00000000000..74d17bffd15 --- /dev/null +++ b/tests/coverage-target/comments.rs @@ -0,0 +1,6 @@ +/// Here's a doc comment! +fn main() { + XX XXX XX XXX + let foo = "bar"; + XX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXX +} diff --git a/tests/system.rs b/tests/system.rs index 6e9a4efe406..4a924c57833 100644 --- a/tests/system.rs +++ b/tests/system.rs @@ -43,13 +43,25 @@ fn system_tests() { // Turn a DirEntry into a String that represents the relative path to the // file. let files = files.map(get_path_string); - let (_reports, count, fails) = check_files(files); + let (_reports, count, fails) = check_files(files, WriteMode::Return); // Display results. println!("Ran {} system tests.", count); assert!(fails == 0, "{} system tests failed", fails); } +// Do the same for tests/coverage-source directory +// the only difference is the coverage mode +#[test] +fn coverage_tests() { + let files = fs::read_dir("tests/coverage-source").ok().expect("Couldn't read source dir."); + let files = files.map(get_path_string); + let (_reports, count, fails) = check_files(files, WriteMode::Coverage); + + println!("Ran {} tests in coverage mode.", count); + assert!(fails == 0, "{} tests failed", fails); +} + // Idempotence tests. Files in tests/target are checked to be unaltered by // rustfmt. #[test] @@ -59,7 +71,7 @@ fn idempotence_tests() { .ok() .expect("Couldn't read target dir.") .map(get_path_string); - let (_reports, count, fails) = check_files(files); + let (_reports, count, fails) = check_files(files, WriteMode::Return); // Display results. println!("Ran {} idempotent tests.", count); @@ -78,7 +90,7 @@ fn self_tests() { // Hack because there's no `IntoIterator` impl for `[T; N]`. let files = files.chain(Some("src/lib.rs".to_owned()).into_iter()); - let (reports, count, fails) = check_files(files); + let (reports, count, fails) = check_files(files, WriteMode::Return); let mut warnings = 0; // Display results. @@ -97,7 +109,7 @@ fn self_tests() { // For each file, run rustfmt and collect the output. // Returns the number of files checked and the number of failures. -fn check_files(files: I) -> (Vec, u32, u32) +fn check_files(files: I, write_mode: WriteMode) -> (Vec, u32, u32) where I: Iterator { let mut count = 0; @@ -107,7 +119,7 @@ fn check_files(files: I) -> (Vec, u32, u32) for file_name in files.filter(|f| f.ends_with(".rs")) { println!("Testing '{}'...", file_name); - match idempotent_check(file_name) { + match idempotent_check(file_name, write_mode) { Ok(report) => reports.push(report), Err(msg) => { print_mismatches(msg); @@ -132,7 +144,9 @@ fn print_mismatches(result: HashMap>) { assert!(t.reset().unwrap()); } -pub fn idempotent_check(filename: String) -> Result>> { +pub fn idempotent_check(filename: String, + write_mode: WriteMode) + -> Result>> { let sig_comments = read_significant_comments(&filename); let mut config = get_config(sig_comments.get("config").map(|x| &(*x)[..])); @@ -145,14 +159,14 @@ pub fn idempotent_check(filename: String) -> Result HashMap { // Compare output to input. // TODO: needs a better name, more explanation. fn handle_result(result: HashMap, - target: Option<&str>) + target: Option<&str>, + write_mode: WriteMode) -> Result<(), HashMap>> { let mut failures = HashMap::new(); for (file_name, fmt_text) in result { // If file is in tests/source, compare to file with same name in tests/target. - let target = get_target(&file_name, target); + let target = get_target(&file_name, target, write_mode); let mut f = fs::File::open(&target).ok().expect("Couldn't open target."); let mut text = String::new(); @@ -231,9 +246,14 @@ fn handle_result(result: HashMap, } // Map source file paths to their target paths. -fn get_target(file_name: &str, target: Option<&str>) -> String { +fn get_target(file_name: &str, target: Option<&str>, write_mode: WriteMode) -> String { let file_path = Path::new(file_name); - let source_path_prefix = Path::new("tests/source/"); + let (source_path_prefix, target_path_prefix) = match write_mode { + WriteMode::Coverage => (Path::new("tests/coverage-source/"), + "tests/coverage-target/"), + _ => (Path::new("tests/source/"), "tests/target/"), + }; + if file_path.starts_with(source_path_prefix) { let mut components = file_path.components(); // Can't skip(2) as the resulting iterator can't as_path() @@ -246,7 +266,7 @@ fn get_target(file_name: &str, target: Option<&str>) -> String { }; let base = target.unwrap_or(new_target); - format!("tests/target/{}", base) + format!("{}{}", target_path_prefix, base) } else { file_name.to_owned() }