25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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