From 85ac1717f70e3090aabe9158d63b46d02c59dea3 Mon Sep 17 00:00:00 2001 From: Brian McGee Date: Tue, 2 Jan 2024 12:11:39 +0000 Subject: [PATCH] feat: add includes and excludes tests Signed-off-by: Brian McGee --- internal/cli/format_test.go | 73 +++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/internal/cli/format_test.go b/internal/cli/format_test.go index f1172b5..3aa319a 100644 --- a/internal/cli/format_test.go +++ b/internal/cli/format_test.go @@ -1,6 +1,7 @@ package cli import ( + "fmt" "testing" "git.numtide.com/numtide/treefmt/internal/test" @@ -77,3 +78,75 @@ func TestSpecifyingFormatters(t *testing.T) { out, err = cmd(t, "--clear-cache", "--config-file", configPath, "--tree-root", tempDir, "--formatters", "bar,foo") as.Errorf(err, "formatter not found in config: bar") } + +func TestIncludesAndExcludes(t *testing.T) { + as := require.New(t) + + tempDir := test.TempExamples(t) + configPath := tempDir + "/echo.toml" + + // test without any excludes + config := format.Config{ + Formatters: map[string]*format.Formatter{ + "echo": { + Command: "echo", + Includes: []string{"*"}, + }, + }, + } + + test.WriteConfig(t, configPath, config) + out, err := cmd(t, "-c", "--config-file", configPath, "--tree-root", tempDir) + as.NoError(err) + as.Contains(string(out), fmt.Sprintf("%d files changed", 29)) + + // globally exclude nix files + config.Global = struct{ Excludes []string }{ + Excludes: []string{"*.nix"}, + } + + test.WriteConfig(t, configPath, config) + out, err = cmd(t, "-c", "--config-file", configPath, "--tree-root", tempDir) + as.NoError(err) + as.Contains(string(out), fmt.Sprintf("%d files changed", 28)) + + // add haskell files to the global exclude + config.Global.Excludes = []string{"*.nix", "*.hs"} + + test.WriteConfig(t, configPath, config) + out, err = cmd(t, "-c", "--config-file", configPath, "--tree-root", tempDir) + as.NoError(err) + as.Contains(string(out), fmt.Sprintf("%d files changed", 22)) + + // remove python files from the echo formatter + config.Formatters["echo"].Excludes = []string{"*.py"} + + test.WriteConfig(t, configPath, config) + out, err = cmd(t, "-c", "--config-file", configPath, "--tree-root", tempDir) + as.NoError(err) + as.Contains(string(out), fmt.Sprintf("%d files changed", 20)) + + // remove go files from the echo formatter + config.Formatters["echo"].Excludes = []string{"*.py", "*.go"} + + test.WriteConfig(t, configPath, config) + out, err = cmd(t, "-c", "--config-file", configPath, "--tree-root", tempDir) + as.NoError(err) + as.Contains(string(out), fmt.Sprintf("%d files changed", 19)) + + // adjust the includes for echo to only include elm files + config.Formatters["echo"].Includes = []string{"*.elm"} + + test.WriteConfig(t, configPath, config) + out, err = cmd(t, "-c", "--config-file", configPath, "--tree-root", tempDir) + as.NoError(err) + as.Contains(string(out), fmt.Sprintf("%d files changed", 1)) + + // add js files to echo formatter + config.Formatters["echo"].Includes = []string{"*.elm", "*.js"} + + test.WriteConfig(t, configPath, config) + out, err = cmd(t, "-c", "--config-file", configPath, "--tree-root", tempDir) + as.NoError(err) + as.Contains(string(out), fmt.Sprintf("%d files changed", 2)) +}