-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: Add spotlight reporter * Capture reporter usage
- Loading branch information
1 parent
824a184
commit 14cb46c
Showing
17 changed files
with
368 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package spotlight | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"sync" | ||
|
||
"github.com/fatih/color" | ||
"github.com/saucelabs/saucectl/internal/imagerunner" | ||
"github.com/saucelabs/saucectl/internal/job" | ||
"github.com/saucelabs/saucectl/internal/junit" | ||
"github.com/saucelabs/saucectl/internal/report" | ||
) | ||
|
||
// Reporter implements report.Reporter and highlights the most important test | ||
// results. | ||
type Reporter struct { | ||
TestResults []report.TestResult | ||
Dst io.Writer | ||
lock sync.Mutex | ||
} | ||
|
||
// Add adds the test result that can be rendered by Render. | ||
func (r *Reporter) Add(t report.TestResult) { | ||
r.lock.Lock() | ||
defer r.lock.Unlock() | ||
|
||
// skip in-progress jobs | ||
if !job.Done(t.Status) && !imagerunner.Done(t.Status) && !t.TimedOut { | ||
return | ||
} | ||
// skip passed jobs | ||
if t.Status == job.StatePassed || t.Status == imagerunner.StateSucceeded { | ||
return | ||
} | ||
|
||
if t.TimedOut { | ||
t.Status = job.StateUnknown | ||
} | ||
|
||
r.TestResults = append(r.TestResults, t) | ||
} | ||
|
||
// Render renders out a test summary. | ||
func (r *Reporter) Render() { | ||
r.lock.Lock() | ||
defer r.lock.Unlock() | ||
|
||
r.println() | ||
rl := color.New(color.FgBlue, color.Underline, color.Bold).Sprintf("Spotlight:") | ||
|
||
if len(r.TestResults) == 0 { | ||
r.printf(" %s Nothing stands out!\n", rl) | ||
return | ||
} | ||
|
||
r.printf(" %s\n", rl) | ||
r.println() | ||
|
||
for _, ts := range r.TestResults { | ||
r.println("", jobStatusSymbol(ts.Status), ts.Name) | ||
r.println(" ● URL:", ts.URL) | ||
|
||
var junitReports []junit.TestSuites | ||
for _, attempt := range ts.Attempts { | ||
junitReports = append(junitReports, attempt.TestSuites) | ||
} | ||
if len(junitReports) > 0 { | ||
junitReport := junit.MergeReports(junitReports...) | ||
testCases := junitReport.TestCases() | ||
|
||
var failedTests []string | ||
for _, tc := range testCases { | ||
if tc.IsError() || tc.IsFailure() { | ||
failedTests = append(failedTests, fmt.Sprintf("%s %s › %s", testCaseStatusSymbol(tc), tc.ClassName, tc.Name)) | ||
} | ||
// only show the first 5 failed tests to conserve space | ||
if len(failedTests) == 5 { | ||
break | ||
} | ||
} | ||
|
||
if len(failedTests) > 0 { | ||
r.println(" ● Failed Tests: (showing max. 5)") | ||
for _, test := range failedTests { | ||
r.println(" ", test) | ||
} | ||
} | ||
r.println() | ||
} | ||
} | ||
} | ||
|
||
func (r *Reporter) println(a ...any) { | ||
_, _ = fmt.Fprintln(r.Dst, a...) | ||
} | ||
|
||
func (r *Reporter) printf(format string, a ...any) { | ||
_, _ = fmt.Fprintf(r.Dst, format, a...) | ||
} | ||
|
||
// Reset resets the reporter to its initial state. This action will delete all test results. | ||
func (r *Reporter) Reset() { | ||
r.lock.Lock() | ||
defer r.lock.Unlock() | ||
r.TestResults = make([]report.TestResult, 0) | ||
} | ||
|
||
// ArtifactRequirements returns a list of artifact types this reporter requires | ||
// to create a proper report. | ||
func (r *Reporter) ArtifactRequirements() []report.ArtifactType { | ||
return []report.ArtifactType{report.JUnitArtifact} | ||
} | ||
|
||
func jobStatusSymbol(status string) string { | ||
switch status { | ||
case job.StatePassed, imagerunner.StateSucceeded: | ||
return color.GreenString("✔") | ||
case job.StateInProgress, job.StateQueued, job.StateNew, imagerunner.StateRunning, imagerunner.StatePending, | ||
imagerunner.StateUploading: | ||
return color.BlueString("*") | ||
default: | ||
return color.RedString("✖") | ||
} | ||
} | ||
|
||
func testCaseStatusSymbol(tc junit.TestCase) string { | ||
if tc.IsError() || tc.IsFailure() { | ||
return color.RedString("✖") | ||
} | ||
if tc.IsSkipped() { | ||
return color.YellowString("-") | ||
} | ||
return color.GreenString("✔") | ||
} |
Oops, something went wrong.