Skip to content

Commit 64f4c8d

Browse files
committed
Implement Test Coverage Reporting
1 parent e0c016c commit 64f4c8d

22 files changed

+231
-21
lines changed

.github/workflows/ci.yml

+15-1
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

+1
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@
1515
/.eunit
1616
.elixir.plt
1717
erl_crash.dump
18+
/cover/

Makefile

+18
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ GIT_REVISION = $(strip $(shell git rev-parse HEAD 2> /dev/null ))
2525
GIT_TAG = $(strip $(shell head="$(call GIT_REVISION)"; git tag --points-at $$head 2> /dev/null | grep -v latest | tail -1))
2626
SOURCE_DATE_EPOCH_PATH = lib/elixir/tmp/ebin_reproducible
2727
SOURCE_DATE_EPOCH_FILE = $(SOURCE_DATE_EPOCH_PATH)/SOURCE_DATE_EPOCH
28+
ROOT_DIR:=$(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
2829

2930
.PHONY: install install_man build_plt clean_plt dialyze test check_reproducible clean clean_elixir clean_man format docs Docs.zip Precompiled.zip zips
3031
.NOTPARALLEL:
@@ -53,6 +54,11 @@ lib/$(1)/ebin/Elixir.$(2).beam: $(wildcard lib/$(1)/lib/*.ex) $(wildcard lib/$(1
5354
test_$(1): test_formatted $(1)
5455
@ echo "==> $(1) (ex_unit)"
5556
$(Q) cd lib/$(1) && ../../bin/elixir -r "test/test_helper.exs" -pr "test/**/$(TEST_FILES)";
57+
58+
cover/ex_unit_$(1).coverdata:
59+
$(Q) mkdir -p "$(ROOT_DIR)/cover"
60+
$(Q) COVER_FILE="$(ROOT_DIR)/cover/ex_unit_$(1).coverdata" $(MAKE) test_$(1)
61+
cover/combined.coverdata: cover/ex_unit_$(1).coverdata
5662
endef
5763

5864
define WRITE_SOURCE_DATE_EPOCH
@@ -175,6 +181,7 @@ clean: clean_man
175181
rm -rf lib/mix/test/fixtures/git_sparse_repo/
176182
rm -rf lib/mix/test/fixtures/archive/ebin/
177183
rm -f erl_crash.dump
184+
rm -rf cover/*
178185

179186
clean_elixir:
180187
$(Q) rm -f lib/*/ebin/Elixir.*.beam
@@ -287,6 +294,17 @@ test_stdlib: compile
287294
cd lib/elixir && ../../bin/elixir --sname primary -r "test/elixir/test_helper.exs" -pr "test/elixir/**/$(TEST_FILES)"; \
288295
fi
289296

297+
cover/ex_unit_stdlib.coverdata:
298+
$(Q) mkdir -p "$(ROOT_DIR)/cover"
299+
$(Q) COVER_FILE="$(ROOT_DIR)/cover/ex_unit_stdlib.coverdata" $(MAKE) test_stdlib
300+
cover/combined.coverdata: cover/ex_unit_stdlib.coverdata
301+
302+
cover/combined.coverdata:
303+
$(Q) bin/elixir ./lib/elixir/scripts/cover.exs
304+
305+
.PHONY: cover
306+
cover: cover/combined.coverdata
307+
290308
#==> Dialyzer tasks
291309

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

lib/eex/test/test_helper.exs

+2
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

+42
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("ex_unit_*.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+
)

lib/elixir/scripts/cover_record.exs

+31
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

+17-5
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

+1
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

+2
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

+1
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)

lib/elixir/test/elixir/protocol/consolidation_test.exs

+1
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ defmodule Protocol.ConsolidationTest do
246246
end
247247
end
248248

249+
@tag :require_ast
249250
test "consolidation errors on missing BEAM files" do
250251
defprotocol NoBeam do
251252
def example(arg)

lib/elixir/test/elixir/test_helper.exs

+12-1
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,20 @@ source_exclude =
120120
[]
121121
end
122122

123+
cover_enabled? = Code.eval_file("../../../scripts/cover_record.exs", __ENV__.file)
124+
125+
cover_exclude =
126+
if cover_enabled? do
127+
[:require_ast]
128+
else
129+
[]
130+
end
131+
123132
ExUnit.start(
124133
trace: !!System.get_env("TRACE"),
125134
assert_receive_timeout: assert_timeout,
126-
exclude: epmd_exclude ++ os_exclude ++ line_exclude ++ distributed_exclude ++ source_exclude,
135+
exclude:
136+
epmd_exclude ++
137+
os_exclude ++ line_exclude ++ distributed_exclude ++ source_exclude ++ cover_exclude,
127138
include: line_include
128139
)

lib/ex_unit/lib/ex_unit/diff.ex

+1
Original file line numberDiff line numberDiff line change
@@ -1162,6 +1162,7 @@ defmodule ExUnit.Diff do
11621162
else
11631163
other
11641164
|> Map.to_list()
1165+
|> Enum.sort()
11651166
|> Enum.map(&escape_pair/1)
11661167
|> build_map_or_struct(struct)
11671168
end

lib/ex_unit/test/ex_unit/formatter_test.exs

+27-10
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ defmodule ExUnit.FormatterTest do
8686

8787
failure = [{:exit, {{error, stack}, {:mod, :fun, []}}, []}]
8888

89-
assert trim_multiline_whitespace(format_test_failure(test(), failure, 1, 80, &formatter/2)) =~
89+
format = trim_multiline_whitespace(format_test_failure(test(), failure, 1, 80, &formatter/2))
90+
91+
assert format =~
9092
"""
9193
1) world (Hello)
9294
test/ex_unit/formatter_test.exs:1
@@ -101,11 +103,16 @@ defmodule ExUnit.FormatterTest do
101103
102104
# 2
103105
:bar
106+
"""
104107

105-
Attempted function clauses (showing 5 out of 5):
108+
if Access not in :cover.modules() do
109+
assert format =~
110+
"""
111+
Attempted function clauses (showing 5 out of 5):
106112
107-
def fetch(%module{} = container, key)
108-
"""
113+
def fetch(%module{} = container, key)
114+
"""
115+
end
109116
end
110117

111118
test "formats test exits with assertion mfa" do
@@ -177,11 +184,16 @@ defmodule ExUnit.FormatterTest do
177184
178185
# 2
179186
:bar
187+
"""
180188

181-
Attempted function clauses (showing 5 out of 5):
189+
if Access not in :cover.modules() do
190+
assert format =~
191+
"""
192+
Attempted function clauses (showing 5 out of 5):
182193
183-
def fetch(%module{} = container, key)
184-
"""
194+
def fetch(%module{} = container, key)
195+
"""
196+
end
185197

186198
assert format =~ ~r"lib/access.ex:\d+: Access.fetch/2"
187199
end
@@ -418,11 +430,16 @@ defmodule ExUnit.FormatterTest do
418430
419431
# 2
420432
:bar
433+
"""
421434

422-
Attempted function clauses (showing 5 out of 5):
435+
if Access not in :cover.modules() do
436+
assert failure =~
437+
"""
438+
Attempted function clauses (showing 5 out of 5):
423439
424-
def fetch(%module{} = container, key)
425-
"""
440+
def fetch(%module{} = container, key)
441+
"""
442+
end
426443

427444
assert failure =~ ~r"\(elixir #{System.version()}\) lib/access\.ex:\d+: Access\.fetch/2"
428445
end

lib/ex_unit/test/test_helper.exs

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Logger.configure_backend(:console, colors: [enabled: false])
77
{line_exclude, line_include} =
88
if line = System.get_env("LINE"), do: {[:test], [line: line]}, else: {[], []}
99

10+
Code.eval_file("../../../elixir/scripts/cover_record.exs", __ENV__.file)
11+
1012
ExUnit.start(
1113
trace: !!System.get_env("TRACE"),
1214
include: line_include,

0 commit comments

Comments
 (0)