Skip to content

Commit

Permalink
feat: refine run in subdir test
Browse files Browse the repository at this point in the history
Signed-off-by: Brian McGee <[email protected]>
  • Loading branch information
brianmcgee committed Oct 21, 2024
1 parent 66bdc82 commit 41712b5
Showing 1 changed file with 28 additions and 29 deletions.
57 changes: 28 additions & 29 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1629,18 +1629,11 @@ func TestRunInSubdir(t *testing.T) {
// Run the same test for each walk type
for _, walkType := range walk.TypeValues() {
t.Run(walkType.String(), func(t *testing.T) {
// 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))
})

tempDir := test.TempExamples(t)
configPath := filepath.Join(tempDir, "/treefmt.toml")

test.ChangeWorkDir(t, tempDir)

// set the walk type via environment variable
t.Setenv("TREEFMT_WALK_TYPE", walkType.String())

Expand Down Expand Up @@ -1678,34 +1671,40 @@ func TestRunInSubdir(t *testing.T) {
},
},
}

test.WriteConfig(t, configPath, cfg)

// without any path args, should reformat the whole tree
statz, err := treefmt(t)
as.NoError(err)

assertStats(t, as, statz, map[stats.Type]int{
stats.Traversed: 32,
stats.Matched: 32,
stats.Formatted: 32,
stats.Changed: 0,
})
treefmt2(t,
withNoError(t),
withStats(t, map[stats.Type]int{
stats.Traversed: 32,
stats.Matched: 32,
stats.Formatted: 32,
stats.Changed: 0,
}),
)

// specify some explicit paths, relative to the tree root
// this should not work, as we're in a subdirectory
_, err = treefmt(t, "-c", "elm/elm.json", "haskell/Nested/Foo.hs")
as.ErrorContains(err, "path elm/elm.json not found")
treefmt2(t,
withArgs("-c", "elm/elm.json", "haskell/Nested/Foo.hs"),
withError(func(err error) {
as.ErrorContains(err, "path elm/elm.json not found")
}),
)

// specify some explicit paths, relative to the current directory
statz, err = treefmt(t, "-c", "elm.json", "../haskell/Nested/Foo.hs")
as.NoError(err)

assertStats(t, as, statz, map[stats.Type]int{
stats.Traversed: 2,
stats.Matched: 2,
stats.Formatted: 2,
stats.Changed: 0,
})
treefmt2(t,
withArgs("-c", "elm.json", "../haskell/Nested/Foo.hs"),
withNoError(t),
withStats(t, map[stats.Type]int{
stats.Traversed: 2,
stats.Matched: 2,
stats.Formatted: 2,
stats.Changed: 0,
}),
)
})
}
}
Expand Down

0 comments on commit 41712b5

Please sign in to comment.