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 Oct 21, 2024
1 parent 5659267 commit 40e18a3
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 55 deletions.
133 changes: 78 additions & 55 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
20 changes: 20 additions & 0 deletions test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package test
import (
"fmt"
"os"
"path/filepath"
"testing"
"time"

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 40e18a3

Please sign in to comment.