style: formatting for time + colors

This commit is contained in:
2026-03-21 08:24:30 +00:00
parent f50a6b6447
commit f4820d8099
4 changed files with 43 additions and 14 deletions
+9 -9
View File
@@ -220,10 +220,10 @@ func TestRunTest(t *testing.T) {
for _, want := range []string{ for _, want := range []string{
"RUN a", "RUN a",
"PASS a in ", "PASS a (",
" seconds", " ms)",
"RUN nested/b", "RUN nested/b",
"PASS nested/b in ", "PASS nested/b (",
"Summary: total=2 passed=2 failed=0", "Summary: total=2 passed=2 failed=0",
} { } {
if !strings.Contains(stdout, want) { if !strings.Contains(stdout, want) {
@@ -266,9 +266,9 @@ func TestRunTestScopedDirectory(t *testing.T) {
for _, want := range []string{ for _, want := range []string{
"RUN nested/b", "RUN nested/b",
"PASS nested/b in ", "PASS nested/b (",
"RUN nested/c", "RUN nested/c",
"PASS nested/c in ", "PASS nested/c (",
"Summary: total=2 passed=2 failed=0", "Summary: total=2 passed=2 failed=0",
} { } {
if !strings.Contains(stdout, want) { if !strings.Contains(stdout, want) {
@@ -313,7 +313,7 @@ func TestRunTestScopedLeafDirectory(t *testing.T) {
for _, want := range []string{ for _, want := range []string{
"RUN nested/b", "RUN nested/b",
"PASS nested/b in ", "PASS nested/b (",
"Summary: total=1 passed=1 failed=0", "Summary: total=1 passed=1 failed=0",
} { } {
if !strings.Contains(stdout, want) { if !strings.Contains(stdout, want) {
@@ -358,9 +358,9 @@ func TestRunTestReturnsZeroWhenScenarioMismatches(t *testing.T) {
for _, want := range []string{ for _, want := range []string{
"RUN a", "RUN a",
"PASS a in ", "PASS a (",
"RUN b", "RUN b",
"FAIL b in ", "FAIL b (",
"output differed", "output differed",
"Summary: total=2 passed=1 failed=1", "Summary: total=2 passed=1 failed=1",
} { } {
@@ -459,7 +459,7 @@ func TestRunTestReturnsZeroWhenCompareMarkerMissing(t *testing.T) {
for _, want := range []string{ for _, want := range []string{
"RUN some", "RUN some",
"FAIL some in ", "FAIL some (",
"missing compare marker", "missing compare marker",
"Summary: total=1 passed=0 failed=1", "Summary: total=1 passed=0 failed=1",
} { } {
+12 -4
View File
@@ -85,15 +85,15 @@ func runTests(path string, tio testIO) error {
start := time.Now() start := time.Now()
if err := replayScenario(scenario, shellPath, tio, cfg.Sandbox); err != nil { if err := replayScenario(scenario, shellPath, tio, cfg.Sandbox); err != nil {
elapsed := time.Since(start).Seconds() elapsed := time.Since(start)
summary.failed++ summary.failed++
output.Fprintf(tio.out, "%s %s in %.3f seconds: %v\n", output.Label("FAIL", output.Fail), scenario.relPath, elapsed, err) output.Fprintf(tio.out, "%s %s (%s): %v\n", output.Label("FAIL", output.Fail), scenario.relPath, formatElapsed(elapsed), err)
continue continue
} }
elapsed := time.Since(start).Seconds() elapsed := time.Since(start)
summary.passed++ summary.passed++
output.Fprintf(tio.out, "%s %s in %.3f seconds\n", output.Label("PASS", output.Pass), scenario.relPath, elapsed) output.Fprintf(tio.out, "%s %s (%s)\n", output.Label("PASS", output.Pass), scenario.relPath, formatElapsed(elapsed))
} }
summaryColor := output.Pass summaryColor := output.Pass
@@ -113,6 +113,14 @@ func runTests(path string, tio testIO) error {
return nil return nil
} }
func formatElapsed(elapsed time.Duration) string {
if elapsed < time.Second {
return fmt.Sprintf("%d ms", elapsed/time.Millisecond)
}
return fmt.Sprintf("%.2f s", elapsed.Seconds())
}
func resolveTestDiscoveryRoot(testDir, path string) (string, error) { func resolveTestDiscoveryRoot(testDir, path string) (string, error) {
if path == "" { if path == "" {
return testDir, nil return testDir, nil
+21
View File
@@ -5,8 +5,29 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"testing" "testing"
"time"
) )
func TestFormatElapsed(t *testing.T) {
tests := []struct {
name string
elapsed time.Duration
wantText string
}{
{name: "sub-second", elapsed: 123 * time.Millisecond, wantText: "123 ms"},
{name: "boundary", elapsed: time.Second, wantText: "1.00 s"},
{name: "multi-second", elapsed: 1534 * time.Millisecond, wantText: "1.53 s"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := formatElapsed(tt.elapsed); got != tt.wantText {
t.Fatalf("formatElapsed(%v) = %q, want %q", tt.elapsed, got, tt.wantText)
}
})
}
}
func TestDiscoverTestScenariosFindsNestedFixturesAndSorts(t *testing.T) { func TestDiscoverTestScenariosFindsNestedFixturesAndSorts(t *testing.T) {
testDir := filepath.Join(t.TempDir(), "e2e") testDir := filepath.Join(t.TempDir(), "e2e")
writeScenarioFixtures(t, filepath.Join(testDir, "nested", "b"), "echo two\n", "echo two\n") writeScenarioFixtures(t, filepath.Join(testDir, "nested", "b"), "echo two\n", "echo two\n")
+1 -1
View File
@@ -18,7 +18,7 @@ var (
miroGreen: 0x70E000, miroGreen: 0x70E000,
chevronTeal: 0x1DD3B0, chevronTeal: 0x1DD3B0,
info: 0xD7FFFF, info: 0xD7FFFF,
pass: 0x87D75F, pass: 0x00d7af,
fail: 0xFF8787, fail: 0xFF8787,
} }
) )