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.

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