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.

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