Skip to content

Commit c313fb1

Browse files
authored
Merge pull request #1973 from topecongiro/issue-1972
Echo back input from stdin when disable_all_formatting is true
2 parents 3637760 + dc26b06 commit c313fb1

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,12 @@ pub fn format_input<T: Write>(
890890
) -> Result<(Summary, FileMap, FormatReport), (io::Error, Summary)> {
891891
let mut summary = Summary::default();
892892
if config.disable_all_formatting() {
893+
// When the input is from stdin, echo back the input.
894+
if let Input::Text(ref buf) = input {
895+
if let Err(e) = io::stdout().write_all(buf.as_bytes()) {
896+
return Err((e, summary));
897+
}
898+
}
893899
return Ok((summary, FileMap::new(), FormatReport::new()));
894900
}
895901
let codemap = Rc::new(CodeMap::new(FilePathMapping::empty()));
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
disable_all_formatting = true

tests/system.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ extern crate term;
1414

1515
use std::collections::HashMap;
1616
use std::fs;
17-
use std::io::{self, BufRead, BufReader, Read};
17+
use std::io::{self, BufRead, BufReader, Read, Write};
1818
use std::path::{Path, PathBuf};
19+
use std::process::{Command, Stdio};
1920

2021
use rustfmt::*;
2122
use rustfmt::filemap::{write_system_newlines, FileMap};
@@ -157,6 +158,28 @@ fn stdin_formatting_smoke_test() {
157158
panic!("no stdin");
158159
}
159160

161+
#[test]
162+
fn stdin_disable_all_formatting_test() {
163+
let input = String::from("fn main() { println!(\"This should not be formatted.\"); }");
164+
let mut child = Command::new("./target/debug/rustfmt")
165+
.stdin(Stdio::piped())
166+
.stdout(Stdio::piped())
167+
.arg("--config-path=./tests/config/disable_all_formatting.toml")
168+
.spawn()
169+
.expect("failed to execute child");
170+
171+
{
172+
let stdin = child.stdin.as_mut().expect("failed to get stdin");
173+
stdin
174+
.write_all(input.as_bytes())
175+
.expect("failed to write stdin");
176+
}
177+
let output = child.wait_with_output().expect("failed to wait on child");
178+
assert!(output.status.success());
179+
assert!(output.stderr.is_empty());
180+
assert_eq!(input, String::from_utf8(output.stdout).unwrap());
181+
}
182+
160183
#[test]
161184
fn format_lines_errors_are_reported() {
162185
let long_identifier = String::from_utf8(vec![b'a'; 239]).unwrap();

tests/target/disable_all_formatting.rs

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)