Skip to content

Commit

Permalink
fix: relative path resolution in filesystem walker
Browse files Browse the repository at this point in the history
Signed-off-by: Brian McGee <brian@bmcgee.ie>
  • Loading branch information
brianmcgee committed Jul 9, 2024
1 parent 65152cb commit a018c29
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 3 deletions.
6 changes: 3 additions & 3 deletions walk/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ func (f filesystemWalker) Walk(_ context.Context, fn WalkFunc) error {
relPathOffset := len(f.root) + 1

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

Expand Down
95 changes: 95 additions & 0 deletions walk/filesystem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package walk

import (
"context"
"os"
"testing"

"git.numtide.com/numtide/treefmt/test"
"github.com/stretchr/testify/require"
)

var examplesPaths = []string{
".",
"elm",
"elm/elm.json",
"elm/src",
"elm/src/Main.elm",
"go",
"go/go.mod",
"go/main.go",
"haskell",
"haskell/CHANGELOG.md",
"haskell/Foo.hs",
"haskell/Main.hs",
"haskell/Nested",
"haskell/Nested/Foo.hs",
"haskell/Setup.hs",
"haskell/haskell.cabal",
"haskell/treefmt.toml",
"haskell-frontend",
"haskell-frontend/CHANGELOG.md",
"haskell-frontend/Main.hs",
"haskell-frontend/Setup.hs",
"haskell-frontend/haskell-frontend.cabal",
"html",
"html/index.html",
"html/scripts",
"html/scripts/.gitkeep",
"javascript",
"javascript/source",
"javascript/source/hello.js",
"nix",
"nix/sources.nix",
"nixpkgs.toml",
"python",
"python/main.py",
"python/requirements.txt",
"python/virtualenv_proxy.py",
"ruby",
"ruby/bundler.rb",
"rust",
"rust/Cargo.toml",
"rust/src",
"rust/src/main.rs",
"shell",
"shell/foo.sh",
"terraform",
"terraform/main.tf",
"terraform/two.tf",
"touch.toml",
"treefmt.toml",
"yaml",
"yaml/test.yaml",
}

func TestFilesystemWalker_Walk(t *testing.T) {
tempDir := test.TempExamples(t)

paths := make(chan string, 1)
go func() {
paths <- tempDir
close(paths)
}()

as := require.New(t)

walker, err := NewFilesystem(tempDir, paths)
as.NoError(err)

idx := 0
err = walker.Walk(context.Background(), func(file *File, err error) error {
as.Equal(examplesPaths[idx], file.RelPath)
idx += 1
return nil
})
as.NoError(err)

// capture current cwd, so we can replace it after the test is finished
cwd, err := os.Getwd()
as.NoError(err)
t.Cleanup(func() {
// return to the previous working directory
as.NoError(os.Chdir(cwd))
})
}

0 comments on commit a018c29

Please sign in to comment.