Skip to content

Commit

Permalink
feat: only delay treefmt in tests if fail on change is enabled
Browse files Browse the repository at this point in the history
This is a short-term fix to improve the time it takes to run tests.

Signed-off-by: Brian McGee <[email protected]>
  • Loading branch information
brianmcgee committed Oct 18, 2024
1 parent d7c39b6 commit 9c96554
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 14 deletions.
22 changes: 14 additions & 8 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"path"
"path/filepath"
"regexp"
"slices"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -1305,15 +1306,20 @@ func treefmt(t *testing.T, args ...string) ([]byte, *stats.Stats, error) {
root.SetOut(tempOut)
root.SetErr(tempOut)

// record the start time
start := time.Now()
failOnChange := os.Getenv("TREEFMT_FAIL_ON_CHANGE") == "true" ||
slices.Index(args, "--fail-on-change") != -1

defer func() {
// Wait until we tick over into the next second before continuing.
// This ensures we correctly detect changes within tests as treefmt compares modtime at second level precision.
waitUntil := start.Truncate(time.Second).Add(time.Second)
time.Sleep(time.Until(waitUntil))
}()
if failOnChange {
// record the start time
start := time.Now()

defer func() {
// Wait until we tick over into the next second before continuing.
// This ensures we correctly detect changes as treefmt compares modtime at second level precision.
waitUntil := start.Truncate(time.Second).Add(time.Second)
time.Sleep(time.Until(waitUntil))
}()
}

// execute the command
cmdErr := root.Execute()
Expand Down
5 changes: 2 additions & 3 deletions format/composite.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,8 @@ func (c *CompositeFormatter) Apply(ctx context.Context, files []*walk.File) erro
return nil
}

// signature takes anything that might affect the paths to be traversed,
// or how they are traversed, and adds it to a sha256 hash.
// This can be used to determine if there has been a material change in config.
// signature generates a formatting signature, which is a combination of the signatures for each of the formatters
// we delegate to.
func (c *CompositeFormatter) signature() (signature, error) {
h := sha256.New()

Expand Down
2 changes: 1 addition & 1 deletion format/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (f *Formatter) Executable() string {

// Hash adds this formatter's config and executable info to the config hash being created.
func (f *Formatter) Hash(h hash.Hash) error {
// including the name helps us to easily detect when formatter's have been added/removed
// including the name helps us to easily detect when formatters have been added/removed
h.Write([]byte(f.name))
// if options change, the outcome of applying the formatter might be different
h.Write([]byte(strings.Join(f.config.Options, " ")))
Expand Down
2 changes: 1 addition & 1 deletion format/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (s *scheduler) formattersSignature(key batchKey, formatters []*Formatter) (

sig = h.Sum(nil)

// store we don't have to re-compute for each file
// store the signature so we don't have to re-compute for each file
s.signatures[key] = sig

return sig, nil
Expand Down
2 changes: 1 addition & 1 deletion test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TempFile(t *testing.T, dir string, pattern string, contents *string) *os.Fi
}

// Lutimes is a convenience wrapper for using unix.Lutimes
// TODO: this will need adapted if we support Windows.
// TODO: this will need to be adapted if we support Windows.
func Lutimes(t *testing.T, path string, atime time.Time, mtime time.Time) error {
t.Helper()

Expand Down

0 comments on commit 9c96554

Please sign in to comment.