Skip to content

Commit bda4138

Browse files
committedOct 21, 2024
feat: refine paths arg test
Signed-off-by: Brian McGee <[email protected]>
1 parent 9fc9e67 commit bda4138

File tree

1 file changed

+55
-35
lines changed

1 file changed

+55
-35
lines changed
 

‎cmd/root_test.go

+55-35
Original file line numberDiff line numberDiff line change
@@ -1371,15 +1371,16 @@ func TestPathsArg(t *testing.T) {
13711371
as.NoError(os.Chdir(cwd))
13721372
})
13731373

1374-
// create a project root under a temp dir, in order verify behavior with
1375-
// files inside of temp dir, but outside of the project root
1374+
// create a project root under a temp dir to verify behaviour with files inside the temp dir, but outside the
1375+
// project root
13761376
tempDir := t.TempDir()
13771377
treeRoot := filepath.Join(tempDir, "tree-root")
1378+
13781379
test.TempExamplesInDir(t, treeRoot)
13791380

13801381
configPath := filepath.Join(treeRoot, "/treefmt.toml")
13811382

1382-
// create a file outside of treeRoot
1383+
// create a file outside the treeRoot
13831384
externalFile, err := os.Create(filepath.Join(tempDir, "outside_tree.go"))
13841385
as.NoError(err)
13851386

@@ -1399,55 +1400,74 @@ func TestPathsArg(t *testing.T) {
13991400
test.WriteConfig(t, configPath, cfg)
14001401

14011402
// without any path args
1402-
_, statz, err := treefmt(t)
1403-
as.NoError(err)
1404-
1405-
assertStats(t, as, statz, map[stats.Type]int{
1406-
stats.Traversed: 32,
1407-
stats.Matched: 32,
1408-
stats.Formatted: 32,
1409-
stats.Changed: 0,
1410-
})
1403+
treefmt2(t,
1404+
withNoError(t),
1405+
withStats(t, map[stats.Type]int{
1406+
stats.Traversed: 32,
1407+
stats.Matched: 32,
1408+
stats.Formatted: 32,
1409+
stats.Changed: 0,
1410+
}),
1411+
)
14111412

14121413
// specify some explicit paths
1413-
_, statz, err = treefmt(t, "-c", "elm/elm.json", "haskell/Nested/Foo.hs")
1414-
as.NoError(err)
1415-
1416-
assertStats(t, as, statz, map[stats.Type]int{
1417-
stats.Traversed: 2,
1418-
stats.Matched: 2,
1419-
stats.Formatted: 2,
1420-
stats.Changed: 0,
1421-
})
1414+
treefmt2(t,
1415+
withArgs("elm/elm.json", "haskell/Nested/Foo.hs"),
1416+
withNoError(t),
1417+
withStats(t, map[stats.Type]int{
1418+
stats.Traversed: 2,
1419+
stats.Matched: 2,
1420+
stats.Formatted: 0,
1421+
stats.Changed: 0,
1422+
}),
1423+
)
14221424

14231425
// specify an absolute path
14241426
absoluteInternalPath, err := filepath.Abs("elm/elm.json")
14251427
as.NoError(err)
14261428

1427-
_, statz, err = treefmt(t, "-c", absoluteInternalPath)
1428-
as.NoError(err)
1429-
1430-
assertStats(t, as, statz, map[stats.Type]int{
1431-
stats.Traversed: 1,
1432-
stats.Matched: 1,
1433-
stats.Formatted: 1,
1434-
stats.Changed: 0,
1435-
})
1429+
treefmt2(t,
1430+
withArgs(absoluteInternalPath),
1431+
withNoError(t),
1432+
withStats(t, map[stats.Type]int{
1433+
stats.Traversed: 1,
1434+
stats.Matched: 1,
1435+
stats.Formatted: 0,
1436+
stats.Changed: 0,
1437+
}),
1438+
)
14361439

14371440
// specify a bad path
1438-
_, _, err = treefmt(t, "-c", "elm/elm.json", "haskell/Nested/Bar.hs")
1439-
as.Errorf(err, "path haskell/Nested/Bar.hs not found")
1441+
treefmt2(t,
1442+
withArgs("elm/elm.json", "haskell/Nested/Bar.hs"),
1443+
withError(func(err error) {
1444+
as.Errorf(err, "path haskell/Nested/Bar.hs not found")
1445+
}),
1446+
)
14401447

14411448
// specify an absolute path outside the tree root
14421449
absoluteExternalPath, err := filepath.Abs(externalFile.Name())
14431450
as.NoError(err)
1444-
as.FileExists(absoluteExternalPath, "exernal file must exist")
1445-
_, _, err = treefmt(t, "-c", absoluteExternalPath)
1446-
as.Errorf(err, "path %s not found within the tree root", absoluteExternalPath)
1451+
as.FileExists(absoluteExternalPath, "external file must exist")
1452+
1453+
treefmt2(t,
1454+
withArgs(absoluteExternalPath),
1455+
withError(func(err error) {
1456+
as.Errorf(err, "path %s not found within the tree root", absoluteExternalPath)
1457+
}),
1458+
)
14471459

14481460
// specify a relative path outside the tree root
14491461
relativeExternalPath := "../outside_tree.go"
14501462
as.FileExists(relativeExternalPath, "exernal file must exist")
1463+
1464+
treefmt2(t,
1465+
withArgs(relativeExternalPath),
1466+
withError(func(err error) {
1467+
as.Errorf(err, "path %s not found within the tree root", relativeExternalPath)
1468+
}),
1469+
)
1470+
14511471
_, _, err = treefmt(t, "-c", relativeExternalPath)
14521472
as.Errorf(err, "path %s not found within the tree root", relativeExternalPath)
14531473
}

0 commit comments

Comments
 (0)
Please sign in to comment.