Skip to content

Commit

Permalink
Merge pull request #59 from dnephin/report-cache-usage
Browse files Browse the repository at this point in the history
Report if a package was cached in short formats
  • Loading branch information
dnephin committed Aug 18, 2019
2 parents 1796188 + 9fd1128 commit 9ed80b5
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 7 deletions.
9 changes: 9 additions & 0 deletions testjson/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ type Package struct {
// with no test failures if an init() or TestMain exits non-zero.
// skip indicates there were no tests.
action Action
// cached is true if the package was marked as (cached)
cached bool
}

// Result returns if the package passed, failed, or was skipped because there
Expand Down Expand Up @@ -148,6 +150,9 @@ func (e *Execution) addPackageEvent(pkg *Package, event TestEvent) {
if isCoverageOutput(event.Output) {
pkg.coverage = strings.TrimRight(event.Output, "\n")
}
if isCachedOutput(event.Output) {
pkg.cached = true
}
pkg.output[""] = append(pkg.output[""], event.Output)
}
}
Expand Down Expand Up @@ -192,6 +197,10 @@ func isCoverageOutput(output string) bool {
strings.HasSuffix(output, "% of statements\n"))
}

func isCachedOutput(output string) bool {
return strings.Contains(output, "\t(cached)")
}

// Output returns the full test output for a test.
func (e *Execution) Output(pkg, test string) string {
return strings.Join(e.packages[pkg].output[test], "")
Expand Down
19 changes: 16 additions & 3 deletions testjson/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,14 @@ func shortVerboseFormat(event TestEvent, exec *Execution) (string, error) {
result = colorEvent(event)("EMPTY")
fallthrough
case ActionPass, ActionFail:
return fmt.Sprintf("%s %s\n", result, relativePackagePath(event.Package)), nil
var cached string
if exec.Package(event.Package).cached {
cached = cachedMessage
}
return fmt.Sprintf("%s %s%s\n",
result,
relativePackagePath(event.Package),
cached), nil
}

case event.Action == ActionFail:
Expand Down Expand Up @@ -99,23 +106,29 @@ func all(cond ...bool) bool {
return true
}

const cachedMessage = " (cached)"

func shortFormat(event TestEvent, exec *Execution) (string, error) {
if !event.PackageEvent() {
return "", nil
}
pkg := exec.Package(event.Package)

fmtElapsed := func() string {
if pkg.cached {
return cachedMessage
}
d := elapsedDuration(event.Elapsed)
if d == 0 {
return ""
}
return fmt.Sprintf(" (%s)", d)
}
fmtCoverage := func() string {
pkg := exec.Package(event.Package)
if pkg.coverage == "" {
return ""
}
return " (" + pkg.coverage + ")"
return " (" + pkg.coverage + ")"
}
fmtEvent := func(action string) (string, error) {
return fmt.Sprintf("%s %s%s%s\n",
Expand Down
1 change: 1 addition & 0 deletions testjson/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ var expectedExecution = &Execution{
{Test: "TestSkippedWitLog"},
},
action: ActionPass,
cached: true,
},
"github.com/gotestyourself/gotestyourself/testjson/internal/stub": {
Total: 28,
Expand Down
4 changes: 2 additions & 2 deletions testjson/testdata/short-format-coverage.out
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
✖ gotestsum/testjson/internal/badmain (1ms)
✓ gotestsum/testjson/internal/good (12ms) (coverage: 0.0% of statements)
✖ gotestsum/testjson/internal/stub (11ms) (coverage: 0.0% of statements)
✓ gotestsum/testjson/internal/good (12ms) (coverage: 0.0% of statements)
✖ gotestsum/testjson/internal/stub (11ms) (coverage: 0.0% of statements)
2 changes: 1 addition & 1 deletion testjson/testdata/short-format.out
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
✖ testjson/internal/badmain (10ms)
✓ testjson/internal/good
✓ testjson/internal/good (cached)
✖ testjson/internal/stub (11ms)
2 changes: 1 addition & 1 deletion testjson/testdata/short-verbose-format.out
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ PASS testjson/internal/good.TestNestedSuccess (0.00s)
PASS testjson/internal/good.TestParallelTheThird (0.00s)
PASS testjson/internal/good.TestParallelTheSecond (0.01s)
PASS testjson/internal/good.TestParallelTheFirst (0.01s)
PASS testjson/internal/good
PASS testjson/internal/good (cached)
PASS testjson/internal/stub.TestPassed (0.00s)
PASS testjson/internal/stub.TestPassedWithLog (0.00s)
PASS testjson/internal/stub.TestPassedWithStdout (0.00s)
Expand Down

0 comments on commit 9ed80b5

Please sign in to comment.