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.

177 lines
6.9 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. -module(rebar_hooks_SUITE).
  2. -export([suite/0,
  3. init_per_suite/1,
  4. end_per_suite/1,
  5. init_per_testcase/2,
  6. end_per_testcase/2,
  7. all/0,
  8. build_and_clean_app/1,
  9. escriptize_artifacts/1,
  10. run_hooks_once/1,
  11. run_hooks_once_profiles/1,
  12. run_hooks_for_plugins/1,
  13. eunit_app_hooks/1,
  14. deps_hook_namespace/1]).
  15. -include_lib("common_test/include/ct.hrl").
  16. -include_lib("eunit/include/eunit.hrl").
  17. -include_lib("kernel/include/file.hrl").
  18. suite() ->
  19. [].
  20. init_per_suite(Config) ->
  21. Config.
  22. end_per_suite(_Config) ->
  23. meck:unload().
  24. init_per_testcase(_, Config) ->
  25. rebar_test_utils:init_rebar_state(Config).
  26. end_per_testcase(_, _Config) ->
  27. catch meck:unload().
  28. all() ->
  29. [build_and_clean_app, run_hooks_once, run_hooks_once_profiles,
  30. escriptize_artifacts, run_hooks_for_plugins, deps_hook_namespace,
  31. eunit_app_hooks].
  32. %% Test post provider hook cleans compiled project app, leaving it invalid
  33. build_and_clean_app(Config) ->
  34. AppDir = ?config(apps, Config),
  35. Name = rebar_test_utils:create_random_name("app1_"),
  36. Vsn = rebar_test_utils:create_random_vsn(),
  37. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  38. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name, valid}]}),
  39. RConfFile =
  40. rebar_test_utils:create_config(AppDir,
  41. [{provider_hooks, [{post, [{compile, clean}]}]}]),
  42. {ok, RConf} = file:consult(RConfFile),
  43. rebar_test_utils:run_and_check(Config, RConf,
  44. ["compile"], {ok, [{app, Name, invalid}]}).
  45. escriptize_artifacts(Config) ->
  46. AppDir = ?config(apps, Config),
  47. Name = rebar_test_utils:create_random_name("app1_"),
  48. Vsn = rebar_test_utils:create_random_vsn(),
  49. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  50. Artifact = "{{profile_dir}}/bin/"++Name,
  51. RConfFile =
  52. rebar_test_utils:create_config(AppDir,
  53. [
  54. {escript_name, list_to_atom(Name)}
  55. ,{artifacts, [Artifact]}
  56. ]),
  57. {ok, RConf} = file:consult(RConfFile),
  58. try rebar_test_utils:run_and_check(Config, RConf, ["compile"], return)
  59. catch
  60. {error,
  61. {rebar_prv_compile,
  62. {missing_artifact, Artifact}}} ->
  63. ok
  64. end,
  65. RConfFile1 =
  66. rebar_test_utils:create_config(AppDir,
  67. [
  68. {escript_name, list_to_atom(Name)}
  69. ,{artifacts, [Artifact]}
  70. ,{provider_hooks, [{post, [{compile, escriptize}]}]}
  71. ]),
  72. {ok, RConf1} = file:consult(RConfFile1),
  73. rebar_test_utils:run_and_check(Config, RConf1,
  74. ["compile"], {ok, [{app, Name, valid}
  75. ,{file, filename:join([AppDir, "_build/default/bin", Name])}]}).
  76. run_hooks_once(Config) ->
  77. AppDir = ?config(apps, Config),
  78. Name = rebar_test_utils:create_random_name("app1_"),
  79. Vsn = rebar_test_utils:create_random_vsn(),
  80. RebarConfig = [{pre_hooks, [{compile, "mkdir blah"}]}],
  81. rebar_test_utils:create_config(AppDir, RebarConfig),
  82. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  83. rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], {ok, [{app, Name, valid}]}).
  84. %% test that even if a hook is defined at the project level in a used profile
  85. %% the hook is not run for each application in the project umbrella
  86. run_hooks_once_profiles(Config) ->
  87. AppDir = ?config(apps, Config),
  88. Name = rebar_test_utils:create_random_name("app1_"),
  89. Vsn = rebar_test_utils:create_random_vsn(),
  90. RebarConfig = [{profiles, [{hooks, [{pre_hooks, [{compile, "mkdir blah"}]}]}]}],
  91. rebar_test_utils:create_config(AppDir, RebarConfig),
  92. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  93. rebar_test_utils:run_and_check(Config, RebarConfig, ["as", "hooks", "compile"], {ok, [{app, Name, valid}]}).
  94. deps_hook_namespace(Config) ->
  95. mock_git_resource:mock([{deps, [{some_dep, "0.0.1"}]}]),
  96. Deps = rebar_test_utils:expand_deps(git, [{"some_dep", "0.0.1", []}]),
  97. TopDeps = rebar_test_utils:top_level_deps(Deps),
  98. RebarConfig = [
  99. {deps, TopDeps},
  100. {overrides, [
  101. {override, some_dep, [
  102. {provider_hooks, [
  103. {pre, [
  104. {compile, clean}
  105. ]}
  106. ]}
  107. ]}
  108. ]}
  109. ],
  110. rebar_test_utils:run_and_check(
  111. Config, RebarConfig, ["compile"],
  112. {ok, [{dep, "some_dep"}]}
  113. ).
  114. %% Checks that a hook that is defined on an app (not a top level hook of a project with subapps) is run
  115. eunit_app_hooks(Config) ->
  116. AppDir = ?config(apps, Config),
  117. Name = rebar_test_utils:create_random_name("app1_"),
  118. Vsn = rebar_test_utils:create_random_vsn(),
  119. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  120. RConfFile =
  121. rebar_test_utils:create_config(AppDir,
  122. [
  123. {escript_name, list_to_atom(Name)}
  124. ,{provider_hooks, [{post, [{eunit, escriptize}]}]}
  125. ]),
  126. {ok, RConf} = file:consult(RConfFile),
  127. rebar_test_utils:run_and_check(Config, RConf,
  128. ["eunit"], {ok, [{app, Name, valid}
  129. ,{file, filename:join([AppDir, "_build/test/bin", Name])}]}).
  130. run_hooks_for_plugins(Config) ->
  131. AppDir = ?config(apps, Config),
  132. Name = rebar_test_utils:create_random_name("app1_"),
  133. Vsn = rebar_test_utils:create_random_vsn(),
  134. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  135. PluginName = rebar_test_utils:create_random_name("plugin1_"),
  136. mock_git_resource:mock([{config, [{pre_hooks, [{compile, "echo whatsup > randomfile"}]}]}]),
  137. RConfFile = rebar_test_utils:create_config(AppDir,
  138. [{plugins, [
  139. {list_to_atom(PluginName),
  140. {git, "http://site.com/user/"++PluginName++".git",
  141. {tag, Vsn}}}
  142. ]}]),
  143. {ok, RConf} = file:consult(RConfFile),
  144. rebar_test_utils:run_and_check(Config, RConf, ["compile"], {ok, [{app, Name, valid},
  145. {plugin, PluginName},
  146. {file, filename:join([AppDir, "_build", "default", "plugins", PluginName, "randomfile"])}]}).