Skip to content

Commit

Permalink
feat: improve prj root env test
Browse files Browse the repository at this point in the history
Signed-off-by: Brian McGee <[email protected]>
  • Loading branch information
brianmcgee committed Oct 21, 2024
1 parent 7909b55 commit d0f5990
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 22 deletions.
63 changes: 41 additions & 22 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions format/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d0f5990

Please sign in to comment.