@@ -38,6 +38,8 @@ func TestOnUnmatched(t *testing.T) {
38
38
39
39
tempDir := test .TempExamples (t )
40
40
41
+ as .NoError (os .Chdir (tempDir ), "failed to change to temp dir" )
42
+
41
43
paths := []string {
42
44
"go/go.mod" ,
43
45
"haskell/haskell.cabal" ,
@@ -51,38 +53,77 @@ func TestOnUnmatched(t *testing.T) {
51
53
// - "haskell/treefmt.toml"
52
54
}
53
55
54
- _ , _ , err = treefmt (t , "-C" , tempDir , "--allow-missing-formatter" , "--on-unmatched" , "fatal" )
55
- as .ErrorContains (err , fmt .Sprintf ("no formatter for path: %s" , paths [0 ]))
56
+ // allow missing formatter
57
+ t .Setenv ("TREEFMT_ALLOW_MISSING_FORMATTER" , "true" )
58
+
59
+ checkOutput := func (level log.Level ) func ([]byte , * stats.Stats , error ) {
60
+ logPrefix := strings .ToUpper (level .String ())[:4 ]
61
+
62
+ return func (out []byte , _ * stats.Stats , err error ) {
63
+ as .NoError (err )
56
64
57
- checkOutput := func ( level string , output [] byte ) {
58
- for _ , p := range paths {
59
- as . Contains ( string ( output ), fmt . Sprintf ( "%s no formatter for path: %s" , level , p ))
65
+ for _ , p := range paths {
66
+ as . Contains ( string ( out ), fmt . Sprintf ( "%s no formatter for path: %s" , logPrefix , p ))
67
+ }
60
68
}
61
69
}
62
70
63
- var out []byte
71
+ // default is WARN
72
+ t .Run ("default" , func (t * testing.T ) {
73
+ treefmt2 (t , args (), checkOutput (log .WarnLevel ))
74
+ })
64
75
65
- // default is warn
66
- out , _ , err = treefmt (t , "-C" , tempDir , "--allow-missing-formatter" )
67
- as .NoError (err )
68
- checkOutput ("WARN" , out )
76
+ // should exit with error when using fatal
77
+ t .Run ("fatal" , func (t * testing.T ) {
78
+ treefmt2 (
79
+ t , args ("--on-unmatched" , "fatal" ),
80
+ func (_ []byte , _ * stats.Stats , err error ) {
81
+ as .ErrorContains (err , fmt .Sprintf ("no formatter for path: %s" , paths [0 ]))
82
+ },
83
+ )
69
84
70
- out , _ , err = treefmt (t , "-C" , tempDir , "--allow-missing-formatter" , "--on-unmatched" , "warn" )
71
- as .NoError (err )
72
- checkOutput ("WARN" , out )
85
+ t .Setenv ("TREEFMT_ON_UNMATCHED" , "fatal" )
73
86
74
- out , _ , err = treefmt (t , "-C" , tempDir , "--allow-missing-formatter" , "-u" , "error" )
75
- as .NoError (err )
76
- checkOutput ("ERRO" , out )
87
+ treefmt2 (
88
+ t , args (),
89
+ func (_ []byte , _ * stats.Stats , err error ) {
90
+ as .ErrorContains (err , fmt .Sprintf ("no formatter for path: %s" , paths [0 ]))
91
+ },
92
+ )
93
+ })
77
94
78
- out , _ , err = treefmt (t , "-C" , tempDir , "--allow-missing-formatter" , "-v" , "--on-unmatched" , "info" )
79
- as .NoError (err )
80
- checkOutput ("INFO" , out )
95
+ // test other levels
96
+ for _ , levelStr := range []string {"debug" , "info" , "warn" , "error" } {
97
+ t .Run (levelStr , func (t * testing.T ) {
98
+ level , err := log .ParseLevel (levelStr )
99
+ as .NoError (err , "failed to parse log level: %s" , level )
81
100
82
- t .Setenv ("TREEFMT_ON_UNMATCHED" , "debug" )
83
- out , _ , err = treefmt (t , "-C" , tempDir , "--allow-missing-formatter" , "-vv" )
84
- as .NoError (err )
85
- checkOutput ("DEBU" , out )
101
+ // otherwise, we check the log output
102
+ treefmt2 (t , args ("-vv" , "--on-unmatched" , levelStr ), checkOutput (level ))
103
+
104
+ t .Setenv ("TREEFMT_ON_UNMATCHED" , levelStr )
105
+ treefmt2 (t , args ("-vv" ), checkOutput (level ))
106
+ })
107
+ }
108
+
109
+ t .Run ("invalid" , func (t * testing.T ) {
110
+ // test bad value
111
+ treefmt2 (
112
+ t , args ("--on-unmatched" , "foo" ),
113
+ func (_ []byte , _ * stats.Stats , err error ) {
114
+ as .ErrorContains (err , fmt .Sprintf (`invalid level: "%s"` , "foo" ))
115
+ },
116
+ )
117
+
118
+ t .Setenv ("TREEFMT_ON_UNMATCHED" , "bar" )
119
+
120
+ treefmt2 (
121
+ t , args (),
122
+ func (_ []byte , _ * stats.Stats , err error ) {
123
+ as .ErrorContains (err , fmt .Sprintf (`invalid level: "%s"` , "bar" ))
124
+ },
125
+ )
126
+ })
86
127
}
87
128
88
129
func TestCpuProfile (t * testing.T ) {
@@ -1340,6 +1381,65 @@ func TestRunInSubdir(t *testing.T) {
1340
1381
}
1341
1382
}
1342
1383
1384
+ func args (args ... string ) []string {
1385
+ return args
1386
+ }
1387
+
1388
+ func treefmt2 (t * testing.T , args []string , fn func (out []byte , statz * stats.Stats , err error )) {
1389
+ t .Helper ()
1390
+
1391
+ t .Logf ("treefmt %s" , strings .Join (args , " " ))
1392
+
1393
+ tempDir := t .TempDir ()
1394
+ tempOut := test .TempFile (t , tempDir , "combined_output" , nil )
1395
+
1396
+ // capture standard outputs before swapping them
1397
+ stdout := os .Stdout
1398
+ stderr := os .Stderr
1399
+
1400
+ // swap them temporarily
1401
+ os .Stdout = tempOut
1402
+ os .Stderr = tempOut
1403
+
1404
+ log .SetOutput (tempOut )
1405
+
1406
+ defer func () {
1407
+ // swap outputs back
1408
+ os .Stdout = stdout
1409
+ os .Stderr = stderr
1410
+ log .SetOutput (stderr )
1411
+ }()
1412
+
1413
+ // run the command
1414
+ root , statz := cmd .NewRoot ()
1415
+
1416
+ if args == nil {
1417
+ // we must pass an empty array otherwise cobra with use os.Args[1:]
1418
+ args = []string {}
1419
+ }
1420
+
1421
+ root .SetArgs (args )
1422
+ root .SetOut (tempOut )
1423
+ root .SetErr (tempOut )
1424
+
1425
+ // execute the command
1426
+ cmdErr := root .Execute ()
1427
+
1428
+ // reset and read the temporary output
1429
+ if _ , resetErr := tempOut .Seek (0 , 0 ); resetErr != nil {
1430
+ t .Fatal (fmt .Errorf ("failed to reset temp output for reading: %w" , resetErr ))
1431
+ }
1432
+
1433
+ out , readErr := io .ReadAll (tempOut )
1434
+ if readErr != nil {
1435
+ t .Fatal (fmt .Errorf ("failed to read temp output: %w" , readErr ))
1436
+ }
1437
+
1438
+ t .Log ("\n " + string (out ))
1439
+
1440
+ fn (out , statz , cmdErr )
1441
+ }
1442
+
1343
1443
func treefmt (t * testing.T , args ... string ) ([]byte , * stats.Stats , error ) {
1344
1444
t .Helper ()
1345
1445
0 commit comments