Skip to content

Commit a018c29

Browse files
committed
fix: relative path resolution in filesystem walker
Signed-off-by: Brian McGee <[email protected]>
1 parent 65152cb commit a018c29

File tree

2 files changed

+98
-3
lines changed

2 files changed

+98
-3
lines changed

walk/filesystem.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ func (f filesystemWalker) Walk(_ context.Context, fn WalkFunc) error {
2020
relPathOffset := len(f.root) + 1
2121

2222
relPathFn := func(path string) (string, error) {
23-
// quick optimisation for the majority of use cases
24-
// todo check that root is a prefix in path?
25-
if len(path) >= relPathOffset {
23+
// quick optimization for the majority of use cases
24+
if len(path) >= relPathOffset && path[:len(f.root)] == f.root {
2625
return path[relPathOffset:], nil
2726
}
27+
// fallback to proper relative path resolution
2828
return filepath.Rel(f.root, path)
2929
}
3030

walk/filesystem_test.go

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package walk
2+
3+
import (
4+
"context"
5+
"os"
6+
"testing"
7+
8+
"git.numtide.com/numtide/treefmt/test"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
var examplesPaths = []string{
13+
".",
14+
"elm",
15+
"elm/elm.json",
16+
"elm/src",
17+
"elm/src/Main.elm",
18+
"go",
19+
"go/go.mod",
20+
"go/main.go",
21+
"haskell",
22+
"haskell/CHANGELOG.md",
23+
"haskell/Foo.hs",
24+
"haskell/Main.hs",
25+
"haskell/Nested",
26+
"haskell/Nested/Foo.hs",
27+
"haskell/Setup.hs",
28+
"haskell/haskell.cabal",
29+
"haskell/treefmt.toml",
30+
"haskell-frontend",
31+
"haskell-frontend/CHANGELOG.md",
32+
"haskell-frontend/Main.hs",
33+
"haskell-frontend/Setup.hs",
34+
"haskell-frontend/haskell-frontend.cabal",
35+
"html",
36+
"html/index.html",
37+
"html/scripts",
38+
"html/scripts/.gitkeep",
39+
"javascript",
40+
"javascript/source",
41+
"javascript/source/hello.js",
42+
"nix",
43+
"nix/sources.nix",
44+
"nixpkgs.toml",
45+
"python",
46+
"python/main.py",
47+
"python/requirements.txt",
48+
"python/virtualenv_proxy.py",
49+
"ruby",
50+
"ruby/bundler.rb",
51+
"rust",
52+
"rust/Cargo.toml",
53+
"rust/src",
54+
"rust/src/main.rs",
55+
"shell",
56+
"shell/foo.sh",
57+
"terraform",
58+
"terraform/main.tf",
59+
"terraform/two.tf",
60+
"touch.toml",
61+
"treefmt.toml",
62+
"yaml",
63+
"yaml/test.yaml",
64+
}
65+
66+
func TestFilesystemWalker_Walk(t *testing.T) {
67+
tempDir := test.TempExamples(t)
68+
69+
paths := make(chan string, 1)
70+
go func() {
71+
paths <- tempDir
72+
close(paths)
73+
}()
74+
75+
as := require.New(t)
76+
77+
walker, err := NewFilesystem(tempDir, paths)
78+
as.NoError(err)
79+
80+
idx := 0
81+
err = walker.Walk(context.Background(), func(file *File, err error) error {
82+
as.Equal(examplesPaths[idx], file.RelPath)
83+
idx += 1
84+
return nil
85+
})
86+
as.NoError(err)
87+
88+
// capture current cwd, so we can replace it after the test is finished
89+
cwd, err := os.Getwd()
90+
as.NoError(err)
91+
t.Cleanup(func() {
92+
// return to the previous working directory
93+
as.NoError(os.Chdir(cwd))
94+
})
95+
}

0 commit comments

Comments
 (0)