Skip to content

Commit 61bfc9d

Browse files
committed
Print flay test logs in the console when no reruns
1 parent 7f71e19 commit 61bfc9d

File tree

2 files changed

+49
-10
lines changed

2 files changed

+49
-10
lines changed

tools/flakeguard/cmd/run.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"fmt"
77
"os"
88
"os/exec"
9+
"path/filepath"
10+
"strings"
911
"time"
1012

1113
"github.com/briandowns/spinner"
@@ -21,6 +23,7 @@ import (
2123
const (
2224
FlakyTestsExitCode = 1
2325
ErrorExitCode = 2
26+
RawOutputDir = "./flakeguard_raw_output"
2427
)
2528

2629
type runConfig struct {
@@ -252,27 +255,54 @@ func initializeRunner(cfg *runConfig) *runner.Runner {
252255
cfg.SelectTests,
253256
cfg.IgnoreParentFailuresOnSubtests,
254257
cfg.OmitOutputsOnSuccess,
258+
RawOutputDir,
255259
nil, // exec
256260
nil, // parser
257261
)
258262
}
259263

260264
// generateMainReport creates the initial test report from the main run results.
261265
func generateMainReport(results []reports.TestResult, cfg *runConfig, goProject string) (*reports.TestReport, error) {
266+
// Get the JSON output paths from the raw output directory
267+
jsonOutputPaths, err := getJSONOutputPaths(RawOutputDir)
268+
if err != nil {
269+
log.Warn().Err(err).Msg("Failed to get JSON output paths")
270+
}
271+
262272
reportVal, err := reports.NewTestReport(results,
263273
reports.WithGoProject(goProject),
264274
reports.WithCodeOwnersPath(cfg.CodeownersPath),
265275
reports.WithMaxPassRatio(cfg.MinPassRatio),
266276
reports.WithGoRaceDetection(cfg.UseRace),
267277
reports.WithExcludedTests(cfg.SkipTests),
268278
reports.WithSelectedTests(cfg.SelectTests),
279+
reports.WithJSONOutputPaths(jsonOutputPaths),
269280
)
270281
if err != nil {
271282
return nil, err
272283
}
273284
return &reportVal, nil
274285
}
275286

287+
// getJSONOutputPaths returns a list of JSON output files from the given directory
288+
func getJSONOutputPaths(dir string) ([]string, error) {
289+
files, err := os.ReadDir(dir)
290+
if err != nil {
291+
if os.IsNotExist(err) {
292+
return nil, nil
293+
}
294+
return nil, fmt.Errorf("failed to read directory %s: %w", dir, err)
295+
}
296+
297+
var paths []string
298+
for _, file := range files {
299+
if !file.IsDir() && strings.HasSuffix(file.Name(), ".json") {
300+
paths = append(paths, filepath.Join(dir, file.Name()))
301+
}
302+
}
303+
return paths, nil
304+
}
305+
276306
// handleReruns manages the process of rerunning failed tests and reporting results.
277307
func handleReruns(exitHandler *summaryAndExit, testRunner *runner.Runner, mainReport *reports.TestReport, cfg *runConfig, goProject string) {
278308
failedTests := reports.FilterTests(mainReport.Results, func(tr reports.TestResult) bool {
@@ -384,6 +414,20 @@ func handleNoReruns(exitHandler *summaryAndExit, mainReport *reports.TestReport,
384414
reports.RenderTestReport(&exitHandler.buffer, *mainReport, false, false)
385415

386416
if len(flakyTests) > 0 {
417+
// Create a new report with only flaky tests to get their gotestsum output
418+
flakyReport, err := reports.NewTestReport(flakyTests,
419+
reports.WithJSONOutputPaths(mainReport.JSONOutputPaths),
420+
)
421+
if err != nil {
422+
log.Error().Err(err).Msg("Error creating flaky tests report")
423+
} else {
424+
fmt.Fprint(&exitHandler.buffer, "\nFlaky Test Logs:\n\n")
425+
err := flakyReport.PrintGotestsumOutput(&exitHandler.buffer, "pkgname")
426+
if err != nil {
427+
log.Error().Err(err).Msg("Error printing gotestsum output for flaky tests")
428+
}
429+
}
430+
387431
exitHandler.logMsgAndExit(zerolog.InfoLevel, "Found flaky tests.", FlakyTestsExitCode, map[string]interface{}{
388432
"flaky_count": len(flakyTests),
389433
"stability_threshold": fmt.Sprintf("%.0f%%", cfg.MinPassRatio*100),

tools/flakeguard/runner/runner.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ import (
1313
"github.com/smartcontractkit/chainlink-testing-framework/tools/flakeguard/runner/parser"
1414
)
1515

16-
const (
17-
RawOutputDir = "./flakeguard_raw_output"
18-
)
19-
2016
// Runner describes the test run parameters and manages test execution and result parsing.
2117
// It delegates command execution to an Executor and result parsing to a Parser.
2218
type Runner struct {
@@ -33,8 +29,8 @@ type Runner struct {
3329
FailFast bool
3430
SkipTests []string
3531
SelectTests []string
32+
RawOutputDir string
3633

37-
// Configuration passed down to the parser
3834
IgnoreParentFailuresOnSubtests bool
3935
OmitOutputsOnSuccess bool
4036

@@ -48,7 +44,6 @@ type Runner struct {
4844
func NewRunner(
4945
projectPath string,
5046
verbose bool,
51-
// Runner specific config
5247
runCount int,
5348
goTestCountFlag *int,
5449
goTestRaceFlag bool,
@@ -59,12 +54,11 @@ func NewRunner(
5954
failFast bool,
6055
skipTests []string,
6156
selectTests []string,
62-
// Parser specific config (passed during initialization)
6357
ignoreParentFailuresOnSubtests bool,
6458
omitOutputsOnSuccess bool,
65-
// Dependencies (allow injection for testing)
59+
rawOutputDir string,
6660
exec executor.Executor,
67-
p parser.Parser, // Use interface type directly
61+
p parser.Parser,
6862
) *Runner {
6963
if exec == nil {
7064
exec = executor.NewCommandExecutor()
@@ -87,6 +81,7 @@ func NewRunner(
8781
SelectTests: selectTests,
8882
IgnoreParentFailuresOnSubtests: ignoreParentFailuresOnSubtests,
8983
OmitOutputsOnSuccess: omitOutputsOnSuccess,
84+
RawOutputDir: rawOutputDir,
9085
exec: exec,
9186
parser: p,
9287
}
@@ -105,7 +100,7 @@ func (r *Runner) getExecutorConfig() executor.Config {
105100
ShuffleSeed: r.ShuffleSeed,
106101
SkipTests: r.SkipTests,
107102
SelectTests: r.SelectTests,
108-
RawOutputDir: RawOutputDir, // Use the constant defined in this package
103+
RawOutputDir: r.RawOutputDir,
109104
}
110105
}
111106

0 commit comments

Comments
 (0)