Skip to content

Commit

Permalink
test/cover_compile.erl: Adapt testing to Erlang/OTP 27's native cover…
Browse files Browse the repository at this point in the history
…age support

[Why]
When native coverage support is enabled, the standalone function's
module won't update the initial module(s) counters.

[How]
The assertions are skipped. The testcases are kind of useless when
native coverage support is enabled.
  • Loading branch information
dumbbell committed Jul 16, 2024
1 parent 609880f commit e243002
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 17 deletions.
14 changes: 1 addition & 13 deletions src/horus.erl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
-include_lib("stdlib/include/assert.hrl").

-include("include/horus.hrl").
-include("src/horus_cover.hrl").
-include("src/horus_fun.hrl").
-include("src/horus_error.hrl").

Expand Down Expand Up @@ -324,19 +325,6 @@ fun((#{calls := #{Call :: mfa() => true},

-define(SF_ENTRYPOINT, run).

-if(?OTP_RELEASE >= 27).
-define(IF_NATIVE_COVERAGE_IS_SUPPORTED(IfSupportedBlock, ElseBlock),
(case code:coverage_support() of
true ->
IfSupportedBlock;
false ->
ElseBlock
end)).
-else.
-define(IF_NATIVE_COVERAGE_IS_SUPPORTED(_IfSupportedBlock, ElseBlock),
(ElseBlock)).
-endif.

-spec to_standalone_fun(Fun) -> StandaloneFun when
Fun :: fun(),
StandaloneFun :: horus_fun().
Expand Down
20 changes: 20 additions & 0 deletions src/horus_cover.hrl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
%% This Source Code Form is subject to the terms of the Mozilla Public
%% License, v. 2.0. If a copy of the MPL was not distributed with this
%% file, You can obtain one at https://mozilla.org/MPL/2.0/.
%%
%% Copyright © 2024 Broadcom. All Rights Reserved. The term "Broadcom" refers
%% to Broadcom Inc. and/or its subsidiaries.
%%

-if(?OTP_RELEASE >= 27).
-define(IF_NATIVE_COVERAGE_IS_SUPPORTED(IfSupportedBlock, ElseBlock),
(case code:coverage_support() of
true ->
IfSupportedBlock;
false ->
ElseBlock
end)).
-else.
-define(IF_NATIVE_COVERAGE_IS_SUPPORTED(_IfSupportedBlock, ElseBlock),
(ElseBlock)).
-endif.
39 changes: 35 additions & 4 deletions test/cover_compile.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,17 @@

-include_lib("eunit/include/eunit.hrl").

-include("src/horus_cover.hrl").
-include("src/horus_fun.hrl").

cover_compilation_works_test() ->
?IF_NATIVE_COVERAGE_IS_SUPPORTED(
begin
_ = code:set_coverage_mode(line_counters),
ok
end,
ok),

Module = cover_compiled_mod1,
Arg = arg,
Ret = Module:run(Arg),
Expand Down Expand Up @@ -51,8 +59,16 @@ cover_compilation_works_test() ->
?assertEqual({ok, InitialState}, cover:analyse(Module)),

StandaloneFun2 = horus:to_standalone_fun(Fun, #{debug_info => true}),
?assertEqual(Ret, horus:exec(StandaloneFun2, [Arg])),
?assertEqual({ok, Analysis}, cover:analyse(Module)),
?IF_NATIVE_COVERAGE_IS_SUPPORTED(
begin
?debugMsg(
"Coverage support testing skipped as native coverage counters "
"can't be modified externally")
end,
begin
?assertEqual(Ret, horus:exec(StandaloneFun2, [Arg])),
?assertEqual({ok, Analysis}, cover:analyse(Module))
end),

ok = cover:reset(Module),

Expand All @@ -75,6 +91,13 @@ cover_compilation_works_test() ->
ok.

cover_on_remote_node_works_test() ->
?IF_NATIVE_COVERAGE_IS_SUPPORTED(
begin
_ = code:set_coverage_mode(line_counters),
ok
end,
ok),

ok = helpers:start_epmd(),
{ok, _} = net_kernel:start(?FUNCTION_NAME, #{name_domain => shortnames}),
_ = cover:start(),
Expand Down Expand Up @@ -132,8 +155,16 @@ cover_on_remote_node_works_test() ->
StandaloneFun2 = erpc:call(
Node,
horus, to_standalone_fun, [Fun, #{debug_info => true}]),
?assertEqual(Ret, erpc:call(Node, horus, exec, [StandaloneFun2, [Arg]])),
?assertEqual({ok, Analysis}, cover:analyse(Module)),
?IF_NATIVE_COVERAGE_IS_SUPPORTED(
begin
?debugMsg(
"Coverage support testing skipped as native coverage counters "
"can't be modified externally")
end,
begin
?assertEqual(Ret, erpc:call(Node, horus, exec, [StandaloneFun2, [Arg]])),
?assertEqual({ok, Analysis}, cover:analyse(Module))
end),

ok = cover:reset(Module),

Expand Down

0 comments on commit e243002

Please sign in to comment.