feat: Report coverage to gitlab

This commit is contained in:
Robert Prehn 2021-07-22 15:39:17 -05:00
parent d57a215ec4
commit 702e41c824
6 changed files with 157 additions and 4 deletions

3
.gitignore vendored
View file

@ -44,3 +44,6 @@ config/*.secret.exs
# Temporary CI build file
build.env
# CI metrics file
metrics.txt

View file

@ -26,7 +26,7 @@ variables:
- name: postgres:12
script: script/cibuild
test:
test_1.10.4:
<<: *test_template
image: "elixir:1.10.4-alpine"
@ -34,9 +34,17 @@ test_1.11.4:
<<: *test_template
image: "elixir:1.11.4-alpine"
test_1.12.1:
test:
<<: *test_template
image: "elixir:1.12.1-alpine"
script:
- script/cibuild
- script/coverage-json-to-metrics
- script/coverage-json-to-cobertura
artifacts:
reports:
metrics: metrics.txt
cobertura: cover/cobertura.xml
credo:
stage: test

View file

@ -13,7 +13,14 @@ defmodule Legendary.Mixfile do
deps: deps(),
test_coverage: [tool: ExCoveralls],
aliases: aliases(),
preferred_cli_env: [coveralls: :test, "coveralls.detail": :test, "coveralls.post": :test, "coveralls.html": :test, "coveralls.json": :test],
preferred_cli_env: [
coveralls: :test,
"coveralls.detail": :test,
"coveralls.post": :test,
"coveralls.html": :test,
"coveralls.json": :test,
"coveralls.xml": :test
],
]
end

View file

@ -12,6 +12,6 @@ mix deps.get
mix ecto.create
mix ecto.migrate
mix test
mix coveralls.json
script/restore-timestamps

105
script/coverage-json-to-cobertura Executable file
View file

@ -0,0 +1,105 @@
#!/usr/bin/env elixir
Mix.install([
{:jason, "~> 1.0"},
{:xml_builder, "~> 2.1"}
])
%{"source_files" => data} =
"cover/excoveralls.json"
|> File.read!()
|> Jason.decode!()
{total_covered, total_total} =
data
|> Enum.reduce({0,0}, fn %{"coverage" => cover}, {covered, total} ->
file_covered =
cover
|> Enum.reduce(0, &(if is_integer(&1) && &1 > 0, do: &2 + 1, else: &2))
file_total =
cover
|> Enum.reduce(0, &(if is_nil(&1), do: &2, else: &2 + 1))
{covered + file_covered, total + file_total}
end)
ratio = total_covered / total_total
files =
data
|> Enum.map(fn %{"coverage" => cover, "name" => name} ->
file_covered =
cover
|> Enum.reduce(0, &(if is_integer(&1) && &1 > 0, do: &2 + 1, else: &2))
file_total =
cover
|> Enum.reduce(0, &(if is_nil(&1), do: &2, else: &2 + 1))
lines =
cover
|> Enum.with_index
|> Enum.map(fn
{nil, _index} ->
nil
{line, index} ->
{:line, %{"number" => index, "hits" => line}, []}
end)
|> Enum.reject(&is_nil/1)
ratio =
if file_total == 0 do
1.0
else
file_covered / file_total
end
{
:class,
%{"filename" => name, "line-rate" => ratio},
[
{
:lines,
%{},
lines
}
]
}
end)
buffer =
XmlBuilder.document([
XmlBuilder.doctype(
"coverage",
system: ["http://cobertura.sourceforge.net/xml/coverage-03.dtd"]
),
{
:coverage,
%{
"line-rate" => ratio,
},
[
{
:packages,
%{},
[
{
:package,
%{"name" => "", "line-rate" => ratio},
[
{
:classes,
%{},
files
}
]
}
]
}
]
}
])
|> XmlBuilder.generate
File.write!("cover/cobertura.xml", buffer)

30
script/coverage-json-to-metrics Executable file
View file

@ -0,0 +1,30 @@
#!/usr/bin/env elixir
Mix.install([
{:jason, "~> 1.0"}
])
%{"source_files" => data} =
"cover/excoveralls.json"
|> File.read!()
|> Jason.decode!()
{total_covered, total_total} =
data
|> Enum.reduce({0,0}, fn %{"coverage" => cover}, {covered, total} ->
file_covered =
cover
|> Enum.reduce(0, &(if is_integer(&1) && &1 > 0, do: &2 + 1, else: &2))
file_total =
cover
|> Enum.reduce(0, &(if is_nil(&1), do: &2, else: &2 + 1))
{covered + file_covered, total + file_total}
end)
ratio =
(total_covered / total_total)
|> Float.round(3)
File.write!("metrics.txt", "coverage #{ratio}")