You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

434 lines
17 KiB

  1. -module(rebar_profiles_SUITE).
  2. -export([init_per_suite/1,
  3. end_per_suite/1,
  4. init_per_testcase/2,
  5. end_per_testcase/2,
  6. all/0,
  7. profile_new_key/1,
  8. profile_merge_keys/1,
  9. explicit_profile_deduplicate_deps/1,
  10. implicit_profile_deduplicate_deps/1,
  11. all_deps_code_paths/1,
  12. profile_merges/1,
  13. same_profile_deduplication/1,
  14. stack_deduplication/1,
  15. add_to_profile/1,
  16. add_to_existing_profile/1,
  17. profiles_remain_applied_with_config_present/1,
  18. deduplicated_paths/1,
  19. test_profile_applied_at_completion/1,
  20. test_profile_applied_before_compile/1,
  21. test_profile_applied_before_eunit/1,
  22. test_profile_applied_to_apps/1]).
  23. -include_lib("common_test/include/ct.hrl").
  24. -include_lib("eunit/include/eunit.hrl").
  25. -include_lib("kernel/include/file.hrl").
  26. all() ->
  27. [profile_new_key, profile_merge_keys, all_deps_code_paths, profile_merges,
  28. explicit_profile_deduplicate_deps, implicit_profile_deduplicate_deps,
  29. same_profile_deduplication, stack_deduplication,
  30. add_to_profile, add_to_existing_profile,
  31. profiles_remain_applied_with_config_present,
  32. deduplicated_paths,
  33. test_profile_applied_at_completion,
  34. test_profile_applied_before_compile,
  35. test_profile_applied_before_eunit,
  36. test_profile_applied_to_apps].
  37. init_per_suite(Config) ->
  38. application:start(meck),
  39. Config.
  40. end_per_suite(_Config) ->
  41. application:stop(meck).
  42. init_per_testcase(_, Config) ->
  43. rebar_test_utils:init_rebar_state(Config, "profiles_").
  44. end_per_testcase(_, Config) ->
  45. meck:unload(),
  46. Config.
  47. profile_new_key(Config) ->
  48. AppDir = ?config(apps, Config),
  49. AllDeps = rebar_test_utils:expand_deps(git, [{"a", "1.0.0", []}
  50. ,{"b", "1.0.0", []}]),
  51. {SrcDeps, []} = rebar_test_utils:flat_deps(AllDeps),
  52. mock_git_resource:mock([{deps, SrcDeps}]),
  53. Name = rebar_test_utils:create_random_name("profile_new_key_"),
  54. Vsn = rebar_test_utils:create_random_vsn(),
  55. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  56. Deps = rebar_test_utils:top_level_deps(
  57. rebar_test_utils:expand_deps(git, [{"a", "1.0.0", []}
  58. ,{"b", "1.0.0", []}])),
  59. ct:pal("Deps ~p", [Deps]),
  60. RebarConfig = [{profiles,
  61. [{ct,
  62. [{deps, Deps}]}]}],
  63. rebar_test_utils:run_and_check(Config, RebarConfig,
  64. ["as", "ct", "compile"], {ok, [{app, Name}
  65. ,{dep, "a", "1.0.0"}
  66. ,{dep, "b", "1.0.0"}]}).
  67. profile_merge_keys(Config) ->
  68. AppDir = ?config(apps, Config),
  69. AllDeps = rebar_test_utils:expand_deps(git, [{"a", "1.0.0", []}
  70. ,{"b", "1.0.0", []}
  71. ,{"b", "2.0.0", []}]),
  72. {SrcDeps, []} = rebar_test_utils:flat_deps(AllDeps),
  73. mock_git_resource:mock([{deps, SrcDeps}]),
  74. Name = rebar_test_utils:create_random_name("profile_new_key_"),
  75. Vsn = rebar_test_utils:create_random_vsn(),
  76. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  77. Deps = rebar_test_utils:top_level_deps(
  78. rebar_test_utils:expand_deps(git, [{"a", "1.0.0", []}
  79. ,{"b", "1.0.0", []}])),
  80. ProfileDeps = rebar_test_utils:top_level_deps(
  81. rebar_test_utils:expand_deps(git, [{"b", "2.0.0", []}])),
  82. RebarConfig = [{deps, Deps},
  83. {profiles,
  84. [{ct,
  85. [{deps, ProfileDeps}]}]}],
  86. rebar_test_utils:run_and_check(Config, RebarConfig,
  87. ["as", "ct", "compile"], {ok, [{app, Name}
  88. ,{dep, "a", "1.0.0"}
  89. ,{dep, "b", "2.0.0"}]}).
  90. explicit_profile_deduplicate_deps(Config) ->
  91. AppDir = ?config(apps, Config),
  92. AllDeps = rebar_test_utils:expand_deps(git, [{"a", "1.0.0", []}
  93. ,{"a", "2.0.0", []}
  94. ,{"b", "1.0.0", []}
  95. ,{"b", "2.0.0", []}]),
  96. {SrcDeps, []} = rebar_test_utils:flat_deps(AllDeps),
  97. mock_git_resource:mock([{deps, SrcDeps}]),
  98. Name = rebar_test_utils:create_random_name("explicit_profile_deduplicate_deps_"),
  99. Vsn = rebar_test_utils:create_random_vsn(),
  100. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  101. FooDeps = rebar_test_utils:top_level_deps(
  102. rebar_test_utils:expand_deps(git, [{"a", "1.0.0", []},
  103. {"b", "2.0.0", []}])),
  104. BarDeps = rebar_test_utils:top_level_deps(
  105. rebar_test_utils:expand_deps(git, [{"b", "1.0.0", []}])),
  106. RebarConfig = [{profiles,
  107. [{foo,
  108. [{deps, FooDeps}]},
  109. {bar,
  110. [{deps, BarDeps}]}]}],
  111. rebar_test_utils:run_and_check(Config, RebarConfig,
  112. ["as", "bar,foo,bar", "compile"], {ok, [{app, Name}
  113. ,{dep, "a", "1.0.0"}
  114. ,{dep, "b", "1.0.0"}]}).
  115. implicit_profile_deduplicate_deps(Config) ->
  116. AppDir = ?config(apps, Config),
  117. AllDeps = rebar_test_utils:expand_deps(git, [{"a", "1.0.0", []}
  118. ,{"a", "2.0.0", []}
  119. ,{"b", "1.0.0", []}
  120. ,{"b", "2.0.0", []}]),
  121. {SrcDeps, []} = rebar_test_utils:flat_deps(AllDeps),
  122. mock_git_resource:mock([{deps, SrcDeps}]),
  123. Name = rebar_test_utils:create_random_name("implicit_profile_deduplicate_deps_"),
  124. Vsn = rebar_test_utils:create_random_vsn(),
  125. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  126. TestDeps = rebar_test_utils:top_level_deps(
  127. rebar_test_utils:expand_deps(git, [{"a", "1.0.0", []},
  128. {"b", "2.0.0", []}])),
  129. ProfileDeps = rebar_test_utils:top_level_deps(
  130. rebar_test_utils:expand_deps(git, [{"b", "1.0.0", []}])),
  131. RebarConfig = [{profiles,
  132. [{test,
  133. [{deps, TestDeps}]},
  134. {bar,
  135. [{deps, ProfileDeps}]}]}],
  136. rebar_test_utils:run_and_check(Config, RebarConfig,
  137. ["as", "test,bar", "eunit"], {ok, [{app, Name}
  138. ,{dep, "a", "1.0.0"}
  139. ,{dep, "b", "2.0.0"}]}).
  140. all_deps_code_paths(Config) ->
  141. AppDir = ?config(apps, Config),
  142. AllDeps = rebar_test_utils:expand_deps(git, [{"a", "1.0.0", []}
  143. ,{"b", "2.0.0", []}]),
  144. {SrcDeps, []} = rebar_test_utils:flat_deps(AllDeps),
  145. mock_git_resource:mock([{deps, SrcDeps}]),
  146. Name = rebar_test_utils:create_random_name("all_deps_code_paths"),
  147. Vsn = rebar_test_utils:create_random_vsn(),
  148. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  149. Deps = rebar_test_utils:top_level_deps(
  150. rebar_test_utils:expand_deps(git, [{"a", "1.0.0", []}])),
  151. ProfileDeps = rebar_test_utils:top_level_deps(
  152. rebar_test_utils:expand_deps(git, [{"b", "2.0.0", []}])),
  153. RebarConfig = [{deps, Deps},
  154. {profiles,
  155. [{all_deps_test,
  156. [{deps, ProfileDeps}]}]}],
  157. os:putenv("REBAR_PROFILE", "all_deps_test"),
  158. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig,
  159. ["compile"], {ok, [{app, Name}
  160. ,{dep, "a", "1.0.0"}
  161. ,{dep, "b", "2.0.0"}]}),
  162. os:putenv("REBAR_PROFILE", ""),
  163. Paths = rebar_state:code_paths(State, all_deps),
  164. Path = lists:reverse(["_build", "all_deps_test", "lib", "b", "ebin"]),
  165. ?assert(lists:any(fun(X) ->
  166. Path =:= lists:sublist(lists:reverse(filename:split(X)), 5)
  167. end, Paths)).
  168. profile_merges(_Config) ->
  169. RebarConfig = [{test1, [{key1, 1, 2}, key2]},
  170. {test2, "hello"},
  171. {test3, [key3]},
  172. {test4, "oldvalue"},
  173. {test5, [{key5, true}]},
  174. {test6, [{key6, false}]},
  175. {profiles,
  176. [{profile1,
  177. [{test1, [{key3, 5}, key1]}]},
  178. {profile2, [{test2, "goodbye"},
  179. {test3, []},
  180. {test4, []},
  181. {test5, [{key5, false}]},
  182. {test6, [{key6, true}]}
  183. ]}]}],
  184. State = rebar_state:new(RebarConfig),
  185. State1 = rebar_state:apply_profiles(State, [profile1, profile2]),
  186. %% Combine lists
  187. ?assertEqual(lists:sort([key1, key2, {key1, 1, 2}, {key3, 5}]),
  188. lists:sort(rebar_state:get(State1, test1))),
  189. %% Use new value for strings
  190. "goodbye" = rebar_state:get(State1, test2),
  191. %% Check that a newvalue of []/"" doesn't override non-string oldvalues
  192. [key3] = rebar_state:get(State1, test3),
  193. [] = rebar_state:get(State1, test4),
  194. [{key5, false}, {key5, true}] = rebar_state:get(State1, test5),
  195. [{key6, true}, {key6, false}] = rebar_state:get(State1, test6).
  196. same_profile_deduplication(_Config) ->
  197. RebarConfig = [{test1, [{key1, 1, 2}, key2]},
  198. {test2, [foo]},
  199. {test3, [key3]},
  200. {profiles,
  201. [{profile1,
  202. [{test1, [{key3, 5}, {key2, "hello"}]},
  203. {test2, [bar]},
  204. {test3, []}
  205. ]}]
  206. }],
  207. State = rebar_state:new(RebarConfig),
  208. State1 = rebar_state:apply_profiles(State, [profile1, profile1, profile1]),
  209. ?assertEqual([default, profile1], rebar_state:current_profiles(State1)),
  210. Test1 = rebar_state:get(State1, test1),
  211. %% Combine lists
  212. ?assertEqual(lists:sort([key2, {key1, 1, 2}, {key3, 5}, {key2, "hello"}]),
  213. lists:sort(Test1)),
  214. %% Key2 from profile1 overrides key2 from default profile
  215. ?assertEqual("hello", proplists:get_value(key2, Test1)),
  216. %% Check that a newvalue of []/"" doesn't override non-string oldvalues
  217. ?assertEqual([key3], rebar_state:get(State1, test3)),
  218. ?assertEqual([bar, foo], rebar_state:get(State1, test2)).
  219. stack_deduplication(_Config) ->
  220. RebarConfig = [
  221. {test_key, default},
  222. {test_list, [ {foo, default} ]},
  223. {profiles, [
  224. {a, [
  225. {test_key, a},
  226. {test_list, [ {foo, a} ]}
  227. ]},
  228. {b, [
  229. {test_key, b},
  230. {test_list, [ {foo, b} ]}
  231. ]},
  232. {c, [
  233. {test_key, c},
  234. {test_list, [ {foo, c} ]}
  235. ]},
  236. {d, [
  237. {test_key, d},
  238. {test_list, [ {foo, d} ]}
  239. ]},
  240. {e, [
  241. {test_key, e},
  242. {test_list, [ {foo, e} ]}
  243. ]}
  244. ]}
  245. ],
  246. State = rebar_state:new(RebarConfig),
  247. State1 = rebar_state:apply_profiles(State, [a, b, c, d, e, a, e, b]),
  248. ?assertEqual(b, rebar_state:get(State1, test_key)),
  249. TestList = rebar_state:get(State1, test_list),
  250. ?assertEqual(
  251. [{foo, b}, {foo, e}, {foo, a}, {foo, d}, {foo, c}, {foo, default} ],
  252. TestList
  253. ),
  254. ?assertEqual(b, proplists:get_value(foo, TestList)).
  255. add_to_profile(_Config) ->
  256. RebarConfig = [{foo, true}, {bar, false}],
  257. State = rebar_state:new(RebarConfig),
  258. State1 = rebar_state:add_to_profile(State, test, [{foo, false}]),
  259. State2 = rebar_state:apply_profiles(State1, test),
  260. Opts = rebar_state:opts(State2),
  261. lists:map(fun(K) -> false = dict:fetch(K, Opts) end, [foo, bar]).
  262. add_to_existing_profile(_Config) ->
  263. RebarConfig = [{foo, true}, {bar, false}, {profiles, [
  264. {test, [{foo, false}]}
  265. ]}],
  266. State = rebar_state:new(RebarConfig),
  267. State1 = rebar_state:add_to_profile(State, test, [{baz, false}]),
  268. State2 = rebar_state:apply_profiles(State1, test),
  269. Opts = rebar_state:opts(State2),
  270. lists:map(fun(K) -> false = dict:fetch(K, Opts) end, [foo, bar, baz]).
  271. profiles_remain_applied_with_config_present(Config) ->
  272. AppDir = ?config(apps, Config),
  273. Name = rebar_test_utils:create_random_name("profiles_remain_applied_"),
  274. Vsn = rebar_test_utils:create_random_vsn(),
  275. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  276. RebarConfig = [{erl_opts, []}, {profiles, [
  277. {not_ok, [{erl_opts, [{d, not_ok}]}]}
  278. ]}],
  279. rebar_test_utils:create_config(AppDir, RebarConfig),
  280. rebar_test_utils:run_and_check(Config, RebarConfig,
  281. ["as", "not_ok", "compile"], {ok, [{app, Name}]}),
  282. Path = filename:join([AppDir, "_build", "not_ok", "lib", Name, "ebin"]),
  283. code:add_patha(Path),
  284. Mod = list_to_atom("not_a_real_src_" ++ Name),
  285. true = lists:member({d, not_ok}, proplists:get_value(options, Mod:module_info(compile), [])).
  286. deduplicated_paths(Config) ->
  287. AppDir = ?config(apps, Config),
  288. Name = rebar_test_utils:create_random_name("deduplicated_paths_"),
  289. Vsn = rebar_test_utils:create_random_vsn(),
  290. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  291. RebarConfig = [],
  292. rebar_test_utils:create_config(AppDir, RebarConfig),
  293. rebar_test_utils:run_and_check(Config, RebarConfig,
  294. ["as", "a,b,c,d,e,a,e,b", "compile"],
  295. {ok, [{app, Name}]}),
  296. Path = filename:join([AppDir, "_build", "c+d+a+e+b", "lib", Name, "ebin"]),
  297. ?assert(filelib:is_dir(Path)).
  298. test_profile_applied_at_completion(Config) ->
  299. AppDir = ?config(apps, Config),
  300. Name = rebar_test_utils:create_random_name("test_profile_at_completion_"),
  301. Vsn = rebar_test_utils:create_random_vsn(),
  302. rebar_test_utils:create_eunit_app(AppDir, Name, Vsn, [kernel, stdlib]),
  303. RebarConfig = [{erl_opts, [{d, some_define}]}],
  304. rebar_test_utils:create_config(AppDir, RebarConfig),
  305. {ok, State} = rebar_test_utils:run_and_check(Config,
  306. RebarConfig,
  307. ["eunit"],
  308. return),
  309. [App] = rebar_state:project_apps(State),
  310. ErlOpts = rebar_app_info:get(App, erl_opts),
  311. true = lists:member({d, 'TEST'}, ErlOpts).
  312. test_profile_applied_before_compile(Config) ->
  313. AppDir = ?config(apps, Config),
  314. Name = rebar_test_utils:create_random_name("test_profile_before_compile_"),
  315. Vsn = rebar_test_utils:create_random_vsn(),
  316. rebar_test_utils:create_eunit_app(AppDir, Name, Vsn, [kernel, stdlib]),
  317. RebarConfig = [{erl_opts, [{d, some_define}]}],
  318. rebar_test_utils:create_config(AppDir, RebarConfig),
  319. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["eunit"], {ok, [{app, Name}]}),
  320. code:add_paths(rebar_state:code_paths(State, all_deps)),
  321. S = list_to_atom("not_a_real_src_" ++ Name),
  322. true = lists:member({d, 'TEST'}, proplists:get_value(options, S:module_info(compile), [])).
  323. test_profile_applied_before_eunit(Config) ->
  324. AppDir = ?config(apps, Config),
  325. Name = rebar_test_utils:create_random_name("test_profile_before_eunit_"),
  326. Vsn = rebar_test_utils:create_random_vsn(),
  327. rebar_test_utils:create_eunit_app(AppDir, Name, Vsn, [kernel, stdlib]),
  328. RebarConfig = [{erl_opts, [{d, some_define}]}],
  329. rebar_test_utils:create_config(AppDir, RebarConfig),
  330. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["eunit"], {ok, [{app, Name}]}),
  331. code:add_paths(rebar_state:code_paths(State, all_deps)),
  332. T = list_to_atom("not_a_real_src_" ++ Name ++ "_tests"),
  333. true = lists:member({d, 'TEST'}, proplists:get_value(options, T:module_info(compile), [])).
  334. test_profile_applied_to_apps(Config) ->
  335. AppDir = ?config(apps, Config),
  336. Name = rebar_test_utils:create_random_name("test_profile_applied_to_apps_"),
  337. Vsn = rebar_test_utils:create_random_vsn(),
  338. rebar_test_utils:create_eunit_app(AppDir, Name, Vsn, [kernel, stdlib]),
  339. RebarConfig = [{erl_opts, [{d, some_define}]}],
  340. rebar_test_utils:create_config(AppDir, RebarConfig),
  341. {ok, State} = rebar_test_utils:run_and_check(Config,
  342. RebarConfig,
  343. ["eunit"],
  344. return),
  345. Apps = rebar_state:project_apps(State),
  346. lists:foreach(fun(App) ->
  347. Opts = rebar_app_info:opts(App),
  348. ErlOpts = dict:fetch(erl_opts, Opts),
  349. true = lists:member({d, 'TEST'}, ErlOpts)
  350. end, Apps).