@@ -2051,17 +2051,204 @@ fn test_stdin_check_without_fix() {
20512051 let stdout = String :: from_utf8_lossy ( & output. stdout ) ;
20522052 let stderr = String :: from_utf8_lossy ( & output. stderr ) ;
20532053
2054- // Should not output content to stdout in check mode
2055- assert_eq ! ( stdout, "" ) ;
2056- // Should report issues on stderr
2057- assert ! ( stderr. contains( "MD009" ) ) ;
2058- assert ! ( stderr. contains( "trailing spaces" ) ) ;
2054+ // The findings are the answer, so they are on stdout as they are for a run
2055+ // over file arguments.
2056+ assert ! ( stdout. contains( "MD009" ) , "got stdout:\n {stdout}" ) ;
2057+ assert ! ( stdout. contains( "trailing spaces" ) ) ;
20592058 // MD047 is also triggered since input doesn't end with newline
2060- assert ! ( stderr. contains( "Found 3 issue(s)" ) ) ;
2059+ assert ! ( stdout. contains( "Found 3 issue(s)" ) ) ;
2060+ assert_eq ! ( stderr, "" , "check mode leaves stderr for problems" ) ;
2061+ // Check mode never rewrites the document, so the fixed form must not appear.
2062+ assert ! (
2063+ !stdout. contains( "# Test\n " ) ,
2064+ "check mode must not emit fixed content, got stdout:\n {stdout}"
2065+ ) ;
20612066 // Should exit with error due to issues
20622067 assert ! ( !output. status. success( ) ) ;
20632068}
20642069
2070+ /// Run rumdl with `args`, piping `input` to stdin, and return (stdout, stderr).
2071+ fn run_piped ( args : & [ & str ] , input : & str ) -> ( String , String ) {
2072+ use std:: io:: Write ;
2073+ use std:: process:: Stdio ;
2074+
2075+ let mut child = Command :: new ( env ! ( "CARGO_BIN_EXE_rumdl" ) )
2076+ . args ( args)
2077+ . stdin ( Stdio :: piped ( ) )
2078+ . stdout ( Stdio :: piped ( ) )
2079+ . stderr ( Stdio :: piped ( ) )
2080+ . spawn ( )
2081+ . expect ( "failed to spawn rumdl" ) ;
2082+ child
2083+ . stdin
2084+ . take ( )
2085+ . expect ( "stdin was piped" )
2086+ . write_all ( input. as_bytes ( ) )
2087+ . expect ( "failed to write to stdin" ) ;
2088+ let output = child. wait_with_output ( ) . expect ( "failed to wait for rumdl" ) ;
2089+ (
2090+ String :: from_utf8_lossy ( & output. stdout ) . into_owned ( ) ,
2091+ String :: from_utf8_lossy ( & output. stderr ) . into_owned ( ) ,
2092+ )
2093+ }
2094+
2095+ #[ test]
2096+ fn stdin_check_writes_machine_readable_output_to_stdout ( ) {
2097+ // Redirecting stdout is how a machine-readable format is consumed, so the
2098+ // document a caller pipes in must not change which stream carries it.
2099+ let ( stdout, stderr) = run_piped (
2100+ & [
2101+ "check" ,
2102+ "--no-cache" ,
2103+ "--no-config" ,
2104+ "--stdin" ,
2105+ "--output-format" ,
2106+ "json" ,
2107+ ] ,
2108+ "# Test \n " ,
2109+ ) ;
2110+
2111+ let parsed: serde_json:: Value = serde_json:: from_str ( stdout. trim ( ) )
2112+ . unwrap_or_else ( |e| panic ! ( "stdout must be the JSON document: {e}, got stdout:\n {stdout}" ) ) ;
2113+ assert_eq ! (
2114+ parsed. as_array( ) . map( Vec :: len) ,
2115+ Some ( 1 ) ,
2116+ "the trailing-space finding is the whole document, got:\n {stdout}"
2117+ ) ;
2118+ assert_eq ! ( parsed[ 0 ] [ "rule" ] , "MD009" ) ;
2119+ assert_eq ! ( stderr, "" , "nothing is left on stderr to be lost" ) ;
2120+ }
2121+
2122+ #[ test]
2123+ fn stdin_check_keeps_prose_out_of_a_streaming_machine_format ( ) {
2124+ // A batch format writes one document and stops, but a streaming format ends
2125+ // wherever the last finding did, so a trailing summary lands inside the
2126+ // output a caller redirects. Every line of it has to stay machine-readable.
2127+ for format in [ "json-lines" , "github" , "azure" , "pylint" ] {
2128+ let ( stdout, stderr) = run_piped (
2129+ & [
2130+ "check" ,
2131+ "--no-cache" ,
2132+ "--no-config" ,
2133+ "--stdin" ,
2134+ "--output-format" ,
2135+ format,
2136+ ] ,
2137+ "# Test \n " ,
2138+ ) ;
2139+
2140+ assert ! (
2141+ stdout. contains( "MD009" ) ,
2142+ "{format} must still report the finding, got stdout:\n {stdout}"
2143+ ) ;
2144+ assert ! (
2145+ !stdout. contains( "Found 1 issue" ) ,
2146+ "{format} carries no summary prose, got stdout:\n {stdout}"
2147+ ) ;
2148+ assert_eq ! ( stderr, "" , "{format} leaves stderr for problems" ) ;
2149+ }
2150+
2151+ // json-lines is the one whose whole contract is per-line parseability, so
2152+ // check that rather than trusting the absence of one sentence.
2153+ let ( stdout, _) = run_piped (
2154+ & [
2155+ "check" ,
2156+ "--no-cache" ,
2157+ "--no-config" ,
2158+ "--stdin" ,
2159+ "--output-format" ,
2160+ "json-lines" ,
2161+ ] ,
2162+ "# Test \n " ,
2163+ ) ;
2164+ for line in stdout. lines ( ) . filter ( |l| !l. trim ( ) . is_empty ( ) ) {
2165+ serde_json:: from_str :: < serde_json:: Value > ( line)
2166+ . unwrap_or_else ( |e| panic ! ( "every json-lines line is a JSON object: {e}, got line:\n {line}" ) ) ;
2167+ }
2168+
2169+ // The control: a human format still gets the sentence, so the suppression
2170+ // tracks the format rather than switching the summary off everywhere.
2171+ let ( human_stdout, _) = run_piped ( & [ "check" , "--no-cache" , "--no-config" , "--stdin" ] , "# Test \n " ) ;
2172+ assert ! (
2173+ human_stdout. contains( "Found 1 issue" ) ,
2174+ "the default format keeps its summary, got stdout:\n {human_stdout}"
2175+ ) ;
2176+ }
2177+
2178+ #[ test]
2179+ fn stdin_check_moves_findings_to_stderr_on_request ( ) {
2180+ // The control for the default above: --stderr is what redirects them, and
2181+ // it means the stream choice is a decision rather than an accident.
2182+ let args = [ "check" , "--no-cache" , "--no-config" , "--stdin" , "--quiet" ] ;
2183+ let ( default_out, default_err) = run_piped ( & args, "# Test \n " ) ;
2184+ let ( moved_out, moved_err) = run_piped ( & [ args. as_slice ( ) , [ "--stderr" ] . as_slice ( ) ] . concat ( ) , "# Test \n " ) ;
2185+
2186+ assert ! ( default_out. contains( "MD009" ) , "got stdout:\n {default_out}" ) ;
2187+ assert_eq ! ( default_err, "" ) ;
2188+ assert_eq ! ( moved_out, "" ) ;
2189+ assert_eq ! (
2190+ moved_err, default_out,
2191+ "--stderr moves the same text rather than producing different text"
2192+ ) ;
2193+ }
2194+
2195+ #[ test]
2196+ fn stdin_fmt_keeps_stdout_for_the_document ( ) {
2197+ // Fix and format modes are the reason the check-mode stream was ever in
2198+ // question: there stdout carries the rewritten document.
2199+ let ( stdout, stderr) = run_piped ( & [ "fmt" , "--no-cache" , "--no-config" , "--stdin" ] , "#Heading\n " ) ;
2200+
2201+ assert_eq ! ( stdout, "# Heading\n " , "got stderr:\n {stderr}" ) ;
2202+ }
2203+
2204+ #[ test]
2205+ fn stdin_fix_keeps_prose_out_of_a_machine_format ( ) {
2206+ // In fix mode the rewritten document owns stdout, so stderr is where a
2207+ // machine-readable format is read from and the same rule applies to it.
2208+ // MD009 is fixable and MD045 is not, so a summary has something to report.
2209+ let input = "# Test \n \n \n " ;
2210+
2211+ let ( stdout, stderr) = run_piped (
2212+ & [
2213+ "check" ,
2214+ "--fix" ,
2215+ "--no-cache" ,
2216+ "--no-config" ,
2217+ "--stdin" ,
2218+ "--output-format" ,
2219+ "json" ,
2220+ ] ,
2221+ input,
2222+ ) ;
2223+ assert_eq ! ( stdout, "# Test\n \n \n " , "stdout is the fixed document" ) ;
2224+ let parsed: serde_json:: Value = serde_json:: from_str ( stderr. trim ( ) )
2225+ . unwrap_or_else ( |e| panic ! ( "stderr must be the JSON document: {e}, got stderr:\n {stderr}" ) ) ;
2226+ assert_eq ! ( parsed[ 0 ] [ "rule" ] , "MD045" , "the unfixable finding remains" ) ;
2227+
2228+ let ( _, stderr) = run_piped (
2229+ & [
2230+ "fmt" ,
2231+ "--no-cache" ,
2232+ "--no-config" ,
2233+ "--stdin" ,
2234+ "--output-format" ,
2235+ "json-lines" ,
2236+ ] ,
2237+ input,
2238+ ) ;
2239+ for line in stderr. lines ( ) . filter ( |l| !l. trim ( ) . is_empty ( ) ) {
2240+ serde_json:: from_str :: < serde_json:: Value > ( line)
2241+ . unwrap_or_else ( |e| panic ! ( "every json-lines line is a JSON object: {e}, got line:\n {line}" ) ) ;
2242+ }
2243+
2244+ // The control: the default format still reports what the fix did.
2245+ let ( _, human_stderr) = run_piped ( & [ "fmt" , "--no-cache" , "--no-config" , "--stdin" ] , input) ;
2246+ assert ! (
2247+ human_stderr. contains( "issue(s) remaining" ) ,
2248+ "the default format keeps its summary, got stderr:\n {human_stderr}"
2249+ ) ;
2250+ }
2251+
20652252#[ test]
20662253fn test_stdin_formatting_no_issues ( ) {
20672254 let rumdl_exe = env ! ( "CARGO_BIN_EXE_rumdl" ) ;
@@ -2118,12 +2305,10 @@ fn test_stdin_dash_syntax() {
21182305 let stdout = String :: from_utf8_lossy ( & output. stdout ) ;
21192306 let stderr = String :: from_utf8_lossy ( & output. stderr ) ;
21202307
2121- // Should not output content to stdout in check mode
2122- assert_eq ! ( stdout, "" ) ;
2123- // Should report issues on stderr
2124- assert ! ( stderr. contains( "MD009" ) ) ;
2125- assert ! ( stderr. contains( "trailing spaces" ) ) ;
2126- assert ! ( stderr. contains( "Found 3 issue(s)" ) ) ;
2308+ assert ! ( stdout. contains( "MD009" ) , "got stdout:\n {stdout}" ) ;
2309+ assert ! ( stdout. contains( "trailing spaces" ) ) ;
2310+ assert ! ( stdout. contains( "Found 3 issue(s)" ) ) ;
2311+ assert_eq ! ( stderr, "" , "check mode leaves stderr for problems" ) ;
21272312}
21282313
21292314#[ test]
@@ -2150,20 +2335,19 @@ fn test_stdin_filename_flag() {
21502335 let stdout = String :: from_utf8_lossy ( & output. stdout ) ;
21512336 let stderr = String :: from_utf8_lossy ( & output. stderr ) ;
21522337
2153- // Should not output content to stdout in check mode
2154- assert_eq ! ( stdout, "" ) ;
21552338 // Should show the custom filename in error messages
21562339 assert ! (
2157- stderr . contains( "test-file.md:1:" ) ,
2158- "Error message should contain custom filename"
2340+ stdout . contains( "test-file.md:1:" ) ,
2341+ "Error message should contain custom filename, got stdout: \n {stdout} "
21592342 ) ;
21602343 assert ! (
2161- stderr . contains( "test-file.md:3:" ) ,
2344+ stdout . contains( "test-file.md:3:" ) ,
21622345 "Error message should contain custom filename for line 3"
21632346 ) ;
2164- assert ! ( stderr . contains( "in test-file.md" ) , "Summary should use custom filename" ) ;
2347+ assert ! ( stdout . contains( "in test-file.md" ) , "Summary should use custom filename" ) ;
21652348 // Should still detect the issues
2166- assert ! ( stderr. contains( "MD009" ) ) ;
2349+ assert ! ( stdout. contains( "MD009" ) ) ;
2350+ assert_eq ! ( stderr, "" , "check mode leaves stderr for problems" ) ;
21672351}
21682352
21692353#[ test]
@@ -3659,14 +3843,14 @@ fn test_stdin_inline_disable_next_line_is_scoped() {
36593843 drop ( stdin) ;
36603844
36613845 let output = child. wait_with_output ( ) . expect ( "Failed to wait for command" ) ;
3662- let stderr = String :: from_utf8_lossy ( & output. stderr ) ;
3846+ let stdout = String :: from_utf8_lossy ( & output. stdout ) ;
36633847
3664- let md009_warnings: Vec < & str > = stderr . lines ( ) . filter ( |l| l. contains ( "MD009" ) ) . collect ( ) ;
3848+ let md009_warnings: Vec < & str > = stdout . lines ( ) . filter ( |l| l. contains ( "MD009" ) ) . collect ( ) ;
36653849
36663850 assert_eq ! (
36673851 md009_warnings. len( ) ,
36683852 1 ,
3669- "Expected exactly 1 MD009 warning (line 5 only), got {}: {stderr }" ,
3853+ "Expected exactly 1 MD009 warning (line 5 only), got {}: {stdout }" ,
36703854 md009_warnings. len( )
36713855 ) ;
36723856 assert ! (
0 commit comments