@@ -9,12 +9,13 @@ import (
9
9
"path"
10
10
"path/filepath"
11
11
"regexp"
12
+ "strings"
12
13
"testing"
13
14
"time"
14
15
15
16
"github.com/charmbracelet/log"
16
17
"github.com/numtide/treefmt/cmd"
17
- format2 "github.com/numtide/treefmt/cmd/format"
18
+ formatCmd "github.com/numtide/treefmt/cmd/format"
18
19
"github.com/numtide/treefmt/config"
19
20
"github.com/numtide/treefmt/format"
20
21
"github.com/numtide/treefmt/stats"
@@ -482,7 +483,7 @@ func TestCache(t *testing.T) {
482
483
483
484
// running should match but not format anything
484
485
_ , statz , err = treefmt (t , "--config-file" , configPath , "--tree-root" , tempDir )
485
- as .NoError (err )
486
+ as .ErrorIs (err , formatCmd . ErrFormattingFailures )
486
487
487
488
assertStats (t , as , statz , map [stats.Type ]int {
488
489
stats .Traversed : 32 ,
@@ -492,8 +493,8 @@ func TestCache(t *testing.T) {
492
493
})
493
494
494
495
// running again should provide the same result
495
- _ , statz , err = treefmt (t , "--config-file" , configPath , "--tree-root" , tempDir , "-vv" )
496
- as .NoError (err )
496
+ _ , statz , err = treefmt (t , "--config-file" , configPath , "--tree-root" , tempDir )
497
+ as .ErrorIs (err , formatCmd . ErrFormattingFailures )
497
498
498
499
assertStats (t , as , statz , map [stats.Type ]int {
499
500
stats .Traversed : 32 ,
@@ -588,13 +589,13 @@ func TestFailOnChange(t *testing.T) {
588
589
589
590
test .WriteConfig (t , configPath , cfg )
590
591
_ , _ , err := treefmt (t , "--fail-on-change" , "--config-file" , configPath , "--tree-root" , tempDir )
591
- as .ErrorIs (err , format2 .ErrFailOnChange )
592
+ as .ErrorIs (err , formatCmd .ErrFailOnChange )
592
593
593
594
// test with no cache
594
595
t .Setenv ("TREEFMT_FAIL_ON_CHANGE" , "true" )
595
596
test .WriteConfig (t , configPath , cfg )
596
597
_ , _ , err = treefmt (t , "--config-file" , configPath , "--tree-root" , tempDir , "--no-cache" )
597
- as .ErrorIs (err , format2 .ErrFailOnChange )
598
+ as .ErrorIs (err , formatCmd .ErrFailOnChange )
598
599
}
599
600
600
601
func TestBustCacheOnFormatterChange (t * testing.T ) {
@@ -1027,7 +1028,7 @@ func TestStdin(t *testing.T) {
1027
1028
// we get an error about the missing filename parameter.
1028
1029
out , _ , err := treefmt (t , "-C" , tempDir , "--allow-missing-formatter" , "--stdin" )
1029
1030
as .EqualError (err , "exactly one path should be specified when using the --stdin flag" )
1030
- as .Equal ("" , string (out ))
1031
+ as .Equal ("Error: exactly one path should be specified when using the --stdin flag \n " , string (out ))
1031
1032
1032
1033
// now pass along the filename parameter
1033
1034
os .Stdin = test .TempFile (t , "" , "stdin" , & contents )
@@ -1051,7 +1052,7 @@ func TestStdin(t *testing.T) {
1051
1052
1052
1053
out , _ , err = treefmt (t , "-C" , tempDir , "--allow-missing-formatter" , "--stdin" , "../test.nix" )
1053
1054
as .Errorf (err , "path ../test.nix not inside the tree root %s" , tempDir )
1054
- as .Equal ( "" , string (out ))
1055
+ as .Contains ( string (out ), "Error: path ../test.nix not inside the tree root" )
1055
1056
1056
1057
// try some markdown instead
1057
1058
contents = `
@@ -1239,6 +1240,8 @@ func TestRunInSubdir(t *testing.T) {
1239
1240
func treefmt (t * testing.T , args ... string ) ([]byte , * stats.Stats , error ) {
1240
1241
t .Helper ()
1241
1242
1243
+ t .Logf ("treefmt %s" , strings .Join (args , " " ))
1244
+
1242
1245
tempDir := t .TempDir ()
1243
1246
tempOut := test .TempFile (t , tempDir , "combined_output" , nil )
1244
1247
@@ -1281,21 +1284,22 @@ func treefmt(t *testing.T, args ...string) ([]byte, *stats.Stats, error) {
1281
1284
time .Sleep (time .Until (waitUntil ))
1282
1285
}()
1283
1286
1284
- if err := root .Execute (); err != nil {
1285
- return nil , nil , err
1286
- }
1287
+ // execute the command
1288
+ cmdErr := root .Execute ()
1287
1289
1288
1290
// reset and read the temporary output
1289
- if _ , err := tempOut .Seek (0 , 0 ); err != nil {
1290
- return nil , nil , fmt .Errorf ("failed to reset temp output for reading: %w" , err )
1291
+ if _ , resetErr := tempOut .Seek (0 , 0 ); resetErr != nil {
1292
+ t . Fatal ( fmt .Errorf ("failed to reset temp output for reading: %w" , resetErr ) )
1291
1293
}
1292
1294
1293
- out , err := io .ReadAll (tempOut )
1294
- if err != nil {
1295
- return nil , nil , fmt .Errorf ("failed to read temp output: %w" , err )
1295
+ out , readErr := io .ReadAll (tempOut )
1296
+ if readErr != nil {
1297
+ t . Fatal ( fmt .Errorf ("failed to read temp output: %w" , readErr ) )
1296
1298
}
1297
1299
1298
- return out , statz , nil
1300
+ t .Log (string (out ))
1301
+
1302
+ return out , statz , cmdErr
1299
1303
}
1300
1304
1301
1305
func assertStats (
0 commit comments