feat: add includes and excludes tests

Signed-off-by: Brian McGee <brian@bmcgee.ie>
This commit is contained in:
Brian McGee 2024-01-02 12:11:39 +00:00
parent 56cf0cf979
commit 85ac1717f7
Signed by: brianmcgee
GPG Key ID: D49016E76AD1E8C0

View File

@ -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))
}