From 40e18a3b2a79248532dcfc03952390a634df7101 Mon Sep 17 00:00:00 2001 From: Brian McGee Date: Sat, 19 Oct 2024 11:04:56 +0100 Subject: [PATCH] feat: improve specifying formatters test Signed-off-by: Brian McGee --- cmd/root_test.go | 133 +++++++++++++++++++++++++++-------------------- test/test.go | 20 +++++++ 2 files changed, 98 insertions(+), 55 deletions(-) diff --git a/cmd/root_test.go b/cmd/root_test.go index 400568b9..04f152d5 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -184,93 +184,116 @@ func TestAllowMissingFormatter(t *testing.T) { func TestSpecifyingFormatters(t *testing.T) { as := require.New(t) + // we use the test formatter to append some whitespace cfg := &config.Config{ FormatterConfigs: map[string]*config.Formatter{ "elm": { - Command: "touch", - Options: []string{"-m"}, + Command: "test-fmt-append", + Options: []string{" "}, Includes: []string{"*.elm"}, }, "nix": { - Command: "touch", - Options: []string{"-m"}, + Command: "test-fmt-append", + Options: []string{" "}, Includes: []string{"*.nix"}, }, "ruby": { - Command: "touch", - Options: []string{"-m"}, + Command: "test-fmt-append", + Options: []string{" "}, Includes: []string{"*.rb"}, }, }, } - var tempDir, configPath string + tempDir := test.TempExamples(t) + configPath := filepath.Join(tempDir, "treefmt.toml") - // 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) - } + test.WriteConfig(t, configPath, cfg) + test.ChangeWorkDir(t, tempDir) - setup() + t.Run("default", func(t *testing.T) { + test.BumpModtimes(t, tempDir, 0, time.Second) - _, statz, err := treefmt(t, "-c", "--config-file", configPath, "--tree-root", tempDir) - as.NoError(err) + treefmt2(t, args(), func(_ []byte, statz *stats.Stats, err error) { + as.NoError(err) - assertStats(t, as, statz, map[stats.Type]int{ - stats.Traversed: 32, - stats.Matched: 3, - stats.Formatted: 3, - stats.Changed: 3, + assertStats(t, as, statz, map[stats.Type]int{ + stats.Traversed: 32, + stats.Matched: 3, + stats.Formatted: 3, + stats.Changed: 3, + }) + }) }) - setup() + t.Run("args", func(t *testing.T) { + test.BumpModtimes(t, tempDir, 0, time.Second) - _, statz, err = treefmt(t, "-c", "--config-file", configPath, "--tree-root", tempDir, "--formatters", "elm,nix") - as.NoError(err) + treefmt2(t, args("--formatters", "elm,nix"), func(_ []byte, statz *stats.Stats, err error) { + as.NoError(err) - assertStats(t, as, statz, map[stats.Type]int{ - stats.Traversed: 32, - stats.Matched: 2, - stats.Formatted: 2, - stats.Changed: 2, - }) + assertStats(t, as, statz, map[stats.Type]int{ + stats.Traversed: 32, + stats.Matched: 2, + stats.Formatted: 2, + stats.Changed: 2, + }) + }) - setup() + test.BumpModtimes(t, tempDir, 0, time.Second) - _, statz, err = treefmt(t, "-c", "--config-file", configPath, "--tree-root", tempDir, "-f", "ruby,nix") - as.NoError(err) + treefmt2(t, args("--formatters", "ruby,nix"), func(_ []byte, statz *stats.Stats, err error) { + as.NoError(err) - assertStats(t, as, statz, map[stats.Type]int{ - stats.Traversed: 32, - stats.Matched: 2, - stats.Formatted: 2, - stats.Changed: 2, - }) + assertStats(t, as, statz, map[stats.Type]int{ + stats.Traversed: 32, + stats.Matched: 2, + stats.Formatted: 2, + stats.Changed: 2, + }) + }) - setup() + test.BumpModtimes(t, tempDir, 0, time.Second) - _, statz, err = treefmt(t, "-c", "--config-file", configPath, "--tree-root", tempDir, "--formatters", "nix") - as.NoError(err) + treefmt2(t, args("--formatters", "nix"), func(_ []byte, statz *stats.Stats, err error) { + as.NoError(err) - assertStats(t, as, statz, map[stats.Type]int{ - stats.Traversed: 32, - stats.Matched: 1, - stats.Formatted: 1, - stats.Changed: 1, + assertStats(t, as, statz, map[stats.Type]int{ + stats.Traversed: 32, + stats.Matched: 1, + stats.Formatted: 1, + stats.Changed: 1, + }) + }) + + // bad name + treefmt2(t, args("--formatters", "foo"), func(_ []byte, _ *stats.Stats, err error) { + as.Errorf(err, "formatter not found in config: foo") + }) }) - // test bad names - setup() + t.Run("env", func(t *testing.T) { + test.BumpModtimes(t, tempDir, 0, time.Second) + + t.Setenv("TREEFMT_FORMATTERS", "ruby,nix") + + treefmt2(t, args("--formatters", "ruby,nix"), func(_ []byte, statz *stats.Stats, err error) { + as.NoError(err) + + assertStats(t, as, statz, map[stats.Type]int{ + stats.Traversed: 32, + stats.Matched: 2, + stats.Formatted: 2, + stats.Changed: 2, + }) + }) - _, _, err = treefmt(t, "-c", "--config-file", configPath, "--tree-root", tempDir, "--formatters", "foo") - as.Errorf(err, "formatter not found in config: foo") + t.Setenv("TREEFMT_FORMATTERS", "bar,foo") - t.Setenv("TREEFMT_FORMATTERS", "bar,foo") - _, _, err = treefmt(t, "-c", "--config-file", configPath, "--tree-root", tempDir) - as.Errorf(err, "formatter not found in config: bar") + treefmt2(t, args("--formatters", "bar,foo"), func(_ []byte, _ *stats.Stats, err error) { + as.Errorf(err, "formatter not found in config: bar") + }) + }) } func TestIncludesAndExcludes(t *testing.T) { diff --git a/test/test.go b/test/test.go index 909cb3ec..e25485c7 100644 --- a/test/test.go +++ b/test/test.go @@ -3,6 +3,7 @@ package test import ( "fmt" "os" + "path/filepath" "testing" "time" @@ -80,6 +81,25 @@ func Lutimes(t *testing.T, path string, atime time.Time, mtime time.Time) error return nil } +func BumpModtimes(t *testing.T, path string, atime time.Duration, mtime time.Duration) { + t.Helper() + + now := time.Now() + newAtime := now.Add(atime) + newMtime := now.Add(mtime) + + err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error { + if err != nil || info.IsDir() { + return err + } + + return Lutimes(t, path, newAtime, newMtime) + }) + if err != nil { + t.Fatalf("failed to bump modtimes: %v", err) + } +} + // ChangeWorkDir changes the current working directory for the duration of the test. // The original directory is restored when the test ends. func ChangeWorkDir(t *testing.T, dir string) {