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

add support for common tests include flag #1099

Merged
merged 2 commits into from
Mar 1, 2016
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
1 change: 1 addition & 0 deletions priv/shell-completion/bash/rebar3
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ _rebar3()
--multiply_timetraps \
--scale_timetraps \
--create_priv_dir \
--include \
--verbose \
--auto_compile \
"
Expand Down
1 change: 1 addition & 0 deletions priv/shell-completion/fish/rebar3.fish
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ complete -f -c 'rebar3' -n '__fish_rebar3_using_command ct' -l abort_if_missing_
complete -f -c 'rebar3' -n '__fish_rebar3_using_command ct' -l multiply_timetraps -d "Multiply timetraps."
complete -f -c 'rebar3' -n '__fish_rebar3_using_command ct' -l scale_timetraps -d "Scale timetraps."
complete -f -c 'rebar3' -n '__fish_rebar3_using_command ct' -l create_priv_dir -d "Create priv dir (auto_per_run | auto_per_tc | manual_per_tc)."
complete -f -c 'rebar3' -n '__fish_rebar3_using_command ct' -l include -d "Directories containing additional include files."
complete -f -c 'rebar3' -n '__fish_rebar3_using_command ct' -s v -l verbose -d "Enable verbose output. Default: false."
complete -f -c 'rebar3' -n '__fish_rebar3_using_command ct' -l auto_compile -d "Let common test compile test suites instead of rebar3."

Expand Down
1 change: 1 addition & 0 deletions priv/shell-completion/zsh/_rebar3
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ _rebar3 () {
'(--multiply_timetraps)--multiply_timetraps' \
'(--scale_timetraps)--scale_timetraps' \
'(--create_priv_dir)--create_priv_dir' \
`(--include)--include[Directories containing additional include files]:includes' \
'(-v --verbose)'{-v,--verbose}'[Print coverage analysis]' \
'(--auto_compile)--auto_compile' \
&& ret=0
Expand Down
44 changes: 30 additions & 14 deletions src/rebar_prv_common_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ transform_opts([{testcase, Cases}|Rest], Acc) ->
transform_opts(Rest, [{testcase, split_string(Cases)}|Acc]);
transform_opts([{config, Configs}|Rest], Acc) ->
transform_opts(Rest, [{config, split_string(Configs)}|Acc]);
transform_opts([{include, Includes}|Rest], Acc) ->
transform_opts(Rest, [{include, split_string(Includes)}|Acc]);
transform_opts([{logopts, LogOpts}|Rest], Acc) ->
transform_opts(Rest, [{logopts, lists:map(fun(P) -> list_to_atom(P) end, split_string(LogOpts))}|Acc]);
transform_opts([{force_stop, "true"}|Rest], Acc) ->
Expand Down Expand Up @@ -243,8 +245,8 @@ application_dirs([App|Rest], Acc) ->
end.

compile(State, {ok, _} = Tests) ->
%% inject `ct_first_files` and `ct_compile_opts` into the applications
%% to be compiled
%% inject `ct_first_files`, `ct_compile_opts` and `include` (from `ct_opts`
%% and command line options) into the applications to be compiled
case inject_ct_state(State, Tests) of
{ok, NewState} -> do_compile(NewState);
Error -> Error
Expand All @@ -264,22 +266,22 @@ do_compile(State) ->

inject_ct_state(State, {ok, Tests}) ->
Apps = rebar_state:project_apps(State),
case inject_ct_state(State, Apps, []) of
case inject_ct_state(State, Tests, Apps, []) of
{ok, {NewState, ModdedApps}} ->
test_dirs(NewState, ModdedApps, Tests);
{error, _} = Error -> Error
end;
inject_ct_state(_State, Error) -> Error.

inject_ct_state(State, [App|Rest], Acc) ->
case inject(rebar_app_info:opts(App), State) of
inject_ct_state(State, Tests, [App|Rest], Acc) ->
case inject(rebar_app_info:opts(App), State, Tests) of
{error, _} = Error -> Error;
NewOpts ->
NewApp = rebar_app_info:opts(App, NewOpts),
inject_ct_state(State, Rest, [NewApp|Acc])
inject_ct_state(State, Tests, Rest, [NewApp|Acc])
end;
inject_ct_state(State, [], Acc) ->
case inject(rebar_state:opts(State), State) of
inject_ct_state(State, Tests, [], Acc) ->
case inject(rebar_state:opts(State), State, Tests) of
{error, _} = Error -> Error;
NewOpts ->
{ok, {rebar_state:opts(State, NewOpts), lists:reverse(Acc)}}
Expand All @@ -292,24 +294,35 @@ opts(Opts, Key, Default) ->
?PRV_ERROR({badconfig, {"Value `~p' of option `~p' must be a list", {Wrong, Key}}})
end.

inject(Opts, State) -> erl_opts(Opts, State).
inject(Opts, State, Tests) -> erl_opts(Opts, State, Tests).

erl_opts(Opts, State) ->
erl_opts(Opts, State, Tests) ->
%% append `ct_compile_opts` to app defined `erl_opts`
ErlOpts = opts(Opts, erl_opts, []),
CTOpts = opts(Opts, ct_compile_opts, []),
case add_transforms(append(CTOpts, ErlOpts), State) of
{error, Error} -> {error, Error};
NewErlOpts -> first_files(rebar_opts:set(Opts, erl_opts, NewErlOpts))
{error, _} = Error -> Error;
NewErlOpts -> first_files(rebar_opts:set(Opts, erl_opts, NewErlOpts), Tests)
end.

first_files(Opts) ->
first_files(Opts, Tests) ->
%% append `ct_first_files` to app defined `erl_first_files`
FirstFiles = opts(Opts, erl_first_files, []),
CTFirstFiles = opts(Opts, ct_first_files, []),
case append(CTFirstFiles, FirstFiles) of
{error, _} = Error -> Error;
NewFirstFiles -> rebar_opts:set(Opts, erl_first_files, NewFirstFiles)
NewFirstFiles -> include_files(rebar_opts:set(Opts, erl_first_files, NewFirstFiles), Tests)
end.

include_files(Opts, Tests) ->
%% append include dirs from command line and `ct_opts` to app defined
%% `erl_opts`
ErlOpts = opts(Opts, erl_opts, []),
Includes = proplists:get_value(include, Tests, []),
Is = lists:map(fun(I) -> {i, I} end, Includes),
case append(Is, ErlOpts) of
{error, _} = Error -> Error;
NewIncludes -> rebar_opts:set(Opts, erl_opts, NewIncludes)
end.

append({error, _} = Error, _) -> Error;
Expand Down Expand Up @@ -597,6 +610,7 @@ ct_opts(_State) ->
{multiply_timetraps, undefined, "multiply_timetraps", integer, help(multiple_timetraps)}, %% Integer
{scale_timetraps, undefined, "scale_timetraps", boolean, help(scale_timetraps)},
{create_priv_dir, undefined, "create_priv_dir", string, help(create_priv_dir)},
{include, undefined, "include", string, help(include)},
{readable, undefined, "readable", boolean, help(readable)},
{verbose, $v, "verbose", boolean, help(verbose)}
].
Expand Down Expand Up @@ -647,6 +661,8 @@ help(scale_timetraps) ->
"Scale timetraps";
help(create_priv_dir) ->
"Create priv dir (auto_per_run | auto_per_tc | manual_per_tc)";
help(include) ->
"Directories containing additional include files";
help(readable) ->
"Shows test case names and only displays logs to shell on failures";
help(verbose) ->
Expand Down
30 changes: 27 additions & 3 deletions test/rebar_ct_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
cmd_multiply_timetraps/1,
cmd_scale_timetraps/1,
cmd_create_priv_dir/1,
cmd_include_dir/1,
cfg_opts/1,
cfg_arbitrary_opts/1,
cfg_test_spec/1,
Expand Down Expand Up @@ -95,7 +96,8 @@ groups() -> [{basic_app, [], [basic_app_default_dirs,
cmd_abort_if_missing_suites,
cmd_multiply_timetraps,
cmd_scale_timetraps,
cmd_create_priv_dir]},
cmd_create_priv_dir,
cmd_include_dir]},
{cover, [], [cover_compiled]}].

init_per_group(basic_app, Config) ->
Expand Down Expand Up @@ -211,7 +213,7 @@ init_per_group(ct_opts, Config) ->

{ok, State} = rebar_test_utils:run_and_check(C, [], ["as", "test", "lock"], return),

[{result, State}|C];
[{result, State}, {name, Name}|C];
init_per_group(cover, Config) ->
C = rebar_test_utils:init_rebar_state(Config, "ct_opts"),

Expand Down Expand Up @@ -700,7 +702,6 @@ suite_at_app_root(Config) ->
data_dir_correct(Config) ->
DataDir = ?config(data_dir, Config),
Parts = filename:split(DataDir),
ct:pal(Parts),
["rebar_ct_SUITE_data","test","rebar","lib","test","_build"|_] = lists:reverse(Parts).

cmd_label(Config) ->
Expand Down Expand Up @@ -973,6 +974,29 @@ cmd_create_priv_dir(Config) ->

true = lists:member({create_priv_dir, manual_per_tc}, TestOpts).

cmd_include_dir(Config) ->
State = ?config(result, Config),
AppDir = ?config(apps, Config),

Providers = rebar_state:providers(State),
Namespace = rebar_state:namespace(State),
CommandProvider = providers:get_provider(ct, Providers, Namespace),
GetOptSpec = providers:opts(CommandProvider),
{ok, GetOptResult} = getopt:parse(GetOptSpec, ["--include=foo/bar/baz,qux"]),

NewState = rebar_state:command_parsed_args(State, GetOptResult),

Tests = rebar_prv_common_test:prepare_tests(NewState),
{ok, _} = rebar_prv_common_test:compile(NewState, Tests),

Name = ?config(name, Config),
Beam = filename:join([AppDir, "_build", "test", "lib", Name, "ebin", Name ++ ".beam"]),

{ok, {_, [{compile_info, Info}]}} = beam_lib:chunks(Beam, [compile_info]),
CompileOpts = proplists:get_value(options, Info),
true = lists:member({i, "foo/bar/baz"}, CompileOpts),
true = lists:member({i, "qux"}, CompileOpts).

cfg_opts(Config) ->
C = rebar_test_utils:init_rebar_state(Config, "ct_cfg_opts_"),

Expand Down