您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

189 行
8.1 KiB

  1. -module(rebar_pkg_alias_SUITE).
  2. -compile(export_all).
  3. -include_lib("common_test/include/ct.hrl").
  4. -include_lib("eunit/include/eunit.hrl").
  5. -include_lib("kernel/include/file.hrl").
  6. all() -> [same_alias, diff_alias, diff_alias_vsn, transitive_alias].
  7. %% {uuid, {pkg, uuid}} = uuid
  8. %% {uuid, {pkg, alias}} = uuid on disk
  9. %% another run should yield the same lock file without error
  10. init_per_suite(Config) ->
  11. mock_config(?MODULE, Config).
  12. end_per_suite(Config) ->
  13. unmock_config(Config).
  14. init_per_testcase(same_alias, Config0) ->
  15. Config = rebar_test_utils:init_rebar_state(Config0,"same_alias_"),
  16. AppDir = ?config(apps, Config),
  17. rebar_test_utils:create_app(AppDir, "A", "0.0.0", [kernel, stdlib]),
  18. RebarConf = rebar_test_utils:create_config(AppDir, [{deps, [{fakelib, {pkg, fakelib}}]}]),
  19. [{rebarconfig, RebarConf} | Config];
  20. init_per_testcase(diff_alias, Config0) ->
  21. Config = rebar_test_utils:init_rebar_state(Config0,"diff_alias_"),
  22. AppDir = ?config(apps, Config),
  23. rebar_test_utils:create_app(AppDir, "A", "0.0.0", [kernel, stdlib]),
  24. RebarConf = rebar_test_utils:create_config(AppDir, [{deps, [{fakelib, {pkg, goodpkg}}]}]),
  25. [{rebarconfig, RebarConf} | Config];
  26. init_per_testcase(diff_alias_vsn, Config0) ->
  27. Config = rebar_test_utils:init_rebar_state(Config0,"diff_alias_vsn_"),
  28. AppDir = ?config(apps, Config),
  29. rebar_test_utils:create_app(AppDir, "A", "0.0.0", [kernel, stdlib]),
  30. RebarConf = rebar_test_utils:create_config(AppDir, [{deps, [{fakelib, "1.0.0", {pkg, goodpkg}}]}]),
  31. [{rebarconfig, RebarConf} | Config];
  32. init_per_testcase(transitive_alias, Config0) ->
  33. Config = rebar_test_utils:init_rebar_state(Config0,"transitive_alias_vsn_"),
  34. AppDir = ?config(apps, Config),
  35. rebar_test_utils:create_app(AppDir, "A", "0.0.0", [kernel, stdlib]),
  36. RebarConf = rebar_test_utils:create_config(AppDir, [{deps, [{topdep, "1.0.0", {pkg, topdep}}]}]),
  37. [{rebarconfig, RebarConf} | Config].
  38. end_per_testcase(_, Config) ->
  39. Config.
  40. same_alias(Config) ->
  41. {ok, RebarConfig} = file:consult(?config(rebarconfig, Config)),
  42. rebar_test_utils:run_and_check(
  43. Config, RebarConfig, ["lock"],
  44. {ok, [{lock, "fakelib"}, {dep, "fakelib"}]}
  45. ).
  46. diff_alias(Config) ->
  47. %% even though the dep is 'fakelib' aliased as 'goodpkg' all
  48. %% internal records use 'fakelib' as a value. Just make sure
  49. %% the lock actually maintains the proper source as 'goodpkg'
  50. AppDir = ?config(apps, Config),
  51. Lockfile = filename:join([AppDir, "rebar.lock"]),
  52. {ok, RebarConfig} = file:consult(?config(rebarconfig, Config)),
  53. rebar_test_utils:run_and_check(
  54. Config, RebarConfig, ["lock"],
  55. {ok, [{lock, "fakelib"},{dep, "fakelib"}]}
  56. ),
  57. {ok, [{_Vsn, LockData}|_]} = file:consult(Lockfile),
  58. ?assert(lists:any(fun({<<"fakelib">>,{pkg,<<"goodpkg">>,_},_}) -> true
  59. ; (_) -> false end, LockData)),
  60. %% An second run yields the same
  61. rebar_test_utils:run_and_check(
  62. Config, RebarConfig, ["lock"],
  63. {ok, [{lock, "fakelib"},{dep, "fakelib"}]}
  64. ),
  65. {ok, [{_Vsn, LockData}|_]} = file:consult(Lockfile),
  66. %% So does an upgrade
  67. rebar_test_utils:run_and_check(
  68. Config, RebarConfig, ["upgrade"],
  69. {ok, [{lock, "fakelib"},{dep, "fakelib"}]}
  70. ),
  71. {ok, [{_Vsn, LockData}|_]} = file:consult(Lockfile).
  72. diff_alias_vsn(Config) -> diff_alias(Config).
  73. transitive_alias(Config) ->
  74. %% ensure that the apps fetched under transitive aliases are
  75. %% locked properly, but also that they are stored in the right
  76. %% directory in the build dir to avoid breaking includes and
  77. %% static analysis tools that rely on the location to work
  78. AppDir = ?config(apps, Config),
  79. Lockfile = filename:join([AppDir, "rebar.lock"]),
  80. {ok, RebarConfig} = file:consult(?config(rebarconfig, Config)),
  81. rebar_test_utils:run_and_check(
  82. Config, RebarConfig, ["lock"],
  83. {ok, [{lock, "topdep"},{dep, "topdep"},
  84. {lock,"transitive_app"},{dep,"transitive_app"}]}
  85. ),
  86. {ok, [{_Vsn, LockData}|_]} = file:consult(Lockfile),
  87. ?assert(lists:any(fun({<<"transitive_app">>,{pkg,<<"transitive">>,_},_}) -> true
  88. ; (_) -> false end, LockData)),
  89. AppDir = ?config(apps, Config),
  90. AliasedName = filename:join([AppDir, "_build", "default", "lib", "transitive_app"]),
  91. PkgName = filename:join([AppDir, "_build", "default", "lib", "transitive"]),
  92. ?assert(filelib:is_dir(AliasedName)),
  93. ?assertNot(filelib:is_dir(PkgName)),
  94. %% An second run yields the same
  95. rebar_test_utils:run_and_check(
  96. Config, RebarConfig, ["lock"],
  97. {ok, [{lock, "topdep"},{dep, "topdep"},
  98. {lock,"transitive_app"},{dep,"transitive_app"}]}
  99. ),
  100. {ok, [{_Vsn, LockData}|_]} = file:consult(Lockfile),
  101. ?assert(filelib:is_dir(AliasedName)),
  102. ?assertNot(filelib:is_dir(PkgName)),
  103. %% So does an upgrade
  104. rebar_test_utils:run_and_check(
  105. Config, RebarConfig, ["upgrade"],
  106. {ok, [{lock, "topdep"},{dep, "topdep"},
  107. {lock,"transitive_app"},{dep,"transitive_app"}]}
  108. ),
  109. {ok, [{_Vsn, LockData}|_]} = file:consult(Lockfile),
  110. ?assert(filelib:is_dir(AliasedName)),
  111. ?assertNot(filelib:is_dir(PkgName)),
  112. ok.
  113. mock_config(Name, Config) ->
  114. {ChkFake, Etag} = create_lib(Name, Config, "fakelib"),
  115. {ChkTop, _} = create_lib(Name, Config, "topdep"),
  116. {ChkTrans, _} = create_lib(Name, Config, "transitive_app", "transitive"),
  117. Priv = ?config(priv_dir, Config),
  118. TmpDir = filename:join([Priv, "tmp", atom_to_list(Name)]),
  119. %% Add an alias for goodpkg -> fakelib by hand
  120. AppDir = filename:join([Priv, "fakelib"]),
  121. CacheRoot = filename:join([Priv, "cache", atom_to_list(Name)]),
  122. CacheDir = filename:join([CacheRoot, "hex", "com", "test", "packages"]),
  123. rebar_test_utils:create_app(AppDir, "fakelib", "1.0.0", [kernel, stdlib]),
  124. {ChkFake, Etag} = rebar_test_utils:package_app(AppDir, CacheDir, "goodpkg-1.0.0"),
  125. Tid = ets:new(registry_table, [public]),
  126. ets:insert_new(Tid, [
  127. {<<"fakelib">>,[[<<"1.0.0">>]]},
  128. {<<"goodpkg">>,[[<<"1.0.0">>]]},
  129. {<<"topdep">>,[[<<"1.0.0">>]]},
  130. {<<"transitive">>, [[<<"1.0.0">>]]},
  131. {{<<"fakelib">>,<<"1.0.0">>}, [[], ChkFake, [<<"rebar3">>]]},
  132. {{<<"goodpkg">>,<<"1.0.0">>}, [[], ChkFake, [<<"rebar3">>]]},
  133. {{<<"topdep">>,<<"1.0.0">>},
  134. [[
  135. [<<"transitive">>, <<"1.0.0">>, false, <<"transitive_app">>]
  136. ], ChkTop, [<<"rebar3">>]]},
  137. {{<<"transitive">>,<<"1.0.0">>}, [[], ChkTrans, [<<"rebar3">>]]}
  138. ]),
  139. ok = ets:tab2file(Tid, filename:join([CacheDir, "registry"])),
  140. ets:delete(Tid),
  141. %% The state returns us a fake registry
  142. meck:new(rebar_dir, [passthrough, no_link]),
  143. meck:expect(rebar_dir, global_cache_dir, fun(_) -> CacheRoot end),
  144. meck:new(rebar_packages, [passthrough, no_link]),
  145. meck:expect(rebar_packages, registry_dir, fun(_) -> {ok, CacheDir} end),
  146. meck:expect(rebar_packages, package_dir, fun(_) -> {ok, CacheDir} end),
  147. rebar_prv_update:hex_to_index(rebar_state:new()),
  148. %% Cache fetches are mocked -- we assume the server and clients are
  149. %% correctly used.
  150. meck:new(httpc, [passthrough, unsticky, no_link]),
  151. meck:expect(httpc, request,
  152. fun(get, {_Url, _Opts}, _, _, _) ->
  153. {ok, {{<<"1.0.0">>, 304, <<"Not Modified">>}, [{"etag", Etag}], <<>>}}
  154. end),
  155. %% Move all packages to cache
  156. NewConf = [{cache_root, CacheRoot},
  157. {cache_dir, CacheDir},
  158. {tmp_dir, TmpDir},
  159. {mock_table, Tid} | Config],
  160. NewConf.
  161. unmock_config(Config) ->
  162. meck:unload(),
  163. Config.
  164. create_lib(Name, Config, PkgName) ->
  165. create_lib(Name, Config, PkgName, PkgName).
  166. create_lib(Name, Config, AppName, PkgName) ->
  167. Priv = ?config(priv_dir, Config),
  168. AppDir = filename:join([Priv, PkgName]),
  169. CacheRoot = filename:join([Priv, "cache", atom_to_list(Name)]),
  170. CacheDir = filename:join([CacheRoot, "hex", "com", "test", "packages"]),
  171. filelib:ensure_dir(filename:join([CacheDir, "registry"])),
  172. rebar_test_utils:create_app(AppDir, AppName, "1.0.0", [kernel, stdlib]),
  173. rebar_test_utils:package_app(AppDir, CacheDir, PkgName++"-1.0.0").