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

Set up code coverage reports on GitHub Actions #350

Closed
MaxDesiatov opened this issue Jan 8, 2021 · 17 comments · Fixed by #378
Closed

Set up code coverage reports on GitHub Actions #350

MaxDesiatov opened this issue Jan 8, 2021 · 17 comments · Fixed by #378
Labels
continuous integration Changes related to the continuous integration process good first issue Good for newcomers

Comments

@MaxDesiatov
Copy link
Collaborator

MaxDesiatov commented Jan 8, 2021

I think stability of our existing code should be of higher priority. We need to be diligent about keeping test coverage high and not reducing it significantly (ideally increasing it, as it's probably already too low) in any PRs. GitHub Actions job that runs in a PR and posts current coverage as a comment would be super helpful.

After that is done we can set up more complex workflows that compute coverage deltas. I know that proprietary solutions like Codecov can already do this, but I'm wary of relying on them as they tend to shut down free access for open-source projects after some period of initial unsustainable growth. GitHub Actions seems to be a more reliable tool for this long term.

@MaxDesiatov MaxDesiatov added the continuous integration Changes related to the continuous integration process label Jan 8, 2021
@mattpolzin
Copy link
Contributor

In the “easy to integrate, easy to move on from” category, I can say my codecov action has been good enough for me so far (and obviously improvements are welcome!).

https://github.com/mattpolzin/swift-codecov-action

here it is in action; it currently just prints a coverage table, optionally fails if coverage standards are not met, and results in a CI checkmark on PRs.
https://github.com/mattpolzin/OpenAPIKit/runs/1623070311?check_suite_focus=true

@MaxDesiatov MaxDesiatov added the good first issue Good for newcomers label Jan 8, 2021
@MaxDesiatov
Copy link
Collaborator Author

Lovely! I wonder how hard it would be to modify it to post coverage results as comments? We previously had some success in creating GitHub releases from shell scripts directly using curl, maybe that could be done in your action too?

@mattpolzin
Copy link
Contributor

One option would be to “fix” the current limitations in what my action outputs and pair it with other GitHub actions (I’m sure there’s one out there that does nothing but post a comment).

@mattpolzin
Copy link
Contributor

(I’m not opposed to adding features like that to my action, I just generally find GitHub actions to be so conveniently composable)

@carson-katri
Copy link
Member

Is it possible to put it in the diff like the linter comments? That way it doesn't clutter the discussion.

@mattpolzin
Copy link
Contributor

Definitely (assuming I catch your drift). Creating a file and committing it is not so different effort-wise to creating a new PR comment from a GitHub action.

@MaxDesiatov
Copy link
Collaborator Author

MaxDesiatov commented Jan 11, 2021

Our SwiftLint warnings are attached in diffs to specific lines, while code coverage percentage is global per project.

I like the idea of committing that percentage as a file though, that way it's simpler to compare coverage of a PR branch with coverage in the main branch. We wouldn't have to run tests again to calculate the value for the main branch then. We could just check out the main branch in the job and load the value from the file for comparison.

@mattpolzin
Copy link
Contributor

mattpolzin commented Jan 12, 2021

Bummer, using swift-codecov-action for this is easier said than done (I am only speaking to one possible way to approach this ticket, of course). swift-codecov-action runs in a docker container which means it needs to run in an ubuntu job rather than a macOS job. Currently, that means this project cannot be tested because of the TokamakDemo product. That product is not even needed by the test target, but swift test (unlike swift build) refuses to build a subset of all targets... (best I have been able to figure)

@MaxDesiatov
Copy link
Collaborator Author

We already work around that by building just the test product with swift build --product TokamakPackageTests and running the test bundle manually with xctest here

swift build --product TokamakPackageTests

@mattpolzin
Copy link
Contributor

We already work around that...

Right, but that runs-on MacOS where xctest is available whereas the test coverage action I was trying to use runs in an Ubuntu Docker image; GitHub does not currently offer Docker support for MacOS build boxes (last I checked).

It would not be so terrible to pull the swift-test-codecov tool that the action currently uses in directly and just build and run it in a MacOS environment in order to get past this, but it’s a different direction than I started taking initially.

@MaxDesiatov
Copy link
Collaborator Author

On Ubuntu you'd do essentially the same thing, build just the tests with swift build --product TokamakPackageTests, then run the test bundle. You won't need xctest though, the test bundle is just a plain executable on Linux.

@MaxDesiatov
Copy link
Collaborator Author

The test bundle path would be the same I guess, .build/debug/TokamakPackageTests.xctest, or .build/debug/TokamakPackageTests, I don't remember which one exactly.

@mattpolzin
Copy link
Contributor

Oh, that’s cool. TIL!

@mattpolzin
Copy link
Contributor

mattpolzin commented Jan 12, 2021

I wonder if it’s possible to generate the test coverage file this way though. Worth exploring. (Referring to the file created by —enable-test-coverage)

@mattpolzin
Copy link
Contributor

mattpolzin commented Jan 19, 2021

Got that figured out; building with the right flags and telling swift test to skip building works.

Proof of concept with a rudimentary comment here: mattpolzin#2 / https://github.com/mattpolzin/Tokamak/pull/2/checks?check_run_id=1724996327

Requires an access token to be added to this repository before it will work (for the commenting).

@MaxDesiatov
Copy link
Collaborator Author

MaxDesiatov commented Jan 19, 2021

Perfect! 👏
Please feel free to submit a PR to this repo, I'll fix the token issue. Or maybe we'll find a way to attach the report to the PR diff, like our current SwiftLint action does, that wouldn't even require a token.

@mattpolzin
Copy link
Contributor

mattpolzin commented Feb 1, 2021

I've been puttering away at this a bit (modifying CI can be such a drag) -- this comment is not-so-bad, I think (image attached). I removed all of the tests for Color to test the diff with main.

It really is just a diff, so not super great, but maybe better than nothing? I'll PR for discussion.

Screen Shot 2021-01-31 at 5 35 05 PM

MaxDesiatov pushed a commit that referenced this issue Feb 5, 2021
Closes #350.

Adds simple code coverage analysis. Until the GitHub token is set up in this repo, you can see the results including a comment on the PR here: mattpolzin#2

* Adding codecov CI workflow.

* doh. forgot to skip build.

* drop unneeded llvm env var from build invocation. make comment always run.

* try a way to update a previous comment

* try to get comments to run on failure.

* use bug fix to codecov action.

* fix comment search string and drop min coverage

* use replace on comment updates

* attempt at diffing coverage from main branch

* comment out tests to affect test coverage

* try method for multiline outputs

* do i need to put it in its own script file

* right, switch coverage gen order to have branch checked out second.

* attempt to get code quotation around diff output.

* switch to git diff

* add note about the new script.

* try echoing a warning.

* post warning on success and error on failure.

* uncomment tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
continuous integration Changes related to the continuous integration process good first issue Good for newcomers
Development

Successfully merging a pull request may close this issue.

3 participants