mirror of
https://github.com/numtide/treefmt.git
synced 2024-09-11 17:07:44 +03:00
Merge pull request #10 from numtide/fix/race-condition
fix: brittle test
This commit is contained in:
commit
3d165d7a94
@ -65,44 +65,58 @@ func TestAllowMissingFormatter(t *testing.T) {
|
|||||||
func TestSpecifyingFormatters(t *testing.T) {
|
func TestSpecifyingFormatters(t *testing.T) {
|
||||||
as := require.New(t)
|
as := require.New(t)
|
||||||
|
|
||||||
tempDir := test.TempExamples(t)
|
cfg := config2.Config{
|
||||||
configPath := tempDir + "/treefmt.toml"
|
|
||||||
|
|
||||||
test.WriteConfig(t, configPath, config2.Config{
|
|
||||||
Formatters: map[string]*config2.Formatter{
|
Formatters: map[string]*config2.Formatter{
|
||||||
"elm": {
|
"elm": {
|
||||||
Command: "touch",
|
Command: "touch",
|
||||||
|
Options: []string{"-m"},
|
||||||
Includes: []string{"*.elm"},
|
Includes: []string{"*.elm"},
|
||||||
},
|
},
|
||||||
"nix": {
|
"nix": {
|
||||||
Command: "touch",
|
Command: "touch",
|
||||||
|
Options: []string{"-m"},
|
||||||
Includes: []string{"*.nix"},
|
Includes: []string{"*.nix"},
|
||||||
},
|
},
|
||||||
"ruby": {
|
"ruby": {
|
||||||
Command: "touch",
|
Command: "touch",
|
||||||
|
Options: []string{"-m"},
|
||||||
Includes: []string{"*.rb"},
|
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)
|
_, err := cmd(t, "-c", "--config-file", configPath, "--tree-root", tempDir)
|
||||||
as.NoError(err)
|
as.NoError(err)
|
||||||
assertStats(t, as, 31, 31, 3, 3)
|
assertStats(t, as, 31, 31, 3, 3)
|
||||||
|
|
||||||
|
setup()
|
||||||
_, err = cmd(t, "-c", "--config-file", configPath, "--tree-root", tempDir, "--formatters", "elm,nix")
|
_, err = cmd(t, "-c", "--config-file", configPath, "--tree-root", tempDir, "--formatters", "elm,nix")
|
||||||
as.NoError(err)
|
as.NoError(err)
|
||||||
assertStats(t, as, 31, 31, 2, 2)
|
assertStats(t, as, 31, 31, 2, 2)
|
||||||
|
|
||||||
|
setup()
|
||||||
_, err = cmd(t, "-c", "--config-file", configPath, "--tree-root", tempDir, "--formatters", "ruby,nix")
|
_, err = cmd(t, "-c", "--config-file", configPath, "--tree-root", tempDir, "--formatters", "ruby,nix")
|
||||||
as.NoError(err)
|
as.NoError(err)
|
||||||
assertStats(t, as, 31, 31, 2, 2)
|
assertStats(t, as, 31, 31, 2, 2)
|
||||||
|
|
||||||
|
setup()
|
||||||
_, err = cmd(t, "-c", "--config-file", configPath, "--tree-root", tempDir, "--formatters", "nix")
|
_, err = cmd(t, "-c", "--config-file", configPath, "--tree-root", tempDir, "--formatters", "nix")
|
||||||
as.NoError(err)
|
as.NoError(err)
|
||||||
assertStats(t, as, 31, 31, 1, 1)
|
assertStats(t, as, 31, 31, 1, 1)
|
||||||
|
|
||||||
// test bad names
|
// test bad names
|
||||||
|
setup()
|
||||||
_, err = cmd(t, "-c", "--config-file", configPath, "--tree-root", tempDir, "--formatters", "foo")
|
_, err = cmd(t, "-c", "--config-file", configPath, "--tree-root", tempDir, "--formatters", "foo")
|
||||||
as.Errorf(err, "formatter not found in config: foo")
|
as.Errorf(err, "formatter not found in config: foo")
|
||||||
|
|
||||||
|
@ -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) {
|
func assertStats(t *testing.T, as *require.Assertions, traversed int32, emitted int32, matched int32, formatted int32) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
as.Equal(traversed, stats.Value(stats.Traversed))
|
as.Equal(traversed, stats.Value(stats.Traversed), "stats.traversed")
|
||||||
as.Equal(emitted, stats.Value(stats.Emitted))
|
as.Equal(emitted, stats.Value(stats.Emitted), "stats.emitted")
|
||||||
as.Equal(matched, stats.Value(stats.Matched))
|
as.Equal(matched, stats.Value(stats.Matched), "stats.matched")
|
||||||
as.Equal(formatted, stats.Value(stats.Formatted))
|
as.Equal(formatted, stats.Value(stats.Formatted), "stats.formatted")
|
||||||
}
|
}
|
||||||
|
|
||||||
func assertFormatted(t *testing.T, as *require.Assertions, output []byte, count int) {
|
func assertFormatted(t *testing.T, as *require.Assertions, output []byte, count int) {
|
||||||
|
@ -87,7 +87,7 @@ func (f *Formatter) Apply(ctx context.Context, files []*walk.File, filter bool)
|
|||||||
if len(out) > 0 {
|
if len(out) > 0 {
|
||||||
_, _ = fmt.Fprintf(os.Stderr, "%s error:\n%s\n", f.name, out)
|
_, _ = 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
[formatter.echo]
|
[formatter.echo]
|
||||||
command = "touch"
|
command = "touch"
|
||||||
|
# only change mtime
|
||||||
|
options = ["-m"]
|
||||||
includes = [ "*.*" ]
|
includes = [ "*.*" ]
|
Loading…
Reference in New Issue
Block a user