Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

211 строки
8.1 KiB

  1. -module(rebar_paths_SUITE).
  2. -include_lib("eunit/include/eunit.hrl").
  3. -include_lib("common_test/include/ct.hrl").
  4. -compile(export_all).
  5. all() ->
  6. [%clashing_apps,
  7. check_modules,
  8. set_paths
  9. ].
  10. init_per_testcase(Case, Config) ->
  11. BasePaths = code:get_path(),
  12. %% This test checks that the right module sets get loaded; however, we must
  13. %% ensure that we do not have clashes with other test suites' loaded modules,
  14. %% which we cannot track. As such, we have to ensure all module names here are
  15. %% unique.
  16. %%
  17. %% This is done by hand; if you see this test suite failing on its own, you
  18. %% probably wrote a test suite that clashes!
  19. Dir = filename:join([?config(priv_dir, Config), atom_to_list(?MODULE),
  20. atom_to_list(Case)]),
  21. InDir = fun(Path) -> filename:join([Dir, Path]) end,
  22. ADep = fake_app(<<"rp_a">>, <<"1.0.0">>, InDir("_build/default/lib/rp_a/")),
  23. BDep = fake_app(<<"rp_b">>, <<"1.0.0">>, InDir("_build/default/lib/rp_b/")),
  24. CDep = fake_app(<<"rp_c">>, <<"1.0.0">>, InDir("_build/default/lib/rp_c/")),
  25. DDep = fake_app(<<"rp_d">>, <<"1.0.0">>, InDir("_build/default/lib/rp_d/")),
  26. RelxDep = fake_app(<<"relx">>, <<"1.0.0">>, InDir("_build/default/lib/relx/")),
  27. APlug = fake_app(<<"rp_a">>, <<"1.0.0">>,
  28. InDir("_build/default/plugins/lib/rp_a/")),
  29. RelxPlug = fake_app(<<"relx">>, <<"1.1.1">>,
  30. InDir("_build/default/plugins/lib/relx")),
  31. EPlug = fake_app(<<"rp_e">>, <<"1.0.0">>,
  32. InDir("_build/default/plugins/lib/rp_e/")),
  33. S0 = rebar_state:new(),
  34. S1 = rebar_state:all_deps(S0, [ADep, BDep, CDep, DDep, RelxDep]),
  35. S2 = rebar_state:all_plugin_deps(S1, [APlug, RelxPlug]),
  36. S3 = rebar_state:code_paths(S2, default, code:get_path()),
  37. S4 = rebar_state:code_paths(
  38. S3,
  39. all_deps,
  40. [rebar_app_info:ebin_dir(A) || A <- [ADep, BDep, CDep, DDep, RelxDep]]
  41. ),
  42. S5 = rebar_state:code_paths(
  43. S4,
  44. all_plugin_deps,
  45. [rebar_app_info:ebin_dir(A) || A <- [APlug, RelxPlug, EPlug]]
  46. ),
  47. [{base_paths, BasePaths}, {root_dir, Dir}, {state, S5} | Config].
  48. end_per_testcase(_, Config) ->
  49. %% this is deeply annoying because we interfere with rebar3's own
  50. %% path handling!
  51. rebar_paths:unset_paths([plugins, deps], ?config(state, Config)),
  52. Config.
  53. fake_app(Name, Vsn, OutDir) ->
  54. {ok, App} = rebar_app_info:new(Name, Vsn, OutDir),
  55. compile_fake_appmod(App),
  56. App.
  57. compile_fake_appmod(App) ->
  58. OutDir = rebar_app_info:ebin_dir(App),
  59. Vsn = rebar_app_info:original_vsn(App),
  60. Name = rebar_app_info:name(App),
  61. ok = filelib:ensure_dir(filename:join([OutDir, ".touch"])),
  62. AppFile = [
  63. "{application,", Name, ", "
  64. " [{description, \"some app\"}, "
  65. " {vsn, \"", Vsn, "\"}, "
  66. " {modules, [",Name,"]}, "
  67. " {registered, []}, "
  68. " {applications, [stdlib, kernel]} "
  69. " ]}. "],
  70. ok = file:write_file(filename:join([OutDir, <<Name/binary, ".app">>]), AppFile),
  71. Mod = [{attribute, 1, module, binary_to_atom(Name, utf8)},
  72. {attribute, 2, export, [{f,0}]},
  73. {function,3,f,0,
  74. [{clause,3, [], [],
  75. [{string,3,OutDir}]
  76. }]}
  77. ],
  78. {ok, _, Bin} = compile:forms(Mod),
  79. ok = file:write_file(filename:join([OutDir, <<Name/binary, ".beam">>]), Bin).
  80. clashing_apps(Config) ->
  81. Clashes = rebar_paths:clashing_apps([deps, plugins],
  82. ?config(state, Config)),
  83. ct:pal("Clashes: ~p", [Clashes]),
  84. ?assertEqual([<<"relx">>, <<"rp_a">>], lists:sort(proplists:get_value(deps, Clashes))),
  85. ?assertEqual(undefined, proplists:get_value(plugins, Clashes)),
  86. ok.
  87. set_paths(Config) ->
  88. State = ?config(state, Config),
  89. RootDir = filename:split(?config(root_dir, Config)),
  90. rebar_paths:set_paths([plugins, deps], State),
  91. PluginPaths = code:get_path(),
  92. rebar_paths:set_paths([deps, plugins], State),
  93. DepPaths = code:get_path(),
  94. ?assertEqual(
  95. RootDir ++ ["_build", "default", "plugins", "lib", "rp_a", "ebin"],
  96. find_first_instance("rp_a", PluginPaths)
  97. ),
  98. ?assertEqual(
  99. RootDir ++ ["_build", "default", "lib", "rp_b", "ebin"],
  100. find_first_instance("rp_b", PluginPaths)
  101. ),
  102. ?assertEqual(
  103. RootDir ++ ["_build", "default", "lib", "rp_c", "ebin"],
  104. find_first_instance("rp_c", PluginPaths)
  105. ),
  106. ?assertEqual(
  107. RootDir ++ ["_build", "default", "lib", "rp_d", "ebin"],
  108. find_first_instance("rp_d", PluginPaths)
  109. ),
  110. ?assertEqual(
  111. RootDir ++ ["_build", "default", "plugins", "lib", "rp_e", "ebin"],
  112. find_first_instance("rp_e", PluginPaths)
  113. ),
  114. ?assertEqual(
  115. RootDir ++ ["_build", "default", "plugins", "lib", "relx", "ebin"],
  116. find_first_instance("relx", PluginPaths)
  117. ),
  118. ?assertEqual(
  119. RootDir ++ ["_build", "default", "lib", "rp_a", "ebin"],
  120. find_first_instance("rp_a", DepPaths)
  121. ),
  122. ?assertEqual(
  123. RootDir ++ ["_build", "default", "lib", "rp_b", "ebin"],
  124. find_first_instance("rp_b", DepPaths)
  125. ),
  126. ?assertEqual(
  127. RootDir ++ ["_build", "default", "lib", "rp_c", "ebin"],
  128. find_first_instance("rp_c", DepPaths)
  129. ),
  130. ?assertEqual(
  131. RootDir ++ ["_build", "default", "lib", "rp_d", "ebin"],
  132. find_first_instance("rp_d", DepPaths)
  133. ),
  134. ?assertEqual(
  135. RootDir ++ ["_build", "default", "plugins", "lib", "rp_e", "ebin"],
  136. find_first_instance("rp_e", DepPaths)
  137. ),
  138. ?assertEqual(
  139. RootDir ++ ["_build", "default", "lib", "relx", "ebin"],
  140. find_first_instance("relx", DepPaths)
  141. ),
  142. ok.
  143. check_modules(Config) ->
  144. State = ?config(state, Config),
  145. RootDir = ?config(root_dir, Config)++"/",
  146. rebar_paths:set_paths([plugins, deps], State),
  147. ct:pal("code:get_path() -> ~p", [code:get_path()]),
  148. ?assertEqual(RootDir ++ "_build/default/plugins/lib/rp_a/ebin", rp_a:f()),
  149. ct:pal("~p", [catch file:list_dir(RootDir ++ "_build/default/lib/")]),
  150. ct:pal("~p", [catch file:list_dir(RootDir ++ "_build/default/lib/rp_b/")]),
  151. ct:pal("~p", [catch file:list_dir(RootDir ++ "_build/default/lib/rp_b/ebin")]),
  152. ct:pal("~p", [catch b:module_info()]),
  153. ?assertEqual(RootDir ++ "_build/default/lib/rp_b/ebin", rp_b:f()),
  154. ?assertEqual(RootDir ++ "_build/default/lib/rp_c/ebin", rp_c:f()),
  155. ?assertEqual(RootDir ++ "_build/default/lib/rp_d/ebin", rp_d:f()),
  156. ?assertEqual(RootDir ++ "_build/default/plugins/lib/rp_e/ebin", rp_e:f()),
  157. ?assertEqual(RootDir ++ "_build/default/plugins/lib/relx/ebin", relx:f()),
  158. ?assertEqual(3, length(relx:module_info(exports))), % can't replace bundled
  159. rebar_paths:set_paths([deps, plugins], State),
  160. ct:pal("code:get_path() -> ~p", [code:get_path()]),
  161. ?assertEqual(RootDir ++ "_build/default/lib/rp_a/ebin", rp_a:f()),
  162. ?assertEqual(RootDir ++ "_build/default/lib/rp_b/ebin", rp_b:f()),
  163. ?assertEqual(RootDir ++ "_build/default/lib/rp_c/ebin", rp_c:f()),
  164. ?assertEqual(RootDir ++ "_build/default/lib/rp_d/ebin", rp_d:f()),
  165. ?assertEqual(RootDir ++ "_build/default/plugins/lib/rp_e/ebin", rp_e:f()),
  166. ?assertEqual(RootDir ++ "_build/default/lib/relx/ebin", relx:f()),
  167. ?assertEqual(3, length(relx:module_info(exports))), % can't replace bundled
  168. %% once again
  169. rebar_paths:set_paths([plugins, deps], State),
  170. ct:pal("code:get_path() -> ~p", [code:get_path()]),
  171. ?assertEqual(RootDir ++ "_build/default/plugins/lib/rp_a/ebin", rp_a:f()),
  172. ?assertEqual(RootDir ++ "_build/default/lib/rp_b/ebin", rp_b:f()),
  173. ?assertEqual(RootDir ++ "_build/default/lib/rp_c/ebin", rp_c:f()),
  174. ?assertEqual(RootDir ++ "_build/default/lib/rp_d/ebin", rp_d:f()),
  175. ?assertEqual(RootDir ++ "_build/default/plugins/lib/rp_e/ebin", rp_e:f()),
  176. ?assertEqual(RootDir ++ "_build/default/plugins/lib/relx/ebin", relx:f()),
  177. ?assertEqual(3, length(relx:module_info(exports))), % can't replace bundled
  178. ok.
  179. find_first_instance(Frag, []) ->
  180. {not_found, Frag};
  181. find_first_instance(Frag, [Path|Rest]) ->
  182. Frags = filename:split(Path),
  183. case lists:member(Frag, Frags) of
  184. true -> Frags;
  185. false -> find_first_instance(Frag, Rest)
  186. end.