Skip to content

Commit d949ac4

Browse files
committed
wip: Implement Test Coverage Reporting
1 parent e0c016c commit d949ac4

File tree

23 files changed

+1431
-1197
lines changed

23 files changed

+1431
-1197
lines changed

.github/workflows/ci.yml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@ permissions:
2121

2222
jobs:
2323
test_linux:
24-
name: Ubuntu 24.04, Erlang/OTP ${{ matrix.otp_version }}${{ matrix.deterministic && ' (deterministic)' || '' }}
24+
name: Ubuntu 24.04, Erlang/OTP ${{ matrix.otp_version }}${{ matrix.deterministic && ' (deterministic)' || '' }}${{ matrix.coverage && ' (coverage)' || '' }}
2525
strategy:
2626
fail-fast: false
2727
matrix:
2828
include:
2929
- otp_version: "27.1"
3030
deterministic: true
31+
- otp_version: "27.1"
32+
erlc_opts: "warnings_as_errors"
33+
coverage: true
3134
- otp_version: "27.1"
3235
otp_latest: true
3336
erlc_opts: "warnings_as_errors"
@@ -65,9 +68,14 @@ jobs:
6568
- name: Erlang test suite
6669
run: make test_erlang
6770
continue-on-error: ${{ matrix.development }}
71+
if: "${{ !matrix.coverage }}"
6872
- name: Elixir test suite
6973
run: make test_elixir
7074
continue-on-error: ${{ matrix.development }}
75+
if: "${{ !matrix.coverage }}"
76+
- name: "Calculate Coverage"
77+
run: make cover
78+
if: "${{ matrix.coverage }}"
7179
- name: Build docs (ExDoc main)
7280
if: ${{ matrix.otp_latest }}
7381
run: |
@@ -85,6 +93,12 @@ jobs:
8593
# Recompile System without .git
8694
cd lib/elixir && ../../bin/elixirc -o ebin lib/system.ex && cd -
8795
taskset 1 make check_reproducible
96+
- name: "Upload Coverage Artifact"
97+
if: "${{ matrix.coverage }}"
98+
uses: actions/upload-artifact@4cec3d8aa04e39d1a68397de0c4cd6fb9dce8ec1 # v4.6.1
99+
with:
100+
name: TestCoverage
101+
path: cover/*
88102

89103
test_windows:
90104
name: Windows Server 2019, Erlang/OTP ${{ matrix.otp_version }}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@
1515
/.eunit
1616
.elixir.plt
1717
erl_crash.dump
18+
/cover/

Makefile

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ lib/$(1)/ebin/Elixir.$(2).beam: $(wildcard lib/$(1)/lib/*.ex) $(wildcard lib/$(1
5353
test_$(1): test_formatted $(1)
5454
@ echo "==> $(1) (ex_unit)"
5555
$(Q) cd lib/$(1) && ../../bin/elixir -r "test/test_helper.exs" -pr "test/**/$(TEST_FILES)";
56+
57+
cover/exunit_$(1).coverdata:
58+
$(Q) mkdir -p cover
59+
$(Q) COVER_FILE="cover/exunit_$(1).coverdata" $(MAKE) test_$(1)
60+
cover/combined.coverdata: cover/exunit_$(1).coverdata
5661
endef
5762

5863
define WRITE_SOURCE_DATE_EPOCH
@@ -175,6 +180,7 @@ clean: clean_man
175180
rm -rf lib/mix/test/fixtures/git_sparse_repo/
176181
rm -rf lib/mix/test/fixtures/archive/ebin/
177182
rm -f erl_crash.dump
183+
rm -rf cover/*
178184

179185
clean_elixir:
180186
$(Q) rm -f lib/*/ebin/Elixir.*.beam
@@ -287,6 +293,22 @@ test_stdlib: compile
287293
cd lib/elixir && ../../bin/elixir --sname primary -r "test/elixir/test_helper.exs" -pr "test/elixir/**/$(TEST_FILES)"; \
288294
fi
289295

296+
cover/exunit_stdlib.coverdata:
297+
$(Q) mkdir -p cover
298+
$(Q) COVER_FILE="$(realpath cover)/exunit_stdlib.coverdata" $(MAKE) test_stdlib
299+
cover/combined.coverdata: cover/exunit_stdlib.coverdata
300+
301+
cover/eunit_stdlib.coverdata:
302+
$(Q) mkdir -p cover
303+
$(Q) COVER_FILE="$(realpath cover)/eunit_stdlib.coverdata" $(MAKE) test_erlang
304+
cover/combined.coverdata: cover/eunit_stdlib.coverdata
305+
306+
cover/combined.coverdata:
307+
$(Q) bin/elixir ./lib/elixir/scripts/cover.exs
308+
309+
.PHONY: cover
310+
cover: cover/combined.coverdata
311+
290312
#==> Dialyzer tasks
291313

292314
DIALYZER_OPTS = --no_check_plt --fullpath -Werror_handling -Wunmatched_returns -Wunderspecs

lib/eex/test/test_helper.exs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
{line_exclude, line_include} =
66
if line = System.get_env("LINE"), do: {[:test], [line: line]}, else: {[], []}
77

8+
Code.eval_file("../../../elixir/scripts/cover_record.exs", __ENV__.file)
9+
810
ExUnit.start(
911
trace: !!System.get_env("TRACE"),
1012
include: line_include,

lib/elixir/scripts/cover.exs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!bin/elixir
2+
3+
# SPDX-License-Identifier: Apache-2.0
4+
# SPDX-FileCopyrightText: 2021 The Elixir Team
5+
6+
root_dir = __ENV__.file |> Path.dirname() |> Path.join("../../..")
7+
cover_dir = Path.join(root_dir, "cover")
8+
coverdata_inputs = cover_dir |> Path.join("{exunit,eunit}_*.coverdata") |> Path.wildcard()
9+
coverdata_output = Path.join(cover_dir, "combined.coverdata")
10+
ebins = root_dir |> Path.join("lib/*/ebin") |> Path.wildcard()
11+
12+
_ = :cover.stop()
13+
{:ok, cover_pid} = :cover.start()
14+
15+
for ebin <- ebins,
16+
result <- :cover.compile_beam_directory(String.to_charlist(ebin)) do
17+
case result do
18+
{:ok, _module} ->
19+
:ok
20+
21+
{:error, reason} ->
22+
raise "Failed to cover compile directory #{ebin} with reason: #{inspect(reason)}"
23+
end
24+
end
25+
26+
for file <- coverdata_inputs do
27+
:ok = :cover.import(String.to_charlist(file))
28+
end
29+
30+
:ok = :cover.export(String.to_charlist(coverdata_output))
31+
32+
{:ok, _} = Application.ensure_all_started(:mix)
33+
34+
# Silence analyse import messages emitted by cover
35+
{:ok, string_io} = StringIO.open("")
36+
Process.group_leader(cover_pid, string_io)
37+
38+
:ok =
39+
Mix.Tasks.Test.Coverage.generate_cover_results(
40+
output: cover_dir,
41+
summary: [threshold: 0]
42+
)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
# SPDX-FileCopyrightText: 2021 The Elixir Team
3+
4+
root_dir = __ENV__.file |> Path.dirname() |> Path.join("../../..")
5+
ebins = root_dir |> Path.join("lib/*/ebin") |> Path.wildcard()
6+
7+
case System.fetch_env("COVER_FILE") do
8+
{:ok, file} ->
9+
_ = :cover.stop()
10+
{:ok, _pid} = :cover.start()
11+
12+
for ebin <- ebins,
13+
result <- :cover.compile_beam_directory(String.to_charlist(ebin)) do
14+
case result do
15+
{:ok, _module} ->
16+
:ok
17+
18+
{:error, reason} ->
19+
raise "Failed to cover compile directory #{ebin} with reason: #{inspect(reason)}"
20+
end
21+
end
22+
23+
System.at_exit(fn _status ->
24+
:ok = :cover.export(String.to_charlist(file))
25+
end)
26+
27+
true
28+
29+
:error ->
30+
false
31+
end

lib/elixir/test/elixir/exception_test.exs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,9 @@ defmodule ExceptionTest do
556556
end
557557

558558
test "annotates function clause errors" do
559-
assert blame_message(Access, & &1.fetch(:foo, :bar)) =~ """
559+
message = blame_message(Access, & &1.fetch(:foo, :bar))
560+
561+
assert message =~ """
560562
no function clause matching in Access.fetch/2
561563
562564
The following arguments were given to Access.fetch/2:
@@ -566,11 +568,15 @@ defmodule ExceptionTest do
566568
567569
# 2
568570
:bar
571+
"""
569572

570-
Attempted function clauses (showing 5 out of 5):
573+
if Access not in :cover.modules() do
574+
assert message =~ """
575+
Attempted function clauses (showing 5 out of 5):
571576
572-
def fetch(-%module{} = container-, key)
573-
"""
577+
def fetch(-%module{} = container-, key)
578+
"""
579+
end
574580
end
575581

576582
test "annotates undefined function error with suggestions" do
@@ -863,10 +869,16 @@ defmodule ExceptionTest do
863869
{exception, stack} =
864870
Exception.blame(:error, :function_clause, [{Keyword, :pop, args, [line: 13]}])
865871

866-
assert %FunctionClauseError{kind: :def, args: ^args, clauses: [_]} = exception
872+
if Keyword in :cover.modules() do
873+
assert %FunctionClauseError{kind: nil, args: ^args, clauses: nil} = exception
874+
else
875+
assert %FunctionClauseError{kind: :def, args: ^args, clauses: [_]} = exception
876+
end
877+
867878
assert stack == [{Keyword, :pop, 3, [line: 13]}]
868879
end
869880

881+
@tag :require_ast
870882
test "annotates args and clauses from mfa" do
871883
import PathHelpers
872884

lib/elixir/test/elixir/kernel/dialyzer_test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ defmodule Kernel.DialyzerTest do
88
use ExUnit.Case, async: true
99

1010
@moduletag :dialyzer
11+
@moduletag :require_ast
1112
import PathHelpers
1213

1314
setup_all do

lib/elixir/test/elixir/module/types/integration_test.exs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,7 @@ defmodule Module.Types.IntegrationTest do
458458
assert_warnings(files, warnings)
459459
end
460460

461+
@tag :require_ast
461462
test "String.Chars protocol dispatch" do
462463
files = %{
463464
"a.ex" => """
@@ -520,6 +521,7 @@ defmodule Module.Types.IntegrationTest do
520521
assert_warnings(files, warnings, consolidate_protocols: true)
521522
end
522523

524+
@tag :require_ast
523525
test "Enumerable protocol dispatch" do
524526
files = %{
525527
"a.ex" => """

lib/elixir/test/elixir/module_test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,7 @@ defmodule ModuleTest do
431431
assert backend.debug_info(:elixir_v1, ModuleCreateNoDebugInfo, data, []) == {:error, :missing}
432432
end
433433

434+
@tag :require_ast
434435
test "compiles to core" do
435436
{:ok, {Atom, [{~c"Dbgi", dbgi}]}} = Atom |> :code.which() |> :beam_lib.chunks([~c"Dbgi"])
436437
{:debug_info_v1, backend, data} = :erlang.binary_to_term(dbgi)

0 commit comments

Comments
 (0)