]> git.lizzy.rs Git - rust.git/blobdiff - tests/system.rs
Merge pull request #966 from MicahChalmer/skip-children-in-plain-write-mode
[rust.git] / tests / system.rs
index b6518a559b9463e98d89b32d661ba56f2f0a5c3b..1af9c0f3462f8c13d00d416f3e4396ef87363fd6 100644 (file)
@@ -85,7 +85,7 @@ fn assert_output(source: &str, expected_filename: &str) {
     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 {
@@ -102,8 +102,8 @@ fn assert_output(source: &str, expected_filename: &str) {
 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.
@@ -116,9 +116,9 @@ fn idempotence_tests() {
 #[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());
 
@@ -139,6 +139,24 @@ fn self_tests() {
             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)
@@ -194,7 +212,8 @@ fn read_config(filename: &str) -> Config {
 
 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>>> {
@@ -245,18 +264,18 @@ fn read_significant_comments(file_name: &str) -> HashMap<String, String> {
 
     // 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.