@@ -305,6 +305,52 @@ fn assert_output(source: &Path, expected_filename: &Path) {
305305 }
306306}
307307
308+ // Helper function for comparing the results of rustfmt
309+ // to a known output generated by one of the write modes.
310+ fn assert_stdin_output (
311+ source : & Path ,
312+ expected_filename : & Path ,
313+ emit_mode : EmitMode ,
314+ has_diff : bool ,
315+ ) {
316+ let mut config = Config :: default ( ) ;
317+ config. set ( ) . newline_style ( NewlineStyle :: Unix ) ;
318+ config. set ( ) . emit_mode ( emit_mode) ;
319+
320+ let mut source_file = fs:: File :: open ( & source) . expect ( "couldn't open source" ) ;
321+ let mut source_text = String :: new ( ) ;
322+ source_file
323+ . read_to_string ( & mut source_text)
324+ . expect ( "Failed reading target" ) ;
325+ let input = Input :: Text ( source_text) ;
326+
327+ // Populate output by writing to a vec.
328+ let mut buf: Vec < u8 > = vec ! [ ] ;
329+ {
330+ let mut session = Session :: new ( config, Some ( & mut buf) ) ;
331+ session. format ( input) . unwrap ( ) ;
332+ let errors = ReportedErrors {
333+ has_diff : has_diff,
334+ ..Default :: default ( )
335+ } ;
336+ assert_eq ! ( session. errors, errors) ;
337+ }
338+
339+ let mut expected_file = fs:: File :: open ( & expected_filename) . expect ( "couldn't open target" ) ;
340+ let mut expected_text = String :: new ( ) ;
341+ expected_file
342+ . read_to_string ( & mut expected_text)
343+ . expect ( "Failed reading target" ) ;
344+
345+ let output = String :: from_utf8 ( buf) . unwrap ( ) ;
346+ let compare = make_diff ( & expected_text, & output, DIFF_CONTEXT_SIZE ) ;
347+ if !compare. is_empty ( ) {
348+ let mut failures = HashMap :: new ( ) ;
349+ failures. insert ( source. to_owned ( ) , compare) ;
350+ print_mismatches_default_message ( failures) ;
351+ panic ! ( "Text does not match expected output" ) ;
352+ }
353+ }
308354// Idempotence tests. Files in tests/target are checked to be unaltered by
309355// rustfmt.
310356#[ test]
@@ -428,6 +474,30 @@ fn stdin_works_with_modified_lines() {
428474 assert_eq ! ( buf, output. as_bytes( ) ) ;
429475}
430476
477+ /// Ensures that `EmitMode::Json` works with input from `stdin`.
478+ #[ test]
479+ fn stdin_works_with_json ( ) {
480+ init_log ( ) ;
481+ assert_stdin_output (
482+ Path :: new ( "tests/writemode/source/stdin.rs" ) ,
483+ Path :: new ( "tests/writemode/target/stdin.json" ) ,
484+ EmitMode :: Json ,
485+ true ,
486+ ) ;
487+ }
488+
489+ /// Ensures that `EmitMode::Checkstyle` works with input from `stdin`.
490+ #[ test]
491+ fn stdin_works_with_checkstyle ( ) {
492+ init_log ( ) ;
493+ assert_stdin_output (
494+ Path :: new ( "tests/writemode/source/stdin.rs" ) ,
495+ Path :: new ( "tests/writemode/target/stdin.xml" ) ,
496+ EmitMode :: Checkstyle ,
497+ false ,
498+ ) ;
499+ }
500+
431501#[ test]
432502fn stdin_disable_all_formatting_test ( ) {
433503 init_log ( ) ;
@@ -865,3 +935,26 @@ fn verify_check_works() {
865935 . status ( )
866936 . expect ( "run with check option failed" ) ;
867937}
938+
939+ #[ test]
940+ fn verify_check_works_with_stdin ( ) {
941+ init_log ( ) ;
942+
943+ let mut child = Command :: new ( rustfmt ( ) . to_str ( ) . unwrap ( ) )
944+ . arg ( "--check" )
945+ . stdin ( Stdio :: piped ( ) )
946+ . stderr ( Stdio :: piped ( ) )
947+ . spawn ( )
948+ . expect ( "run with check option failed" ) ;
949+
950+ {
951+ let stdin = child. stdin . as_mut ( ) . expect ( "Failed to open stdin" ) ;
952+ stdin
953+ . write_all ( "fn main() {}\n " . as_bytes ( ) )
954+ . expect ( "Failed to write to rustfmt --check" ) ;
955+ }
956+ let output = child
957+ . wait_with_output ( )
958+ . expect ( "Failed to wait on rustfmt child" ) ;
959+ assert ! ( output. status. success( ) ) ;
960+ }
0 commit comments