|
| 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