@@ -620,7 +620,7 @@ func TestGitWorktree(t *testing.T) {
620
620
621
621
// try with a bad path
622
622
_ , _ , err = treefmt (t , "-C" , tempDir , "-c" , "haskell" , "foo" )
623
- as .ErrorContains (err , "path foo not found within the tree root " )
623
+ as .ErrorContains (err , "path foo not found" )
624
624
625
625
// try with a path not in the git index, e.g. it is skipped
626
626
_ , err = os .Create (filepath .Join (tempDir , "foo.txt" ))
@@ -647,11 +647,20 @@ func TestPathsArg(t *testing.T) {
647
647
as .NoError (os .Chdir (cwd ))
648
648
})
649
649
650
- tempDir := test .TempExamples (t )
651
- configPath := filepath .Join (tempDir , "/treefmt.toml" )
650
+ // create a project root under a temp dir, in order verify behavior with
651
+ // files inside of temp dir, but outside of the project root
652
+ tempDir := t .TempDir ()
653
+ treeRoot := filepath .Join (tempDir , "tree-root" )
654
+ test .TempExamplesInDir (t , treeRoot )
652
655
653
- // change working directory to temp root
654
- as .NoError (os .Chdir (tempDir ))
656
+ configPath := filepath .Join (treeRoot , "/treefmt.toml" )
657
+
658
+ // create a file outside of treeRoot
659
+ externalFile , err := os .Create (filepath .Join (tempDir , "outside_tree.go" ))
660
+ as .NoError (err )
661
+
662
+ // change working directory to project root
663
+ as .NoError (os .Chdir (treeRoot ))
655
664
656
665
// basic config
657
666
cfg := & config.Config {
@@ -675,14 +684,29 @@ func TestPathsArg(t *testing.T) {
675
684
as .NoError (err )
676
685
assertStats (t , as , statz , 2 , 2 , 2 , 0 )
677
686
687
+ // specify an absolute path
688
+ absoluteInternalPath , err := filepath .Abs ("elm/elm.json" )
689
+ as .NoError (err )
690
+ _ , statz , err = treefmt (t , "-c" , absoluteInternalPath )
691
+ as .NoError (err )
692
+ assertStats (t , as , statz , 1 , 1 , 1 , 0 )
693
+
678
694
// specify a bad path
679
695
_ , _ , err = treefmt (t , "-c" , "elm/elm.json" , "haskell/Nested/Bar.hs" )
680
- as .ErrorContains (err , "path haskell/Nested/Bar.hs not found within the tree root " )
696
+ as .Errorf (err , "path haskell/Nested/Bar.hs not found" )
681
697
682
- // specify a path outside the tree root
683
- externalPath := filepath .Join (cwd , "go.mod" )
684
- _ , _ , err = treefmt (t , "-c" , externalPath )
685
- as .ErrorContains (err , fmt .Sprintf ("path %s not found within the tree root" , externalPath ))
698
+ // specify an absolute path outside the tree root
699
+ absoluteExternalPath , err := filepath .Abs (externalFile .Name ())
700
+ as .NoError (err )
701
+ as .FileExists (absoluteExternalPath , "exernal file must exist" )
702
+ _ , _ , err = treefmt (t , "-c" , absoluteExternalPath )
703
+ as .Errorf (err , "path %s not found within the tree root" , absoluteExternalPath )
704
+
705
+ // specify a relative path outside the tree root
706
+ relativeExternalPath := "../outside_tree.go"
707
+ as .FileExists (relativeExternalPath , "exernal file must exist" )
708
+ _ , _ , err = treefmt (t , "-c" , relativeExternalPath )
709
+ as .Errorf (err , "path %s not found within the tree root" , relativeExternalPath )
686
710
}
687
711
688
712
func TestStdin (t * testing.T ) {
@@ -852,7 +876,12 @@ func TestRunInSubdir(t *testing.T) {
852
876
assertStats (t , as , statz , 32 , 32 , 32 , 0 )
853
877
854
878
// specify some explicit paths, relative to the tree root
855
- _ , statz , err = treefmt (t , "-c" , "elm/elm.json" , "haskell/Nested/Foo.hs" )
879
+ // this should not work, as we're in a subdirectory
880
+ _ , _ , err = treefmt (t , "-c" , "elm/elm.json" , "haskell/Nested/Foo.hs" )
881
+ as .ErrorContains (err , "path elm/elm.json not found" )
882
+
883
+ // specify some explicit paths, relative to the current directory
884
+ _ , statz , err = treefmt (t , "-c" , "elm.json" , "../haskell/Nested/Foo.hs" )
856
885
as .NoError (err )
857
886
assertStats (t , as , statz , 2 , 2 , 2 , 0 )
858
887
}
0 commit comments