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.

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