@@ -592,52 +592,100 @@ func TestCache(t *testing.T) {
592
592
func TestChangeWorkingDirectory (t * testing.T ) {
593
593
as := require .New (t )
594
594
595
- // capture current cwd, so we can replace it after the test is finished
596
- cwd , err := os .Getwd ()
597
- as .NoError (err )
598
-
599
- t .Cleanup (func () {
600
- // return to the previous working directory
601
- as .NoError (os .Chdir (cwd ))
602
- })
603
-
604
- tempDir := test .TempExamples (t )
605
- configPath := tempDir + "/treefmt.toml"
606
-
607
- // test without any excludes
608
595
cfg := & config.Config {
609
596
FormatterConfigs : map [string ]* config.Formatter {
610
- "echo" : {
611
- Command : "echo" ,
597
+ "append" : {
598
+ Command : "test-fmt-append" ,
599
+ Options : []string {" " },
612
600
Includes : []string {"*" },
613
601
},
614
602
},
615
603
}
616
604
617
- test .WriteConfig (t , configPath , cfg )
605
+ t .Run ("default" , func (t * testing.T ) {
606
+ // capture current cwd, so we can replace it after the test is finished
607
+ cwd , err := os .Getwd ()
608
+ as .NoError (err )
618
609
619
- // by default, we look for ./treefmt.toml and use the cwd for the tree root
620
- // this should fail if the working directory hasn't been changed first
621
- _ , statz , err := treefmt ( t , "-C" , tempDir )
622
- as . NoError ( err )
610
+ t . Cleanup ( func () {
611
+ // return to the previous working directory
612
+ as . NoError ( os . Chdir ( cwd ) )
613
+ } )
623
614
624
- assertStats (t , as , statz , map [stats.Type ]int {
625
- stats .Traversed : 32 ,
626
- stats .Matched : 32 ,
627
- stats .Formatted : 32 ,
628
- stats .Changed : 0 ,
615
+ tempDir := test .TempExamples (t )
616
+ configPath := filepath .Join (tempDir , "treefmt.toml" )
617
+
618
+ // change to an empty temp dir and try running without specifying a working directory
619
+ as .NoError (os .Chdir (t .TempDir ()))
620
+
621
+ treefmt2 (t ,
622
+ withConfig (configPath , cfg ),
623
+ withError (func (err error ) {
624
+ as .ErrorContains (err , "failed to find treefmt config file" )
625
+ }),
626
+ )
627
+
628
+ // now change to the examples temp directory
629
+ as .NoError (os .Chdir (tempDir ), "failed to change to temp directory" )
630
+
631
+ treefmt2 (t ,
632
+ withConfig (configPath , cfg ),
633
+ withNoError (as ),
634
+ withStats (as , map [stats.Type ]int {
635
+ stats .Traversed : 32 ,
636
+ }),
637
+ )
629
638
})
630
639
631
- // use env
632
- t .Setenv ("TREEFMT_WORKING_DIR" , tempDir )
633
- _ , statz , err = treefmt (t , "-c" )
634
- as .NoError (err )
640
+ execute := func (t * testing.T , configFile string , env bool ) {
641
+ t .Run (configFile , func (t * testing.T ) {
642
+ // capture current cwd, so we can replace it after the test is finished
643
+ cwd , err := os .Getwd ()
644
+ as .NoError (err )
635
645
636
- assertStats (t , as , statz , map [stats.Type ]int {
637
- stats .Traversed : 32 ,
638
- stats .Matched : 32 ,
639
- stats .Formatted : 32 ,
640
- stats .Changed : 0 ,
646
+ t .Cleanup (func () {
647
+ // return to the previous working directory
648
+ as .NoError (os .Chdir (cwd ))
649
+ })
650
+
651
+ tempDir := test .TempExamples (t )
652
+ configPath := filepath .Join (tempDir , configFile )
653
+
654
+ // delete treefmt.toml that comes with the example folder
655
+ as .NoError (os .Remove (filepath .Join (tempDir , "treefmt.toml" )))
656
+
657
+ var args []string
658
+
659
+ if env {
660
+ t .Setenv ("TREEFMT_WORKING_DIR" , tempDir )
661
+ } else {
662
+ args = []string {"-C" , tempDir }
663
+ }
664
+
665
+ treefmt2 (t ,
666
+ withArgs (args ... ),
667
+ withConfig (configPath , cfg ),
668
+ withNoError (as ),
669
+ withStats (as , map [stats.Type ]int {
670
+ stats .Traversed : 32 ,
671
+ }),
672
+ )
673
+ })
674
+ }
675
+
676
+ // by default, we look for a config file at ./treefmt.toml or ./.treefmt.toml in the current working directory
677
+ configFiles := []string {"treefmt.toml" , ".treefmt.toml" }
678
+
679
+ t .Run ("arg" , func (t * testing.T ) {
680
+ for _ , configFile := range configFiles {
681
+ execute (t , configFile , false )
682
+ }
683
+ })
684
+
685
+ t .Run ("env" , func (t * testing.T ) {
686
+ for _ , configFile := range configFiles {
687
+ execute (t , configFile , true )
688
+ }
641
689
})
642
690
}
643
691
0 commit comments