ausimian

ausimian

Code Coverage for Umbrella Projects in GitLab

I spent a bit of time looking at getting code-coverage metrics and reporting working for an umbrella project in a self-hosted GitLab instance (15.3). There were a few things I tried along the way and I’m posting it here in the hope that it might save others a little bit of time in the future. Also I don’t have a blog, and no interest in maintaining one, so this will be my own aide memoir.

Regarding GitLab, there appear to be basically two different aspects of support for code-coverage. First, a single metric providing a % measure of total code-coverage which is used in a number of places:

  • On pipelines, i.e. what is the coverage of a particular build
  • On merge requests, which also tells us the delta vs the current coverage of the default branch, typically main
  • As a charted time-series value in ‘Analytics’
  • As a value you can stick on a badge, also taken from the default branch

The second, is a more detailed view, allowing you to visualise the actual changes to coverage in individual files within an existing merge request (discussed in more detail here).

With that out of the way, here’s what I landed on:

Run all the tests with coverage

In the .gitlab-ci.yml:

script:
  - MIX_ENV=test mix test --cover export-coverage default

Note, I didn’t need to add any test_coverage: configuration to any of the mix projects, nor the root one.

Calculate the total test coverage

This is what will be used for pipelines, stats and badges.

after_script:
  # Generate test coverage metric
  - MIX_ENV=test mix test.coverage || true
...
coverage:
  - '/    Coverage:   \d+.\d+%/'

Generate Cobertura files

In order to present coverage in merge requests, GitLab requires that coverage reports in Cobertura format be available. Generating a single coverage file from all umbrella applications was achieved using covertool.

In the root mix.exs, add the following dependency:

def deps do
  [
    {:covertool, "~> 2.0.4", only: :test, runtime: false, app: false, compile: "rebar3 escriptize"}
  ]
end

Then, in the after_script: section of the .gitlab-ci.yml, have covertool build a single coverage.xml file from the raw coverage data generated during the test run, first by using cover to combine the individual coverage files into one, and then using covertool to convert that into cobertura format. In the example below, there are 3 projects in the umbrella, imaginatively named :proj_a, :proj_b and :proj_c.

after_script:
  # Generate test coverage metric
  - MIX_ENV=test mix test.coverage || true
  # Build a single coverage file, all.coverdata
  - elixir 
    -e "Enum.each(~w(proj_a proj_b proj_c), fn app -> :cover.import(~c(./apps/#{app}/cover/default.coverdata)) end)"
    -e ":cover.export(~c(./all.coverdata))"
  # Convert it to cobertura format in a single coverage.xml file
  - ./deps/covertool/_build/default/bin/covertool
    -cover all.coverdata
    -output coverage.xml
    -ebin _build/test/lib/proj_a/ebin,_build/test/lib/proj_b/ebin,_build/test/lib/proj_c/ebin
  # Fix up the paths in the resulting file
  - sed -i "/\/builds\/umbrella\///g" coverage.xml

The final step fixes up the paths in the coverage.xml file so the <filename> elements contain the same path that GitLab uses in the merge requests. In the above example this has the affect of changing
<filename>/builds/umbrella/apps/...</filename to <filename>apps/...</filename.

Upload the coverage report

artifacts:
  reports:
    coverage_report:
      coverage_format: cobertura
      path: coverage.xml

A final note - in the above examples I’ve used multiline yaml in some of the commands to improve the formatting, but in my own system it’s not been written and tested that way so you might need to tweak it a bit.

Where Next?

Popular in Discussions Top

CharlesO
Erlang :list.nth simple, but 1 - based nth(1, [H|_]) -&gt; H; nth(N, [_|T]) when N &gt; 1 -&gt; nth(N - 1, T). Elixir Enum.at … coo...
New
tomekowal
Hey guys! I want to create a toy project that shows a chart of temperature over time and updates every 5 seconds. I feel LiveView is per...
New
thojanssens1
It would be nice to be able to define a redirect from one route to another from the router.ex file. E.g.: redirect "/", UserController, ...
New
owaisqayum
I have a sample string sentence = "Hello, world ... 123 *** ^%&amp;*())^% %%:&gt;" From this string, I want to only keep the integers, ...
New
marciol
Please, let me know if this kind of discussion already took place in another topic . Hi all, how do you consider if is better to build ...
New
paulanthonywilson
I like Umbrella projects and pretty much always use them for personal Elixir stuff, especially Nerves things. But I don’t think this is ...
New
arcanemachine
https://nitter.net/josevalim/status/1744395345872683471 https://twitter.com/josevalim/status/1744395345872683471
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42576 114
New

We're in Beta

About us Mission Statement