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 metrics with simplecov #12798

Merged
merged 7 commits into from
Aug 28, 2024

Conversation

johansenja
Copy link
Contributor

What? Why?

The complexity here clearly comes from the parallelism in the tests with knapsack; each of the portions of tests runs in its own runner with its own disk, so the different runners don't have access to the same filesystem.

My solution here is that each of the runners still uploads its own test results, and then there is a separate job which runs afterwards, which downloads each of the results and combines them into one (similar to the recommended steps in https://docs.knapsackpro.com/ruby/simplecov/#how-to-merge-simplecov-reports-from-parallel-ci-nodes, https://github.com/simplecov-ruby/simplecov/tree/main?tab=readme-ov-file#merging-test-runs-under-different-execution-environments and simplecov-ruby/simplecov#986 (comment)). The main drawback of this approach is that there would now be loads of artifacts, which is a bit more noise when it comes to accessing the useful ones (screenshots, and the final combined report)

This could open the doorway for other features too, such as badges, undercover, adding a PR comment with a coverage report, designating simplecov groups, setting thresholds etc.

What should we test?

  • Visit a CI run
  • Download the combined coverage report
  • Open index.html and browse the coverage

Release notes

Changelog Category (reviewers may add a label for the release notes):

  • Technical changes only

The title of the pull request will be included in the release notes.

Dependencies

Documentation updates

@johansenja johansenja force-pushed the enable-simplecov branch 2 times, most recently from b2116b0 to 715adec Compare August 21, 2024 12:48
@johansenja
Copy link
Contributor Author

Unfortunately, it seems like this is affected by actions/download-artifact#297 - it seems to work perfectly with v3, but with v4 none of the chunks seem to be available

Copy link
Collaborator

@rioug rioug left a comment

Choose a reason for hiding this comment

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

Nice one ! I noticed the index file in archive is missing the css/js do you think they could be added to the uploaded artificact ?

I am also wondering if we could also add a link to the artifact in the PR, so it's easier to find/access , maybe something like this : https://medium.com/@vlyskouski/github-actions-auto-reply-in-pull-requests-with-a-link-to-build-artifacts-ae1ef55034d8. Would you be willing to give this a go ?

@dacook dacook self-requested a review August 26, 2024 23:58
Copy link
Member

@dacook dacook left a comment

Choose a reason for hiding this comment

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

Great work, thank you.

I can see it's all working beautifully:
Screenshot 2024-08-27 at 10 12 32 am

95% doesn't sound too bad!

Generated 2024-08-23T10:33:26+00:00
All Files ( 95.45% covered at 1024.53 hits/line )
801 files in total.
20959 relevant lines, 20006 lines covered and 953 lines missed. ( 95.45% )

spec/base_spec_helper.rb Show resolved Hide resolved
spec/lib/tasks/simplecov_spec.rb Show resolved Hide resolved
SimpleCov.collate Dir[File.join(path_to_results, "**", ".resultset.json")], "rails" do
formatter SimpleCov::Formatter::HTMLFormatter

coverage_dir coverage_dir
Copy link
Member

Choose a reason for hiding this comment

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

I'm wondering if this was only meant to be called once?

Copy link
Member

Choose a reason for hiding this comment

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

Calling the variable twice would be a syntax error. There's a method with the same name. It would be more obvious to write it this way:

Suggested change
coverage_dir coverage_dir
coverage_dir(coverage_dir)

But I think that it would be better to rename the variable as well. Most configure blocks have the config as variable and then you would call config.coverage_dir(destination_path) but SimpleCov decided to simplify and provide methods directly in the code block.

Comment on lines +542 to +543
with:
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
Copy link
Member

Choose a reason for hiding this comment

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

I guess we only need to use the simplecov gem, and don't need all the other gems in the gemfile.
In that case, perhaps this could be optimised. But hopefully loading all the gems from cache doesn't take too long anyway.

Copy link
Member

@mkllnk mkllnk left a comment

Choose a reason for hiding this comment

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

Nice, I'm looking forward to these results.

This is exactly what we needed but didn't have time to implement. I wonder if that rake task would be useful to others. Maybe we could put it in a gem and publish it, alongside instructions for GH Actions to use it.

I think that it would be good to resolve the name clash in the configuration. But I would merge it anyway.

Thank you very much!

require 'rake'

RSpec.describe "simplecov.rake" do
let(:output_dir) { "tmp/test_#{Process.pid}_coverage" }
Copy link
Member

Choose a reason for hiding this comment

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

This is not cleaned up after the test run. What do you think about using Dir.mktmpdir?

SimpleCov.collate Dir[File.join(path_to_results, "**", ".resultset.json")], "rails" do
formatter SimpleCov::Formatter::HTMLFormatter

coverage_dir coverage_dir
Copy link
Member

Choose a reason for hiding this comment

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

Calling the variable twice would be a syntax error. There's a method with the same name. It would be more obvious to write it this way:

Suggested change
coverage_dir coverage_dir
coverage_dir(coverage_dir)

But I think that it would be better to rename the variable as well. Most configure blocks have the config as variable and then you would call config.coverage_dir(destination_path) but SimpleCov decided to simplify and provide methods directly in the code block.

@mkllnk mkllnk added technical changes only These pull requests do not contain user facing changes and are grouped in release notes feedback-needed labels Aug 27, 2024
Best viewed ignoring whitespace changes.
A variable and a method were called the same.
Also made method calls more obvious with parenthesis.
@mkllnk
Copy link
Member

mkllnk commented Aug 28, 2024

I just went ahead and made a couple of changes to clarify the code. In this kind of community project, it's really important to make code super-simple to avoid confusion and as fool-proof as possible. That will enable more people to contribute in the future.

@mkllnk
Copy link
Member

mkllnk commented Aug 28, 2024

That's such a useful report!

I noticed the index file in archive is missing the css/js do you think they could be added to the uploaded artificact ?

That would be convenient. It would take up more space though. I just ran the coverage locally for one tiny spec and then copied the artifacts.

@mkllnk mkllnk merged commit 43a3660 into openfoodfoundation:master Aug 28, 2024
51 checks passed
@johansenja
Copy link
Contributor Author

Thanks for the feedback and revisions! I opened a small PR here as well just to resolve the styling issue: #12822

@johansenja
Copy link
Contributor Author

In terms of commenting on the PR, I have found https://github.com/marketplace/actions/code-coverage-summary to be quite nice in the past - it comments the report as a nice table so you don't even have to click through or download the artifacts (but they are still there if you want the more detailed view)

Screenshot 2024-08-28 at 11 19 45

@mkllnk
Copy link
Member

mkllnk commented Aug 28, 2024

That looks like a good option. Happy to try out the summary comments. Are you up for adding it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
technical changes only These pull requests do not contain user facing changes and are grouped in release notes
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Set up code coverage metrics
4 participants