1-module(rebar_unlock_SUITE).
2-include_lib("common_test/include/ct.hrl").
3-include_lib("eunit/include/eunit.hrl").
4-compile(export_all).
5
6all() -> [pkgunlock, unlock, unlock_all].
7
8init_per_testcase(pkgunlock, Config0) ->
9    Config = rebar_test_utils:init_rebar_state(Config0, "pkgunlock"),
10    Lockfile = filename:join(?config(apps, Config), "rebar.lock"),
11    ec_file:copy(filename:join(?config(data_dir, Config), "pkg.rebar.lock"),
12                 Lockfile),
13    [{lockfile, Lockfile} | Config];
14init_per_testcase(Case, Config0) ->
15    Config = rebar_test_utils:init_rebar_state(Config0, atom_to_list(Case)),
16    Lockfile = filename:join(?config(apps, Config), "rebar.lock"),
17    ec_file:copy(filename:join(?config(data_dir, Config), "rebar.lock"),
18                 Lockfile),
19    [{lockfile, Lockfile} | Config].
20
21end_per_testcase(_, Config) ->
22    Config.
23
24pkgunlock(Config) ->
25    Locks = read_locks(Config),
26    Hashes = read_hashes(Config),
27    rebar_test_utils:run_and_check(Config, [], ["unlock", "fakeapp"], {ok, []}),
28    Locks = read_locks(Config),
29    Hashes = read_hashes(Config),
30    rebar_test_utils:run_and_check(Config, [], ["unlock", "bbmustache"], {ok, []}),
31    ?assertEqual(Locks -- ["bbmustache"], read_locks(Config)),
32    ?assertEqual(Hashes -- ["bbmustache"], read_hashes(Config)),
33    rebar_test_utils:run_and_check(Config, [], ["unlock", "cf,certifi"], {ok, []}),
34    ?assertEqual(Locks -- ["bbmustache","cf","certifi"], read_locks(Config)),
35    ?assertEqual(Hashes -- ["bbmustache","cf","certifi"], read_hashes(Config)),
36    rebar_test_utils:run_and_check(Config, [], ["unlock", rebar_string:join(Locks,",")], {ok, []}),
37    ?assertEqual({error, enoent}, read_locks(Config)),
38    ?assertEqual({error, enoent}, read_hashes(Config)),
39    ok.
40
41unlock(Config) ->
42    Locks = read_locks(Config),
43    rebar_test_utils:run_and_check(Config, [], ["unlock", "fakeapp"], {ok, []}),
44    Locks = read_locks(Config),
45    {ok, State} = rebar_test_utils:run_and_check(Config, [], ["unlock", "uuid"], return),
46    ?assertEqual(Locks -- ["uuid"], read_locks(Config)),
47    ?assert(false =:= lists:keyfind(<<"uuid">>, 1, rebar_state:get(State, {locks, default}))),
48    ?assert(false =/= lists:keyfind(<<"itc">>, 1, rebar_state:get(State, {locks, default}))),
49    rebar_test_utils:run_and_check(Config, [], ["unlock", "gproc,itc"], {ok, []}),
50    ?assertEqual(Locks -- ["uuid","gproc","itc"], read_locks(Config)),
51    rebar_test_utils:run_and_check(Config, [], ["unlock", rebar_string:join(Locks,",")], {ok, []}),
52    ?assertEqual({error, enoent}, read_locks(Config)),
53    ok.
54
55unlock_all(Config) ->
56    [_|_] = read_locks(Config),
57    {ok, State} = rebar_test_utils:run_and_check(Config, [], ["unlock"], return),
58    ?assertEqual({error, enoent}, read_locks(Config)),
59    ?assertEqual([], rebar_state:get(State, {locks, default})),
60    ok.
61
62read_locks(Config) ->
63    case file:consult(?config(lockfile, Config)) of
64        {ok, _} ->
65            Locks = rebar_config:consult_lock_file(?config(lockfile, Config)),
66            [binary_to_list(element(1,Lock)) || Lock <- Locks];
67        Other ->
68            Other
69    end.
70
71read_hashes(Config) ->
72    case file:consult(?config(lockfile, Config)) of
73        {ok, [{_Vsn, _Locks},Props|_]} ->
74            Hashes = proplists:get_value(pkg_hash, Props, []),
75            [binary_to_list(element(1,Hash)) || Hash <- Hashes];
76        {ok, [{_Vsn, _Locks}]} ->
77            [];
78        Other ->
79            Other
80    end.
81