Skip to content

Commit 41712b5

Browse files
committed
feat: refine run in subdir test
Signed-off-by: Brian McGee <[email protected]>
1 parent 66bdc82 commit 41712b5

File tree

1 file changed

+28
-29
lines changed

1 file changed

+28
-29
lines changed

cmd/root_test.go

+28-29
Original file line numberDiff line numberDiff line change
@@ -1629,18 +1629,11 @@ func TestRunInSubdir(t *testing.T) {
16291629
// Run the same test for each walk type
16301630
for _, walkType := range walk.TypeValues() {
16311631
t.Run(walkType.String(), func(t *testing.T) {
1632-
// capture current cwd, so we can replace it after the test is finished
1633-
cwd, err := os.Getwd()
1634-
as.NoError(err)
1635-
1636-
t.Cleanup(func() {
1637-
// return to the previous working directory
1638-
as.NoError(os.Chdir(cwd))
1639-
})
1640-
16411632
tempDir := test.TempExamples(t)
16421633
configPath := filepath.Join(tempDir, "/treefmt.toml")
16431634

1635+
test.ChangeWorkDir(t, tempDir)
1636+
16441637
// set the walk type via environment variable
16451638
t.Setenv("TREEFMT_WALK_TYPE", walkType.String())
16461639

@@ -1678,34 +1671,40 @@ func TestRunInSubdir(t *testing.T) {
16781671
},
16791672
},
16801673
}
1674+
16811675
test.WriteConfig(t, configPath, cfg)
16821676

16831677
// without any path args, should reformat the whole tree
1684-
statz, err := treefmt(t)
1685-
as.NoError(err)
1686-
1687-
assertStats(t, as, statz, map[stats.Type]int{
1688-
stats.Traversed: 32,
1689-
stats.Matched: 32,
1690-
stats.Formatted: 32,
1691-
stats.Changed: 0,
1692-
})
1678+
treefmt2(t,
1679+
withNoError(t),
1680+
withStats(t, map[stats.Type]int{
1681+
stats.Traversed: 32,
1682+
stats.Matched: 32,
1683+
stats.Formatted: 32,
1684+
stats.Changed: 0,
1685+
}),
1686+
)
16931687

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

16991697
// specify some explicit paths, relative to the current directory
1700-
statz, err = treefmt(t, "-c", "elm.json", "../haskell/Nested/Foo.hs")
1701-
as.NoError(err)
1702-
1703-
assertStats(t, as, statz, map[stats.Type]int{
1704-
stats.Traversed: 2,
1705-
stats.Matched: 2,
1706-
stats.Formatted: 2,
1707-
stats.Changed: 0,
1708-
})
1698+
treefmt2(t,
1699+
withArgs("-c", "elm.json", "../haskell/Nested/Foo.hs"),
1700+
withNoError(t),
1701+
withStats(t, map[stats.Type]int{
1702+
stats.Traversed: 2,
1703+
stats.Matched: 2,
1704+
stats.Formatted: 2,
1705+
stats.Changed: 0,
1706+
}),
1707+
)
17091708
})
17101709
}
17111710
}

0 commit comments

Comments
 (0)