Skip to content

Commit

Permalink
Fix --check -l with stdin. (#3910)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
jugglerchris authored and topecongiro committed Nov 12, 2019
1 parent c1ada05 commit fb5fea6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/emitter/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 });
}

Expand Down
26 changes: 26 additions & 0 deletions src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

0 comments on commit fb5fea6

Please sign in to comment.