@@ -696,6 +696,8 @@ func TestFailOnChange(t *testing.T) {
696
696
tempDir := test .TempExamples (t )
697
697
configPath := filepath .Join (tempDir , "treefmt.toml" )
698
698
699
+ test .ChangeWorkDir (t , tempDir )
700
+
699
701
cfg := & config.Config {
700
702
FormatterConfigs : map [string ]* config.Formatter {
701
703
"append" : {
@@ -707,34 +709,43 @@ func TestFailOnChange(t *testing.T) {
707
709
},
708
710
},
709
711
}
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 )
714
712
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
+ )
725
728
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
+ )
732
741
})
733
742
734
743
t .Run ("change modtime" , func (t * testing.T ) {
735
744
tempDir := test .TempExamples (t )
736
745
configPath := filepath .Join (tempDir , "treefmt.toml" )
737
746
747
+ test .ChangeWorkDir (t , tempDir )
748
+
738
749
dateFormat := "2006 01 02 15:04.05"
739
750
replacer := strings .NewReplacer (" " , "" , ":" , "" )
740
751
@@ -743,48 +754,51 @@ func TestFailOnChange(t *testing.T) {
743
754
return replacer .Replace (t .Format (dateFormat ))
744
755
}
745
756
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
+ },
760
776
},
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
+ )
781
789
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
+ )
788
802
})
789
803
}
790
804
@@ -1560,6 +1574,13 @@ func withConfig(path string, cfg *config.Config) option {
1560
1574
}
1561
1575
}
1562
1576
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
+
1563
1584
func withStats (as * require.Assertions , expected map [stats.Type ]int ) option {
1564
1585
return func (o * options ) {
1565
1586
o .assertStats = func (s * stats.Stats ) {
0 commit comments