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.

256 lines
9.7 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  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. HookFile = filename:join([?config(priv_dir, Config), "bare-post.hook"]),
  110. Cmd = case os:type() of
  111. {win32, _} -> "dir";
  112. _ -> "ls"
  113. end,
  114. ConfOpts = [{provider_hooks, [{post, [{compile, clean}]}]},
  115. {post_hooks, [{compile, Cmd ++ " > " ++ HookFile}]}],
  116. RConfFile = rebar_test_utils:create_config(AppDir, ConfOpts),
  117. {ok, RConf} = file:consult(RConfFile),
  118. rebar_test_utils:run_and_check(
  119. Config, RConf, ["bare", "compile", "--paths", "."],
  120. {ok, []}
  121. ),
  122. %% check that hooks did actually run
  123. ?assertMatch({ok, _}, file:read_file(HookFile)),
  124. ok.
  125. deps_clean_hook_namespace(Config) ->
  126. mock_git_resource:mock([{deps, [{some_dep, "0.0.1"}]}]),
  127. Deps = rebar_test_utils:expand_deps(git, [{"some_dep", "0.0.1", []}]),
  128. TopDeps = rebar_test_utils:top_level_deps(Deps),
  129. RebarConfig = [
  130. {deps, TopDeps},
  131. {overrides, [
  132. {override, some_dep, [
  133. {provider_hooks, [
  134. {pre, [
  135. {compile, clean}
  136. ]}
  137. ]}
  138. ]}
  139. ]}
  140. ],
  141. rebar_test_utils:run_and_check(
  142. Config, RebarConfig, ["clean"],
  143. {ok, [{dep, "some_dep"}]}
  144. ).
  145. %% Checks that a hook that is defined on an app (not a top level hook of a project with subapps) is run
  146. eunit_app_hooks(Config) ->
  147. AppDir = ?config(apps, Config),
  148. Name = rebar_test_utils:create_random_name("app1_"),
  149. Vsn = rebar_test_utils:create_random_vsn(),
  150. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  151. RConfFile =
  152. rebar_test_utils:create_config(AppDir,
  153. [
  154. {escript_name, list_to_atom(Name)}
  155. ,{provider_hooks, [{post, [{eunit, escriptize}]}]}
  156. ]),
  157. {ok, RConf} = file:consult(RConfFile),
  158. rebar_test_utils:run_and_check(Config, RConf,
  159. ["eunit"], {ok, [{app, Name, valid}
  160. ,{file, filename:join([AppDir, "_build/test/bin", Name])}]}).
  161. run_hooks_for_plugins(Config) ->
  162. AppDir = ?config(apps, Config),
  163. Name = rebar_test_utils:create_random_name("app1_"),
  164. Vsn = rebar_test_utils:create_random_vsn(),
  165. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  166. PluginName = rebar_test_utils:create_random_name("plugin1_"),
  167. mock_git_resource:mock([{config, [{pre_hooks, [{compile, "echo whatsup > randomfile"}]}]}]),
  168. RConfFile = rebar_test_utils:create_config(AppDir,
  169. [{plugins, [
  170. {list_to_atom(PluginName),
  171. {git, "http://site.com/user/"++PluginName++".git",
  172. {tag, Vsn}}}
  173. ]}]),
  174. {ok, RConf} = file:consult(RConfFile),
  175. rebar_test_utils:run_and_check(Config, RConf, ["compile"], {ok, [{app, Name, valid},
  176. {plugin, PluginName},
  177. {file, filename:join([AppDir, "_build", "default", "plugins", PluginName, "randomfile"])}]}).
  178. %% test that a subapp of a project keeps its hooks
  179. sub_app_hooks(Config) ->
  180. AppDir = ?config(apps, Config),
  181. Name = rebar_test_utils:create_random_name("sub_app1_"),
  182. Vsn = rebar_test_utils:create_random_vsn(),
  183. SubAppsDir = filename:join([AppDir, "apps", Name]),
  184. rebar_test_utils:create_app(SubAppsDir, Name, Vsn, [kernel, stdlib]),
  185. rebar_test_utils:create_config(SubAppsDir, [{provider_hooks, [{post, [{compile, clean}]}]}]),
  186. RConfFile = rebar_test_utils:create_config(AppDir, []),
  187. {ok, RConf} = file:consult(RConfFile),
  188. %% Build with deps.
  189. rebar_test_utils:run_and_check(
  190. Config, RConf, ["compile"],
  191. {ok, [{app, Name, invalid}]}
  192. ).
  193. %% test that hooks at the top level don't run in the subapps
  194. root_hooks(Config) ->
  195. AppDir = ?config(apps, Config),
  196. Name = rebar_test_utils:create_random_name("sub_app1_"),
  197. Vsn = rebar_test_utils:create_random_vsn(),
  198. SubAppsDir = filename:join([AppDir, "apps", Name]),
  199. rebar_test_utils:create_app(SubAppsDir, Name, Vsn, [kernel, stdlib]),
  200. rebar_test_utils:create_config(SubAppsDir, [{provider_hooks, [{post, [{compile, clean}]}]}]),
  201. RConfFile = rebar_test_utils:create_config(AppDir, [{pre_hooks, [{compile, "mkdir \"$REBAR_ROOT_DIR/blah\""}]}]),
  202. {ok, RConf} = file:consult(RConfFile),
  203. %% Build with deps.
  204. rebar_test_utils:run_and_check(
  205. Config, RConf, ["compile"],
  206. {ok, [{app, Name, invalid}]}
  207. ).