Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update meck for OTP 18.0 #139

Merged
merged 2 commits into from
Apr 1, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rebar.config
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
%% Compiler Options ===========================================================
{erl_opts, [
%% Erlang releases after 17 don't put R in front of their name, and also require dict() to be written like dict:dict()
{platform_define, "^[0-9]+", namespaced_dicts},
{platform_define, "^[0-9]+", namespaced_types},
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was there a functional reason for renaming namespaced_dicts to namespaced_types? (Apart from that it looks better?) 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, it does look a bit better :) but this is actually needed for meck_tests.erl to compile in 18.0 (those non-namespaced types are completely removed now). I tried to explain it in the commit message but let me know if I should make it clearer.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, you mean the define is needed, and the new name better describes it (because it is more general)? If so, then keep the change. 😄

warn_export_all,
warn_export_vars,
warn_shadow_vars,
Expand Down
35 changes: 27 additions & 8 deletions src/meck_cover.erl
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@

%% @doc Enabled cover on `<name>_meck_original'.
compile_beam(OriginalMod, Bin) ->
alter_cover(),
{ok, _} = cover:compile_beam(OriginalMod, Bin).
CompileBeams = alter_cover(),
[{ok, _}] = CompileBeams([{OriginalMod, Bin}]).

%% @doc Given a cover file `File' exported by `cover:export' overwrite
%% the module name with `Name'.
Expand All @@ -52,19 +52,38 @@ rename_module(File, Name) ->
%%
%% 2. In order to avoid creating temporary files meck needs direct
%% access to `compile_beam/2' which allows passing a binary.
%% In OTP 18.0 the internal API of cover changed a bit and
%% compile_beam/2 was replaced by compile_beams/1.
alter_cover() ->
case lists:member({compile_beam,2}, cover:module_info(exports)) of
true ->
ok;
false ->
CoverExports = cover:module_info(exports),
case {lists:member({compile_beams,1}, CoverExports),
lists:member({compile_beam,2}, CoverExports)} of
{true, _} ->
fun cover:compile_beams/1;
{_, true} ->
fun compile_beam_wrapper/1;
{false, false} ->
Beam = meck_code:beam_file(cover),
AbsCode = meck_code:abstract_code(Beam),
Exports = [{compile_beam, 2}, {get_term, 1}, {write, 2}],
{Exports, CompileBeams} =
case lists:member({analyse,0}, CoverExports) of
true ->
%% new API from OTP 18.0 on
{[{compile_beams, 1}, {get_term, 1}, {write, 2}],
fun cover:compile_beams/1};
false ->
{[{compile_beam, 2}, {get_term, 1}, {write, 2}],
fun compile_beam_wrapper/1}
end,
AbsCode2 = meck_code:add_exports(Exports, AbsCode),
_Bin = meck_code:compile_and_load_forms(AbsCode2),
ok
CompileBeams
end.

%% wrap cover's pre-18.0 internal API to simulate the new API
compile_beam_wrapper(ModFiles) ->
[cover:compile_beam(Mod, Bin)||{Mod, Bin} <- ModFiles].

change_cover_mod_name(CoverTerms, Name) ->
{_, Terms} = lists:foldl(fun change_name_in_term/2, {Name,[]}, CoverTerms),
Terms.
Expand Down
2 changes: 1 addition & 1 deletion src/meck_proc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
%%% Definitions
%%%============================================================================

-ifdef(namespaced_dicts).
-ifdef(namespaced_types).
-type meck_dict() :: dict:dict().
-else.
-type meck_dict() :: dict().
Expand Down
4 changes: 2 additions & 2 deletions test.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
{branch, "master"}}}]}.

%% Compiler Options ===========================================================
% FIXME: Add warnings_as_errors once Hamcrest is fixed
{erl_opts, [
%% Erlang releases after 17 don't put R in front of their name, and also require dict() to be written like dict:dict()
{platform_define, "^[0-9]+", namespaced_dicts},
{platform_define, "^[0-9]+", namespaced_types},
{platform_define, "^R(?!16B03)", cover_empty_compile_opts},
warnings_as_errors,
debug_info
]}.

Expand Down