Skip to content

Commit 642dfde

Browse files
committed
Implement Test Coverage Reporting
1 parent c0ba8a1 commit 642dfde

File tree

18 files changed

+200
-14
lines changed

18 files changed

+200
-14
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"
@@ -68,6 +71,11 @@ jobs:
6871
- name: Elixir test suite
6972
run: make test_elixir
7073
continue-on-error: ${{ matrix.development }}
74+
env:
75+
COVER: "${{ 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

+22
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="1" $(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,21 @@ 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_elixir.coverdata:
298+
$(Q) mkdir -p "$(ROOT_DIR)/cover"
299+
$(Q) COVER="1" $(MAKE) test_stdlib
300+
cover/combined.coverdata: cover/ex_unit_elixir.coverdata
301+
302+
cover/combined.coverdata:
303+
$(Q) if [ -z "$(GITHUB_STEP_SUMMARY)" ]; then\
304+
bin/elixir ./lib/elixir/scripts/cover.exs;\
305+
else\
306+
bin/elixir ./lib/elixir/scripts/cover.exs | tee "$(GITHUB_STEP_SUMMARY)";\
307+
fi
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

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
{line_exclude, line_include} =
66
if line = System.get_env("LINE"), do: {[:test], [line: line]}, else: {[], []}
77

8+
Code.require_file("../../elixir/scripts/cover_record.exs", __DIR__)
9+
CoverageRecorder.maybe_record("eex")
10+
811
ExUnit.start(
912
trace: !!System.get_env("TRACE"),
1013
include: line_include,

lib/elixir/scripts/cover.exs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!bin/elixir
2+
3+
# SPDX-License-Identifier: Apache-2.0
4+
# SPDX-FileCopyrightText: 2021 The Elixir Team
5+
6+
Code.require_file("cover_record.exs", __DIR__)
7+
cover_pid = CoverageRecorder.enable_coverage()
8+
9+
coverdata_inputs =
10+
CoverageRecorder.cover_dir() |> Path.join("ex_unit_*.coverdata") |> Path.wildcard()
11+
12+
coverdata_output = Path.join(CoverageRecorder.cover_dir(), "combined.coverdata")
13+
14+
for file <- coverdata_inputs do
15+
:ok = :cover.import(String.to_charlist(file))
16+
end
17+
18+
:ok = :cover.export(String.to_charlist(coverdata_output))
19+
20+
{:ok, _} = Application.ensure_all_started(:mix)
21+
22+
# Silence analyse import messages emitted by cover
23+
{:ok, string_io} = StringIO.open("")
24+
Process.group_leader(cover_pid, string_io)
25+
26+
:ok =
27+
Mix.Tasks.Test.Coverage.generate_cover_results(
28+
output: CoverageRecorder.cover_dir(),
29+
summary: [threshold: 0]
30+
)

lib/elixir/scripts/cover_record.exs

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
# SPDX-FileCopyrightText: 2021 The Elixir Team
3+
4+
defmodule CoverageRecorder do
5+
def enabled? do
6+
case System.fetch_env("COVER") do
7+
{:ok, truthy} when truthy in ~w[1 true yes y] ->
8+
true
9+
10+
_ ->
11+
false
12+
end
13+
end
14+
15+
def maybe_record(suite_name) do
16+
if enabled?() do
17+
record(suite_name)
18+
end
19+
end
20+
21+
def enable_coverage do
22+
_ = :cover.stop()
23+
{:ok, pid} = :cover.start()
24+
25+
cover_compile_ebins()
26+
27+
pid
28+
end
29+
30+
def cover_dir, do: Path.join(root_dir(), "cover")
31+
32+
defp root_dir, do: Path.join(__DIR__, "../../..")
33+
defp ebins, do: root_dir() |> Path.join("lib/*/ebin") |> Path.wildcard()
34+
35+
defp record(suite_name) do
36+
file = Path.join(cover_dir(), "ex_unit_#{suite_name}.coverdata")
37+
38+
enable_coverage()
39+
40+
System.at_exit(fn _status ->
41+
:ok = :cover.export(String.to_charlist(file))
42+
end)
43+
end
44+
45+
defp cover_compile_ebins do
46+
for ebin <- ebins(),
47+
result <- :cover.compile_beam_directory(String.to_charlist(ebin)) do
48+
case result do
49+
{:ok, _module} ->
50+
:ok
51+
52+
{:error, reason} ->
53+
raise "Failed to cover compile directory #{ebin} with reason: #{inspect(reason)}"
54+
end
55+
end
56+
end
57+
end

lib/elixir/test/elixir/exception_test.exs

+1
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,7 @@ defmodule ExceptionTest do
888888
assert stack == [{BlameModule, :fun, 1, [line: 13]}]
889889
end
890890

891+
@tag :require_ast
891892
test "annotates args and clauses from mfa" do
892893
import PathHelpers
893894

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/test_helper.exs

+13-1
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,21 @@ source_exclude =
124124
[]
125125
end
126126

127+
Code.require_file("../../scripts/cover_record.exs", __DIR__)
128+
CoverageRecorder.maybe_record("elixir")
129+
130+
cover_exclude =
131+
if CoverageRecorder.enabled?() do
132+
[:require_ast]
133+
else
134+
[]
135+
end
136+
127137
ExUnit.start(
128138
trace: !!System.get_env("TRACE"),
129139
assert_receive_timeout: assert_timeout,
130-
exclude: epmd_exclude ++ os_exclude ++ line_exclude ++ distributed_exclude ++ source_exclude,
140+
exclude:
141+
epmd_exclude ++
142+
os_exclude ++ line_exclude ++ distributed_exclude ++ source_exclude ++ cover_exclude,
131143
include: line_include
132144
)

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

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ 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.require_file("../../elixir/scripts/cover_record.exs", __DIR__)
11+
CoverageRecorder.maybe_record("ex_unit")
12+
1013
ExUnit.start(
1114
trace: !!System.get_env("TRACE"),
1215
include: line_include,

lib/iex/test/iex/helpers_test.exs

+5
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,12 @@ defmodule IEx.HelpersTest do
168168
~r/#{@example_module_path}:\d+$/
169169
end
170170

171+
@tag :require_ast
171172
test "opens function" do
172173
assert capture_iex("open(h)") |> maybe_trim_quotes() =~ ~r/#{@iex_helpers}:\d+$/
173174
end
174175

176+
@tag :require_ast
175177
test "opens function/arity" do
176178
assert capture_iex("open(b/1)") |> maybe_trim_quotes() =~ ~r/#{@iex_helpers}:\d+$/
177179
assert capture_iex("open(h/0)") |> maybe_trim_quotes() =~ ~r/#{@iex_helpers}:\d+$/
@@ -193,14 +195,17 @@ defmodule IEx.HelpersTest do
193195
~r/#{@example_module_path}:\d+$/
194196
end
195197

198+
@tag :require_ast
196199
test "opens Erlang module" do
197200
assert capture_iex("open(:elixir)") |> maybe_trim_quotes() =~ ~r/#{@elixir_erl}:\d+$/
198201
end
199202

203+
@tag :require_ast
200204
test "opens Erlang module.function" do
201205
assert capture_iex("open(:elixir.start)") |> maybe_trim_quotes() =~ ~r/#{@elixir_erl}:\d+$/
202206
end
203207

208+
@tag :require_ast
204209
test "opens Erlang module.function/arity" do
205210
assert capture_iex("open(:elixir.start/2)") |> maybe_trim_quotes() =~
206211
~r/#{@elixir_erl}:\d+$/

lib/iex/test/test_helper.exs

+11-1
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,21 @@ source_exclude =
3232
[]
3333
end
3434

35+
Code.require_file("../../elixir/scripts/cover_record.exs", __DIR__)
36+
CoverageRecorder.maybe_record("iex")
37+
38+
cover_exclude =
39+
if CoverageRecorder.enabled?() do
40+
[:require_ast]
41+
else
42+
[]
43+
end
44+
3545
ExUnit.start(
3646
assert_receive_timeout: assert_timeout,
3747
trace: !!System.get_env("TRACE"),
3848
include: line_include,
39-
exclude: line_exclude ++ erlang_doc_exclude ++ source_exclude
49+
exclude: line_exclude ++ erlang_doc_exclude ++ source_exclude ++ cover_exclude
4050
)
4151

4252
defmodule IEx.Case do

0 commit comments

Comments
 (0)