From ede179c62ee27f835037fa81869183f71cba1382 Mon Sep 17 00:00:00 2001 From: Seiichi Uchida Date: Sun, 17 Sep 2017 22:14:28 +0900 Subject: [PATCH 1/3] Echo back input from stdin when disable_all_formatting is true --- src/lib.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index ee231e2e63c5..6e41f6a5f24d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -881,6 +881,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())); From 75a36f2886334e241384f9046ed15a4c21beacf8 Mon Sep 17 00:00:00 2001 From: topecongiro Date: Mon, 18 Sep 2017 14:19:50 +0900 Subject: [PATCH 2/3] Add a test for 'disable_all_formatting = true' with stdin --- tests/config/disable_all_formatting.toml | 1 + tests/system.rs | 25 +++++++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 tests/config/disable_all_formatting.toml 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(); From dc26b069f66b856aecf638755ae99e7b32548f74 Mon Sep 17 00:00:00 2001 From: topecongiro Date: Mon, 18 Sep 2017 14:20:40 +0900 Subject: [PATCH 3/3] Remove a duplicate test --- tests/target/disable_all_formatting.rs | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 tests/target/disable_all_formatting.rs 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."); }