Skip to content
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

Implemented combine coverprofile in the case of rerun failed #387

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

yihuaf
Copy link

@yihuaf yihuaf commented Feb 15, 2024

Fix #274

As discussed in #274, when gotestsum rerun failed test cases, the coverprofile gets overridden by the each calls to the underlying go test, causing coverprofile to per in correct.

I tested the code with the repros provided in #274: https://github.com/neiser/gotestsum-rerun-dependent-subtests/tree/master

The code is just a proof of concept. If the general approach is acceptable, then I will go ahead and clean up the PR with necessary unit tests and comments.

The general approach taken in this PR:

  • if coverprofile flag is discovered, we parse the flag to get the file path.
  • in normal case, we pass the coverprofile directly to go test unmodified.
  • in the case that we need to rerun the failed test cases, we modify the coverprofile flag with a new file name.
  • after the run, we combine the new coverprofile output with the original file path passed in.

@yihuaf yihuaf marked this pull request as draft February 15, 2024 21:28
@yihuaf yihuaf marked this pull request as ready for review February 15, 2024 22:12
@yihuaf yihuaf force-pushed the feature/yihuaf/rerun-cov-combine branch from 3313900 to 2df1e19 Compare February 16, 2024 00:03
@yihuaf
Copy link
Author

yihuaf commented Feb 16, 2024

Looks like test-windows is broken unrelated to my change. I tried tip of the tree and the test still fails.

=== Failed
=== FAIL: cmd TestE2E_RerunFails/first_run_has_errors,_abort_rerun (0.39s)
    main_e2e_test.go:69: assertion failed: 
        --- expected
        +++ actual
        @@ -1,6 +1,11 @@
        +FAIL testjson/internal/broken
        +
        +=== Failed
        +=== FAIL: testjson/internal/broken 
        +FAIL   gotest.tools/gotestsum/testjson/internal/broken [build failed]
         
         === Errors
         ../testjson/internal/broken/broken.go:5:21: undefined: somepackage
         
        -DONE 0 tests, 1 error
        +DONE 0 tests, 1 failure, 1 error

@yihuaf yihuaf force-pushed the feature/yihuaf/rerun-cov-combine branch 2 times, most recently from 1554d71 to 55e31ce Compare February 16, 2024 07:29
Copy link
Member

@dnephin dnephin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the PR! I appreciate the contribution.

I had a quick look and overall it sounds reasonable to me. I agree the test failure seems unrelated.

I will find some more time to review in more detail. Some test coverage would also be appreciated!

// gocovmerge takes the results from multiple `go test -coverprofile` runs and
// merges them into one profile

// Taken from: https://github.com/wadey/gocovmerge @ b5bfa59ec0adc420475f97f89b58045c721d761c
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we may need to copy the license file
(https://github.com/wadey/gocovmerge/blob/master/LICENSE) into internal/coverprofile to comply with the license.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the LICENSE to the file header since it is only the file we copied over. Please let me know if this is good enough.

cmd/rerunfails.go Outdated Show resolved Hide resolved
// Even if there is errors, we still need to combine the
// coverprofile files.
if isCoverprofile {
if err := coverprofile.Combine(mainProfile, newCoverprofile); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be better to wait until all the attempts are complete and then combine all the files. That way we aren't re-reading the main file each time.

Would that work?

Copy link
Author

@yihuaf yihuaf Mar 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this would work and better.

One caveat is in the case of failure. When go test fails, the coverage profile still contain information up to the test failures. In the case of failure re-run, if we error out, we should still have 1 valid coverage profile and not leave profiles un-combined. So leaving all the combine to the end may not be desirable.

If reading the main profile multiple times is a concern, I can parse the main profile 1 time upfront and save the combined profile in memory each time when combine the re-run profile. Would this work? Not too sure if the performance gain from this optimization would justify the complexity increase.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After some more thoughts, I implemented the logic as follow:

  • each rerun will write the coverage profile into it's own file
  • after each rerun, read the rerun coverage profile into memory and delete the file
  • after all rerun is done before success, we combine all the rerun coverage profile with the main profile

This achieve 2 goals:

  • Only reading the main coverage profile once.
  • The coverage is valid upto the latest failed rerun and the rerun cover profile file is cleaned up

PTAL and let me know what you think.

@gaby
Copy link
Contributor

gaby commented Mar 10, 2024

@dnephin I was trying to do a PR to update your dependencies but tests are failing locally with the same error posted by @yihuaf

@yihuaf
Copy link
Author

yihuaf commented Mar 19, 2024

@dnephin Please let me know if there is anything you would like me to follow up :)

@yihuaf yihuaf force-pushed the feature/yihuaf/rerun-cov-combine branch from 0cfa31f to c53ac02 Compare April 18, 2024 06:14
@yihuaf
Copy link
Author

yihuaf commented Apr 18, 2024

@dnephin Let me know if there is anything else you would like me to fix :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rerun fails with coverage and "dependent" subtests
3 participants