1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
14 use std::collections::HashMap;
19 // For now, the only supported regression tests are idempotent tests - the input and
20 // output must match exactly.
21 // FIXME(#28) would be good to check for error messages and fail on them, or at least report.
23 fn idempotent_tests() {
24 println!("Idempotent tests:");
26 // Get all files in the tests/idem directory
27 let files = fs::read_dir("tests/idem").unwrap();
28 let files2 = fs::read_dir("tests").unwrap();
29 let files3 = fs::read_dir("src/bin").unwrap();
30 // For each file, run rustfmt and collect the output
34 for entry in files.chain(files2).chain(files3) {
35 let path = entry.unwrap().path();
36 let file_name = path.to_str().unwrap();
37 if !file_name.ends_with(".rs") {
40 println!("Testing '{}'...", file_name);
41 match idempotent_check(file_name.to_owned()) {
50 // And also dogfood rustfmt!
51 println!("Testing 'src/lib.rs'...");
52 match idempotent_check("src/lib.rs".to_owned()) {
62 println!("Ran {} idempotent tests; {} failures.", count, fails);
63 assert!(fails == 0, "{} idempotent tests failed", fails);
66 // Compare output to input.
67 fn print_mismatches(result: HashMap<String, String>) {
68 for (file_name, fmt_text) in result {
69 println!("Mismatch in {}.", file_name);
70 println!("{}", fmt_text);
74 // Ick, just needed to get a &'static to handle_result.
75 static HANDLE_RESULT: &'static Fn(HashMap<String, String>) = &handle_result;
77 pub fn idempotent_check(filename: String) -> Result<(), HashMap<String, String>> {
81 let args = vec!["rustfmt".to_owned(), filename];
82 thread::spawn(move || {
83 run(args, WriteMode::Return(HANDLE_RESULT));
84 }).join().map_err(|mut any|
85 any.downcast_mut::<HashMap<String, String>>()
86 .unwrap() // i know it is a hashmap
87 .drain() // i only get a reference :(
88 .collect() // so i need to turn it into an iter and then back
92 // Compare output to input.
93 fn handle_result(result: HashMap<String, String>) {
94 let mut failures = HashMap::new();
96 for (file_name, fmt_text) in result {
97 let mut f = fs::File::open(&file_name).unwrap();
98 let mut text = String::new();
99 // TODO: speedup by running through bytes iterator
100 f.read_to_string(&mut text).unwrap();
101 if fmt_text != text {
102 failures.insert(file_name, fmt_text);
105 if !failures.is_empty() {