Skip to content

Commit 13849f2

Browse files
committed
feat: improve fail on change test
Signed-off-by: Brian McGee <[email protected]>
1 parent 9d9ea4e commit 13849f2

File tree

1 file changed

+81
-60
lines changed

1 file changed

+81
-60
lines changed

cmd/root_test.go

+81-60
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,8 @@ func TestFailOnChange(t *testing.T) {
696696
tempDir := test.TempExamples(t)
697697
configPath := filepath.Join(tempDir, "treefmt.toml")
698698

699+
test.ChangeWorkDir(t, tempDir)
700+
699701
cfg := &config.Config{
700702
FormatterConfigs: map[string]*config.Formatter{
701703
"append": {
@@ -707,34 +709,43 @@ func TestFailOnChange(t *testing.T) {
707709
},
708710
},
709711
}
710-
test.WriteConfig(t, configPath, cfg)
711-
712-
_, statz, err := treefmt(t, "--fail-on-change", "--config-file", configPath, "--tree-root", tempDir)
713-
as.ErrorIs(err, formatCmd.ErrFailOnChange)
714712

715-
assertStats(t, as, statz, map[stats.Type]int{
716-
stats.Traversed: 32,
717-
stats.Matched: 2,
718-
stats.Formatted: 2,
719-
stats.Changed: 2,
720-
})
721-
722-
// cached
723-
_, statz, err = treefmt(t, "--fail-on-change", "--config-file", configPath, "--tree-root", tempDir)
724-
as.NoError(err)
713+
// running with a cold cache, we should see the elm files being formatted, resulting in changes, which should
714+
// trigger an error
715+
treefmt2(t,
716+
withArgs("--fail-on-change"),
717+
withConfig(configPath, cfg),
718+
withError(func(err error) {
719+
as.ErrorIs(err, formatCmd.ErrFailOnChange)
720+
}),
721+
withStats(as, map[stats.Type]int{
722+
stats.Traversed: 32,
723+
stats.Matched: 2,
724+
stats.Formatted: 2,
725+
stats.Changed: 2,
726+
}),
727+
)
725728

726-
assertStats(t, as, statz, map[stats.Type]int{
727-
stats.Traversed: 32,
728-
stats.Matched: 2,
729-
stats.Formatted: 0,
730-
stats.Changed: 0,
731-
})
729+
// running with a hot cache, we should see matches for the elm files, but no attempt to format them as the
730+
// underlying files have not changed since we last ran
731+
treefmt2(t,
732+
withArgs("--fail-on-change"),
733+
withNoError(as),
734+
withStats(as, map[stats.Type]int{
735+
stats.Traversed: 32,
736+
stats.Matched: 2,
737+
stats.Formatted: 0,
738+
stats.Changed: 0,
739+
}),
740+
)
732741
})
733742

734743
t.Run("change modtime", func(t *testing.T) {
735744
tempDir := test.TempExamples(t)
736745
configPath := filepath.Join(tempDir, "treefmt.toml")
737746

747+
test.ChangeWorkDir(t, tempDir)
748+
738749
dateFormat := "2006 01 02 15:04.05"
739750
replacer := strings.NewReplacer(" ", "", ":", "")
740751

@@ -743,48 +754,51 @@ func TestFailOnChange(t *testing.T) {
743754
return replacer.Replace(t.Format(dateFormat))
744755
}
745756

746-
writeConfig := func() {
747-
// new mod time is in the next second
748-
modTime := time.Now().Truncate(time.Second).Add(time.Second)
749-
750-
cfg := &config.Config{
751-
FormatterConfigs: map[string]*config.Formatter{
752-
"append": {
753-
// test-fmt-modtime is a helper defined in nix/packages/treefmt/formatters.nix which lets us set
754-
// a file's modtime to an arbitrary date.
755-
// in this case, we move it forward more than a second so that our second level modtime comparison
756-
// will detect it as a change.
757-
Command: "test-fmt-modtime",
758-
Options: []string{formatTime(modTime)},
759-
Includes: []string{"haskell/*"},
757+
// running with a cold cache, we should see the haskell files being formatted, resulting in changes, which should
758+
// trigger an error
759+
treefmt2(t,
760+
withArgs("--fail-on-change"),
761+
withConfigFunc(configPath, func() *config.Config {
762+
// new mod time is in the next second
763+
modTime := time.Now().Truncate(time.Second).Add(time.Second)
764+
765+
return &config.Config{
766+
FormatterConfigs: map[string]*config.Formatter{
767+
"append": {
768+
// test-fmt-modtime is a helper defined in nix/packages/treefmt/formatters.nix which lets us set
769+
// a file's modtime to an arbitrary date.
770+
// in this case, we move it forward more than a second so that our second level modtime comparison
771+
// will detect it as a change.
772+
Command: "test-fmt-modtime",
773+
Options: []string{formatTime(modTime)},
774+
Includes: []string{"haskell/*"},
775+
},
760776
},
761-
},
762-
}
763-
test.WriteConfig(t, configPath, cfg)
764-
}
765-
766-
writeConfig()
767-
768-
_, statz, err := treefmt(t, "--fail-on-change", "--config-file", configPath, "--tree-root", tempDir)
769-
as.ErrorIs(err, formatCmd.ErrFailOnChange)
770-
771-
assertStats(t, as, statz, map[stats.Type]int{
772-
stats.Traversed: 32,
773-
stats.Matched: 7,
774-
stats.Formatted: 7,
775-
stats.Changed: 7,
776-
})
777-
778-
// cached
779-
_, statz, err = treefmt(t, "--fail-on-change", "--config-file", configPath, "--tree-root", tempDir)
780-
as.NoError(err)
777+
}
778+
}),
779+
withError(func(err error) {
780+
as.ErrorIs(err, formatCmd.ErrFailOnChange)
781+
}),
782+
withStats(as, map[stats.Type]int{
783+
stats.Traversed: 32,
784+
stats.Matched: 7,
785+
stats.Formatted: 7,
786+
stats.Changed: 7,
787+
}),
788+
)
781789

782-
assertStats(t, as, statz, map[stats.Type]int{
783-
stats.Traversed: 32,
784-
stats.Matched: 7,
785-
stats.Formatted: 0,
786-
stats.Changed: 0,
787-
})
790+
// running with a hot cache, we should see matches for the haskell files, but no attempt to format them as the
791+
// underlying files have not changed since we last ran
792+
treefmt2(t,
793+
withArgs("--fail-on-change"),
794+
withNoError(as),
795+
withStats(as, map[stats.Type]int{
796+
stats.Traversed: 32,
797+
stats.Matched: 7,
798+
stats.Formatted: 0,
799+
stats.Changed: 0,
800+
}),
801+
)
788802
})
789803
}
790804

@@ -1560,6 +1574,13 @@ func withConfig(path string, cfg *config.Config) option {
15601574
}
15611575
}
15621576

1577+
func withConfigFunc(path string, fn func() *config.Config) option {
1578+
return func(o *options) {
1579+
o.config.path = path
1580+
o.config.value = fn()
1581+
}
1582+
}
1583+
15631584
func withStats(as *require.Assertions, expected map[stats.Type]int) option {
15641585
return func(o *options) {
15651586
o.assertStats = func(s *stats.Stats) {

0 commit comments

Comments
 (0)