diff options
| author | Chris Emerson <github@mail.nosreme.org> | 2019-11-12 02:55:04 +0000 |
|---|---|---|
| committer | Caleb Cartwright <calebcartwright@users.noreply.github.com> | 2022-01-02 10:06:06 -0600 |
| commit | 34263cd6bdb29df23258a96ea18fe986efe8dad0 (patch) | |
| tree | fce6b7e54f8e332c5d63bc5716a6ee1720f80fe6 /src | |
| parent | 93b7de5b0110a1ab79714c3dfd009a4aae3e34b1 (diff) | |
| download | rust-34263cd6bdb29df23258a96ea18fe986efe8dad0.tar.gz rust-34263cd6bdb29df23258a96ea18fe986efe8dad0.zip | |
Fix --check -l with stdin. (#3910)
* 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. * Add test for `rustfmt --check -l` with stdin.
Diffstat (limited to 'src')
| -rw-r--r-- | src/emitter/diff.rs | 5 | ||||
| -rw-r--r-- | src/test/mod.rs | 26 |
2 files changed, 28 insertions, 3 deletions
diff --git a/src/emitter/diff.rs b/src/emitter/diff.rs index 7264ad8bbf3..5e1f1344656 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 }); } diff --git a/src/test/mod.rs b/src/test/mod.rs index e1a7972ec82..db1cf88479c 100644 --- a/src/test/mod.rs +++ b/src/test/mod.rs @@ -989,3 +989,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"); +} |
