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.

305 lines
12 KiB

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