Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

121 linhas
4.9 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].
  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. end_per_testcase(_, Config) ->
  33. Config.
  34. same_alias(Config) ->
  35. {ok, RebarConfig} = file:consult(?config(rebarconfig, Config)),
  36. rebar_test_utils:run_and_check(
  37. Config, RebarConfig, ["lock"],
  38. {ok, [{lock, "fakelib"}, {dep, "fakelib"}]}
  39. ).
  40. diff_alias(Config) ->
  41. %% even though the dep is 'fakelib' aliased as 'goodpkg' all
  42. %% internal records use 'fakelib' as a value. Just make sure
  43. %% the lock actually maintains the proper source as 'goodpkg'
  44. AppDir = ?config(apps, Config),
  45. Lockfile = filename:join([AppDir, "rebar.lock"]),
  46. {ok, RebarConfig} = file:consult(?config(rebarconfig, Config)),
  47. rebar_test_utils:run_and_check(
  48. Config, RebarConfig, ["lock"],
  49. {ok, [{lock, "fakelib"},{dep, "fakelib"}]}
  50. ),
  51. {ok, [LockData]} = file:consult(Lockfile),
  52. ?assert(lists:any(fun({<<"fakelib">>,{pkg,<<"goodpkg">>,_},_}) -> true
  53. ; (_) -> false end, LockData)),
  54. %% An second run yields the same
  55. rebar_test_utils:run_and_check(
  56. Config, RebarConfig, ["lock"],
  57. {ok, [{lock, "fakelib"},{dep, "fakelib"}]}
  58. ),
  59. {ok, [LockData]} = file:consult(Lockfile),
  60. %% So does an upgrade
  61. rebar_test_utils:run_and_check(
  62. Config, RebarConfig, ["upgrade"],
  63. {ok, [{lock, "fakelib"},{dep, "fakelib"}]}
  64. ),
  65. {ok, [LockData]} = file:consult(Lockfile).
  66. diff_alias_vsn(Config) -> diff_alias(Config).
  67. mock_config(Name, Config) ->
  68. Priv = ?config(priv_dir, Config),
  69. AppDir = filename:join([Priv, "fakelib"]),
  70. CacheRoot = filename:join([Priv, "cache", atom_to_list(Name)]),
  71. TmpDir = filename:join([Priv, "tmp", atom_to_list(Name)]),
  72. CacheDir = filename:join([CacheRoot, "hex", "com", "test", "packages"]),
  73. filelib:ensure_dir(filename:join([CacheDir, "registry"])),
  74. rebar_test_utils:create_app(AppDir, "fakelib", "1.0.0", [kernel, stdlib]),
  75. {Chk,Etag} = rebar_test_utils:package_app(AppDir, CacheDir, "fakelib-1.0.0"),
  76. {Chk,Etag} = rebar_test_utils:package_app(AppDir, CacheDir, "goodpkg-1.0.0"),
  77. Tid = ets:new(registry_table, [public]),
  78. ets:insert_new(Tid, [
  79. {<<"fakelib">>,[[<<"1.0.0">>]]},
  80. {<<"goodpkg">>,[[<<"1.0.0">>]]},
  81. {{<<"fakelib">>,<<"1.0.0">>}, [[], Chk, [<<"rebar3">>]]},
  82. {{<<"goodpkg">>,<<"1.0.0">>}, [[], Chk, [<<"rebar3">>]]}
  83. ]),
  84. ok = ets:tab2file(Tid, filename:join([CacheDir, "registry"])),
  85. ets:delete(Tid),
  86. %% The state returns us a fake registry
  87. meck:new(rebar_dir, [passthrough, no_link]),
  88. meck:expect(rebar_dir, global_cache_dir, fun(_) -> CacheRoot end),
  89. meck:new(rebar_packages, [passthrough, no_link]),
  90. meck:expect(rebar_packages, registry_dir, fun(_) -> CacheDir end),
  91. meck:expect(rebar_packages, package_dir, fun(_) -> CacheDir end),
  92. rebar_prv_update:hex_to_index(rebar_state:new()),
  93. %% Cache fetches are mocked -- we assume the server and clients are
  94. %% correctly used.
  95. meck:new(httpc, [passthrough, unsticky, no_link]),
  96. meck:expect(httpc, request,
  97. fun(get, {_Url, _Opts}, _, _, _) ->
  98. {ok, {{<<"1.0.0">>, 304, <<"Not Modified">>}, [{"etag", Etag}], <<>>}}
  99. end),
  100. %% Move all packages to cache
  101. NewConf = [{cache_root, CacheRoot},
  102. {cache_dir, CacheDir},
  103. {tmp_dir, TmpDir},
  104. {mock_table, Tid} | Config],
  105. NewConf.
  106. unmock_config(Config) ->
  107. meck:unload(),
  108. Config.