From d0f5990be4eed2e676deb720b88b1f9ff3b8074d Mon Sep 17 00:00:00 2001 From: Brian McGee Date: Sat, 19 Oct 2024 16:32:01 +0100 Subject: [PATCH] feat: improve prj root env test Signed-off-by: Brian McGee --- cmd/root_test.go | 63 +++++++++++++++++++++++++++++---------------- format/formatter.go | 5 ++++ 2 files changed, 46 insertions(+), 22 deletions(-) diff --git a/cmd/root_test.go b/cmd/root_test.go index b59b8c30..b79ea153 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -294,7 +294,9 @@ func TestIncludesAndExcludes(t *testing.T) { as := require.New(t) tempDir := test.TempExamples(t) - configPath := tempDir + "/touch.toml" + configPath := tempDir + "/treefmt.toml" + + test.ChangeWorkDir(t, tempDir) // test without any excludes cfg := &config.Config{ @@ -410,29 +412,28 @@ func TestPrjRootEnvVariable(t *testing.T) { as := require.New(t) tempDir := test.TempExamples(t) - configPath := tempDir + "/treefmt.toml" - - // test without any excludes - cfg := &config.Config{ - FormatterConfigs: map[string]*config.Formatter{ - "echo": { - Command: "echo", - Includes: []string{"*"}, - }, - }, - } + configPath := filepath.Join(tempDir, "treefmt.toml") - test.WriteConfig(t, configPath, cfg) t.Setenv("PRJ_ROOT", tempDir) - _, statz, err := treefmt(t, "--config-file", configPath) - as.NoError(err) - assertStats(t, as, statz, map[stats.Type]int{ - stats.Traversed: 32, - stats.Matched: 32, - stats.Formatted: 32, - stats.Changed: 0, - }) + treefmt2(t, + withConfig(configPath, &config.Config{ + FormatterConfigs: map[string]*config.Formatter{ + "echo": { + Command: "echo", + Includes: []string{"*"}, + }, + }, + }), + withArgs("--config-file", configPath), + withNoError(as), + withStats(as, map[stats.Type]int{ + stats.Traversed: 32, + stats.Matched: 32, + stats.Formatted: 32, + stats.Changed: 0, + }), + ) } func TestCache(t *testing.T) { @@ -1478,7 +1479,13 @@ func assertStats( } type options struct { - args []string + args []string + + config struct { + path string + value *config.Config + } + assertOut func([]byte) assertError func(error) assertStats func(*stats.Stats) @@ -1498,6 +1505,13 @@ func withArgs(args ...string) option { } } +func withConfig(path string, cfg *config.Config) option { + return func(o *options) { + o.config.path = path + o.config.value = cfg + } +} + func withStats(as *require.Assertions, expected map[stats.Type]int) option { return func(o *options) { o.assertStats = func(s *stats.Stats) { @@ -1555,6 +1569,11 @@ func treefmt2( args = []string{} } + // write config + if opts.config.value != nil { + test.WriteConfig(t, opts.config.path, opts.config.value) + } + // bump mod times before running if opts.bump.path != "" { test.LutimesBump(t, opts.bump.path, opts.bump.atime, opts.bump.modtime) diff --git a/format/formatter.go b/format/formatter.go index 1323b773..c207303c 100644 --- a/format/formatter.go +++ b/format/formatter.go @@ -167,6 +167,11 @@ func newFormatter( f.log = log.WithPrefix(fmt.Sprintf("formatter | %s", name)) } + // check there is at least one include + if len(cfg.Includes) == 0 { + return nil, fmt.Errorf("formatter '%v' has no includes", f.name) + } + f.includes, err = compileGlobs(cfg.Includes) if err != nil { return nil, fmt.Errorf("failed to compile formatter '%v' includes: %w", f.name, err)