-
Notifications
You must be signed in to change notification settings - Fork 157
feat: add support for running failed tests first based on previous ru… #498
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shay-dahan
wants to merge
1
commit into
gotestyourself:main
Choose a base branch
from
jfrog-fastci:feature/fail-first
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,16 @@ | ||
| { | ||
| // Use IntelliSense to learn about possible attributes. | ||
| // Hover to view descriptions of existing attributes. | ||
| // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
| "version": "0.2.0", | ||
| "configurations": [ | ||
| { | ||
| "name": "Launch Package", | ||
| "type": "go", | ||
| "request": "launch", | ||
| "mode": "debug", | ||
| "args": ["--rerun-fails"], | ||
| "program": "${workspaceFolder}/main.go" | ||
| } | ||
| ] | ||
| } |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ package cmd | |
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
|
|
@@ -126,6 +127,8 @@ func setupFlags(name string) (*pflag.FlagSet, *options) { | |
|
|
||
| flags.BoolVar(&opts.debug, "debug", false, "enabled debug logging") | ||
| flags.BoolVar(&opts.version, "version", false, "show version and exit") | ||
| flags.StringVar(&opts.failedFirstJSONFile, "failed-first", "", | ||
| "path to previous run's JSON file. Failed tests from this file will be run first") | ||
| return flags, opts | ||
| } | ||
|
|
||
|
|
@@ -200,13 +203,19 @@ type options struct { | |
| watchChdir bool | ||
| maxFails int | ||
| version bool | ||
| failedFirstJSONFile string | ||
|
|
||
| // shims for testing | ||
| stdout io.Writer | ||
| stderr io.Writer | ||
| } | ||
|
|
||
| func (o options) Validate() error { | ||
| if o.failedFirstJSONFile != "" { | ||
| if _, err := os.Stat(o.failedFirstJSONFile); os.IsNotExist(err) { | ||
| return fmt.Errorf("failed-first JSON file does not exist: %s", o.failedFirstJSONFile) | ||
| } | ||
| } | ||
| if o.rerunFailsMaxAttempts > 0 && len(o.args) > 0 && !o.rawCommand && len(o.packages) == 0 { | ||
| return fmt.Errorf( | ||
| "when go test args are used with --rerun-fails " + | ||
|
|
@@ -270,6 +279,71 @@ func run(opts *options) error { | |
| return err | ||
| } | ||
|
|
||
| // --failed-first logic | ||
| if opts.failedFirstJSONFile != "" { | ||
| data, err := os.ReadFile(opts.failedFirstJSONFile) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to read previous run JSON file: %w", err) | ||
| } | ||
| // Parse JSON lines | ||
| var failedTests []struct{ Package, Test, Action string } | ||
| for _, line := range strings.Split(string(data), "\n") { | ||
| if strings.TrimSpace(line) == "" { | ||
| continue | ||
| } | ||
| var evt struct{ Package, Test, Action string } | ||
| _ = json.Unmarshal([]byte(line), &evt) | ||
| if evt.Action == "fail" && evt.Test != "" { | ||
| failedTests = append(failedTests, evt) | ||
| } | ||
| } | ||
|
Comment on lines
+289
to
+299
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's code in the I'd suggest using that to read the JSON. |
||
| // Run failed tests first | ||
| for _, tc := range failedTests { | ||
| goTestProc, err := startGoTestFn(ctx, "", goTestCmdArgs(opts, rerunOpts{ | ||
| runFlag: goTestRunFlagForTestCase(testjson.TestName(tc.Test)), | ||
| pkg: tc.Package, | ||
| })) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| cfg := testjson.ScanConfig{ | ||
| Stdout: goTestProc.stdout, | ||
| Stderr: goTestProc.stderr, | ||
| Handler: &noopHandler{}, | ||
| Stop: cancel, | ||
| } | ||
| _, _ = testjson.ScanTestOutput(cfg) | ||
| _ = goTestProc.cmd.Wait() | ||
| } | ||
| // Then run all tests | ||
| goTestProc, err := startGoTestFn(ctx, "", goTestCmdArgs(opts, rerunOpts{})) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| handler, err := newEventHandler(opts) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer handler.Close() | ||
| cfg := testjson.ScanConfig{ | ||
| Stdout: goTestProc.stdout, | ||
| Stderr: goTestProc.stderr, | ||
| Handler: handler, | ||
| Stop: cancel, | ||
| IgnoreNonJSONOutputLines: opts.ignoreNonJSONOutputLines, | ||
| } | ||
| exec, err := testjson.ScanTestOutput(cfg) | ||
| handler.Flush() | ||
| if err != nil { | ||
| return finishRun(opts, exec, err) | ||
| } | ||
| exitErr := goTestProc.cmd.Wait() | ||
| if signum := atomic.LoadInt32(&goTestProc.signal); signum != 0 { | ||
| return finishRun(opts, exec, exitError{num: signalExitCode + int(signum)}) | ||
| } | ||
| return finishRun(opts, exec, exitErr) | ||
| } | ||
|
|
||
| goTestProc, err := startGoTestFn(ctx, "", goTestCmdArgs(opts, rerunOpts{})) | ||
| if err != nil { | ||
| return err | ||
|
|
@@ -541,3 +615,8 @@ func (w *cancelWaiter) Wait() error { | |
| w.cancel() | ||
| return err | ||
| } | ||
|
|
||
| type noopHandler struct{} | ||
|
|
||
| func (s noopHandler) Event(testjson.TestEvent, *testjson.Execution) error { return nil } | ||
| func (s noopHandler) Err(string) error { return nil } | ||
This file contains hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this file was committed by accident? Let's leave launch config up to the individual developer. If you'd like to add something consider adding it to
contrib/vscode/launch.json.sample.