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

Ensure provider hook calls don't crash because of invalid command line arguments #2154

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
10 changes: 7 additions & 3 deletions src/rebar_hooks.erl
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@ run_all_hooks(Dir, Type, Command, Providers, State) ->
run_hooks(Dir, Type, Command, rebar_state:opts(State), State).

run_project_and_app_hooks(Dir, Type, Command, Providers, State) ->
ProjectApps = rebar_state:project_apps(State),
[rebar_hooks:run_all_hooks(Dir, Type, Command, Providers, AppInfo, State) || AppInfo <- ProjectApps],
run_all_hooks(Dir, Type, Command, Providers, State).
% The command_args which the original invocation of rebar received are most
% likely not applicable to the hooks which will be called. Therefore, the
% args are discarded to not crash provider hook invocations.
State1 = rebar_state:command_args(State, []),
ProjectApps = rebar_state:project_apps(State1),
[rebar_hooks:run_all_hooks(Dir, Type, Command, Providers, AppInfo, State1) || AppInfo <- ProjectApps],
run_all_hooks(Dir, Type, Command, Providers, State1).

run_provider_hooks(Dir, Type, Command, Providers, Opts, State) ->
case rebar_opts:get(Opts, provider_hooks, []) of
Expand Down
16 changes: 15 additions & 1 deletion test/rebar_hooks_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ all() ->
[build_and_clean_app, run_hooks_once, run_hooks_once_profiles,
escriptize_artifacts, run_hooks_for_plugins, deps_hook_namespace,
bare_compile_hooks_default_ns, deps_clean_hook_namespace, eunit_app_hooks,
sub_app_hooks, root_hooks].
sub_app_hooks, root_hooks, run_hooks_with_top_level_cmdargs].

%% Test post provider hook cleans compiled project app, leaving it invalid
build_and_clean_app(Config) ->
Expand Down Expand Up @@ -254,3 +254,17 @@ root_hooks(Config) ->
Config, RConf, ["compile"],
{ok, [{app, Name, invalid}]}
).

%% test that command-line arguments don't crash provider hooks invocation
run_hooks_with_top_level_cmdargs(Config) ->
AppDir = ?config(apps, Config),

Name = rebar_test_utils:create_random_name("app1_"),
Vsn = rebar_test_utils:create_random_vsn(),

RebarConfig = [{pre_hooks, [{compile, ct}]}],

rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
rebar_test_utils:create_config(AppDir, RebarConfig),

rebar_test_utils:run_and_check(Config, RebarConfig, ["compile", "-d"], {ok, [{app, Name, valid}]}).