Skip to content

Commit 59d61d8

Browse files
andrei-mihailaeproxus
authored andcommitted
Fix an issue with the code coverage in Erlang.mk
It seems that just purging away the meck generated module (in `meck:unload`) is not removing it from the cover data. Exporting the cover data using `cover:export/1` will also export data about the meck generated module. Erlang.mk [uses](https://github.com/ninenines/erlang.mk/blob/b8a27ab752389394bfdb8c15d1b69455c313991e/plugins/cover.mk#L52) the export & import flow to generate the HTML coverage report, so the report will show the purged module - which is not correct. This might be due to an issue in the coverage tool and it would be better to investigate and fix it there, but this fix shouldn't break anything, so I think it is acceptable.
1 parent dfb47c5 commit 59d61d8

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/meck_proc.erl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,13 @@ cleanup(Mod) ->
660660
code:purge(Mod),
661661
code:delete(Mod),
662662
code:purge(meck_util:original_name(Mod)),
663-
code:delete(meck_util:original_name(Mod)).
663+
Res = code:delete(meck_util:original_name(Mod)),
664+
665+
% `cover:export` might still export the meck generated module,
666+
% make sure that does not happen.
667+
_ = cover:reset(meck_util:original_name(Mod)),
668+
669+
Res.
664670

665671
-spec times_called(OptFunc::'_' | atom(),
666672
meck_args_matcher:args_matcher(),

test/meck_tests.erl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,6 +1181,30 @@ cover_passthrough_test() ->
11811181
?assertEqual({ok, {meck_test_module, {2,1}}},
11821182
cover:analyze(meck_test_module, module)).
11831183

1184+
cover_no_meck_original_in_cover_export_test() ->
1185+
{ok, _} = cover:compile("test/meck_test_module.erl"),
1186+
1187+
passthrough_test([]),
1188+
1189+
Filename =
1190+
lists:flatten(
1191+
io_lib:format(
1192+
"tmp_~b_~p.coverdata",
1193+
[rand:uniform(100_000), meck_util:original_name(meck_test_module)]
1194+
)
1195+
),
1196+
try
1197+
ok = cover:export(Filename),
1198+
ok = cover:import(Filename)
1199+
after
1200+
_ = file:delete(Filename)
1201+
end,
1202+
1203+
?assertNot(
1204+
lists:member(meck_util:original_name(meck_test_module), cover:imported_modules()),
1205+
"the meck generated module should not be in the exported cover data"
1206+
).
1207+
11841208
cover_path_test() ->
11851209
{ok, _} = cover:compile("test/meck_test_module.erl"),
11861210
?assertEqual({ok, {meck_test_module, {0,3}}},

0 commit comments

Comments
 (0)