@@ -307,6 +307,52 @@ fn assert_output(source: &Path, expected_filename: &Path) {
307307 }
308308}
309309
310+ // Helper function for comparing the results of rustfmt
311+ // to a known output generated by one of the write modes.
312+ fn assert_stdin_output (
313+ source : & Path ,
314+ expected_filename : & Path ,
315+ emit_mode : EmitMode ,
316+ has_diff : bool ,
317+ ) {
318+ let mut config = Config :: default ( ) ;
319+ config. set ( ) . newline_style ( NewlineStyle :: Unix ) ;
320+ config. set ( ) . emit_mode ( emit_mode) ;
321+
322+ let mut source_file = fs:: File :: open ( & source) . expect ( "couldn't open source" ) ;
323+ let mut source_text = String :: new ( ) ;
324+ source_file
325+ . read_to_string ( & mut source_text)
326+ . expect ( "Failed reading target" ) ;
327+ let input = Input :: Text ( source_text) ;
328+
329+ // Populate output by writing to a vec.
330+ let mut buf: Vec < u8 > = vec ! [ ] ;
331+ {
332+ let mut session = Session :: new ( config, Some ( & mut buf) ) ;
333+ session. format ( input) . unwrap ( ) ;
334+ let errors = ReportedErrors {
335+ has_diff : has_diff,
336+ ..Default :: default ( )
337+ } ;
338+ assert_eq ! ( session. errors, errors) ;
339+ }
340+
341+ let mut expected_file = fs:: File :: open ( & expected_filename) . expect ( "couldn't open target" ) ;
342+ let mut expected_text = String :: new ( ) ;
343+ expected_file
344+ . read_to_string ( & mut expected_text)
345+ . expect ( "Failed reading target" ) ;
346+
347+ let output = String :: from_utf8 ( buf) . unwrap ( ) ;
348+ let compare = make_diff ( & expected_text, & output, DIFF_CONTEXT_SIZE ) ;
349+ if !compare. is_empty ( ) {
350+ let mut failures = HashMap :: new ( ) ;
351+ failures. insert ( source. to_owned ( ) , compare) ;
352+ print_mismatches_default_message ( failures) ;
353+ panic ! ( "Text does not match expected output" ) ;
354+ }
355+ }
310356// Idempotence tests. Files in tests/target are checked to be unaltered by
311357// rustfmt.
312358#[ nightly_only_test]
@@ -463,6 +509,30 @@ fn stdin_works_with_modified_lines() {
463509 assert_eq ! ( buf, output. as_bytes( ) ) ;
464510}
465511
512+ /// Ensures that `EmitMode::Json` works with input from `stdin`.
513+ #[ test]
514+ fn stdin_works_with_json ( ) {
515+ init_log ( ) ;
516+ assert_stdin_output (
517+ Path :: new ( "tests/writemode/source/stdin.rs" ) ,
518+ Path :: new ( "tests/writemode/target/stdin.json" ) ,
519+ EmitMode :: Json ,
520+ true ,
521+ ) ;
522+ }
523+
524+ /// Ensures that `EmitMode::Checkstyle` works with input from `stdin`.
525+ #[ test]
526+ fn stdin_works_with_checkstyle ( ) {
527+ init_log ( ) ;
528+ assert_stdin_output (
529+ Path :: new ( "tests/writemode/source/stdin.rs" ) ,
530+ Path :: new ( "tests/writemode/target/stdin.xml" ) ,
531+ EmitMode :: Checkstyle ,
532+ false ,
533+ ) ;
534+ }
535+
466536#[ test]
467537fn stdin_disable_all_formatting_test ( ) {
468538 init_log ( ) ;
@@ -896,3 +966,26 @@ fn verify_check_works() {
896966 . status ( )
897967 . expect ( "run with check option failed" ) ;
898968}
969+
970+ #[ test]
971+ fn verify_check_works_with_stdin ( ) {
972+ init_log ( ) ;
973+
974+ let mut child = Command :: new ( rustfmt ( ) . to_str ( ) . unwrap ( ) )
975+ . arg ( "--check" )
976+ . stdin ( Stdio :: piped ( ) )
977+ . stderr ( Stdio :: piped ( ) )
978+ . spawn ( )
979+ . expect ( "run with check option failed" ) ;
980+
981+ {
982+ let stdin = child. stdin . as_mut ( ) . expect ( "Failed to open stdin" ) ;
983+ stdin
984+ . write_all ( "fn main() {}\n " . as_bytes ( ) )
985+ . expect ( "Failed to write to rustfmt --check" ) ;
986+ }
987+ let output = child
988+ . wait_with_output ( )
989+ . expect ( "Failed to wait on rustfmt child" ) ;
990+ assert ! ( output. status. success( ) ) ;
991+ }
0 commit comments