1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
-module(rebar_pkg_alias_SUITE).
-compile(export_all).
-include_lib("common_test/include/ct.hrl").
-include_lib("eunit/include/eunit.hrl").
-include_lib("kernel/include/file.hrl").
-include("rebar.hrl").
all() -> [same_alias, diff_alias, diff_alias_vsn, transitive_alias%% ,
%% transitive_hash_mismatch
].
%% {uuid, {pkg, uuid}} = uuid
%% {uuid, {pkg, alias}} = uuid on disk
%% another run should yield the same lock file without error
init_per_suite(Config) ->
Config.
%% mock_config(?MODULE, Config).
end_per_suite(Config) ->
Config.
%% unmock_config(Config).
init_per_testcase(same_alias, Config0) ->
mock_config(?MODULE, Config0),
Config = rebar_test_utils:init_rebar_state(Config0,"same_alias_"),
AppDir = ?config(apps, Config),
rebar_test_utils:create_app(AppDir, "A", "0.0.0", [kernel, stdlib]),
RebarConf = rebar_test_utils:create_config(AppDir, [{deps, [{fakelib, {pkg, fakelib}}]}]),
[{rebarconfig, RebarConf} | Config];
init_per_testcase(diff_alias, Config0) ->
mock_config(?MODULE, Config0),
Config = rebar_test_utils:init_rebar_state(Config0,"diff_alias_"),
AppDir = ?config(apps, Config),
rebar_test_utils:create_app(AppDir, "A", "0.0.0", [kernel, stdlib]),
RebarConf = rebar_test_utils:create_config(AppDir, [{deps, [{fakelib, {pkg, goodpkg}}]}]),
[{rebarconfig, RebarConf} | Config];
init_per_testcase(diff_alias_vsn, Config0) ->
mock_config(?MODULE, Config0),
Config = rebar_test_utils:init_rebar_state(Config0,"diff_alias_vsn_"),
AppDir = ?config(apps, Config),
rebar_test_utils:create_app(AppDir, "A", "0.0.0", [kernel, stdlib]),
RebarConf = rebar_test_utils:create_config(AppDir, [{deps, [{fakelib, "1.0.0", {pkg, goodpkg}}]}]),
[{rebarconfig, RebarConf} | Config];
init_per_testcase(transitive_alias, Config0) ->
mock_config(?MODULE, Config0),
Config = rebar_test_utils:init_rebar_state(Config0,"transitive_alias_vsn_"),
AppDir = ?config(apps, Config),
rebar_test_utils:create_app(AppDir, "A", "0.0.0", [kernel, stdlib]),
RebarConf = rebar_test_utils:create_config(AppDir, [{deps, [{topdep, "1.0.0", {pkg, topdep}}]}]),
[{rebarconfig, RebarConf} | Config];
init_per_testcase(transitive_hash_mismatch, Config0) ->
mock_config(?MODULE, Config0),
Config = rebar_test_utils:init_rebar_state(Config0,"transitive_alias_vsn_"),
AppDir = ?config(apps, Config),
rebar_test_utils:create_app(AppDir, "A", "0.0.0", [kernel, stdlib]),
RebarConf = rebar_test_utils:create_config(AppDir, [{deps, [{topdep, "1.0.0", {pkg, topdep}}]}]),
[{rebarconfig, RebarConf} | Config].
end_per_testcase(_, Config) ->
unmock_config(Config),
Config.
same_alias(Config) ->
{ok, RebarConfig} = file:consult(?config(rebarconfig, Config)),
rebar_test_utils:run_and_check(
Config, RebarConfig, ["lock"],
{ok, [{lock, "fakelib"}, {dep, "fakelib"}]}
).
diff_alias(Config) ->
%% even though the dep is 'fakelib' aliased as 'goodpkg' all
%% internal records use 'fakelib' as a value. Just make sure
%% the lock actually maintains the proper source as 'goodpkg'
AppDir = ?config(apps, Config),
Lockfile = filename:join([AppDir, "rebar.lock"]),
{ok, RebarConfig} = file:consult(?config(rebarconfig, Config)),
rebar_test_utils:run_and_check(
Config, RebarConfig, ["lock"],
{ok, [{lock, "fakelib"},{dep, "fakelib"}]}
),
{ok, [{_Vsn, LockData}|_]} = file:consult(Lockfile),
?assert(lists:any(fun({<<"fakelib">>,{pkg,<<"goodpkg">>,_},_}) -> true
; (_) -> false end, LockData)),
%% An second run yields the same
rebar_test_utils:run_and_check(
Config, RebarConfig, ["lock"],
{ok, [{lock, "fakelib"},{dep, "fakelib"}]}
),
{ok, [{_Vsn, LockData}|_]} = file:consult(Lockfile),
%% So does an upgrade
rebar_test_utils:run_and_check(
Config, RebarConfig, ["upgrade"],
{ok, [{lock, "fakelib"},{dep, "fakelib"}]}
),
{ok, [{_Vsn, LockData}|_]} = file:consult(Lockfile).
diff_alias_vsn(Config) -> diff_alias(Config).
transitive_alias(Config) ->
%% ensure that the apps fetched under transitive aliases are
%% locked properly, but also that they are stored in the right
%% directory in the build dir to avoid breaking includes and
%% static analysis tools that rely on the location to work
AppDir = ?config(apps, Config),
Lockfile = filename:join([AppDir, "rebar.lock"]),
{ok, RebarConfig} = file:consult(?config(rebarconfig, Config)),
rebar_test_utils:run_and_check(
Config, RebarConfig, ["lock"],
{ok, [{lock, "topdep"},{dep, "topdep"},
{lock,"transitive_app"},{dep,"transitive_app"}]}
),
{ok, [{_Vsn, LockData}|_]} = file:consult(Lockfile),
?assert(lists:any(fun({<<"transitive_app">>,{pkg,<<"transitive">>,_},_}) -> true
; (_) -> false end, LockData)),
AppDir = ?config(apps, Config),
AliasedName = filename:join([AppDir, "_build", "default", "lib", "transitive_app"]),
PkgName = filename:join([AppDir, "_build", "default", "lib", "transitive"]),
?assert(filelib:is_dir(AliasedName)),
?assertNot(filelib:is_dir(PkgName)),
%% An second run yields the same
rebar_test_utils:run_and_check(
Config, RebarConfig, ["lock"],
{ok, [{lock, "topdep"},{dep, "topdep"},
{lock,"transitive_app"},{dep,"transitive_app"}]}
),
{ok, [{_Vsn, LockData}|_]} = file:consult(Lockfile),
?assert(filelib:is_dir(AliasedName)),
?assertNot(filelib:is_dir(PkgName)),
%% So does an upgrade
rebar_test_utils:run_and_check(
Config, RebarConfig, ["upgrade"],
{ok, [{lock, "topdep"},{dep, "topdep"},
{lock,"transitive_app"},{dep,"transitive_app"}]}
),
{ok, [{_Vsn, LockData}|_]} = file:consult(Lockfile),
?assert(filelib:is_dir(AliasedName)),
?assertNot(filelib:is_dir(PkgName)),
ok.
transitive_hash_mismatch(Config) ->
%% ensure that the apps fetched under transitive aliases are
%% locked properly, but also that they are stored in the right
%% directory in the build dir to avoid breaking includes and
%% static analysis tools that rely on the location to work
AppDir = ?config(apps, Config),
Lockfile = filename:join([AppDir, "rebar.lock"]),
{ok, RebarConfig} = file:consult(?config(rebarconfig, Config)),
rebar_test_utils:run_and_check(
Config, RebarConfig, ["lock"],
{ok, [{lock, "topdep"},{dep, "topdep"},
{lock,"transitive_app"},{dep,"transitive_app"}]}
),
{ok, [LockData|Attrs]} = file:consult(Lockfile),
%% Change Lock hash data to cause a failure next time, but on transitive
%% deps only
NewLock = [LockData|lists:map(
fun([{pkg_hash, Hashes}|Rest]) ->
[{pkg_hash, [{<<"transitive_app">>, <<"fakehash">>}
| lists:keydelete(<<"transitive_app">>, 1, Hashes)]}
| Rest]
; (Attr) ->
Attr
end, Attrs)],
{ok, Io} = file:open(Lockfile, [write]),
[io:format(Io, "~p.~n", [Attr]) || Attr <- NewLock],
file:close(Io),
ct:pal("lock: ~p", [file:consult(Lockfile)]),
ec_file:remove(filename:join([AppDir, "_build"]), [recursive]),
?assertMatch(
{error, {rebar_fetch, {unexpected_hash, _, _, _}}},
rebar_test_utils:run_and_check(Config, RebarConfig, ["lock"], return)
),
ok.
parse_deps(Deps) ->
[{maps:get(app, D, Name), {pkg, Name, Constraint, undefined}} || D=#{package := Name,
requirement := Constraint} <- Deps].
mock_config(Name, Config) ->
{ChkFake, Etag} = create_lib(Name, Config, "fakelib"),
{ChkTop, _} = create_lib(Name, Config, "topdep"),
{ChkTrans, _} = create_lib(Name, Config, "transitive_app", "transitive"),
ct:pal("{~p, _}",[ChkTop]),
ct:pal("{~p, _}",[ChkTrans]),
Priv = ?config(priv_dir, Config),
TmpDir = filename:join([Priv, "tmp", atom_to_list(Name)]),
%% Add an alias for goodpkg -> fakelib by hand
AppDir = filename:join([Priv, "fakelib"]),
CacheRoot = filename:join([Priv, "cache", atom_to_list(Name)]),
CacheDir = filename:join([CacheRoot, "hex", "com", "test", "packages"]),
rebar_test_utils:create_app(AppDir, "fakelib", "1.0.0", [kernel, stdlib]),
ct:pal("{~p, ~p}",[ChkFake, Etag]),
{ChkGood, EtagGood} = rebar_test_utils:package_app(AppDir, CacheDir, "goodpkg", "1.0.0"),
AllDeps = [
{<<"fakelib">>,[[<<"1.0.0">>]]},
{<<"goodpkg">>,[[<<"1.0.0">>]]},
{<<"topdep">>,[[<<"1.0.0">>]]},
{<<"transitive">>, [[<<"1.0.0">>]]},
{{<<"fakelib">>,<<"1.0.0">>}, [[], ChkFake, [<<"rebar3">>]]},
{{<<"goodpkg">>,<<"1.0.0">>}, [[], ChkGood, [<<"rebar3">>]]},
{{<<"topdep">>,<<"1.0.0">>},
[[
{<<"transitive">>, <<"1.0.0">>, false, <<"transitive_app">>}
], ChkTop, [<<"rebar3">>]]},
{{<<"transitive">>,<<"1.0.0">>}, [[], ChkTrans, [<<"rebar3">>]]}
],
Tid = ets:new(registry_table, [public]),
ets:insert_new(Tid, AllDeps),
ok = ets:tab2file(Tid, filename:join([CacheDir, "registry"])),
%% ets:delete(Tid),
%% The state returns us a fake registry
meck:new(rebar_dir, [passthrough, no_link]),
meck:expect(rebar_dir, global_cache_dir, fun(_) -> CacheRoot end),
meck:new(rebar_packages, [passthrough, no_link]),
meck:expect(rebar_packages, registry_dir, fun(_) -> {ok, CacheDir} end),
meck:expect(rebar_packages, package_dir, fun(_, _) -> {ok, CacheDir} end),
%% TODO: is something else wrong that we need this for transitive_alias to pass
meck:expect(rebar_packages, update_package, fun(_, _, _) -> ok end),
meck:new(rebar_prv_update, [passthrough]),
meck:expect(rebar_prv_update, do, fun(State) -> {ok, State} end),
catch ets:delete(?PACKAGE_TABLE),
rebar_packages:new_package_table(),
lists:foreach(fun({{N, Vsn}, [Deps, Checksum, _]}) ->
case ets:member(?PACKAGE_TABLE, {ec_cnv:to_binary(N), Vsn, <<"hexpm">>}) of
false ->
ets:insert(?PACKAGE_TABLE, #package{key={ec_cnv:to_binary(N), Vsn, <<"hexpm">>},
dependencies=[{DAppName, {pkg, DN, DV, undefined}} || {DN, DV, _, DAppName} <- Deps],
retired=false,
checksum=Checksum});
true ->
ok
end;
({_N, _Vsns}) ->
ok
end, AllDeps),
meck:new(hex_repo, [passthrough]),
meck:expect(hex_repo, get_package,
fun(_Config, PkgName) ->
Matches = ets:match_object(Tid, {{PkgName,'_'}, '_'}),
Releases =
[#{checksum => Checksum,
version => Vsn,
dependencies => [{DAppName, {pkg, DN, DV, undefined}} ||
{DN, DV, _, DAppName} <- Deps]} ||
{{_, Vsn}, [Deps, Checksum, _]} <- Matches],
{ok, {200, #{}, #{releases => Releases}}}
end),
meck:expect(hex_repo, get_tarball, fun(_, _, _) ->
{ok, {304, #{<<"etag">> => EtagGood}, <<>>}}
end),
%% Move all packages to cache
NewConf = [{cache_root, CacheRoot},
{cache_dir, CacheDir},
{tmp_dir, TmpDir},
{mock_table, Tid} | Config],
NewConf.
unmock_config(Config) ->
meck:unload(),
Config.
create_lib(Name, Config, PkgName) ->
create_lib(Name, Config, PkgName, PkgName).
create_lib(Name, Config, AppName, PkgName) ->
Priv = ?config(priv_dir, Config),
AppDir = filename:join([Priv, PkgName]),
CacheRoot = filename:join([Priv, "cache", atom_to_list(Name)]),
CacheDir = filename:join([CacheRoot, "hex", "com", "test", "packages"]),
filelib:ensure_dir(filename:join([CacheDir, "registry"])),
rebar_test_utils:create_app(AppDir, AppName, "1.0.0", [kernel, stdlib]),
rebar_test_utils:package_app(AppDir, CacheDir, PkgName, "1.0.0").
|