diff --git a/cli/format_test.go b/cli/format_test.go index df9c10c8..e8b48047 100644 --- a/cli/format_test.go +++ b/cli/format_test.go @@ -702,3 +702,43 @@ func TestDeterministicOrderingInPipeline(t *testing.T) { } } } + +func TestRunInSubdir(t *testing.T) { + as := require.New(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") + + // change working directory to sub directory + as.NoError(os.Chdir(filepath.Join(tempDir, "elm"))) + + // basic config + cfg := config.Config{ + Formatters: map[string]*config.Formatter{ + "echo": { + Command: "echo", + Includes: []string{"*"}, + }, + }, + } + test.WriteConfig(t, configPath, cfg) + + // without any path args, should reformat the whole tree + _, err = cmd(t) + as.NoError(err) + assertStats(t, as, 32, 32, 32, 0) + + // specify some explicit paths, relative to the elm/ sub-folder + _, err = cmd(t, "-c", "elm.json", "../haskell/Nested/Foo.hs") + as.NoError(err) + assertStats(t, as, 2, 2, 2, 0) +}