Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

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