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

Support for all argument in unlock #2624

Merged
merged 6 commits into from
Nov 26, 2021
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
45 changes: 31 additions & 14 deletions src/rebar_prv_unlock.erl
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ init(State) ->
{deps, ?DEPS},
{example, ""},
{short_desc, "Unlock dependencies."},
{desc, "Unlock project dependencies. Mentioning no application "
"will unlock all dependencies. To unlock specific dependencies, "
{desc, "Unlock project dependencies. Use the --all option "
"to unlock all dependencies. To unlock specific dependencies, "
"their name can be listed in the command."},
{opts, [
{opts, [{all, $a, "all", undefined, "Unlock all dependencies and remove the lock file."},
{package, undefined, undefined, string,
"List of packages to unlock. If not specified, all dependencies are unlocked."}
"List of packages to unlock."}
]}
])
),
Expand All @@ -57,24 +57,41 @@ format_error({file, Reason}) ->
io_lib:format("Lock file editing failed for reason ~p", [Reason]);
format_error(unknown_lock_format) ->
"Lock file format unknown";
format_error(no_arg) ->
"Specify a list of dependencies to unlock, or --all to unlock them all";
format_error(Reason) ->
io_lib:format("~p", [Reason]).

handle_unlocks(State, Locks, LockFile) ->
{Args, _} = rebar_state:command_parsed_args(State),
Names = parse_names(rebar_utils:to_binary(proplists:get_value(package, Args, <<"">>))),
case [Lock || Lock = {Name, _, _} <- Locks, not lists:member(Name, Names)] of
[] ->
file:delete(LockFile),
{ok, []};
_ when Names =:= [] -> % implicitly all locks
case handle_args(State) of
%% a list of dependencies or --all is required
{false, []} ->
throw(?PRV_ERROR(no_arg));
%% if --all is specified, delete the lock file
{true, _} ->
file:delete(LockFile),
{ok, []};
NewLocks ->
rebar_config:write_lock_file(LockFile, NewLocks),
{ok, NewLocks}
%% otherwise, unlock the given list of dependency names. if none are left, delete the lock file
{false, Names} ->
case [Lock || Lock = {Name, _, _} <- Locks, not lists:member(Name, Names)] of
[] ->
file:delete(LockFile),
{ok, []};
_ when Names =:= [] -> % implicitly all locks
file:delete(LockFile),
{ok, []};
NewLocks ->
rebar_config:write_lock_file(LockFile, NewLocks),
{ok, NewLocks}
end
end.

handle_args(State) ->
{Args, _} = rebar_state:command_parsed_args(State),
All = proplists:get_value(all, Args, false),
Names = parse_names(rebar_utils:to_binary(proplists:get_value(package, Args, <<"">>))),
{All, Names}.

parse_names(Bin) ->
case lists:usort(re:split(Bin, <<" *, *">>, [trim, unicode])) of
[<<"">>] -> []; % nothing submitted
Expand Down
4 changes: 2 additions & 2 deletions test/rebar_alias_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ command(Config) ->
Name = rebar_test_utils:create_random_name("alias_command_"),
Vsn = rebar_test_utils:create_random_vsn(),
rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
RebarConfig = [{alias, [{test, [compile, unlock]}]}],
RebarConfig = [{alias, [{test, [compile, {unlock,"-a"}]}]}],

%% compile job ran
rebar_test_utils:run_and_check(Config, RebarConfig,
Expand Down Expand Up @@ -64,7 +64,7 @@ many(Config) ->
Vsn = rebar_test_utils:create_random_vsn(),
rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
RebarConfig = [{alias, [{test, [{eunit,"-c"}, cover]},
{nolock, [compile, unlock]}]}],
{nolock, [compile, {unlock,"-a"}]}]}],

%% test job ran (compiled and succeeded)
rebar_test_utils:run_and_check(Config, RebarConfig,
Expand Down
11 changes: 9 additions & 2 deletions test/rebar_unlock_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
-include_lib("eunit/include/eunit.hrl").
-compile(export_all).

all() -> [pkgunlock, unlock, unlock_all].
all() -> [pkgunlock, unlock, unlock_all, unlock_no_args].

init_per_testcase(pkgunlock, Config0) ->
Config = rebar_test_utils:init_rebar_state(Config0, "pkgunlock"),
Expand Down Expand Up @@ -54,11 +54,18 @@ unlock(Config) ->

unlock_all(Config) ->
[_|_] = read_locks(Config),
{ok, State} = rebar_test_utils:run_and_check(Config, [], ["unlock"], return),
{ok, State} = rebar_test_utils:run_and_check(Config, [], ["unlock", "--all"], return),
?assertEqual({error, enoent}, read_locks(Config)),
?assertEqual([], rebar_state:get(State, {locks, default})),
ok.

unlock_no_args(Config) ->
try rebar_test_utils:run_and_check(Config, [], ["unlock"], return)
catch {error, {rebar_prv_unlock, no_arg}} ->
ok
end,
ok.

read_locks(Config) ->
case file:consult(?config(lockfile, Config)) of
{ok, _} ->
Expand Down