@@ -72,9 +72,9 @@ func TestOnUnmatched(t *testing.T) {
7272 }
7373 }
7474
75- // default is WARN
75+ // default is INFO
7676 t .Run ("default" , func (t * testing.T ) {
77- treefmt (t , withNoError (t ), withOutput (checkOutput (log .WarnLevel )))
77+ treefmt (t , withArgs ( "-v" ), withNoError (t ), withStderr (checkOutput (log .InfoLevel )))
7878 })
7979
8080 // should exit with error when using fatal
@@ -99,15 +99,15 @@ func TestOnUnmatched(t *testing.T) {
9999 treefmt (t ,
100100 withArgs ("-vv" , "--on-unmatched" , levelStr ),
101101 withNoError (t ),
102- withOutput (checkOutput (level )),
102+ withStderr (checkOutput (level )),
103103 )
104104
105105 t .Setenv ("TREEFMT_ON_UNMATCHED" , levelStr )
106106
107107 treefmt (t ,
108108 withArgs ("-vv" ),
109109 withNoError (t ),
110- withOutput (checkOutput (level )),
110+ withStderr (checkOutput (level )),
111111 )
112112 })
113113 }
@@ -131,6 +131,33 @@ func TestOnUnmatched(t *testing.T) {
131131 })
132132}
133133
134+ func TestQuiet (t * testing.T ) {
135+ as := require .New (t )
136+ tempDir := test .TempExamples (t )
137+
138+ test .ChangeWorkDir (t , tempDir )
139+
140+ // allow missing formatter
141+ t .Setenv ("TREEFMT_ALLOW_MISSING_FORMATTER" , "true" )
142+
143+ noOutput := func (out []byte ) {
144+ as .Empty (out )
145+ }
146+
147+ treefmt (t , withArgs ("-q" ), withNoError (t ), withStdout (noOutput ), withStderr (noOutput ))
148+ treefmt (t , withArgs ("--quiet" ), withNoError (t ), withStdout (noOutput ), withStderr (noOutput ))
149+
150+ t .Setenv ("TREEFMT_QUIET" , "true" )
151+ treefmt (t , withNoError (t ), withStdout (noOutput ), withStderr (noOutput ))
152+
153+ t .Setenv ("TREEFMT_ALLOW_MISSING_FORMATTER" , "false" )
154+
155+ // check it doesn't suppress errors
156+ treefmt (t , withError (func (err error ) {
157+ as .ErrorContains (err , "error looking up 'foo-fmt'" )
158+ }))
159+ }
160+
134161func TestCpuProfile (t * testing.T ) {
135162 as := require .New (t )
136163 tempDir := test .TempExamples (t )
@@ -1583,7 +1610,7 @@ func TestStdin(t *testing.T) {
15831610 withError (func (err error ) {
15841611 as .EqualError (err , "exactly one path should be specified when using the --stdin flag" )
15851612 }),
1586- withOutput (func (out []byte ) {
1613+ withStderr (func (out []byte ) {
15871614 as .Equal ("Error: exactly one path should be specified when using the --stdin flag\n " , string (out ))
15881615 }),
15891616 )
@@ -1600,7 +1627,7 @@ func TestStdin(t *testing.T) {
16001627 stats .Formatted : 1 ,
16011628 stats .Changed : 1 ,
16021629 }),
1603- withOutput (func (out []byte ) {
1630+ withStdout (func (out []byte ) {
16041631 as .Equal (`{ ...}: "hello"
16051632` , string (out ))
16061633 }),
@@ -1616,7 +1643,7 @@ func TestStdin(t *testing.T) {
16161643 withError (func (err error ) {
16171644 as .Errorf (err , "path ../test.nix not inside the tree root %s" , tempDir )
16181645 }),
1619- withOutput (func (out []byte ) {
1646+ withStderr (func (out []byte ) {
16201647 as .Contains (string (out ), "Error: path ../test.nix not inside the tree root" )
16211648 }),
16221649 )
@@ -1639,7 +1666,7 @@ func TestStdin(t *testing.T) {
16391666 stats .Formatted : 1 ,
16401667 stats .Changed : 1 ,
16411668 }),
1642- withOutput (func (out []byte ) {
1669+ withStdout (func (out []byte ) {
16431670 as .Equal (`| col1 | col2 |
16441671| ------ | --------- |
16451672| nice | fits |
@@ -1806,7 +1833,9 @@ type options struct {
18061833 value * config.Config
18071834 }
18081835
1809- assertOut func ([]byte )
1836+ assertStdout func ([]byte )
1837+ assertStderr func ([]byte )
1838+
18101839 assertError func (error )
18111840 assertStats func (* stats.Stats )
18121841
@@ -1873,9 +1902,15 @@ func withNoError(t *testing.T) option {
18731902 }
18741903}
18751904
1876- func withOutput (fn func ([]byte )) option {
1905+ func withStdout (fn func ([]byte )) option {
1906+ return func (o * options ) {
1907+ o .assertStdout = fn
1908+ }
1909+ }
1910+
1911+ func withStderr (fn func ([]byte )) option {
18771912 return func (o * options ) {
1878- o .assertOut = fn
1913+ o .assertStderr = fn
18791914 }
18801915}
18811916
@@ -1931,17 +1966,19 @@ func treefmt(
19311966 t .Logf ("treefmt %s" , strings .Join (args , " " ))
19321967
19331968 tempDir := t .TempDir ()
1934- tempOut := test .TempFile (t , tempDir , "combined_output" , nil )
1969+
1970+ tempStdout := test .TempFile (t , tempDir , "stdout" , nil )
1971+ tempStderr := test .TempFile (t , tempDir , "stderr" , nil )
19351972
19361973 // capture standard outputs before swapping them
19371974 stdout := os .Stdout
19381975 stderr := os .Stderr
19391976
19401977 // swap them temporarily
1941- os .Stdout = tempOut
1942- os .Stderr = tempOut
1978+ os .Stdout = tempStdout
1979+ os .Stderr = tempStderr
19431980
1944- log .SetOutput (tempOut )
1981+ log .SetOutput (tempStdout )
19451982
19461983 defer func () {
19471984 // swap outputs back
@@ -1954,30 +1991,49 @@ func treefmt(
19541991 root , statz := cmd .NewRoot ()
19551992
19561993 root .SetArgs (args )
1957- root .SetOut (tempOut )
1958- root .SetErr (tempOut )
1994+ root .SetOut (tempStdout )
1995+ root .SetErr (tempStderr )
19591996
19601997 // execute the command
19611998 cmdErr := root .Execute ()
19621999
1963- // reset and read the temporary output
1964- if _ , resetErr := tempOut .Seek (0 , 0 ); resetErr != nil {
2000+ // reset and read the temporary outputs
2001+ if _ , resetErr := tempStdout .Seek (0 , 0 ); resetErr != nil {
19652002 t .Fatal (fmt .Errorf ("failed to reset temp output for reading: %w" , resetErr ))
19662003 }
19672004
1968- out , readErr := io .ReadAll (tempOut )
2005+ if _ , resetErr := tempStderr .Seek (0 , 0 ); resetErr != nil {
2006+ t .Fatal (fmt .Errorf ("failed to reset temp output for reading: %w" , resetErr ))
2007+ }
2008+
2009+ // read back stderr and validate
2010+ out , readErr := io .ReadAll (tempStderr )
19692011 if readErr != nil {
1970- t .Fatal (fmt .Errorf ("failed to read temp output: %w" , readErr ))
2012+ t .Fatal (fmt .Errorf ("failed to read temp stderr: %w" , readErr ))
2013+ }
2014+
2015+ if opts .assertStderr != nil {
2016+ opts .assertStderr (out )
19712017 }
19722018
19732019 t .Log ("\n " + string (out ))
19742020
1975- if opts .assertStats != nil {
1976- opts .assertStats (statz )
2021+ // read back stdout and validate
2022+ out , readErr = io .ReadAll (tempStdout )
2023+ if readErr != nil {
2024+ t .Fatal (fmt .Errorf ("failed to read temp stdout: %w" , readErr ))
19772025 }
19782026
1979- if opts .assertOut != nil {
1980- opts .assertOut (out )
2027+ t .Log ("\n " + string (out ))
2028+
2029+ if opts .assertStdout != nil {
2030+ opts .assertStdout (out )
2031+ }
2032+
2033+ // assert other properties
2034+
2035+ if opts .assertStats != nil {
2036+ opts .assertStats (statz )
19812037 }
19822038
19832039 if opts .assertError != nil {
0 commit comments