From 8279a9cf4e2ef40b63dfc279e44607e5962ddbeb Mon Sep 17 00:00:00 2001 From: Chris Emerson Date: Thu, 7 Nov 2019 21:07:04 +0000 Subject: [PATCH 1/2] Fix some possible panics when using `--check` with stdin. One case which doesn't work is when there are only line ending fixes; with stdin rustfmt is unable to detect the difference as it stores the input with Unix line endings. --- src/emitter/diff.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/emitter/diff.rs b/src/emitter/diff.rs index 9be4fb28f99..09b250a13ce 100644 --- a/src/emitter/diff.rs +++ b/src/emitter/diff.rs @@ -28,7 +28,7 @@ impl Emitter for DiffEmitter { if has_diff { if self.config.print_misformatted_file_names() { - writeln!(output, "{}", ensure_real_path(filename).display())?; + writeln!(output, "{}", filename)?; } else { print_diff( mismatch, @@ -40,8 +40,7 @@ impl Emitter for DiffEmitter { // This occurs when the only difference between the original and formatted values // is the newline style. This happens because The make_diff function compares the // original and formatted values line by line, independent of line endings. - let file_path = ensure_real_path(filename); - writeln!(output, "Incorrect newline style in {}", file_path.display())?; + writeln!(output, "Incorrect newline style in {}", filename)?; return Ok(EmitterResult { has_diff: true }); } From bcac2fb524f92959ff700c74e99dbf5ecac05f27 Mon Sep 17 00:00:00 2001 From: Chris Emerson Date: Thu, 7 Nov 2019 21:19:24 +0000 Subject: [PATCH 2/2] Add test for `rustfmt --check -l` with stdin. --- src/test/mod.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/test/mod.rs b/src/test/mod.rs index bb92b184be4..fe3378ac1e0 100644 --- a/src/test/mod.rs +++ b/src/test/mod.rs @@ -958,3 +958,29 @@ fn verify_check_works_with_stdin() { .expect("Failed to wait on rustfmt child"); assert!(output.status.success()); } + +#[test] +fn verify_check_l_works_with_stdin() { + init_log(); + + let mut child = Command::new(rustfmt().to_str().unwrap()) + .arg("--check") + .arg("-l") + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .expect("run with check option failed"); + + { + let stdin = child.stdin.as_mut().expect("Failed to open stdin"); + stdin + .write_all("fn main()\n{}\n".as_bytes()) + .expect("Failed to write to rustfmt --check"); + } + let output = child + .wait_with_output() + .expect("Failed to wait on rustfmt child"); + assert!(output.status.success()); + assert_eq!(std::str::from_utf8(&output.stdout).unwrap(), "stdin\n"); +}