From 53ea16a42ce026de9e36c5019a03ac851257bbb9 Mon Sep 17 00:00:00 2001 From: Brian McGee Date: Fri, 10 May 2024 11:26:10 +0100 Subject: [PATCH 1/2] feat: improve stats assertion feedback Signed-off-by: Brian McGee --- cli/helpers_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cli/helpers_test.go b/cli/helpers_test.go index a2a04f2..04c438b 100644 --- a/cli/helpers_test.go +++ b/cli/helpers_test.go @@ -74,10 +74,10 @@ func cmd(t *testing.T, args ...string) ([]byte, error) { func assertStats(t *testing.T, as *require.Assertions, traversed int32, emitted int32, matched int32, formatted int32) { t.Helper() - as.Equal(traversed, stats.Value(stats.Traversed)) - as.Equal(emitted, stats.Value(stats.Emitted)) - as.Equal(matched, stats.Value(stats.Matched)) - as.Equal(formatted, stats.Value(stats.Formatted)) + as.Equal(traversed, stats.Value(stats.Traversed), "stats.traversed") + as.Equal(emitted, stats.Value(stats.Emitted), "stats.emitted") + as.Equal(matched, stats.Value(stats.Matched), "stats.matched") + as.Equal(formatted, stats.Value(stats.Formatted), "stats.formatted") } func assertFormatted(t *testing.T, as *require.Assertions, output []byte, count int) { From 92321c873705af68639e92e61fb5be6c3dcdfb92 Mon Sep 17 00:00:00 2001 From: Brian McGee Date: Fri, 10 May 2024 11:43:28 +0100 Subject: [PATCH 2/2] feat: improve specifying formatters test Signed-off-by: Brian McGee --- cli/format_test.go | 26 ++++++++++++++++++++------ format/formatter.go | 2 +- test/examples/touch.toml | 2 ++ 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/cli/format_test.go b/cli/format_test.go index 7a4b8e8..0d86cba 100644 --- a/cli/format_test.go +++ b/cli/format_test.go @@ -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") diff --git a/format/formatter.go b/format/formatter.go index 477f88b..cb70ca1 100644 --- a/format/formatter.go +++ b/format/formatter.go @@ -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) } // diff --git a/test/examples/touch.toml b/test/examples/touch.toml index e1db694..3938e1f 100644 --- a/test/examples/touch.toml +++ b/test/examples/touch.toml @@ -1,3 +1,5 @@ [formatter.echo] command = "touch" +# only change mtime +options = ["-m"] includes = [ "*.*" ] \ No newline at end of file