@@ -72,9 +72,9 @@ func TestOnUnmatched(t *testing.T) {
72
72
}
73
73
}
74
74
75
- // default is WARN
75
+ // default is INFO
76
76
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 )))
78
78
})
79
79
80
80
// should exit with error when using fatal
@@ -99,15 +99,15 @@ func TestOnUnmatched(t *testing.T) {
99
99
treefmt (t ,
100
100
withArgs ("-vv" , "--on-unmatched" , levelStr ),
101
101
withNoError (t ),
102
- withOutput (checkOutput (level )),
102
+ withStderr (checkOutput (level )),
103
103
)
104
104
105
105
t .Setenv ("TREEFMT_ON_UNMATCHED" , levelStr )
106
106
107
107
treefmt (t ,
108
108
withArgs ("-vv" ),
109
109
withNoError (t ),
110
- withOutput (checkOutput (level )),
110
+ withStderr (checkOutput (level )),
111
111
)
112
112
})
113
113
}
@@ -131,6 +131,33 @@ func TestOnUnmatched(t *testing.T) {
131
131
})
132
132
}
133
133
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
+
134
161
func TestCpuProfile (t * testing.T ) {
135
162
as := require .New (t )
136
163
tempDir := test .TempExamples (t )
@@ -1583,7 +1610,7 @@ func TestStdin(t *testing.T) {
1583
1610
withError (func (err error ) {
1584
1611
as .EqualError (err , "exactly one path should be specified when using the --stdin flag" )
1585
1612
}),
1586
- withOutput (func (out []byte ) {
1613
+ withStderr (func (out []byte ) {
1587
1614
as .Equal ("Error: exactly one path should be specified when using the --stdin flag\n " , string (out ))
1588
1615
}),
1589
1616
)
@@ -1600,7 +1627,7 @@ func TestStdin(t *testing.T) {
1600
1627
stats .Formatted : 1 ,
1601
1628
stats .Changed : 1 ,
1602
1629
}),
1603
- withOutput (func (out []byte ) {
1630
+ withStdout (func (out []byte ) {
1604
1631
as .Equal (`{ ...}: "hello"
1605
1632
` , string (out ))
1606
1633
}),
@@ -1616,7 +1643,7 @@ func TestStdin(t *testing.T) {
1616
1643
withError (func (err error ) {
1617
1644
as .Errorf (err , "path ../test.nix not inside the tree root %s" , tempDir )
1618
1645
}),
1619
- withOutput (func (out []byte ) {
1646
+ withStderr (func (out []byte ) {
1620
1647
as .Contains (string (out ), "Error: path ../test.nix not inside the tree root" )
1621
1648
}),
1622
1649
)
@@ -1639,7 +1666,7 @@ func TestStdin(t *testing.T) {
1639
1666
stats .Formatted : 1 ,
1640
1667
stats .Changed : 1 ,
1641
1668
}),
1642
- withOutput (func (out []byte ) {
1669
+ withStdout (func (out []byte ) {
1643
1670
as .Equal (`| col1 | col2 |
1644
1671
| ------ | --------- |
1645
1672
| nice | fits |
@@ -1806,7 +1833,9 @@ type options struct {
1806
1833
value * config.Config
1807
1834
}
1808
1835
1809
- assertOut func ([]byte )
1836
+ assertStdout func ([]byte )
1837
+ assertStderr func ([]byte )
1838
+
1810
1839
assertError func (error )
1811
1840
assertStats func (* stats.Stats )
1812
1841
@@ -1873,9 +1902,15 @@ func withNoError(t *testing.T) option {
1873
1902
}
1874
1903
}
1875
1904
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 {
1877
1912
return func (o * options ) {
1878
- o .assertOut = fn
1913
+ o .assertStderr = fn
1879
1914
}
1880
1915
}
1881
1916
@@ -1931,17 +1966,19 @@ func treefmt(
1931
1966
t .Logf ("treefmt %s" , strings .Join (args , " " ))
1932
1967
1933
1968
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 )
1935
1972
1936
1973
// capture standard outputs before swapping them
1937
1974
stdout := os .Stdout
1938
1975
stderr := os .Stderr
1939
1976
1940
1977
// swap them temporarily
1941
- os .Stdout = tempOut
1942
- os .Stderr = tempOut
1978
+ os .Stdout = tempStdout
1979
+ os .Stderr = tempStderr
1943
1980
1944
- log .SetOutput (tempOut )
1981
+ log .SetOutput (tempStdout )
1945
1982
1946
1983
defer func () {
1947
1984
// swap outputs back
@@ -1954,30 +1991,49 @@ func treefmt(
1954
1991
root , statz := cmd .NewRoot ()
1955
1992
1956
1993
root .SetArgs (args )
1957
- root .SetOut (tempOut )
1958
- root .SetErr (tempOut )
1994
+ root .SetOut (tempStdout )
1995
+ root .SetErr (tempStderr )
1959
1996
1960
1997
// execute the command
1961
1998
cmdErr := root .Execute ()
1962
1999
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 {
1965
2002
t .Fatal (fmt .Errorf ("failed to reset temp output for reading: %w" , resetErr ))
1966
2003
}
1967
2004
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 )
1969
2011
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 )
1971
2017
}
1972
2018
1973
2019
t .Log ("\n " + string (out ))
1974
2020
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 ))
1977
2025
}
1978
2026
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 )
1981
2037
}
1982
2038
1983
2039
if opts .assertError != nil {
0 commit comments