@@ -74,7 +74,7 @@ func TestOnUnmatched(t *testing.T) {
74
74
75
75
// default is WARN
76
76
t .Run ("default" , func (t * testing.T ) {
77
- treefmt (t , withNoError (t ), withOutput (checkOutput (log .WarnLevel )))
77
+ treefmt (t , withNoError (t ), withStderr (checkOutput (log .WarnLevel )))
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
}
@@ -1583,7 +1583,7 @@ func TestStdin(t *testing.T) {
1583
1583
withError (func (err error ) {
1584
1584
as .EqualError (err , "exactly one path should be specified when using the --stdin flag" )
1585
1585
}),
1586
- withOutput (func (out []byte ) {
1586
+ withStderr (func (out []byte ) {
1587
1587
as .Equal ("Error: exactly one path should be specified when using the --stdin flag\n " , string (out ))
1588
1588
}),
1589
1589
)
@@ -1600,7 +1600,7 @@ func TestStdin(t *testing.T) {
1600
1600
stats .Formatted : 1 ,
1601
1601
stats .Changed : 1 ,
1602
1602
}),
1603
- withOutput (func (out []byte ) {
1603
+ withStdout (func (out []byte ) {
1604
1604
as .Equal (`{ ...}: "hello"
1605
1605
` , string (out ))
1606
1606
}),
@@ -1616,7 +1616,7 @@ func TestStdin(t *testing.T) {
1616
1616
withError (func (err error ) {
1617
1617
as .Errorf (err , "path ../test.nix not inside the tree root %s" , tempDir )
1618
1618
}),
1619
- withOutput (func (out []byte ) {
1619
+ withStderr (func (out []byte ) {
1620
1620
as .Contains (string (out ), "Error: path ../test.nix not inside the tree root" )
1621
1621
}),
1622
1622
)
@@ -1639,7 +1639,7 @@ func TestStdin(t *testing.T) {
1639
1639
stats .Formatted : 1 ,
1640
1640
stats .Changed : 1 ,
1641
1641
}),
1642
- withOutput (func (out []byte ) {
1642
+ withStdout (func (out []byte ) {
1643
1643
as .Equal (`| col1 | col2 |
1644
1644
| ------ | --------- |
1645
1645
| nice | fits |
@@ -1806,7 +1806,9 @@ type options struct {
1806
1806
value * config.Config
1807
1807
}
1808
1808
1809
- assertOut func ([]byte )
1809
+ assertStdout func ([]byte )
1810
+ assertStderr func ([]byte )
1811
+
1810
1812
assertError func (error )
1811
1813
assertStats func (* stats.Stats )
1812
1814
@@ -1873,9 +1875,15 @@ func withNoError(t *testing.T) option {
1873
1875
}
1874
1876
}
1875
1877
1876
- func withOutput (fn func ([]byte )) option {
1878
+ func withStdout (fn func ([]byte )) option {
1879
+ return func (o * options ) {
1880
+ o .assertStdout = fn
1881
+ }
1882
+ }
1883
+
1884
+ func withStderr (fn func ([]byte )) option {
1877
1885
return func (o * options ) {
1878
- o .assertOut = fn
1886
+ o .assertStderr = fn
1879
1887
}
1880
1888
}
1881
1889
@@ -1931,17 +1939,19 @@ func treefmt(
1931
1939
t .Logf ("treefmt %s" , strings .Join (args , " " ))
1932
1940
1933
1941
tempDir := t .TempDir ()
1934
- tempOut := test .TempFile (t , tempDir , "combined_output" , nil )
1942
+
1943
+ tempStdout := test .TempFile (t , tempDir , "stdout" , nil )
1944
+ tempStderr := test .TempFile (t , tempDir , "stderr" , nil )
1935
1945
1936
1946
// capture standard outputs before swapping them
1937
1947
stdout := os .Stdout
1938
1948
stderr := os .Stderr
1939
1949
1940
1950
// swap them temporarily
1941
- os .Stdout = tempOut
1942
- os .Stderr = tempOut
1951
+ os .Stdout = tempStdout
1952
+ os .Stderr = tempStderr
1943
1953
1944
- log .SetOutput (tempOut )
1954
+ log .SetOutput (tempStdout )
1945
1955
1946
1956
defer func () {
1947
1957
// swap outputs back
@@ -1954,30 +1964,49 @@ func treefmt(
1954
1964
root , statz := cmd .NewRoot ()
1955
1965
1956
1966
root .SetArgs (args )
1957
- root .SetOut (tempOut )
1958
- root .SetErr (tempOut )
1967
+ root .SetOut (tempStdout )
1968
+ root .SetErr (tempStderr )
1959
1969
1960
1970
// execute the command
1961
1971
cmdErr := root .Execute ()
1962
1972
1963
- // reset and read the temporary output
1964
- if _ , resetErr := tempOut .Seek (0 , 0 ); resetErr != nil {
1973
+ // reset and read the temporary outputs
1974
+ if _ , resetErr := tempStdout .Seek (0 , 0 ); resetErr != nil {
1965
1975
t .Fatal (fmt .Errorf ("failed to reset temp output for reading: %w" , resetErr ))
1966
1976
}
1967
1977
1968
- out , readErr := io .ReadAll (tempOut )
1978
+ if _ , resetErr := tempStderr .Seek (0 , 0 ); resetErr != nil {
1979
+ t .Fatal (fmt .Errorf ("failed to reset temp output for reading: %w" , resetErr ))
1980
+ }
1981
+
1982
+ // read back stderr and validate
1983
+ out , readErr := io .ReadAll (tempStderr )
1969
1984
if readErr != nil {
1970
- t .Fatal (fmt .Errorf ("failed to read temp output: %w" , readErr ))
1985
+ t .Fatal (fmt .Errorf ("failed to read temp stderr: %w" , readErr ))
1986
+ }
1987
+
1988
+ if opts .assertStderr != nil {
1989
+ opts .assertStderr (out )
1971
1990
}
1972
1991
1973
1992
t .Log ("\n " + string (out ))
1974
1993
1975
- if opts .assertStats != nil {
1976
- opts .assertStats (statz )
1994
+ // read back stdout and validate
1995
+ out , readErr = io .ReadAll (tempStdout )
1996
+ if readErr != nil {
1997
+ t .Fatal (fmt .Errorf ("failed to read temp stdout: %w" , readErr ))
1998
+ }
1999
+
2000
+ t .Log ("\n " + string (out ))
2001
+
2002
+ if opts .assertStdout != nil {
2003
+ opts .assertStdout (out )
1977
2004
}
1978
2005
1979
- if opts .assertOut != nil {
1980
- opts .assertOut (out )
2006
+ // assert other properties
2007
+
2008
+ if opts .assertStats != nil {
2009
+ opts .assertStats (statz )
1981
2010
}
1982
2011
1983
2012
if opts .assertError != nil {
0 commit comments