Skip to content

Commit

Permalink
feat: improve specifying formatters test
Browse files Browse the repository at this point in the history
Signed-off-by: Brian McGee <[email protected]>
  • Loading branch information
brianmcgee committed May 10, 2024
1 parent 53ea16a commit 92321c8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
26 changes: 20 additions & 6 deletions cli/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,44 +65,58 @@ func TestAllowMissingFormatter(t *testing.T) {
func TestSpecifyingFormatters(t *testing.T) {
as := require.New(t)

tempDir := test.TempExamples(t)
configPath := tempDir + "/treefmt.toml"

test.WriteConfig(t, configPath, config2.Config{
cfg := config2.Config{
Formatters: map[string]*config2.Formatter{
"elm": {
Command: "touch",
Options: []string{"-m"},
Includes: []string{"*.elm"},
},
"nix": {
Command: "touch",
Options: []string{"-m"},
Includes: []string{"*.nix"},
},
"ruby": {
Command: "touch",
Options: []string{"-m"},
Includes: []string{"*.rb"},
},
},
})
}

var tempDir, configPath string

// we reset the temp dir between successive runs as it appears that touching the file and modifying the mtime can
// is not granular enough between assertions in quick succession
setup := func() {
tempDir = test.TempExamples(t)
configPath = tempDir + "/treefmt.toml"
test.WriteConfig(t, configPath, cfg)
}

setup()
_, err := cmd(t, "-c", "--config-file", configPath, "--tree-root", tempDir)
as.NoError(err)
assertStats(t, as, 31, 31, 3, 3)

setup()
_, err = cmd(t, "-c", "--config-file", configPath, "--tree-root", tempDir, "--formatters", "elm,nix")
as.NoError(err)
assertStats(t, as, 31, 31, 2, 2)

setup()
_, err = cmd(t, "-c", "--config-file", configPath, "--tree-root", tempDir, "--formatters", "ruby,nix")
as.NoError(err)
assertStats(t, as, 31, 31, 2, 2)

setup()
_, err = cmd(t, "-c", "--config-file", configPath, "--tree-root", tempDir, "--formatters", "nix")
as.NoError(err)
assertStats(t, as, 31, 31, 1, 1)

// test bad names

setup()
_, err = cmd(t, "-c", "--config-file", configPath, "--tree-root", tempDir, "--formatters", "foo")
as.Errorf(err, "formatter not found in config: foo")

Expand Down
2 changes: 1 addition & 1 deletion format/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (f *Formatter) Apply(ctx context.Context, files []*walk.File, filter bool)
if len(out) > 0 {
_, _ = fmt.Fprintf(os.Stderr, "%s error:\n%s\n", f.name, out)
}
return fmt.Errorf("formatter %s failed to apply: %w", f.name, err)
return fmt.Errorf("formatter '%s' with options '%v' failed to apply: %w", f.config.Command, f.config.Options, err)
}

//
Expand Down
2 changes: 2 additions & 0 deletions test/examples/touch.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[formatter.echo]
command = "touch"
# only change mtime
options = ["-m"]
includes = [ "*.*" ]

0 comments on commit 92321c8

Please sign in to comment.