let mut expected_file = fs::File::open(&expected_filename).expect("Couldn't open target");
let mut expected_text = String::new();
expected_file.read_to_string(&mut expected_text)
- .expect("Failed reading target");
+ .expect("Failed reading target");
let compare = make_diff(&expected_text, &output, DIFF_CONTEXT_SIZE);
if compare.len() > 0 {
fn idempotence_tests() {
// Get all files in the tests/target directory.
let files = fs::read_dir("tests/target")
- .expect("Couldn't read target dir")
- .map(get_path_string);
+ .expect("Couldn't read target dir")
+ .map(get_path_string);
let (_reports, count, fails) = check_files(files);
// Display results.
#[test]
fn self_tests() {
let files = fs::read_dir("src/bin")
- .expect("Couldn't read src dir")
- .chain(fs::read_dir("tests").expect("Couldn't read tests dir"))
- .map(get_path_string);
+ .expect("Couldn't read src dir")
+ .chain(fs::read_dir("tests").expect("Couldn't read tests dir"))
+ .map(get_path_string);
// Hack because there's no `IntoIterator` impl for `[T; N]`.
let files = files.chain(Some("src/lib.rs".to_owned()).into_iter());
warnings);
}
+#[test]
+fn stdin_formatting_smoke_test() {
+ let input = Input::Text("fn main () {}".to_owned());
+ let config = Config::default();
+ let (error_summary, file_map, _report) = format_input(input, &config);
+ assert!(error_summary.has_no_errors());
+ assert_eq!(file_map["stdin"].to_string(), "fn main() {}\n")
+}
+
+#[test]
+fn format_lines_errors_are_reported() {
+ let long_identifier = String::from_utf8(vec![b'a'; 239]).unwrap();
+ let input = Input::Text(format!("fn {}() {{}}", long_identifier));
+ let config = Config::default();
+ let (error_summary, _file_map, _report) = format_input(input, &config);
+ assert!(error_summary.has_formatting_errors());
+}
+
// For each file, run rustfmt and collect the output.
// Returns the number of files checked and the number of failures.
fn check_files<I>(files: I) -> (Vec<FormatReport>, u32, u32)
fn format_file<P: Into<PathBuf>>(filename: P, config: &Config) -> (FileMap, FormatReport) {
let input = Input::File(filename.into());
- format_input(input, &config)
+ let (_error_summary, file_map, report) = format_input(input, &config);
+ return (file_map, report);
}
pub fn idempotent_check(filename: String) -> Result<FormatReport, HashMap<String, Vec<Mismatch>>> {
// Matches lines containing significant comments or whitespace.
let line_regex = regex::Regex::new(r"(^\s*$)|(^\s*//\s*rustfmt-[^:]+:\s*\S+)")
- .expect("Failed creating pattern 2");
+ .expect("Failed creating pattern 2");
reader.lines()
- .map(|line| line.expect("Failed getting line"))
- .take_while(|line| line_regex.is_match(&line))
- .filter_map(|line| {
- regex.captures_iter(&line).next().map(|capture| {
- (capture.at(1).expect("Couldn't unwrap capture").to_owned(),
- capture.at(2).expect("Couldn't unwrap capture").to_owned())
- })
- })
- .collect()
+ .map(|line| line.expect("Failed getting line"))
+ .take_while(|line| line_regex.is_match(&line))
+ .filter_map(|line| {
+ regex.captures_iter(&line).next().map(|capture| {
+ (capture.at(1).expect("Couldn't unwrap capture").to_owned(),
+ capture.at(2).expect("Couldn't unwrap capture").to_owned())
+ })
+ })
+ .collect()
}
// Compare output to input.