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.

246 lines
9.3 KiB

преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 7 години
преди 9 години
преди 10 години
преди 10 години
  1. -module(rebar_hooks_SUITE).
  2. -compile(export_all).
  3. -include_lib("common_test/include/ct.hrl").
  4. -include_lib("eunit/include/eunit.hrl").
  5. -include_lib("kernel/include/file.hrl").
  6. suite() ->
  7. [].
  8. init_per_suite(Config) ->
  9. Config.
  10. end_per_suite(_Config) ->
  11. meck:unload().
  12. init_per_testcase(_, Config) ->
  13. rebar_test_utils:init_rebar_state(Config).
  14. end_per_testcase(_, _Config) ->
  15. catch meck:unload().
  16. all() ->
  17. [build_and_clean_app, run_hooks_once, run_hooks_once_profiles,
  18. escriptize_artifacts, run_hooks_for_plugins, deps_hook_namespace,
  19. bare_compile_hooks_default_ns, deps_clean_hook_namespace, eunit_app_hooks,
  20. sub_app_hooks, root_hooks].
  21. %% Test post provider hook cleans compiled project app, leaving it invalid
  22. build_and_clean_app(Config) ->
  23. AppDir = ?config(apps, Config),
  24. Name = rebar_test_utils:create_random_name("app1_"),
  25. Vsn = rebar_test_utils:create_random_vsn(),
  26. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  27. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name, valid}]}),
  28. RConfFile =
  29. rebar_test_utils:create_config(AppDir,
  30. [{provider_hooks, [{post, [{compile, clean}]}]}]),
  31. {ok, RConf} = file:consult(RConfFile),
  32. rebar_test_utils:run_and_check(Config, RConf,
  33. ["compile"], {ok, [{app, Name, invalid}]}).
  34. escriptize_artifacts(Config) ->
  35. AppDir = ?config(apps, Config),
  36. Name = rebar_test_utils:create_random_name("app1_"),
  37. Vsn = rebar_test_utils:create_random_vsn(),
  38. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  39. Artifact = "{{profile_dir}}/bin/"++Name,
  40. RConfFile =
  41. rebar_test_utils:create_config(AppDir,
  42. [
  43. {escript_name, list_to_atom(Name)}
  44. ,{artifacts, [Artifact]}
  45. ]),
  46. {ok, RConf} = file:consult(RConfFile),
  47. try rebar_test_utils:run_and_check(Config, RConf, ["compile"], return)
  48. catch
  49. {error,
  50. {rebar_prv_compile,
  51. {missing_artifact, Artifact}}} ->
  52. ok
  53. end,
  54. RConfFile1 =
  55. rebar_test_utils:create_config(AppDir,
  56. [
  57. {escript_name, list_to_atom(Name)}
  58. ,{artifacts, [Artifact]}
  59. ,{provider_hooks, [{post, [{compile, escriptize}]}]}
  60. ]),
  61. {ok, RConf1} = file:consult(RConfFile1),
  62. rebar_test_utils:run_and_check(Config, RConf1,
  63. ["compile"], {ok, [{app, Name, valid}
  64. ,{file, filename:join([AppDir, "_build/default/bin", Name])}]}).
  65. run_hooks_once(Config) ->
  66. AppDir = ?config(apps, Config),
  67. Name = rebar_test_utils:create_random_name("app1_"),
  68. Vsn = rebar_test_utils:create_random_vsn(),
  69. RebarConfig = [{pre_hooks, [{compile, "mkdir \"$REBAR_ROOT_DIR/blah\""}]}],
  70. rebar_test_utils:create_config(AppDir, RebarConfig),
  71. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  72. rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], {ok, [{app, Name, valid}]}).
  73. %% test that even if a hook is defined at the project level in a used profile
  74. %% the hook is not run for each application in the project umbrella
  75. run_hooks_once_profiles(Config) ->
  76. AppDir = ?config(apps, Config),
  77. Name = rebar_test_utils:create_random_name("app1_"),
  78. Vsn = rebar_test_utils:create_random_vsn(),
  79. RebarConfig = [{profiles, [{hooks, [{pre_hooks, [{compile, "mkdir \"$REBAR_ROOT_DIR/blah\""}]}]}]}],
  80. rebar_test_utils:create_config(AppDir, RebarConfig),
  81. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  82. rebar_test_utils:run_and_check(Config, RebarConfig, ["as", "hooks", "compile"], {ok, [{app, Name, valid}]}).
  83. deps_hook_namespace(Config) ->
  84. mock_git_resource:mock([{deps, [{some_dep, "0.0.1"}]}]),
  85. Deps = rebar_test_utils:expand_deps(git, [{"some_dep", "0.0.1", []}]),
  86. TopDeps = rebar_test_utils:top_level_deps(Deps),
  87. RebarConfig = [
  88. {deps, TopDeps},
  89. {overrides, [
  90. {override, some_dep, [
  91. {provider_hooks, [
  92. {pre, [
  93. {compile, clean}
  94. ]}
  95. ]}
  96. ]}
  97. ]}
  98. ],
  99. rebar_test_utils:run_and_check(
  100. Config, RebarConfig, ["compile"],
  101. {ok, [{dep, "some_dep"}]}
  102. ).
  103. %% tests that hooks to compile when running bare compile run in the default namespace
  104. bare_compile_hooks_default_ns(Config) ->
  105. AppDir = ?config(apps, Config),
  106. Name = rebar_test_utils:create_random_name("app1_"),
  107. Vsn = rebar_test_utils:create_random_vsn(),
  108. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  109. RConfFile = rebar_test_utils:create_config(AppDir,
  110. [{provider_hooks, [{post, [{compile, clean}]}]}]),
  111. {ok, RConf} = file:consult(RConfFile),
  112. rebar_test_utils:run_and_check(
  113. Config, RConf, ["bare", "compile", "--paths", "."],
  114. {ok, []}
  115. ).
  116. deps_clean_hook_namespace(Config) ->
  117. mock_git_resource:mock([{deps, [{some_dep, "0.0.1"}]}]),
  118. Deps = rebar_test_utils:expand_deps(git, [{"some_dep", "0.0.1", []}]),
  119. TopDeps = rebar_test_utils:top_level_deps(Deps),
  120. RebarConfig = [
  121. {deps, TopDeps},
  122. {overrides, [
  123. {override, some_dep, [
  124. {provider_hooks, [
  125. {pre, [
  126. {compile, clean}
  127. ]}
  128. ]}
  129. ]}
  130. ]}
  131. ],
  132. rebar_test_utils:run_and_check(
  133. Config, RebarConfig, ["clean"],
  134. {ok, [{dep, "some_dep"}]}
  135. ).
  136. %% Checks that a hook that is defined on an app (not a top level hook of a project with subapps) is run
  137. eunit_app_hooks(Config) ->
  138. AppDir = ?config(apps, Config),
  139. Name = rebar_test_utils:create_random_name("app1_"),
  140. Vsn = rebar_test_utils:create_random_vsn(),
  141. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  142. RConfFile =
  143. rebar_test_utils:create_config(AppDir,
  144. [
  145. {escript_name, list_to_atom(Name)}
  146. ,{provider_hooks, [{post, [{eunit, escriptize}]}]}
  147. ]),
  148. {ok, RConf} = file:consult(RConfFile),
  149. rebar_test_utils:run_and_check(Config, RConf,
  150. ["eunit"], {ok, [{app, Name, valid}
  151. ,{file, filename:join([AppDir, "_build/test/bin", Name])}]}).
  152. run_hooks_for_plugins(Config) ->
  153. AppDir = ?config(apps, Config),
  154. Name = rebar_test_utils:create_random_name("app1_"),
  155. Vsn = rebar_test_utils:create_random_vsn(),
  156. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  157. PluginName = rebar_test_utils:create_random_name("plugin1_"),
  158. mock_git_resource:mock([{config, [{pre_hooks, [{compile, "echo whatsup > randomfile"}]}]}]),
  159. RConfFile = rebar_test_utils:create_config(AppDir,
  160. [{plugins, [
  161. {list_to_atom(PluginName),
  162. {git, "http://site.com/user/"++PluginName++".git",
  163. {tag, Vsn}}}
  164. ]}]),
  165. {ok, RConf} = file:consult(RConfFile),
  166. rebar_test_utils:run_and_check(Config, RConf, ["compile"], {ok, [{app, Name, valid},
  167. {plugin, PluginName},
  168. {file, filename:join([AppDir, "_build", "default", "plugins", PluginName, "randomfile"])}]}).
  169. %% test that a subapp of a project keeps its hooks
  170. sub_app_hooks(Config) ->
  171. AppDir = ?config(apps, Config),
  172. Name = rebar_test_utils:create_random_name("sub_app1_"),
  173. Vsn = rebar_test_utils:create_random_vsn(),
  174. SubAppsDir = filename:join([AppDir, "apps", Name]),
  175. rebar_test_utils:create_app(SubAppsDir, Name, Vsn, [kernel, stdlib]),
  176. rebar_test_utils:create_config(SubAppsDir, [{provider_hooks, [{post, [{compile, clean}]}]}]),
  177. RConfFile = rebar_test_utils:create_config(AppDir, []),
  178. {ok, RConf} = file:consult(RConfFile),
  179. %% Build with deps.
  180. rebar_test_utils:run_and_check(
  181. Config, RConf, ["compile"],
  182. {ok, [{app, Name, invalid}]}
  183. ).
  184. %% test that hooks at the top level don't run in the subapps
  185. root_hooks(Config) ->
  186. AppDir = ?config(apps, Config),
  187. Name = rebar_test_utils:create_random_name("sub_app1_"),
  188. Vsn = rebar_test_utils:create_random_vsn(),
  189. SubAppsDir = filename:join([AppDir, "apps", Name]),
  190. rebar_test_utils:create_app(SubAppsDir, Name, Vsn, [kernel, stdlib]),
  191. rebar_test_utils:create_config(SubAppsDir, [{provider_hooks, [{post, [{compile, clean}]}]}]),
  192. RConfFile = rebar_test_utils:create_config(AppDir, [{pre_hooks, [{compile, "mkdir \"$REBAR_ROOT_DIR/blah\""}]}]),
  193. {ok, RConf} = file:consult(RConfFile),
  194. %% Build with deps.
  195. rebar_test_utils:run_and_check(
  196. Config, RConf, ["compile"],
  197. {ok, [{app, Name, invalid}]}
  198. ).