From feb57adfcf64aece586f024679b7297c49baa7d6 Mon Sep 17 00:00:00 2001 From: Chris Emerson Date: Sat, 19 Oct 2019 16:44:05 +0100 Subject: [PATCH 01/15] Exit on error if --check is used with stdin. --- src/bin/main.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/bin/main.rs b/src/bin/main.rs index 7290c2f30d5..0c1c2bbb5b6 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -73,6 +73,10 @@ pub enum OperationError { /// An io error during reading or writing. #[fail(display = "io error: {}", _0)] IoError(IoError), + /// Attempt to use --check with stdin, which isn't currently + /// supported. + #[fail(display = "The `--check` option is not supported with standard input.")] + CheckWithStdin, } impl From for OperationError { @@ -464,6 +468,9 @@ fn determine_operation(matches: &Matches) -> Result { if minimal_config_path.is_some() { return Err(OperationError::MinimalPathWithStdin); } + if matches.opt_present("check") { + return Err(OperationError::CheckWithStdin); + } let mut buffer = String::new(); io::stdin().read_to_string(&mut buffer)?; From bec2038fb0fa96c7bf159ed239ef5e19be856bb6 Mon Sep 17 00:00:00 2001 From: Chris Emerson Date: Sat, 19 Oct 2019 16:54:20 +0100 Subject: [PATCH 02/15] Print an error when --emit=json is used with stdin. --- src/bin/main.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/bin/main.rs b/src/bin/main.rs index 0c1c2bbb5b6..b83f09fc086 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -77,6 +77,10 @@ pub enum OperationError { /// supported. #[fail(display = "The `--check` option is not supported with standard input.")] CheckWithStdin, + /// Attempt to use --emit=json with stdin, which isn't currently + /// supported. + #[fail(display = "Emitting json is not supported with standard input.")] + EmitJsonWithStdin, } impl From for OperationError { @@ -471,6 +475,11 @@ fn determine_operation(matches: &Matches) -> Result { if matches.opt_present("check") { return Err(OperationError::CheckWithStdin); } + if let Some(mode) = matches.opt_str("emit") { + if mode == "json" { + return Err(OperationError::EmitJsonWithStdin); + } + } let mut buffer = String::new(); io::stdin().read_to_string(&mut buffer)?; From 05d3dc65ed4569d82b3ec071dddec68cafe446f0 Mon Sep 17 00:00:00 2001 From: Chris Emerson Date: Sat, 19 Oct 2019 17:41:36 +0100 Subject: [PATCH 03/15] Move the checks for --emit or --check into format_string, and check for other --emit values. --- src/bin/main.rs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/bin/main.rs b/src/bin/main.rs index b83f09fc086..2682b52cc3b 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -79,8 +79,8 @@ pub enum OperationError { CheckWithStdin, /// Attempt to use --emit=json with stdin, which isn't currently /// supported. - #[fail(display = "Emitting json is not supported with standard input.")] - EmitJsonWithStdin, + #[fail(display = "Using `--emit` other than stdout is not supported with standard input.")] + EmitWithStdin, } impl From for OperationError { @@ -250,6 +250,12 @@ fn format_string(input: String, options: GetOptsOptions) -> Result Result { if minimal_config_path.is_some() { return Err(OperationError::MinimalPathWithStdin); } - if matches.opt_present("check") { - return Err(OperationError::CheckWithStdin); - } - if let Some(mode) = matches.opt_str("emit") { - if mode == "json" { - return Err(OperationError::EmitJsonWithStdin); - } - } let mut buffer = String::new(); io::stdin().read_to_string(&mut buffer)?; From 8037cb86d3fa6dd0bbcea05fac11dd7959b45f25 Mon Sep 17 00:00:00 2001 From: Chris Emerson Date: Sat, 19 Oct 2019 19:06:48 +0100 Subject: [PATCH 04/15] Change options.emit_mode into an option, so that it can be defaulted depending on the mode. --- src/bin/main.rs | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/bin/main.rs b/src/bin/main.rs index 2682b52cc3b..661502a2e0e 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -253,8 +253,10 @@ fn format_string(input: String, options: GetOptsOptions) -> Result, inline_config: HashMap, - emit_mode: EmitMode, + emit_mode: Option, backup: bool, check: bool, edition: Option, @@ -588,7 +590,7 @@ impl GetOptsOptions { return Err(format_err!("Invalid to use `--emit` and `--check`")); } - options.emit_mode = emit_mode_from_emit_str(emit_str)?; + options.emit_mode = Some(emit_mode_from_emit_str(emit_str)?); } if let Some(ref edition_str) = matches.opt_str("edition") { @@ -604,11 +606,13 @@ impl GetOptsOptions { } if !rust_nightly { - if !STABLE_EMIT_MODES.contains(&options.emit_mode) { - return Err(format_err!( - "Invalid value for `--emit` - using an unstable \ - value without `--unstable-features`", - )); + if let Some(ref emit_mode) = options.emit_mode { + if !STABLE_EMIT_MODES.contains(emit_mode) { + return Err(format_err!( + "Invalid value for `--emit` - using an unstable \ + value without `--unstable-features`", + )); + } } } @@ -657,8 +661,8 @@ impl CliOptions for GetOptsOptions { } if self.check { config.set().emit_mode(EmitMode::Diff); - } else { - config.set().emit_mode(self.emit_mode); + } else if let Some(emit_mode) = self.emit_mode { + config.set().emit_mode(emit_mode); } if self.backup { config.set().make_backup(true); From 38cacd6cdecd27e760c83fe9f6df08e2acd43e28 Mon Sep 17 00:00:00 2001 From: Chris Emerson Date: Sat, 19 Oct 2019 19:59:49 +0100 Subject: [PATCH 05/15] Allow emitting JSON when working with stdin; use "stdin" as the filename. --- src/bin/main.rs | 11 +---------- src/emitter/json.rs | 10 ++++------ 2 files changed, 5 insertions(+), 16 deletions(-) diff --git a/src/bin/main.rs b/src/bin/main.rs index 661502a2e0e..7188b8ffbf3 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -77,10 +77,6 @@ pub enum OperationError { /// supported. #[fail(display = "The `--check` option is not supported with standard input.")] CheckWithStdin, - /// Attempt to use --emit=json with stdin, which isn't currently - /// supported. - #[fail(display = "Using `--emit` other than stdout is not supported with standard input.")] - EmitWithStdin, } impl From for OperationError { @@ -253,13 +249,8 @@ fn format_string(input: String, options: GetOptsOptions) -> Result, ) -> Result { const CONTEXT_SIZE: usize = 0; - let filename = ensure_real_path(filename); let diff = make_diff(original_text, formatted_text, CONTEXT_SIZE); let has_diff = !diff.is_empty(); @@ -62,7 +60,7 @@ impl Emitter for JsonEmitter { fn output_json_file( mut writer: T, - filename: &Path, + filename: &FileName, diff: Vec, num_emitted_files: u32, ) -> Result<(), io::Error> @@ -106,7 +104,7 @@ where }); } let json = to_json_string(&MismatchedFile { - name: String::from(filename.to_str().unwrap()), + name: format!("{}", filename), mismatches, })?; let prefix = if num_emitted_files > 0 { "," } else { "" }; @@ -148,7 +146,7 @@ mod tests { let mut writer = Vec::new(); let exp_json = to_json_string(&mismatched_file).unwrap(); - let _ = output_json_file(&mut writer, &PathBuf::from(file), vec![mismatch], 0); + let _ = output_json_file(&mut writer, &FileName::Real(PathBuf::from(file)), vec![mismatch], 0); assert_eq!(&writer[..], format!("{}", exp_json).as_bytes()); } @@ -188,7 +186,7 @@ mod tests { let mut writer = Vec::new(); let exp_json = to_json_string(&mismatched_file).unwrap(); - let _ = output_json_file(&mut writer, &PathBuf::from(file), vec![mismatch], 0); + let _ = output_json_file(&mut writer, &FileName::Real(PathBuf::from(file)), vec![mismatch], 0); assert_eq!(&writer[..], format!("{}", exp_json).as_bytes()); } From aff15e5af98a2f36f75d5a72ab7bc40c70d1a829 Mon Sep 17 00:00:00 2001 From: Chris Emerson Date: Sat, 19 Oct 2019 20:02:14 +0100 Subject: [PATCH 06/15] Run cargo fmt. --- src/bin/main.rs | 4 +++- src/emitter/json.rs | 14 ++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/bin/main.rs b/src/bin/main.rs index 7188b8ffbf3..315a1f53abc 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -250,7 +250,9 @@ fn format_string(input: String, options: GetOptsOptions) -> Result Date: Sat, 19 Oct 2019 20:20:07 +0100 Subject: [PATCH 07/15] Return an error for modes still not supported when operating on stdin. --- src/bin/main.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/bin/main.rs b/src/bin/main.rs index 315a1f53abc..b3ee36e11ab 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -77,6 +77,10 @@ pub enum OperationError { /// supported. #[fail(display = "The `--check` option is not supported with standard input.")] CheckWithStdin, + /// Attempt to use --emit with a mode which is not currently + /// supported with stdandard input. + #[fail(display = "Emit mode {} not supported with standard output.", _0)] + StdinBadEmit(EmitMode), } impl From for OperationError { @@ -249,6 +253,16 @@ fn format_string(input: String, options: GetOptsOptions) -> Result {} + Some(emit_mode) => { + return Err(OperationError::StdinBadEmit(emit_mode).into()); + }, + } // emit mode is always Stdout for Stdin. config .set() From d958ec5e6f46e2ca2ee9b2cf0b83f744686d4cea Mon Sep 17 00:00:00 2001 From: Chris Emerson Date: Sun, 20 Oct 2019 07:59:33 +0100 Subject: [PATCH 08/15] Allow --emit=checkstyle to work with standard input. --- src/bin/main.rs | 1 + src/emitter/checkstyle.rs | 8 +++----- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/bin/main.rs b/src/bin/main.rs index b3ee36e11ab..1a870b61988 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -258,6 +258,7 @@ fn format_string(input: String, options: GetOptsOptions) -> Result {} Some(emit_mode) => { return Err(OperationError::StdinBadEmit(emit_mode).into()); diff --git a/src/emitter/checkstyle.rs b/src/emitter/checkstyle.rs index 4448214f3ff..c1736ecdd49 100644 --- a/src/emitter/checkstyle.rs +++ b/src/emitter/checkstyle.rs @@ -2,7 +2,6 @@ use self::xml::XmlEscaped; use super::*; use crate::rustfmt_diff::{make_diff, DiffLine, Mismatch}; use std::io::{self, Write}; -use std::path::Path; mod xml; @@ -30,7 +29,6 @@ impl Emitter for CheckstyleEmitter { }: FormattedFile<'_>, ) -> Result { const CONTEXT_SIZE: usize = 0; - let filename = ensure_real_path(filename); let diff = make_diff(original_text, formatted_text, CONTEXT_SIZE); output_checkstyle_file(output, filename, diff)?; Ok(EmitterResult::default()) @@ -39,13 +37,13 @@ impl Emitter for CheckstyleEmitter { pub(crate) fn output_checkstyle_file( mut writer: T, - filename: &Path, + filename: &FileName, diff: Vec, ) -> Result<(), io::Error> where T: Write, { - write!(writer, r#""#, filename.display())?; + write!(writer, r#""#, filename)?; for mismatch in diff { let begin_line = mismatch.line_number; let mut current_line; @@ -77,7 +75,7 @@ mod tests { fn emits_empty_record_on_file_with_no_mismatches() { let file_name = "src/well_formatted.rs"; let mut writer = Vec::new(); - let _ = output_checkstyle_file(&mut writer, &PathBuf::from(file_name), vec![]); + let _ = output_checkstyle_file(&mut writer, &FileName::Real(PathBuf::from(file_name)), vec![]); assert_eq!( &writer[..], format!(r#""#, file_name).as_bytes() From e84a3782aaecf4c6de41b0d6d78d027ceec653ff Mon Sep 17 00:00:00 2001 From: Chris Emerson Date: Sun, 20 Oct 2019 08:00:11 +0100 Subject: [PATCH 09/15] Run cargo fmt. --- src/bin/main.rs | 7 ++----- src/emitter/checkstyle.rs | 6 +++++- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/bin/main.rs b/src/bin/main.rs index 1a870b61988..fb781074827 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -256,13 +256,10 @@ fn format_string(input: String, options: GetOptsOptions) -> Result {} + None | Some(EmitMode::Stdout) | Some(EmitMode::Checkstyle) | Some(EmitMode::Json) => {} Some(emit_mode) => { return Err(OperationError::StdinBadEmit(emit_mode).into()); - }, + } } // emit mode is always Stdout for Stdin. config diff --git a/src/emitter/checkstyle.rs b/src/emitter/checkstyle.rs index c1736ecdd49..00db5b234ee 100644 --- a/src/emitter/checkstyle.rs +++ b/src/emitter/checkstyle.rs @@ -75,7 +75,11 @@ mod tests { fn emits_empty_record_on_file_with_no_mismatches() { let file_name = "src/well_formatted.rs"; let mut writer = Vec::new(); - let _ = output_checkstyle_file(&mut writer, &FileName::Real(PathBuf::from(file_name)), vec![]); + let _ = output_checkstyle_file( + &mut writer, + &FileName::Real(PathBuf::from(file_name)), + vec![], + ); assert_eq!( &writer[..], format!(r#""#, file_name).as_bytes() From 954383878e520c3da1cbca4de8eee9d900b986db Mon Sep 17 00:00:00 2001 From: Chris Emerson Date: Sun, 27 Oct 2019 22:40:10 +0000 Subject: [PATCH 10/15] Add some tests for the Json and Checkstyle emitters using stdin. I've added #[rustfmt::skip] to a couple of long lines; this would probably be better as test files rather than inline. --- src/test/mod.rs | 52 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/src/test/mod.rs b/src/test/mod.rs index 6b7a9365c6f..399ae4db18e 100644 --- a/src/test/mod.rs +++ b/src/test/mod.rs @@ -428,6 +428,58 @@ fn stdin_works_with_modified_lines() { assert_eq!(buf, output.as_bytes()); } +/// Ensures that `EmitMode::Json` works with input from `stdin`. +#[test] +fn stdin_works_with_json() { + init_log(); + let input = "\nfn\n some( )\n{\n}\nfn main () {}\n"; + #[rustfmt::skip] + let output = r#"[{"name":"stdin","mismatches":[{"original_begin_line":1,"original_end_line":6,"expected_begin_line":1,"expected_end_line":2,"original":"\nfn\n some( )\n{\n}\nfn main () {}","expected":"fn some() {}\nfn main() {}"}]}]"#; + + let input = Input::Text(input.to_owned()); + let mut config = Config::default(); + config.set().newline_style(NewlineStyle::Unix); + config.set().emit_mode(EmitMode::Json); + let mut buf: Vec = vec![]; + { + let mut session = Session::new(config, Some(&mut buf)); + session.format(input).unwrap(); + let errors = ReportedErrors { + has_diff: true, + ..Default::default() + }; + assert_eq!(session.errors, errors); + } + assert_eq!(buf, output.as_bytes()); +} + +/// Ensures that `EmitMode::Checkstyle` works with input from `stdin`. +#[test] +fn stdin_works_with_checkstyle() { + init_log(); + let input = "\nfn\n some( )\n{\n}\nfn main () {}\n"; + #[rustfmt::skip] + let output = r#" + +"#; + + let input = Input::Text(input.to_owned()); + let mut config = Config::default(); + config.set().newline_style(NewlineStyle::Unix); + config.set().emit_mode(EmitMode::Checkstyle); + let mut buf: Vec = vec![]; + { + let mut session = Session::new(config, Some(&mut buf)); + session.format(input).unwrap(); + let errors = ReportedErrors { + has_diff: false, + ..Default::default() + }; + assert_eq!(session.errors, errors); + } + assert_eq!(buf, output.as_bytes()); +} + #[test] fn stdin_disable_all_formatting_test() { init_log(); From 9679fce6228bf25b567929e9e21b8c88537f85cd Mon Sep 17 00:00:00 2001 From: Chris Emerson Date: Mon, 28 Oct 2019 21:34:46 +0000 Subject: [PATCH 11/15] Load stdin_works_with_json input and output from files. --- src/test/mod.rs | 63 +++++++++++++++++++++---------- tests/writemode/source/stdin.rs | 6 +++ tests/writemode/target/stdin.json | 1 + 3 files changed, 51 insertions(+), 19 deletions(-) create mode 100644 tests/writemode/source/stdin.rs create mode 100644 tests/writemode/target/stdin.json diff --git a/src/test/mod.rs b/src/test/mod.rs index 399ae4db18e..8c32c6a4929 100644 --- a/src/test/mod.rs +++ b/src/test/mod.rs @@ -305,6 +305,47 @@ fn assert_output(source: &Path, expected_filename: &Path) { } } +// Helper function for comparing the results of rustfmt +// to a known output generated by one of the write modes. +fn assert_stdin_output(source: &Path, expected_filename: &Path, emit_mode: EmitMode) { + let mut config = Config::default(); + config.set().newline_style(NewlineStyle::Unix); + config.set().emit_mode(emit_mode); + + let mut source_file = fs::File::open(&source).expect("couldn't open source"); + let mut source_text = String::new(); + source_file + .read_to_string(&mut source_text) + .expect("Failed reading target"); + let input = Input::Text(source_text); + + // Populate output by writing to a vec. + let mut buf: Vec = vec![]; + { + let mut session = Session::new(config, Some(&mut buf)); + session.format(input).unwrap(); + let errors = ReportedErrors { + has_diff: true, + ..Default::default() + }; + assert_eq!(session.errors, errors); + } + + let mut expected_file = fs::File::open(&expected_filename).expect("couldn't open target"); + let mut expected_text = String::new(); + expected_file + .read_to_string(&mut expected_text) + .expect("Failed reading target"); + + let output = String::from_utf8(buf).unwrap(); + let compare = make_diff(&expected_text, &output, DIFF_CONTEXT_SIZE); + if !compare.is_empty() { + let mut failures = HashMap::new(); + failures.insert(source.to_owned(), compare); + print_mismatches_default_message(failures); + panic!("Text does not match expected output"); + } +} // Idempotence tests. Files in tests/target are checked to be unaltered by // rustfmt. #[test] @@ -432,25 +473,9 @@ fn stdin_works_with_modified_lines() { #[test] fn stdin_works_with_json() { init_log(); - let input = "\nfn\n some( )\n{\n}\nfn main () {}\n"; - #[rustfmt::skip] - let output = r#"[{"name":"stdin","mismatches":[{"original_begin_line":1,"original_end_line":6,"expected_begin_line":1,"expected_end_line":2,"original":"\nfn\n some( )\n{\n}\nfn main () {}","expected":"fn some() {}\nfn main() {}"}]}]"#; - - let input = Input::Text(input.to_owned()); - let mut config = Config::default(); - config.set().newline_style(NewlineStyle::Unix); - config.set().emit_mode(EmitMode::Json); - let mut buf: Vec = vec![]; - { - let mut session = Session::new(config, Some(&mut buf)); - session.format(input).unwrap(); - let errors = ReportedErrors { - has_diff: true, - ..Default::default() - }; - assert_eq!(session.errors, errors); - } - assert_eq!(buf, output.as_bytes()); + assert_stdin_output(Path::new("tests/writemode/source/stdin.rs"), + Path::new("tests/writemode/target/stdin.json"), + EmitMode::Json); } /// Ensures that `EmitMode::Checkstyle` works with input from `stdin`. diff --git a/tests/writemode/source/stdin.rs b/tests/writemode/source/stdin.rs new file mode 100644 index 00000000000..06f8a0c288d --- /dev/null +++ b/tests/writemode/source/stdin.rs @@ -0,0 +1,6 @@ + +fn + some( ) +{ +} +fn main () {} diff --git a/tests/writemode/target/stdin.json b/tests/writemode/target/stdin.json new file mode 100644 index 00000000000..20e38f57f4a --- /dev/null +++ b/tests/writemode/target/stdin.json @@ -0,0 +1 @@ +[{"name":"stdin","mismatches":[{"original_begin_line":1,"original_end_line":6,"expected_begin_line":1,"expected_end_line":2,"original":"\nfn\n some( )\n{\n}\nfn main () {}","expected":"fn some() {}\nfn main() {}"}]}] \ No newline at end of file From 28c62c7d0c6c3537fb83d47f61b69d7ae8b74543 Mon Sep 17 00:00:00 2001 From: Chris Emerson Date: Mon, 28 Oct 2019 21:38:45 +0000 Subject: [PATCH 12/15] Switch stdin_works_with_checkstyle() over to assert_stdin_output(). --- src/test/mod.rs | 32 ++++++++------------------------ tests/writemode/target/stdin.xml | 2 ++ 2 files changed, 10 insertions(+), 24 deletions(-) create mode 100644 tests/writemode/target/stdin.xml diff --git a/src/test/mod.rs b/src/test/mod.rs index 8c32c6a4929..e190131ade2 100644 --- a/src/test/mod.rs +++ b/src/test/mod.rs @@ -307,7 +307,7 @@ fn assert_output(source: &Path, expected_filename: &Path) { // Helper function for comparing the results of rustfmt // to a known output generated by one of the write modes. -fn assert_stdin_output(source: &Path, expected_filename: &Path, emit_mode: EmitMode) { +fn assert_stdin_output(source: &Path, expected_filename: &Path, emit_mode: EmitMode, has_diff: bool) { let mut config = Config::default(); config.set().newline_style(NewlineStyle::Unix); config.set().emit_mode(emit_mode); @@ -325,7 +325,7 @@ fn assert_stdin_output(source: &Path, expected_filename: &Path, emit_mode: EmitM let mut session = Session::new(config, Some(&mut buf)); session.format(input).unwrap(); let errors = ReportedErrors { - has_diff: true, + has_diff: has_diff, ..Default::default() }; assert_eq!(session.errors, errors); @@ -475,34 +475,18 @@ fn stdin_works_with_json() { init_log(); assert_stdin_output(Path::new("tests/writemode/source/stdin.rs"), Path::new("tests/writemode/target/stdin.json"), - EmitMode::Json); + EmitMode::Json, + true); } /// Ensures that `EmitMode::Checkstyle` works with input from `stdin`. #[test] fn stdin_works_with_checkstyle() { init_log(); - let input = "\nfn\n some( )\n{\n}\nfn main () {}\n"; - #[rustfmt::skip] - let output = r#" - -"#; - - let input = Input::Text(input.to_owned()); - let mut config = Config::default(); - config.set().newline_style(NewlineStyle::Unix); - config.set().emit_mode(EmitMode::Checkstyle); - let mut buf: Vec = vec![]; - { - let mut session = Session::new(config, Some(&mut buf)); - session.format(input).unwrap(); - let errors = ReportedErrors { - has_diff: false, - ..Default::default() - }; - assert_eq!(session.errors, errors); - } - assert_eq!(buf, output.as_bytes()); + assert_stdin_output(Path::new("tests/writemode/source/stdin.rs"), + Path::new("tests/writemode/target/stdin.xml"), + EmitMode::Checkstyle, + false); } #[test] diff --git a/tests/writemode/target/stdin.xml b/tests/writemode/target/stdin.xml new file mode 100644 index 00000000000..e70708338f5 --- /dev/null +++ b/tests/writemode/target/stdin.xml @@ -0,0 +1,2 @@ + + From 36fc5607428d72d0fda59d36e8ca62907986e1ec Mon Sep 17 00:00:00 2001 From: Chris Emerson Date: Mon, 28 Oct 2019 21:39:57 +0000 Subject: [PATCH 13/15] Rerun cargo fmt. --- src/test/mod.rs | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/test/mod.rs b/src/test/mod.rs index e190131ade2..09bd97c9af3 100644 --- a/src/test/mod.rs +++ b/src/test/mod.rs @@ -307,7 +307,12 @@ fn assert_output(source: &Path, expected_filename: &Path) { // Helper function for comparing the results of rustfmt // to a known output generated by one of the write modes. -fn assert_stdin_output(source: &Path, expected_filename: &Path, emit_mode: EmitMode, has_diff: bool) { +fn assert_stdin_output( + source: &Path, + expected_filename: &Path, + emit_mode: EmitMode, + has_diff: bool, +) { let mut config = Config::default(); config.set().newline_style(NewlineStyle::Unix); config.set().emit_mode(emit_mode); @@ -473,20 +478,24 @@ fn stdin_works_with_modified_lines() { #[test] fn stdin_works_with_json() { init_log(); - assert_stdin_output(Path::new("tests/writemode/source/stdin.rs"), - Path::new("tests/writemode/target/stdin.json"), - EmitMode::Json, - true); + assert_stdin_output( + Path::new("tests/writemode/source/stdin.rs"), + Path::new("tests/writemode/target/stdin.json"), + EmitMode::Json, + true, + ); } /// Ensures that `EmitMode::Checkstyle` works with input from `stdin`. #[test] fn stdin_works_with_checkstyle() { init_log(); - assert_stdin_output(Path::new("tests/writemode/source/stdin.rs"), - Path::new("tests/writemode/target/stdin.xml"), - EmitMode::Checkstyle, - false); + assert_stdin_output( + Path::new("tests/writemode/source/stdin.rs"), + Path::new("tests/writemode/target/stdin.xml"), + EmitMode::Checkstyle, + false, + ); } #[test] From c6450177e4fbde5d191e2b18d000b9043388328f Mon Sep 17 00:00:00 2001 From: Chris Emerson Date: Mon, 28 Oct 2019 22:02:21 +0000 Subject: [PATCH 14/15] Allow --check with stdin. --- src/bin/main.rs | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/src/bin/main.rs b/src/bin/main.rs index fb781074827..9c7ff736cfb 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -73,10 +73,6 @@ pub enum OperationError { /// An io error during reading or writing. #[fail(display = "io error: {}", _0)] IoError(IoError), - /// Attempt to use --check with stdin, which isn't currently - /// supported. - #[fail(display = "The `--check` option is not supported with standard input.")] - CheckWithStdin, /// Attempt to use --emit with a mode which is not currently /// supported with stdandard input. #[fail(display = "Emit mode {} not supported with standard output.", _0)] @@ -251,20 +247,20 @@ fn format_string(input: String, options: GetOptsOptions) -> Result {} - Some(emit_mode) => { - return Err(OperationError::StdinBadEmit(emit_mode).into()); + config.set().emit_mode(EmitMode::Diff); + } else { + match options.emit_mode { + // Emit modes which work with standard input + // None means default, which is Stdout. + None | Some(EmitMode::Stdout) | Some(EmitMode::Checkstyle) | Some(EmitMode::Json) => {} + Some(emit_mode) => { + return Err(OperationError::StdinBadEmit(emit_mode).into()); + } } + config + .set() + .emit_mode(options.emit_mode.unwrap_or(EmitMode::Stdout)); } - // emit mode is always Stdout for Stdin. - config - .set() - .emit_mode(options.emit_mode.unwrap_or(EmitMode::Stdout)); config.set().verbose(Verbosity::Quiet); // parse file_lines From 070cb8b69758a7ad072e452a4c432bcfb2aae848 Mon Sep 17 00:00:00 2001 From: Chris Emerson Date: Mon, 28 Oct 2019 22:17:15 +0000 Subject: [PATCH 15/15] Add a check that `rustfmt --check` works with input on stdin. --- src/test/mod.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/test/mod.rs b/src/test/mod.rs index 09bd97c9af3..bb92b184be4 100644 --- a/src/test/mod.rs +++ b/src/test/mod.rs @@ -935,3 +935,26 @@ fn verify_check_works() { .status() .expect("run with check option failed"); } + +#[test] +fn verify_check_works_with_stdin() { + init_log(); + + let mut child = Command::new(rustfmt().to_str().unwrap()) + .arg("--check") + .stdin(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".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()); +}