diff --git a/src/lib.rs b/src/lib.rs index cb48b3c7fcd5..043f6f506d05 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -890,6 +890,12 @@ pub fn format_input( ) -> Result<(Summary, FileMap, FormatReport), (io::Error, Summary)> { let mut summary = Summary::default(); if config.disable_all_formatting() { + // When the input is from stdin, echo back the input. + if let Input::Text(ref buf) = input { + if let Err(e) = io::stdout().write_all(buf.as_bytes()) { + return Err((e, summary)); + } + } return Ok((summary, FileMap::new(), FormatReport::new())); } let codemap = Rc::new(CodeMap::new(FilePathMapping::empty())); diff --git a/tests/config/disable_all_formatting.toml b/tests/config/disable_all_formatting.toml new file mode 100644 index 000000000000..c7ad93bafe36 --- /dev/null +++ b/tests/config/disable_all_formatting.toml @@ -0,0 +1 @@ +disable_all_formatting = true diff --git a/tests/system.rs b/tests/system.rs index ce575f9f4923..44cfb732f4b4 100644 --- a/tests/system.rs +++ b/tests/system.rs @@ -14,8 +14,9 @@ extern crate term; use std::collections::HashMap; use std::fs; -use std::io::{self, BufRead, BufReader, Read}; +use std::io::{self, BufRead, BufReader, Read, Write}; use std::path::{Path, PathBuf}; +use std::process::{Command, Stdio}; use rustfmt::*; use rustfmt::filemap::{write_system_newlines, FileMap}; @@ -157,6 +158,28 @@ fn stdin_formatting_smoke_test() { panic!("no stdin"); } +#[test] +fn stdin_disable_all_formatting_test() { + let input = String::from("fn main() { println!(\"This should not be formatted.\"); }"); + let mut child = Command::new("./target/debug/rustfmt") + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .arg("--config-path=./tests/config/disable_all_formatting.toml") + .spawn() + .expect("failed to execute child"); + + { + let stdin = child.stdin.as_mut().expect("failed to get stdin"); + stdin + .write_all(input.as_bytes()) + .expect("failed to write stdin"); + } + let output = child.wait_with_output().expect("failed to wait on child"); + assert!(output.status.success()); + assert!(output.stderr.is_empty()); + assert_eq!(input, String::from_utf8(output.stdout).unwrap()); +} + #[test] fn format_lines_errors_are_reported() { let long_identifier = String::from_utf8(vec![b'a'; 239]).unwrap(); diff --git a/tests/target/disable_all_formatting.rs b/tests/target/disable_all_formatting.rs deleted file mode 100644 index ef7e4ee93626..000000000000 --- a/tests/target/disable_all_formatting.rs +++ /dev/null @@ -1,4 +0,0 @@ -// rustfmt-disable_all_formatting: true -// Don't format anything. - -fn main() { println!("This should not be formatted."); }