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 887a321
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 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,24 @@ func treefmt(t *testing.T, args ...string) ([]byte, *stats.Stats, error) {
root.SetOut(tempOut)
root.SetErr(tempOut)

// record the start time
start := time.Now()
var failOnChange bool
if os.Getenv("TREEFMT_FAIL_ON_CHANGE") == "true" {
failOnChange = true
} else if slices.Index(args, "--fail-on-change") != -1 {
failOnChange = true
}

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

0 comments on commit 887a321

Please sign in to comment.