diff --git a/src/horus.erl b/src/horus.erl index 01ae592..f8e7794 100644 --- a/src/horus.erl +++ b/src/horus.erl @@ -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"). @@ -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(). diff --git a/src/horus_cover.hrl b/src/horus_cover.hrl new file mode 100644 index 0000000..1c599b7 --- /dev/null +++ b/src/horus_cover.hrl @@ -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. diff --git a/test/cover_compile.erl b/test/cover_compile.erl index f684f8c..541d957 100644 --- a/test/cover_compile.erl +++ b/test/cover_compile.erl @@ -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), @@ -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), @@ -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(), @@ -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),