@@ -79,7 +79,7 @@ func TestOnUnmatched(t *testing.T) {
7979
8080 // should exit with error when using fatal
8181 t .Run ("fatal" , func (t * testing.T ) {
82- errorFn := func (err error ) {
82+ errorFn := func (as * require. Assertions , err error ) {
8383 as .ErrorContains (err , "no formatter for path: " + expectedPaths [0 ])
8484 }
8585
@@ -114,8 +114,8 @@ func TestOnUnmatched(t *testing.T) {
114114
115115 t .Run ("invalid" , func (t * testing.T ) {
116116 // test bad value
117- errorFn := func (arg string ) func (err error ) {
118- return func (err error ) {
117+ errorFn := func (arg string ) func (as * require. Assertions , err error ) {
118+ return func (as * require. Assertions , err error ) {
119119 as .ErrorContains (err , fmt .Sprintf (`invalid level: "%s"` , arg ))
120120 }
121121 }
@@ -153,7 +153,7 @@ func TestQuiet(t *testing.T) {
153153 t .Setenv ("TREEFMT_ALLOW_MISSING_FORMATTER" , "false" )
154154
155155 // check it doesn't suppress errors
156- treefmt (t , withError (func (err error ) {
156+ treefmt (t , withError (func (as * require. Assertions , err error ) {
157157 as .ErrorContains (err , "error looking up 'foo-fmt'" )
158158 }))
159159}
@@ -183,8 +183,6 @@ func TestCpuProfile(t *testing.T) {
183183}
184184
185185func TestAllowMissingFormatter (t * testing.T ) {
186- as := require .New (t )
187-
188186 tempDir := test .TempExamples (t )
189187 configPath := filepath .Join (tempDir , "treefmt.toml" )
190188
@@ -200,7 +198,7 @@ func TestAllowMissingFormatter(t *testing.T) {
200198
201199 t .Run ("default" , func (t * testing.T ) {
202200 treefmt (t ,
203- withError (func (err error ) {
201+ withError (func (as * require. Assertions , err error ) {
204202 as .ErrorIs (err , format .ErrCommandNotFound )
205203 }),
206204 )
@@ -226,8 +224,6 @@ func TestAllowMissingFormatter(t *testing.T) {
226224}
227225
228226func TestSpecifyingFormatters (t * testing.T ) {
229- as := require .New (t )
230-
231227 // we use the test formatter to append some whitespace
232228 cfg := & config.Config {
233229 FormatterConfigs : map [string ]* config.Formatter {
@@ -308,8 +304,8 @@ func TestSpecifyingFormatters(t *testing.T) {
308304 // bad name
309305 treefmt (t ,
310306 withArgs ("--formatters" , "foo" ),
311- withError (func (err error ) {
312- as .Errorf (err , "formatter not found in config: foo " )
307+ withError (func (as * require. Assertions , err error ) {
308+ as .ErrorContains (err , "formatter foo not found in config" )
313309 }),
314310 )
315311 })
@@ -331,8 +327,8 @@ func TestSpecifyingFormatters(t *testing.T) {
331327 t .Setenv ("TREEFMT_FORMATTERS" , "bar,foo" )
332328
333329 treefmt (t ,
334- withError (func (err error ) {
335- as .Errorf (err , "formatter not found in config: bar " )
330+ withError (func (as * require. Assertions , err error ) {
331+ as .ErrorContains (err , "formatter bar not found in config" )
336332 }),
337333 )
338334 })
@@ -541,7 +537,7 @@ func TestConfigFile(t *testing.T) {
541537 withEnv (map [string ]string {
542538 "PRJ_ROOT" : configSubDir ,
543539 }),
544- withError (func (err error ) {
540+ withError (func (as * require. Assertions , err error ) {
545541 as .ErrorContains (err , "failed to find treefmt config file" )
546542 }),
547543 )
@@ -550,8 +546,6 @@ func TestConfigFile(t *testing.T) {
550546}
551547
552548func TestCache (t * testing.T ) {
553- as := require .New (t )
554-
555549 tempDir := test .TempExamples (t )
556550 configPath := filepath .Join (tempDir , "treefmt.toml" )
557551
@@ -657,7 +651,7 @@ func TestCache(t *testing.T) {
657651 // running should match but not format anything
658652
659653 treefmt (t ,
660- withError (func (err error ) {
654+ withError (func (as * require. Assertions , err error ) {
661655 as .ErrorIs (err , format .ErrFormattingFailures )
662656 }),
663657 withStats (t , map [stats.Type ]int {
@@ -670,7 +664,7 @@ func TestCache(t *testing.T) {
670664
671665 // running again should provide the same result
672666 treefmt (t ,
673- withError (func (err error ) {
667+ withError (func (as * require. Assertions , err error ) {
674668 as .ErrorIs (err , format .ErrFormattingFailures )
675669 }),
676670 withStats (t , map [stats.Type ]int {
@@ -733,7 +727,7 @@ func TestChangeWorkingDirectory(t *testing.T) {
733727
734728 treefmt (t ,
735729 withConfig (configPath , cfg ),
736- withError (func (err error ) {
730+ withError (func (as * require. Assertions , err error ) {
737731 as .ErrorContains (err , "failed to find treefmt config file" )
738732 }),
739733 )
@@ -804,8 +798,6 @@ func TestChangeWorkingDirectory(t *testing.T) {
804798}
805799
806800func TestFailOnChange (t * testing.T ) {
807- as := require .New (t )
808-
809801 t .Run ("change size" , func (t * testing.T ) {
810802 tempDir := test .TempExamples (t )
811803 configPath := filepath .Join (tempDir , "treefmt.toml" )
@@ -829,7 +821,7 @@ func TestFailOnChange(t *testing.T) {
829821 treefmt (t ,
830822 withArgs ("--fail-on-change" ),
831823 withConfig (configPath , cfg ),
832- withError (func (err error ) {
824+ withError (func (as * require. Assertions , err error ) {
833825 as .ErrorIs (err , formatCmd .ErrFailOnChange )
834826 }),
835827 withStats (t , map [stats.Type ]int {
@@ -890,7 +882,7 @@ func TestFailOnChange(t *testing.T) {
890882 },
891883 }
892884 }),
893- withError (func (err error ) {
885+ withError (func (as * require. Assertions , err error ) {
894886 as .ErrorIs (err , formatCmd .ErrFailOnChange )
895887 }),
896888 withStats (t , map [stats.Type ]int {
@@ -1428,7 +1420,7 @@ func TestGit(t *testing.T) {
14281420 treefmt (t ,
14291421 withArgs ("-C" , tempDir , "haskell" , "foo" ),
14301422 withConfig (configPath , cfg ),
1431- withError (func (err error ) {
1423+ withError (func (as * require. Assertions , err error ) {
14321424 as .ErrorContains (err , "path foo not found" )
14331425 }),
14341426 )
@@ -1555,8 +1547,8 @@ func TestPathsArg(t *testing.T) {
15551547 // specify a bad path
15561548 treefmt (t ,
15571549 withArgs ("elm/elm.json" , "haskell/Nested/Bar.hs" ),
1558- withError (func (err error ) {
1559- as .Errorf (err , "path haskell/Nested/Bar.hs not found" )
1550+ withError (func (as * require. Assertions , err error ) {
1551+ as .ErrorContains (err , "path haskell/Nested/Bar.hs not found" )
15601552 }),
15611553 )
15621554
@@ -1567,8 +1559,8 @@ func TestPathsArg(t *testing.T) {
15671559
15681560 treefmt (t ,
15691561 withArgs (absoluteExternalPath ),
1570- withError (func (err error ) {
1571- as .Errorf (err , "path %s not found within the tree root" , absoluteExternalPath )
1562+ withError (func (as * require. Assertions , err error ) {
1563+ as .ErrorContains (err , fmt . Sprintf ( "path %s not inside the tree root" , absoluteExternalPath ) )
15721564 }),
15731565 )
15741566
@@ -1578,8 +1570,8 @@ func TestPathsArg(t *testing.T) {
15781570
15791571 treefmt (t ,
15801572 withArgs (relativeExternalPath ),
1581- withError (func (err error ) {
1582- as .Errorf (err , "path %s not found within the tree root" , relativeExternalPath )
1573+ withError (func (as * require. Assertions , err error ) {
1574+ as .ErrorContains (err , fmt . Sprintf ( "path %s not inside the tree root" , relativeExternalPath ) )
15831575 }),
15841576 )
15851577}
@@ -1607,7 +1599,7 @@ func TestStdin(t *testing.T) {
16071599 // we get an error about the missing filename parameter.
16081600 treefmt (t ,
16091601 withArgs ("--stdin" ),
1610- withError (func (err error ) {
1602+ withError (func (as * require. Assertions , err error ) {
16111603 as .EqualError (err , "exactly one path should be specified when using the --stdin flag" )
16121604 }),
16131605 withStderr (func (out []byte ) {
@@ -1640,8 +1632,8 @@ func TestStdin(t *testing.T) {
16401632
16411633 treefmt (t ,
16421634 withArgs ("--stdin" , "../test.nix" ),
1643- withError (func (err error ) {
1644- as .Errorf (err , "path ../test.nix not inside the tree root %s" , tempDir )
1635+ withError (func (as * require. Assertions , err error ) {
1636+ as .ErrorContains (err , fmt . Sprintf ( "path ../test.nix not inside the tree root %s" , tempDir ) )
16451637 }),
16461638 withStderr (func (out []byte ) {
16471639 as .Contains (string (out ), "Error: path ../test.nix not inside the tree root" )
@@ -1804,7 +1796,7 @@ func TestRunInSubdir(t *testing.T) {
18041796 // this should not work, as we're in a subdirectory
18051797 treefmt (t ,
18061798 withArgs ("-c" , "elm/elm.json" , "haskell/Nested/Foo.hs" ),
1807- withError (func (err error ) {
1799+ withError (func (as * require. Assertions , err error ) {
18081800 as .ErrorContains (err , "path elm/elm.json not found" )
18091801 }),
18101802 )
@@ -1836,7 +1828,7 @@ type options struct {
18361828 assertStdout func ([]byte )
18371829 assertStderr func ([]byte )
18381830
1839- assertError func (error )
1831+ assertError func (* require. Assertions , error )
18401832 assertStats func (* stats.Stats )
18411833
18421834 bump struct {
@@ -1886,7 +1878,7 @@ func withStats(t *testing.T, expected map[stats.Type]int) option {
18861878 }
18871879}
18881880
1889- func withError (fn func (error )) option {
1881+ func withError (fn func (* require. Assertions , error )) option {
18901882 return func (o * options ) {
18911883 o .assertError = fn
18921884 }
@@ -1896,8 +1888,8 @@ func withNoError(t *testing.T) option {
18961888 t .Helper ()
18971889
18981890 return func (o * options ) {
1899- o .assertError = func (err error ) {
1900- require .NoError (t , err )
1891+ o .assertError = func (as * require. Assertions , err error ) {
1892+ as .NoError (err )
19011893 }
19021894 }
19031895}
@@ -1928,6 +1920,8 @@ func treefmt(
19281920) {
19291921 t .Helper ()
19301922
1923+ as := require .New (t )
1924+
19311925 // build options
19321926 opts := & options {}
19331927 for _ , option := range opt {
@@ -2037,6 +2031,6 @@ func treefmt(
20372031 }
20382032
20392033 if opts .assertError != nil {
2040- opts .assertError (cmdErr )
2034+ opts .assertError (as , cmdErr )
20412035 }
20422036}
0 commit comments