Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

2976 lignes
117 KiB

il y a 10 ans
support for hex v2, multiple repository fetching, private organizations (#1884) * update to hex_core for hex-v2 repo support (#1865) * update to hex_core for hex-v2 repo support This patch adds only single repo hex-v2 support through hex_core. Packages no longer filtered out by buildtool metadata and the package index is updated per-package instead of fetched as one large ets dump. * tell travis to also build hex_core branch * support list of repos for hex packages (#1866) * support list of repos for hex packages repos are defined under the hex key in rebar configs. They can be defined at the top level of a project or globally, but not in profiles and the repos configured in dependencies are also ignored. Searching for packages involves first checking for a match in the local repo index cache, in the order repos are defined. If not found each repo is checked through the hex api for any known versions of the package and the first repo with a version that fits the constraint is used. * add {repos, replace, []} for overriding the global & default repos * add hex auth handling for repos (#1874) auth token are kept in a hex.config file that is modified by the rebar3 hex plugin. Repo names that have a : separating a parent and child are considered organizations. The parent repo's auth will be included with the child. So an organization named hexpm:rebar3_test will include any hexpm auth tokens found in the rebar3_test organization's configuration. * move packages to top level of of hexpm cache dir (#1876) * move packages to top level of of hexpm cache dir * append organization name to parent's repo_url when parsing repos * only eval config scripts and apply overrides once per app (#1879) * only eval config scripts and apply overrides once per app * move new resource behaviour to rebar_resource_v2 and keep v1 * cleanup use of rebar_resource module and unused functions * cleanup error messages and unused code * when discovering apps support mix packages as unbuilt apps (#1882) * use hex_core tarball unpacking support in pkg resource (#1883) * use hex_core tarball unpacking support in pkg resource * ignore etag if package doesn't exist and delete if checksum fails * add back tests for bad package checksums * improve bad registry checksum error message
il y a 6 ans
il y a 10 ans
Fix handling of updated files in extra_src_dirs This change fixes cases where changes in .hrl files would not be picked up in .erl files that are in extra source directories (such as those defined with `extra_src_dirs` or modules in the test/ directory during a CT or Eunit run). The problem was due to the way the Directed Acyclic Graph (DAG) of dependencies between files was being loaded and stored by the compiler modules. Prior to this fix, a single DAG would be used for all runs. On a regular run, the prior DAG is loaded from disk, re-checked, and if changed, it would get re-written to disk with the changes deciding what to re-compile. However, whenever extra source directories were specified, a second run would be done which swaps target directories around in the compiler modules. Bug 1: this second run was done without properly tracking the private .hrl files (in src/), so the changes were invisible. This has been fixed by re-adding the paths. The problem is that the DAG handling is self-contained; just invoking it was sufficient to get it loaded and rewritten to disk. But since runs with extra src dirs were done on different sets, the compilation of extra src dirs would be done with bad historical data (all the modules in src/ are dropped, all those in test/ are re-added); this DAG was then written to disk once again, polluting the next non-extra run. This is bug 2, and it is fixed by adding an optional label to each run so that a regular or extra compile round can be distinguished, each tracking their own files in their own DAG. A single test (and a lot of diffing) were sufficient for this.
il y a 6 ans
Fix directory recursion in compiler This patch contains two behaviour changes and reasserts other behaviours that now line things up with user and documentation expectations: 1. The src directories remain recursive. We turned it on by accident at some point in the past and now people rely on it, so we're stuck with it. However a new test ensures that the feature can be turned off on-demand as documented on the website. 2. The test directories are no longer recursive by default. The fix is done by properly fixing how rebar3.erl does its feature injection by mandating the default value there. I'm somewhat nervous that this change could negatively impact some users and older compiler module versions, but if users stick to the rebar_dir interface, everything should keep working transparently. 3. The test directories' configuration is no longer silently dropped. Due to how rebar3.erl injected test state without looking for what the user may have specified, multiple extra_src_dirs entries existed at once and were run; one with the recursion set to true and one with whatever the user specified. If the user disabled recursion of the "test" extra_src_dir, then the injected value still ran it once... 4. The handling of extra files in the compiler module is fixed to actually use the rebar_dir interface properly, and reinjects non-default directory recursion settings into the swapped options for the shimmed extra apps. Not doing this annotation step resulted in the write for swapped opts to actually drop the configured recursion value and make everything recursive all the time. A single new test actually validates all of that behaviour and seems to work fine.
il y a 4 ans
il y a 10 ans
il y a 10 ans
il y a 10 ans
il y a 10 ans
il y a 10 ans
il y a 10 ans
il y a 10 ans
il y a 10 ans
Fix handling of updated files in extra_src_dirs This change fixes cases where changes in .hrl files would not be picked up in .erl files that are in extra source directories (such as those defined with `extra_src_dirs` or modules in the test/ directory during a CT or Eunit run). The problem was due to the way the Directed Acyclic Graph (DAG) of dependencies between files was being loaded and stored by the compiler modules. Prior to this fix, a single DAG would be used for all runs. On a regular run, the prior DAG is loaded from disk, re-checked, and if changed, it would get re-written to disk with the changes deciding what to re-compile. However, whenever extra source directories were specified, a second run would be done which swaps target directories around in the compiler modules. Bug 1: this second run was done without properly tracking the private .hrl files (in src/), so the changes were invisible. This has been fixed by re-adding the paths. The problem is that the DAG handling is self-contained; just invoking it was sufficient to get it loaded and rewritten to disk. But since runs with extra src dirs were done on different sets, the compilation of extra src dirs would be done with bad historical data (all the modules in src/ are dropped, all those in test/ are re-added); this DAG was then written to disk once again, polluting the next non-extra run. This is bug 2, and it is fixed by adding an optional label to each run so that a regular or extra compile round can be distinguished, each tracking their own files in their own DAG. A single test (and a lot of diffing) were sufficient for this.
il y a 6 ans
support for hex v2, multiple repository fetching, private organizations (#1884) * update to hex_core for hex-v2 repo support (#1865) * update to hex_core for hex-v2 repo support This patch adds only single repo hex-v2 support through hex_core. Packages no longer filtered out by buildtool metadata and the package index is updated per-package instead of fetched as one large ets dump. * tell travis to also build hex_core branch * support list of repos for hex packages (#1866) * support list of repos for hex packages repos are defined under the hex key in rebar configs. They can be defined at the top level of a project or globally, but not in profiles and the repos configured in dependencies are also ignored. Searching for packages involves first checking for a match in the local repo index cache, in the order repos are defined. If not found each repo is checked through the hex api for any known versions of the package and the first repo with a version that fits the constraint is used. * add {repos, replace, []} for overriding the global & default repos * add hex auth handling for repos (#1874) auth token are kept in a hex.config file that is modified by the rebar3 hex plugin. Repo names that have a : separating a parent and child are considered organizations. The parent repo's auth will be included with the child. So an organization named hexpm:rebar3_test will include any hexpm auth tokens found in the rebar3_test organization's configuration. * move packages to top level of of hexpm cache dir (#1876) * move packages to top level of of hexpm cache dir * append organization name to parent's repo_url when parsing repos * only eval config scripts and apply overrides once per app (#1879) * only eval config scripts and apply overrides once per app * move new resource behaviour to rebar_resource_v2 and keep v1 * cleanup use of rebar_resource module and unused functions * cleanup error messages and unused code * when discovering apps support mix packages as unbuilt apps (#1882) * use hex_core tarball unpacking support in pkg resource (#1883) * use hex_core tarball unpacking support in pkg resource * ignore etag if package doesn't exist and delete if checksum fails * add back tests for bad package checksums * improve bad registry checksum error message
il y a 6 ans
Fix directory recursion in compiler This patch contains two behaviour changes and reasserts other behaviours that now line things up with user and documentation expectations: 1. The src directories remain recursive. We turned it on by accident at some point in the past and now people rely on it, so we're stuck with it. However a new test ensures that the feature can be turned off on-demand as documented on the website. 2. The test directories are no longer recursive by default. The fix is done by properly fixing how rebar3.erl does its feature injection by mandating the default value there. I'm somewhat nervous that this change could negatively impact some users and older compiler module versions, but if users stick to the rebar_dir interface, everything should keep working transparently. 3. The test directories' configuration is no longer silently dropped. Due to how rebar3.erl injected test state without looking for what the user may have specified, multiple extra_src_dirs entries existed at once and were run; one with the recursion set to true and one with whatever the user specified. If the user disabled recursion of the "test" extra_src_dir, then the injected value still ran it once... 4. The handling of extra files in the compiler module is fixed to actually use the rebar_dir interface properly, and reinjects non-default directory recursion settings into the swapped options for the shimmed extra apps. Not doing this annotation step resulted in the write for swapped opts to actually drop the configured recursion value and make everything recursive all the time. A single new test actually validates all of that behaviour and seems to work fine.
il y a 4 ans
  1. -module(rebar_compile_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. all() ->
  9. [{group, basic_app}, {group, release_apps},
  10. {group, checkout_apps}, {group, checkout_deps},
  11. {group, basic_srcdirs}, {group, release_srcdirs}, {group, unbalanced_srcdirs},
  12. {group, basic_extras}, {group, release_extras}, {group, unbalanced_extras},
  13. {group, root_extras},
  14. recompile_when_hrl_changes, recompile_when_included_hrl_changes,
  15. recompile_extra_when_hrl_in_src_changes,
  16. recompile_when_opts_included_hrl_changes,
  17. recompile_when_foreign_included_hrl_changes,
  18. recompile_when_foreign_behaviour_changes,
  19. recompile_when_recursive_behaviour_changes,
  20. recompile_when_opts_change, recompile_when_dag_opts_change,
  21. dont_recompile_when_opts_dont_change, dont_recompile_yrl_or_xrl,
  22. delete_beam_if_source_deleted,
  23. deps_in_path, checkout_priority, highest_version_of_pkg_dep,
  24. parse_transform_test, erl_first_files_test, mib_test,
  25. umbrella_mib_first_test, deps_mib_test,
  26. only_default_transitive_deps, clean_all,
  27. clean_specific, profile_deps, deps_build_in_prod, only_deps,
  28. override_deps, git_subdir_deps, override_add_deps, override_del_deps,
  29. override_del_pkg_deps, override_opts, override_add_opts, override_del_opts,
  30. apply_overrides_exactly_once, override_only_deps,
  31. profile_override_deps, profile_override_add_deps, profile_override_del_deps,
  32. profile_override_opts, profile_override_add_opts, profile_override_del_opts,
  33. include_file_relative_to_working_directory, include_file_in_src,
  34. include_file_relative_to_working_directory_test, include_file_in_src_test,
  35. include_file_in_src_test_multiapp,
  36. recompile_when_parse_transform_as_opt_changes,
  37. recompile_when_parse_transform_inline_changes,
  38. regex_filter_skip, regex_filter_regression,
  39. recursive, no_recursive, extra_recursion,
  40. always_recompile_when_erl_compiler_options_set,
  41. dont_recompile_when_erl_compiler_options_env_does_not_change,
  42. recompile_when_erl_compiler_options_env_changes,
  43. rebar_config_os_var, split_project_apps_hooks,
  44. app_file_linting].
  45. groups() ->
  46. [{basic_app, [], [build_basic_app, paths_basic_app, clean_basic_app]},
  47. {release_apps, [], [build_release_apps, paths_release_apps, clean_release_apps]},
  48. {checkout_apps, [], [paths_checkout_apps]},
  49. {checkout_deps, [], [build_checkout_deps, paths_checkout_deps]},
  50. {basic_srcdirs, [], [build_basic_srcdirs, paths_basic_srcdirs]},
  51. {release_srcdirs, [], [build_release_srcdirs,
  52. paths_release_srcdirs]},
  53. {unbalanced_srcdirs, [], [build_unbalanced_srcdirs,
  54. paths_unbalanced_srcdirs]},
  55. {basic_extras, [], [build_basic_extra_dirs,
  56. paths_basic_extra_dirs,
  57. clean_basic_extra_dirs]},
  58. {release_extras, [], [build_release_extra_dirs,
  59. paths_release_extra_dirs,
  60. clean_release_extra_dirs]},
  61. {unbalanced_extras, [], [build_unbalanced_extra_dirs,
  62. paths_unbalanced_extra_dirs]},
  63. {root_extras, [], [build_extra_dirs_in_project_root,
  64. paths_extra_dirs_in_project_root,
  65. clean_extra_dirs_in_project_root]}].
  66. init_per_group(basic_app, Config) ->
  67. NewConfig = rebar_test_utils:init_rebar_state(Config, "basic_app_"),
  68. AppDir = ?config(apps, NewConfig),
  69. Name = rebar_test_utils:create_random_name("app1"),
  70. Vsn = rebar_test_utils:create_random_vsn(),
  71. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  72. [{app_names, [Name]}, {vsns, [Vsn]}|NewConfig];
  73. init_per_group(release_apps, Config) ->
  74. NewConfig = rebar_test_utils:init_rebar_state(Config, "release_apps_"),
  75. AppDir = ?config(apps, NewConfig),
  76. Name1 = rebar_test_utils:create_random_name("relapp1_"),
  77. Vsn1 = rebar_test_utils:create_random_vsn(),
  78. rebar_test_utils:create_app(filename:join([AppDir,"apps",Name1]), Name1, Vsn1, [kernel, stdlib]),
  79. Name2 = rebar_test_utils:create_random_name("relapp2_"),
  80. Vsn2 = rebar_test_utils:create_random_vsn(),
  81. rebar_test_utils:create_app(filename:join([AppDir,"apps",Name2]), Name2, Vsn2, [kernel, stdlib]),
  82. [{app_names, [Name1, Name2]}, {vsns, [Vsn1, Vsn2]}|NewConfig];
  83. init_per_group(checkout_apps, Config) ->
  84. NewConfig = rebar_test_utils:init_rebar_state(Config, "checkout_apps_"),
  85. AppDir = ?config(apps, NewConfig),
  86. CheckoutsDir = ?config(checkouts, NewConfig),
  87. Name1 = rebar_test_utils:create_random_name("checkapp1_"),
  88. Vsn1 = rebar_test_utils:create_random_vsn(),
  89. rebar_test_utils:create_app(AppDir, Name1, Vsn1, [kernel, stdlib]),
  90. Name2 = rebar_test_utils:create_random_name("checkapp2_"),
  91. Vsn2 = rebar_test_utils:create_random_vsn(),
  92. rebar_test_utils:create_app(filename:join([CheckoutsDir,Name2]), Name2, Vsn2, [kernel, stdlib]),
  93. [{app_names, [Name1, Name2]}, {vsns, [Vsn1, Vsn2]}|NewConfig];
  94. init_per_group(checkout_deps, Config) ->
  95. NewConfig = rebar_test_utils:init_rebar_state(Config, "checkout_deps_"),
  96. AppDir = ?config(apps, NewConfig),
  97. CheckoutsDir = ?config(checkouts, NewConfig),
  98. DepsDir = filename:join([AppDir, "_build", "default", "lib"]),
  99. Name1 = rebar_test_utils:create_random_name("checkapp1_"),
  100. Vsn1 = rebar_test_utils:create_random_vsn(),
  101. rebar_test_utils:create_app(AppDir, Name1, Vsn1, [kernel, stdlib]),
  102. Name2 = rebar_test_utils:create_random_name("checkapp2_"),
  103. Vsn2 = rebar_test_utils:create_random_vsn(),
  104. rebar_test_utils:create_app(filename:join([CheckoutsDir,Name2]), Name2, Vsn2, [kernel, stdlib]),
  105. rebar_test_utils:create_app(filename:join([DepsDir,Name2]), Name2, Vsn1, [kernel, stdlib]),
  106. [{app_names, [Name1, Name2]}, {vsns, [Vsn1, Vsn2]}|NewConfig];
  107. init_per_group(Group, Config) when Group == basic_srcdirs; Group == basic_extras ->
  108. NewConfig = rebar_test_utils:init_rebar_state(Config, "basic_srcdirs_"),
  109. AppDir = ?config(apps, NewConfig),
  110. Name = rebar_test_utils:create_random_name("app1_"),
  111. Vsn = rebar_test_utils:create_random_vsn(),
  112. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  113. ExtraSrc = io_lib:format("-module(~ts_extra).\n-export([ok/0]).\nok() -> ok.\n", [Name]),
  114. ok = filelib:ensure_dir(filename:join([AppDir, "extra", "dummy"])),
  115. ok = file:write_file(filename:join([AppDir, "extra", io_lib:format("~ts_extra.erl", [Name])]),
  116. ExtraSrc),
  117. [{app_names, [Name]}, {vsns, [Vsn]}|NewConfig];
  118. init_per_group(Group, Config) when Group == release_srcdirs; Group == release_extras ->
  119. NewConfig = rebar_test_utils:init_rebar_state(Config, "release_srcdirs_"),
  120. AppDir = ?config(apps, NewConfig),
  121. Name1 = rebar_test_utils:create_random_name("relapp1_"),
  122. Vsn1 = rebar_test_utils:create_random_vsn(),
  123. rebar_test_utils:create_app(filename:join([AppDir, "apps", Name1]), Name1, Vsn1, [kernel, stdlib]),
  124. Name2 = rebar_test_utils:create_random_name("relapp2_"),
  125. Vsn2 = rebar_test_utils:create_random_vsn(),
  126. rebar_test_utils:create_app(filename:join([AppDir, "apps", Name2]), Name2, Vsn2, [kernel, stdlib]),
  127. ExtraOne = io_lib:format("-module(~ts_extra).\n-export([ok/0]).\nok() -> ok.\n", [Name1]),
  128. ok = filelib:ensure_dir(filename:join([AppDir, "apps", Name1, "extra", "dummy"])),
  129. ok = file:write_file(filename:join([AppDir, "apps", Name1, "extra",
  130. io_lib:format("~ts_extra.erl", [Name1])]),
  131. ExtraOne),
  132. ExtraTwo = io_lib:format("-module(~ts_extra).\n-export([ok/0]).\nok() -> ok.\n", [Name2]),
  133. ok = filelib:ensure_dir(filename:join([AppDir, "apps", Name2, "extra", "dummy"])),
  134. ok = file:write_file(filename:join([AppDir, "apps", Name2, "extra",
  135. io_lib:format("~ts_extra.erl", [Name2])]),
  136. ExtraTwo),
  137. [{app_names, [Name1, Name2]}, {vsns, [Vsn1, Vsn2]}|NewConfig];
  138. init_per_group(Group, Config) when Group == unbalanced_srcdirs; Group == unbalanced_extras ->
  139. NewConfig = rebar_test_utils:init_rebar_state(Config, "unbalanced_srcdirs_"),
  140. AppDir = ?config(apps, NewConfig),
  141. Name1 = rebar_test_utils:create_random_name("relapp1_"),
  142. Vsn1 = rebar_test_utils:create_random_vsn(),
  143. rebar_test_utils:create_app(filename:join([AppDir, "apps", Name1]), Name1, Vsn1, [kernel, stdlib]),
  144. Name2 = rebar_test_utils:create_random_name("relapp2_"),
  145. Vsn2 = rebar_test_utils:create_random_vsn(),
  146. rebar_test_utils:create_app(filename:join([AppDir, "apps", Name2]), Name2, Vsn2, [kernel, stdlib]),
  147. ExtraOne = io_lib:format("-module(~ts_extra).\n-export([ok/0]).\nok() -> ok.\n", [Name1]),
  148. ok = filelib:ensure_dir(filename:join([AppDir, "apps", Name1, "extra", "dummy"])),
  149. ok = file:write_file(filename:join([AppDir, "apps", Name1, "extra",
  150. io_lib:format("~ts_extra.erl", [Name1])]),
  151. ExtraOne),
  152. [{app_names, [Name1, Name2]}, {vsns, [Vsn1, Vsn2]}|NewConfig];
  153. init_per_group(root_extras, Config) ->
  154. NewConfig = rebar_test_utils:init_rebar_state(Config, "root_extras_"),
  155. AppDir = ?config(apps, NewConfig),
  156. Name1 = rebar_test_utils:create_random_name("relapp1_"),
  157. Vsn1 = rebar_test_utils:create_random_vsn(),
  158. rebar_test_utils:create_app(filename:join([AppDir, "apps", Name1]), Name1, Vsn1, [kernel, stdlib]),
  159. Name2 = rebar_test_utils:create_random_name("relapp2_"),
  160. Vsn2 = rebar_test_utils:create_random_vsn(),
  161. rebar_test_utils:create_app(filename:join([AppDir, "apps", Name2]), Name2, Vsn2, [kernel, stdlib]),
  162. Extra = <<"-module(extra).\n-export([ok/0]).\nok() -> ok.\n">>,
  163. ok = filelib:ensure_dir(filename:join([AppDir, "extra", "dummy"])),
  164. ok = file:write_file(filename:join([AppDir, "extra", "extra.erl"]), Extra),
  165. [{app_names, [Name1, Name2]}, {vsns, [Vsn1, Vsn2]}|NewConfig].
  166. end_per_group(_Group, _Config) ->
  167. ok.
  168. init_per_suite(Config) ->
  169. Config.
  170. end_per_suite(_Config) ->
  171. ok.
  172. init_per_testcase(Test, Config) when
  173. Test == dont_recompile_when_erl_compiler_options_env_does_not_change
  174. orelse
  175. Test == recompile_when_erl_compiler_options_env_changes ->
  176. _ = code:ensure_loaded(os),
  177. UnSetEnv = erlang:function_exported(os, unsetenv, 1),
  178. _ = code:ensure_loaded(compile),
  179. EnvOpts = erlang:function_exported(compile, env_compiler_options, 0),
  180. case {UnSetEnv, EnvOpts} of
  181. {true, true} -> maybe_init_config(Config);
  182. _ -> {skip, "compile:env_compiler_options/0 unavailable"}
  183. end;
  184. init_per_testcase(always_recompile_when_erl_compiler_options_set, Config) ->
  185. _ = code:ensure_loaded(os),
  186. UnSetEnv = erlang:function_exported(os, unsetenv, 1),
  187. _ = code:ensure_loaded(compile),
  188. EnvOpts = erlang:function_exported(compile, env_compiler_options, 0),
  189. case {UnSetEnv, EnvOpts} of
  190. {true, true} -> {skip, "compile:env_compiler_options/0 available"};
  191. {true, false} -> maybe_init_config(Config);
  192. _ -> {skip, "os:unsetenv/1 unavailable"}
  193. end;
  194. init_per_testcase(_, Config) -> maybe_init_config(Config).
  195. maybe_init_config(Config) ->
  196. case ?config(apps, Config) of
  197. undefined -> rebar_test_utils:init_rebar_state(Config);
  198. _ -> Config
  199. end.
  200. end_per_testcase(_, _Config) ->
  201. catch meck:unload().
  202. %% test cases
  203. build_basic_app(Config) ->
  204. [Name] = ?config(app_names, Config),
  205. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}).
  206. build_release_apps(Config) ->
  207. [Name1, Name2] = ?config(app_names, Config),
  208. rebar_test_utils:run_and_check(
  209. Config, [], ["compile"],
  210. {ok, [{app, Name1}, {app, Name2}]}
  211. ).
  212. build_checkout_deps(Config) ->
  213. AppDir = ?config(apps, Config),
  214. [Name1, Name2] = ?config(app_names, Config),
  215. [_, Vsn2] = ?config(vsns, Config),
  216. Deps = [{list_to_atom(Name2), Vsn2, {git, "", ""}}],
  217. {ok, RebarConfig} = file:consult(rebar_test_utils:create_config(AppDir, [{deps, Deps}])),
  218. rebar_test_utils:run_and_check(
  219. Config, RebarConfig, ["compile"],
  220. {ok, [{app, Name1}, {checkout, Name2}]}
  221. ).
  222. build_basic_srcdirs(Config) ->
  223. AppDir = ?config(apps, Config),
  224. [Name] = ?config(app_names, Config),
  225. RebarConfig = [{erl_opts, [{src_dirs, ["src", "extra"]}]}],
  226. %% check a beam corresponding to the src in the extra src_dir exists
  227. ExtraBeam = filename:join([AppDir, "_build", "default", "lib", Name, "ebin",
  228. io_lib:format("~ts_extra.beam", [Name])]),
  229. %% check the extra src_dir was copied/linked into the _build dir
  230. ExtraDir = filename:join([AppDir, "_build", "default", "lib", Name, "extra"]),
  231. rebar_test_utils:run_and_check(
  232. Config, RebarConfig, ["compile"],
  233. {ok, [{app, Name}, {file, ExtraBeam}, {dir, ExtraDir}]}
  234. ).
  235. build_release_srcdirs(Config) ->
  236. AppDir = ?config(apps, Config),
  237. [Name1, Name2] = ?config(app_names, Config),
  238. RebarConfig = [{erl_opts, [{src_dirs, ["src", "extra"]}]}],
  239. %% check a beam corresponding to the src in the extra src_dir exists
  240. Extra1Beam = filename:join([AppDir, "_build", "default", "lib", Name1, "ebin",
  241. io_lib:format("~ts_extra.beam", [Name1])]),
  242. Extra2Beam = filename:join([AppDir, "_build", "default", "lib", Name2, "ebin",
  243. io_lib:format("~ts_extra.beam", [Name2])]),
  244. %% check the extra src_dir was copied/linked into the _build dir
  245. Extra1Dir = filename:join([AppDir, "_build", "default", "lib", Name1, "extra"]),
  246. Extra2Dir = filename:join([AppDir, "_build", "default", "lib", Name2, "extra"]),
  247. rebar_test_utils:run_and_check(
  248. Config, RebarConfig, ["compile"],
  249. {ok, [{app, Name1}, {app, Name2},
  250. {file, Extra1Beam}, {file, Extra2Beam},
  251. {dir, Extra1Dir}, {dir, Extra2Dir}]}
  252. ).
  253. build_unbalanced_srcdirs(Config) ->
  254. AppDir = ?config(apps, Config),
  255. [Name1, Name2] = ?config(app_names, Config),
  256. RebarConfig = [{erl_opts, [{src_dirs, ["src", "extra"]}]}],
  257. %% check a beam corresponding to the src in the extra src_dir exists
  258. Extra1Beam = filename:join([AppDir, "_build", "default", "lib", Name1, "ebin",
  259. io_lib:format("~ts_extra.beam", [Name1])]),
  260. %% check the extra src_dir was copied/linked into the _build dir
  261. Extra1Dir = filename:join([AppDir, "_build", "default", "lib", Name1, "extra"]),
  262. rebar_test_utils:run_and_check(
  263. Config, RebarConfig, ["compile"],
  264. {ok, [{app, Name1}, {app, Name2}, {file, Extra1Beam}, {dir, Extra1Dir}]}
  265. ),
  266. %% check no extra src_dir were copied/linked into the _build dir
  267. Extra2Dir = filename:join([AppDir, "_build", "default", "lib", Name2, "extra"]),
  268. false = filelib:is_dir(Extra2Dir),
  269. %% check only expected beams are in the ebin dir
  270. {ok, Files} = rebar_utils:list_dir(filename:join([AppDir, "_build", "default", "lib", Name2, "ebin"])),
  271. lists:all(fun(Beam) -> lists:member(Beam, [Name2 ++ ".app", "not_a_real_src_" ++ Name2 ++ ".beam"]) end,
  272. Files).
  273. build_basic_extra_dirs(Config) ->
  274. AppDir = ?config(apps, Config),
  275. [Name] = ?config(app_names, Config),
  276. RebarConfig = [{erl_opts, [{extra_src_dirs, ["extra"]}]}],
  277. %% check a beam corresponding to the src in the extra src_dir exists
  278. ExtraBeam = filename:join([AppDir, "_build", "default", "lib", Name, "extra",
  279. io_lib:format("~ts_extra.beam", [Name])]),
  280. rebar_test_utils:run_and_check(
  281. Config, RebarConfig, ["compile"],
  282. {ok, [{app, Name}, {file, ExtraBeam}]}
  283. ).
  284. build_release_extra_dirs(Config) ->
  285. AppDir = ?config(apps, Config),
  286. [Name1, Name2] = ?config(app_names, Config),
  287. RebarConfig = [{erl_opts, [{extra_src_dirs, ["extra"]}]}],
  288. %% check a beam corresponding to the src in the extra src_dir exists
  289. Extra1Beam = filename:join([AppDir, "_build", "default", "lib", Name1, "extra",
  290. io_lib:format("~ts_extra.beam", [Name1])]),
  291. Extra2Beam = filename:join([AppDir, "_build", "default", "lib", Name2, "extra",
  292. io_lib:format("~ts_extra.beam", [Name2])]),
  293. rebar_test_utils:run_and_check(
  294. Config, RebarConfig, ["compile"],
  295. {ok, [{app, Name1}, {app, Name2}, {file, Extra1Beam}, {file, Extra2Beam}]}
  296. ).
  297. build_unbalanced_extra_dirs(Config) ->
  298. AppDir = ?config(apps, Config),
  299. [Name1, Name2] = ?config(app_names, Config),
  300. RebarConfig = [{erl_opts, [{extra_src_dirs, ["extra"]}]}],
  301. %% check a beam corresponding to the src in the extra src_dir exists
  302. Extra1Beam = filename:join([AppDir, "_build", "default", "lib", Name1, "extra",
  303. io_lib:format("~ts_extra.beam", [Name1])]),
  304. rebar_test_utils:run_and_check(
  305. Config, RebarConfig, ["compile"],
  306. {ok, [{app, Name1}, {app, Name2}, {file, Extra1Beam}]}
  307. ),
  308. %% check no extra src_dir were copied/linked into the _build dir
  309. false = filelib:is_dir(filename:join([AppDir, "_build", "default", "lib", Name2, "extra"])),
  310. %% check only expected beams are in the ebin dir
  311. {ok, Files} = rebar_utils:list_dir(filename:join([AppDir, "_build", "default", "lib", Name2, "ebin"])),
  312. lists:all(fun(Beam) -> lists:member(Beam, [Name2 ++ ".app", "not_a_real_src_" ++ Name2 ++ ".beam"]) end,
  313. Files).
  314. build_extra_dirs_in_project_root(Config) ->
  315. AppDir = ?config(apps, Config),
  316. [Name1, Name2] = ?config(app_names, Config),
  317. RebarConfig = [{erl_opts, [{extra_src_dirs, ["extra"]}]}],
  318. %% check a beam corresponding to the src in the extra src_dir exists
  319. ExtraBeam = filename:join([AppDir, "_build", "default", "extras", "extra", "extra.beam"]),
  320. rebar_test_utils:run_and_check(
  321. Config, RebarConfig, ["compile"],
  322. {ok, [{app, Name1}, {app, Name2}, {file, ExtraBeam}]}
  323. ).
  324. paths_basic_app(Config) ->
  325. [Name] = ?config(app_names, Config),
  326. [Vsn] = ?config(vsns, Config),
  327. {ok, State} = rebar_test_utils:run_and_check(Config, [], ["compile"], return),
  328. code:add_paths(rebar_state:code_paths(State, all_deps)),
  329. ok = application:load(list_to_atom(Name)),
  330. Loaded = application:loaded_applications(),
  331. {_, _, Vsn} = lists:keyfind(list_to_atom(Name), 1, Loaded).
  332. paths_release_apps(Config) ->
  333. [Name1, Name2] = ?config(app_names, Config),
  334. [Vsn1, Vsn2] = ?config(vsns, Config),
  335. {ok, State} = rebar_test_utils:run_and_check(Config, [], ["compile"], return),
  336. code:add_paths(rebar_state:code_paths(State, all_deps)),
  337. ok = application:load(list_to_atom(Name1)),
  338. ok = application:load(list_to_atom(Name2)),
  339. Loaded = application:loaded_applications(),
  340. {_, _, Vsn1} = lists:keyfind(list_to_atom(Name1), 1, Loaded),
  341. {_, _, Vsn2} = lists:keyfind(list_to_atom(Name2), 1, Loaded).
  342. paths_checkout_apps(Config) ->
  343. [Name1, _Name2] = ?config(app_names, Config),
  344. [Vsn1, _Vsn2] = ?config(vsns, Config),
  345. {ok, State} = rebar_test_utils:run_and_check(Config, [], ["compile"], return),
  346. code:add_paths(rebar_state:code_paths(State, all_deps)),
  347. ok = application:load(list_to_atom(Name1)),
  348. Loaded = application:loaded_applications(),
  349. {_, _, Vsn1} = lists:keyfind(list_to_atom(Name1), 1, Loaded).
  350. paths_checkout_deps(Config) ->
  351. AppDir = ?config(apps, Config),
  352. [_Name1, Name2] = ?config(app_names, Config),
  353. [_Vsn1, Vsn2] = ?config(vsns, Config),
  354. %% rebar_test_utils:init_rebar_state/1,2 uses rebar_state:new/3 which
  355. %% maybe incorrectly sets deps to [] (based on `rebar.lock`) instead of
  356. %% to the checkapps
  357. %% until that is sorted out the lock file has to be removed before
  358. %% this test will pass
  359. file:delete(filename:join([AppDir, "rebar.lock"])),
  360. {ok, RebarConfig} = file:consult(filename:join([AppDir, "rebar.config"])),
  361. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  362. [AppName2] = rebar_state:all_checkout_deps(State),
  363. Name2Bin = binary:list_to_bin(Name2),
  364. Name2Bin = rebar_app_info:name(AppName2),
  365. code:add_paths(rebar_state:code_paths(State, all_deps)),
  366. ok = application:load(list_to_atom(Name2)),
  367. Loaded = application:loaded_applications(),
  368. {_, _, Vsn2} = lists:keyfind(list_to_atom(Name2), 1, Loaded).
  369. paths_basic_srcdirs(Config) ->
  370. AppDir = ?config(apps, Config),
  371. [Name] = ?config(app_names, Config),
  372. RebarConfig = [{erl_opts, [{src_dirs, ["src", "extra"]}]}],
  373. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  374. code:add_paths(rebar_state:code_paths(State, all_deps)),
  375. Mod = list_to_atom(lists:flatten(io_lib:format("~ts_extra", [Name]))),
  376. {module, Mod} = code:ensure_loaded(Mod),
  377. Expect = filename:join([AppDir, "_build", "default", "lib", Name, "ebin",
  378. io_lib:format("~ts_extra.beam", [Name])]),
  379. Expect = code:which(Mod).
  380. paths_release_srcdirs(Config) ->
  381. AppDir = ?config(apps, Config),
  382. [Name1, Name2] = ?config(app_names, Config),
  383. RebarConfig = [{erl_opts, [{src_dirs, ["src", "extra"]}]}],
  384. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  385. code:add_paths(rebar_state:code_paths(State, all_deps)),
  386. Mod1 = list_to_atom(lists:flatten(io_lib:format("~ts_extra", [Name1]))),
  387. {module, Mod1} = code:ensure_loaded(Mod1),
  388. Mod2 = list_to_atom(lists:flatten(io_lib:format("~ts_extra", [Name2]))),
  389. {module, Mod2} = code:ensure_loaded(Mod2),
  390. ExpectOne = filename:join([AppDir, "_build", "default", "lib", Name1, "ebin",
  391. io_lib:format("~ts_extra.beam", [Name1])]),
  392. ExpectOne = code:which(Mod1),
  393. ExpectTwo = filename:join([AppDir, "_build", "default", "lib", Name2, "ebin",
  394. io_lib:format("~ts_extra.beam", [Name2])]),
  395. ExpectTwo = code:which(Mod2).
  396. paths_unbalanced_srcdirs(Config) ->
  397. AppDir = ?config(apps, Config),
  398. [Name1, Name2] = ?config(app_names, Config),
  399. RebarConfig = [{erl_opts, [{src_dirs, ["src", "extra"]}]}],
  400. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  401. code:add_paths(rebar_state:code_paths(State, all_deps)),
  402. Mod1 = list_to_atom(lists:flatten(io_lib:format("~ts_extra", [Name1]))),
  403. {module, Mod1} = code:ensure_loaded(Mod1),
  404. Mod2 = list_to_atom(lists:flatten(io_lib:format("~ts_extra", [Name2]))),
  405. {error, nofile} = code:ensure_loaded(Mod2),
  406. ExpectOne = filename:join([AppDir, "_build", "default", "lib", Name1, "ebin",
  407. io_lib:format("~ts_extra.beam", [Name1])]),
  408. ExpectOne = code:which(Mod1).
  409. paths_basic_extra_dirs(Config) ->
  410. AppDir = ?config(apps, Config),
  411. [Name] = ?config(app_names, Config),
  412. RebarConfig = [{erl_opts, [{extra_src_dirs, ["extra"]}]}],
  413. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  414. code:add_paths(rebar_state:code_paths(State, all_deps)),
  415. Mod = list_to_atom(lists:flatten(io_lib:format("~ts_extra", [Name]))),
  416. {module, Mod} = code:ensure_loaded(Mod),
  417. Expect = filename:join([AppDir, "_build", "default", "lib", Name, "extra",
  418. io_lib:format("~ts_extra.beam", [Name])]),
  419. Expect = code:which(Mod).
  420. paths_release_extra_dirs(Config) ->
  421. AppDir = ?config(apps, Config),
  422. [Name1, Name2] = ?config(app_names, Config),
  423. RebarConfig = [{erl_opts, [{extra_src_dirs, ["extra"]}]}],
  424. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  425. code:add_paths(rebar_state:code_paths(State, all_deps)),
  426. Mod1 = list_to_atom(lists:flatten(io_lib:format("~ts_extra", [Name1]))),
  427. {module, Mod1} = code:ensure_loaded(Mod1),
  428. Mod2 = list_to_atom(lists:flatten(io_lib:format("~ts_extra", [Name2]))),
  429. {module, Mod2} = code:ensure_loaded(Mod2),
  430. ExpectOne = filename:join([AppDir, "_build", "default", "lib", Name1, "extra",
  431. io_lib:format("~ts_extra.beam", [Name1])]),
  432. ExpectOne = code:which(Mod1),
  433. ExpectTwo = filename:join([AppDir, "_build", "default", "lib", Name2, "extra",
  434. io_lib:format("~ts_extra.beam", [Name2])]),
  435. ExpectTwo = code:which(Mod2).
  436. paths_unbalanced_extra_dirs(Config) ->
  437. AppDir = ?config(apps, Config),
  438. [Name1, Name2] = ?config(app_names, Config),
  439. RebarConfig = [{erl_opts, [{extra_src_dirs, ["extra"]}]}],
  440. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  441. code:add_paths(rebar_state:code_paths(State, all_deps)),
  442. Mod1 = list_to_atom(lists:flatten(io_lib:format("~ts_extra", [Name1]))),
  443. {module, Mod1} = code:ensure_loaded(Mod1),
  444. Mod2 = list_to_atom(lists:flatten(io_lib:format("~ts_extra", [Name2]))),
  445. {error, nofile} = code:ensure_loaded(Mod2),
  446. ExpectOne = filename:join([AppDir, "_build", "default", "lib", Name1, "extra",
  447. io_lib:format("~ts_extra.beam", [Name1])]),
  448. ExpectOne = code:which(Mod1).
  449. paths_extra_dirs_in_project_root(Config) ->
  450. AppDir = ?config(apps, Config),
  451. RebarConfig = [{erl_opts, [{extra_src_dirs, ["extra"]}]}],
  452. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  453. code:add_paths(rebar_state:code_paths(State, all_deps)),
  454. {module, extra} = code:ensure_loaded(extra),
  455. Expect = filename:join([AppDir, "_build", "default", "extras", "extra", "extra.beam"]),
  456. Expect = code:which(extra).
  457. clean_basic_app(Config) ->
  458. [Name] = ?config(app_names, Config),
  459. rebar_test_utils:run_and_check(Config, [], ["clean"], {ok, [{app, Name, invalid}]}).
  460. clean_release_apps(Config) ->
  461. [Name1, Name2] = ?config(app_names, Config),
  462. rebar_test_utils:run_and_check(Config, [], ["clean"],
  463. {ok, [{app, Name1, invalid}, {app, Name2, invalid}]}).
  464. clean_basic_extra_dirs(Config) ->
  465. AppDir = ?config(apps, Config),
  466. [Name] = ?config(app_names, Config),
  467. rebar_test_utils:run_and_check(Config, [], ["clean"], {ok, [{app, Name, invalid}]}),
  468. Beam = lists:flatten(io_lib:format("~ts_extra", [Name])),
  469. false = ec_file:exists(filename:join([AppDir, "_build", "default", "lib", Name, "extras", Beam])).
  470. clean_release_extra_dirs(Config) ->
  471. AppDir = ?config(apps, Config),
  472. [Name1, Name2] = ?config(app_names, Config),
  473. rebar_test_utils:run_and_check(Config, [], ["clean"],
  474. {ok, [{app, Name1, invalid}, {app, Name2, invalid}]}),
  475. Beam1 = lists:flatten(io_lib:format("~ts_extra", [Name1])),
  476. false = ec_file:exists(filename:join([AppDir, "_build", "default", "lib", Name1, "extras", Beam1])),
  477. Beam2 = lists:flatten(io_lib:format("~ts_extra", [Name2])),
  478. false = ec_file:exists(filename:join([AppDir, "_build", "default", "lib", Name2, "extras", Beam2])).
  479. clean_extra_dirs_in_project_root(Config) ->
  480. AppDir = ?config(apps, Config),
  481. [Name1, Name2] = ?config(app_names, Config),
  482. rebar_test_utils:run_and_check(Config, [], ["clean"],
  483. {ok, [{app, Name1, invalid}, {app, Name2, invalid}]}),
  484. false = ec_file:exists(filename:join([AppDir, "_build", "default", "extras"])).
  485. recompile_when_hrl_changes(Config) ->
  486. AppDir = ?config(apps, Config),
  487. Name = rebar_test_utils:create_random_name("app1_"),
  488. Vsn = rebar_test_utils:create_random_vsn(),
  489. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  490. ExtraSrc = <<"-module(test_header_include).\n"
  491. "-export([main/0]).\n"
  492. "-include(\"test_header_include.hrl\").\n"
  493. "main() -> ?SOME_DEFINE.\n">>,
  494. ExtraHeader = <<"-define(SOME_DEFINE, true).\n">>,
  495. HeaderFile = filename:join([AppDir, "src", "test_header_include.hrl"]),
  496. ok = file:write_file(filename:join([AppDir, "src", "test_header_include.erl"]), ExtraSrc),
  497. ok = file:write_file(HeaderFile, ExtraHeader),
  498. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  499. EbinDir = filename:join([AppDir, "_build", "default", "lib", Name, "ebin"]),
  500. {ok, Files} = rebar_utils:list_dir(EbinDir),
  501. ModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  502. || F <- Files, filename:extension(F) == ".beam"],
  503. timer:sleep(1000),
  504. NewExtraHeader = <<"-define(SOME_DEFINE, false).\n">>,
  505. ok = file:write_file(HeaderFile, NewExtraHeader),
  506. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  507. {ok, NewFiles} = rebar_utils:list_dir(EbinDir),
  508. NewModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  509. || F <- NewFiles, filename:extension(F) == ".beam"],
  510. ?assert(ModTime =/= NewModTime).
  511. recompile_when_included_hrl_changes(Config) ->
  512. AppDir = ?config(apps, Config),
  513. Name = rebar_test_utils:create_random_name("app1_"),
  514. Vsn = rebar_test_utils:create_random_vsn(),
  515. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  516. ExtraSrc = <<"-module(test_header_include).\n"
  517. "-export([main/0]).\n"
  518. "-include(\"test_header_include.hrl\").\n"
  519. "main() -> ?SOME_DEFINE.\n">>,
  520. ExtraHeader = <<"-define(SOME_DEFINE, true).\n">>,
  521. ok = filelib:ensure_dir(filename:join([AppDir, "include", "dummy"])),
  522. HeaderFile = filename:join([AppDir, "include", "test_header_include.hrl"]),
  523. ok = file:write_file(filename:join([AppDir, "src", "test_header_include.erl"]), ExtraSrc),
  524. ok = file:write_file(HeaderFile, ExtraHeader),
  525. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  526. EbinDir = filename:join([AppDir, "_build", "default", "lib", Name, "ebin"]),
  527. {ok, Files} = rebar_utils:list_dir(EbinDir),
  528. ModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  529. || F <- Files, filename:extension(F) == ".beam"],
  530. timer:sleep(1000),
  531. NewExtraHeader = <<"-define(SOME_DEFINE, false).\n">>,
  532. ok = file:write_file(HeaderFile, NewExtraHeader),
  533. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  534. {ok, NewFiles} = rebar_utils:list_dir(EbinDir),
  535. NewModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  536. || F <- NewFiles, filename:extension(F) == ".beam"],
  537. ?assert(ModTime =/= NewModTime).
  538. recompile_extra_when_hrl_in_src_changes(Config) ->
  539. AppDir = ?config(apps, Config),
  540. Name = rebar_test_utils:create_random_name("app1_"),
  541. Vsn = rebar_test_utils:create_random_vsn(),
  542. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  543. ExtraSrc = <<"-module(test_header_include).\n"
  544. "-export([main/0]).\n"
  545. "-include(\"test_header_include.hrl\").\n"
  546. "main() -> ?SOME_DEFINE.\n">>,
  547. ExtraHeader = <<"-define(SOME_DEFINE, true).\n">>,
  548. HeaderFile = filename:join([AppDir, "src", "test_header_include.hrl"]),
  549. SrcFile = filename:join([AppDir, "extra", "test_header_include.erl"]),
  550. filelib:ensure_dir(SrcFile),
  551. ok = file:write_file(SrcFile, ExtraSrc),
  552. ok = file:write_file(HeaderFile, ExtraHeader),
  553. RebarCfg = [{extra_src_dirs, ["extra"]}],
  554. rebar_test_utils:run_and_check(Config, RebarCfg, ["compile"],
  555. {ok, [{app, Name}]}),
  556. EbinDir = filename:join([AppDir, "_build", "default", "lib", Name, "extra"]),
  557. {ok, Files} = rebar_utils:list_dir(EbinDir),
  558. ModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  559. || F <- Files, filename:extension(F) == ".beam"],
  560. timer:sleep(1000),
  561. NewExtraHeader = <<"-define(SOME_DEFINE, false).\n">>,
  562. ok = file:write_file(HeaderFile, NewExtraHeader, [sync]),
  563. rebar_test_utils:run_and_check(Config, RebarCfg, ["compile"],
  564. {ok, [{app, Name}]}),
  565. {ok, NewFiles} = rebar_utils:list_dir(EbinDir),
  566. NewModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  567. || F <- NewFiles, filename:extension(F) == ".beam"],
  568. ?assert(ModTime =/= NewModTime).
  569. recompile_when_opts_included_hrl_changes(Config) ->
  570. AppsDir = ?config(apps, Config),
  571. Name = rebar_test_utils:create_random_name("app1_"),
  572. Vsn = rebar_test_utils:create_random_vsn(),
  573. AppDir = filename:join([AppsDir, "apps", Name]),
  574. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  575. ExtraSrc = <<"-module(test_header_include).\n"
  576. "-export([main/0]).\n"
  577. "-include(\"test_header_include.hrl\").\n"
  578. "main() -> ?SOME_DEFINE.\n">>,
  579. ExtraHeader = <<"-define(SOME_DEFINE, true).\n">>,
  580. ok = filelib:ensure_dir(filename:join([AppsDir, "include", "dummy"])),
  581. HeaderFile = filename:join([AppsDir, "include", "test_header_include.hrl"]),
  582. ok = file:write_file(filename:join([AppDir, "src", "test_header_include.erl"]), ExtraSrc),
  583. ok = file:write_file(HeaderFile, ExtraHeader),
  584. %% Using relative path from the project root
  585. RebarConfig = [{erl_opts, [{i, "include/"}]}],
  586. {ok,Cwd} = file:get_cwd(),
  587. ok = file:set_cwd(AppsDir),
  588. rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], {ok, [{app, Name}]}),
  589. EbinDir = filename:join([AppsDir, "_build", "default", "lib", Name, "ebin"]),
  590. {ok, Files} = rebar_utils:list_dir(EbinDir),
  591. ModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  592. || F <- Files, filename:extension(F) == ".beam"],
  593. timer:sleep(1000),
  594. NewExtraHeader = <<"-define(SOME_DEFINE, false).\n">>,
  595. ok = file:write_file(HeaderFile, NewExtraHeader),
  596. rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], {ok, [{app, Name}]}),
  597. {ok, NewFiles} = rebar_utils:list_dir(EbinDir),
  598. NewModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  599. || F <- NewFiles, filename:extension(F) == ".beam"],
  600. ok = file:set_cwd(Cwd),
  601. ?assert(ModTime =/= NewModTime).
  602. recompile_when_foreign_included_hrl_changes(Config) ->
  603. AppDir = ?config(apps, Config),
  604. AppsDir = filename:join([AppDir, "apps"]),
  605. Name1 = rebar_test_utils:create_random_name("app1_"),
  606. Name2 = rebar_test_utils:create_random_name("app2_"),
  607. Vsn = rebar_test_utils:create_random_vsn(),
  608. rebar_test_utils:create_app(filename:join(AppsDir, Name1),
  609. Name1, Vsn, [kernel, stdlib]),
  610. rebar_test_utils:create_app(filename:join(AppsDir, Name2),
  611. Name2, Vsn, [kernel, stdlib]),
  612. ExtraSrc = [<<"-module(test_header_include).\n"
  613. "-export([main/0]).\n"
  614. "-include_lib(\"">>, Name2, <<"/include/test_header_include.hrl\").\n"
  615. "main() -> ?SOME_DEFINE.\n">>],
  616. ExtraHeader = <<"-define(SOME_DEFINE, true).\n">>,
  617. ok = filelib:ensure_dir(filename:join([AppsDir, Name1, "src", "dummy"])),
  618. ok = filelib:ensure_dir(filename:join([AppsDir, Name2, "include", "dummy"])),
  619. HeaderFile = filename:join([AppsDir, Name2, "include", "test_header_include.hrl"]),
  620. ok = file:write_file(filename:join([AppsDir, Name1, "src", "test_header_include.erl"]), ExtraSrc),
  621. ok = file:write_file(HeaderFile, ExtraHeader),
  622. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name1}]}),
  623. EbinDir = filename:join([AppDir, "_build", "default", "lib", Name1, "ebin"]),
  624. {ok, Files} = rebar_utils:list_dir(EbinDir),
  625. ModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  626. || F <- Files, filename:extension(F) == ".beam"],
  627. timer:sleep(1000),
  628. NewExtraHeader = <<"-define(SOME_DEFINE, false).\n">>,
  629. ok = file:write_file(HeaderFile, NewExtraHeader),
  630. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name1}]}),
  631. {ok, NewFiles} = rebar_utils:list_dir(EbinDir),
  632. NewModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  633. || F <- NewFiles, filename:extension(F) == ".beam"],
  634. ?assert(ModTime =/= NewModTime).
  635. recompile_when_foreign_behaviour_changes(Config) ->
  636. AppDir = ?config(apps, Config),
  637. AppsDir = filename:join([AppDir, "apps"]),
  638. Name1 = rebar_test_utils:create_random_name("app1_"),
  639. Name2 = rebar_test_utils:create_random_name("app2_"),
  640. Vsn = rebar_test_utils:create_random_vsn(),
  641. rebar_test_utils:create_app(filename:join(AppsDir, Name1),
  642. Name1, Vsn, [kernel, stdlib]),
  643. rebar_test_utils:create_app(filename:join(AppsDir, Name2),
  644. Name2, Vsn, [kernel, stdlib]),
  645. ExtraSrc = <<"-module(test_behaviour_include).\n"
  646. "-export([main/0]).\n"
  647. "-behaviour(app2_behaviour).\n"
  648. "main() -> 1.\n">>,
  649. Behaviour = <<"-module(app2_behaviour).\n"
  650. "-callback main() -> term().\n">>,
  651. ok = filelib:ensure_dir(filename:join([AppsDir, Name1, "src", "dummy"])),
  652. ok = filelib:ensure_dir(filename:join([AppsDir, Name2, "src", "dummy"])),
  653. BehaviourFile = filename:join([AppsDir, Name2, "src", "app2_behaviour.erl"]),
  654. ok = file:write_file(filename:join([AppsDir, Name1, "src", "test_behaviour_include.erl"]), ExtraSrc),
  655. ok = file:write_file(BehaviourFile, Behaviour),
  656. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name1}]}),
  657. EbinDir = filename:join([AppDir, "_build", "default", "lib", Name1, "ebin"]),
  658. {ok, Files} = rebar_utils:list_dir(EbinDir),
  659. ModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  660. || F <- Files, filename:extension(F) == ".beam"],
  661. timer:sleep(1000),
  662. NewBehaviour = <<"-module(app2_behaviour).\n"
  663. "-callback main(_) -> term().\n">>,
  664. ok = file:write_file(BehaviourFile, NewBehaviour),
  665. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name1}]}),
  666. {ok, NewFiles} = rebar_utils:list_dir(EbinDir),
  667. NewModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  668. || F <- NewFiles, filename:extension(F) == ".beam"],
  669. ?assert(ModTime =/= NewModTime).
  670. recompile_when_recursive_behaviour_changes(Config) ->
  671. AppDir = ?config(apps, Config),
  672. AppsDir = filename:join([AppDir, "apps"]),
  673. Name1 = rebar_test_utils:create_random_name("app1_"),
  674. Vsn = rebar_test_utils:create_random_vsn(),
  675. rebar_test_utils:create_app(filename:join(AppsDir, Name1),
  676. Name1, Vsn, [kernel, stdlib]),
  677. ExtraSrc = <<"-module(test_behaviour_include).\n"
  678. "-export([main/0]).\n"
  679. "-behaviour(app1_behaviour).\n"
  680. "main() -> 1.\n">>,
  681. Behaviour = <<"-module(app1_behaviour).\n"
  682. "-callback main() -> term().\n">>,
  683. ok = filelib:ensure_dir(filename:join([AppsDir, Name1, "src", "dummy"])),
  684. ok = filelib:ensure_dir(filename:join([AppsDir, Name1, "src", "sub", "dummy"])),
  685. BehaviourFile = filename:join([AppsDir, Name1, "src", "sub", "app1_behaviour.erl"]),
  686. ok = file:write_file(filename:join([AppsDir, Name1, "src", "test_behaviour_include.erl"]), ExtraSrc),
  687. ok = file:write_file(BehaviourFile, Behaviour),
  688. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name1}]}),
  689. EbinDir = filename:join([AppDir, "_build", "default", "lib", Name1, "ebin"]),
  690. {ok, Files} = rebar_utils:list_dir(EbinDir),
  691. ModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  692. || F <- Files,
  693. filename:extension(F) == ".beam",
  694. filename:basename(F) =/= "app1_behaviour.beam"],
  695. timer:sleep(1000),
  696. NewBehaviour = <<"-module(app1_behaviour).\n"
  697. "-callback main(_) -> term().\n">>,
  698. ok = file:write_file(BehaviourFile, NewBehaviour),
  699. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name1}]}),
  700. {ok, NewFiles} = rebar_utils:list_dir(EbinDir),
  701. NewModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  702. || F <- NewFiles,
  703. filename:extension(F) == ".beam",
  704. filename:basename(F) =/= "app1_behaviour.beam"],
  705. ?assert(ModTime =/= NewModTime).
  706. recompile_when_opts_change(Config) ->
  707. AppDir = ?config(apps, Config),
  708. Name = rebar_test_utils:create_random_name("app1_"),
  709. Vsn = rebar_test_utils:create_random_vsn(),
  710. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  711. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  712. EbinDir = filename:join([AppDir, "_build", "default", "lib", Name, "ebin"]),
  713. {ok, Files} = rebar_utils:list_dir(EbinDir),
  714. ModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  715. || F <- Files, filename:extension(F) == ".beam"],
  716. timer:sleep(1000),
  717. rebar_test_utils:create_config(AppDir, [{erl_opts, [{d, some_define}]}]),
  718. rebar_test_utils:run_and_check(Config, [{erl_opts, [{d, some_define}]}], ["compile"], {ok, [{app, Name}]}),
  719. {ok, NewFiles} = rebar_utils:list_dir(EbinDir),
  720. NewModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  721. || F <- NewFiles, filename:extension(F) == ".beam"],
  722. ?assert(ModTime =/= NewModTime).
  723. recompile_when_dag_opts_change(Config) ->
  724. AppDir = ?config(apps, Config),
  725. Name = rebar_test_utils:create_random_name("app1_"),
  726. Vsn = rebar_test_utils:create_random_vsn(),
  727. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  728. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  729. EbinDir = filename:join([AppDir, "_build", "default", "lib", Name, "ebin"]),
  730. {ok, Files} = rebar_utils:list_dir(EbinDir),
  731. Beams = [filename:join([EbinDir, F])
  732. || F <- Files, filename:extension(F) == ".beam"],
  733. ModTime = [filelib:last_modified(Beam) || Beam <- Beams],
  734. timer:sleep(1000),
  735. DepsDir = filename:join([AppDir, "_build", "default", "lib"]),
  736. G = rebar_compiler_dag:init(DepsDir, rebar_compiler_erl, "project_apps", []),
  737. %% change the config in the DAG...
  738. [digraph:add_vertex(G, Beam, {artifact, [{d, some_define}]}) || Beam <- Beams],
  739. digraph:add_vertex(G, '$r3_dirty_bit', true), % trigger a save
  740. rebar_compiler_dag:maybe_store(G, DepsDir, rebar_compiler_erl, "project_apps", []),
  741. rebar_compiler_dag:terminate(G),
  742. %% ... but don't change the actual rebar3 config...
  743. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  744. %% ... and checks that it rebuilds anyway due to DAG changes
  745. {ok, NewFiles} = rebar_utils:list_dir(EbinDir),
  746. NewBeams = [filename:join([EbinDir, F])
  747. || F <- NewFiles, filename:extension(F) == ".beam"],
  748. NewModTime = [filelib:last_modified(Beam) || Beam <- NewBeams],
  749. ?assert(ModTime =/= NewModTime).
  750. dont_recompile_when_opts_dont_change(Config) ->
  751. AppDir = ?config(apps, Config),
  752. Name = rebar_test_utils:create_random_name("app1_"),
  753. Vsn = rebar_test_utils:create_random_vsn(),
  754. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  755. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  756. EbinDir = filename:join([AppDir, "_build", "default", "lib", Name, "ebin"]),
  757. {ok, Files} = rebar_utils:list_dir(EbinDir),
  758. ModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  759. || F <- Files, filename:extension(F) == ".beam"],
  760. timer:sleep(1000),
  761. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  762. {ok, NewFiles} = rebar_utils:list_dir(EbinDir),
  763. NewModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  764. || F <- NewFiles, filename:extension(F) == ".beam"],
  765. ?assertEqual(ModTime, NewModTime).
  766. dont_recompile_yrl_or_xrl(Config) ->
  767. AppDir = ?config(apps, Config),
  768. Name = rebar_test_utils:create_random_name("app1_"),
  769. Vsn = rebar_test_utils:create_random_vsn(),
  770. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  771. Xrl = filename:join([AppDir, "src", "not_a_real_xrl_" ++ Name ++ ".xrl"]),
  772. ok = filelib:ensure_dir(Xrl),
  773. XrlBody =
  774. "Definitions."
  775. "\n\n"
  776. "D = [0-9]"
  777. "\n\n"
  778. "Rules."
  779. "\n\n"
  780. "{D}+ :"
  781. " {token,{integer,TokenLine,list_to_integer(TokenChars)}}."
  782. "\n\n"
  783. "{D}+\\.{D}+((E|e)(\\+|\\-)?{D}+)? :"
  784. " {token,{float,TokenLine,list_to_float(TokenChars)}}."
  785. "\n\n"
  786. "Erlang code.",
  787. ok = ec_file:write(Xrl, XrlBody),
  788. Yrl = filename:join([AppDir, "src", "not_a_real_yrl_" ++ Name ++ ".yrl"]),
  789. ok = filelib:ensure_dir(Yrl),
  790. YrlBody = ["Nonterminals E T F.\n"
  791. "Terminals '+' '*' '(' ')' number.\n"
  792. "Rootsymbol E.\n"
  793. "E -> E '+' T: {'$2', '$1', '$3'}.\n"
  794. "E -> T : '$1'.\n"
  795. "T -> T '*' F: {'$2', '$1', '$3'}.\n"
  796. "T -> F : '$1'.\n"
  797. "F -> '(' E ')' : '$2'.\n"
  798. "F -> number : '$1'.\n"],
  799. ok = ec_file:write(Yrl, YrlBody),
  800. XrlErl = filename:join([AppDir, "src", filename:basename(Xrl, ".xrl") ++ ".erl"]),
  801. YrlErl = filename:join([AppDir, "src", filename:basename(Yrl, ".yrl") ++ ".erl"]),
  802. EbinDir = filename:join([AppDir, "_build", "default", "lib", Name, "ebin"]),
  803. XrlBeam = filename:join([EbinDir, filename:basename(Xrl, ".xrl") ++ ".beam"]),
  804. YrlBeam = filename:join([EbinDir, filename:basename(Yrl, ".yrl") ++ ".beam"]),
  805. Hrl = filename:join([AppDir, "include", "some_header.hrl"]),
  806. ok = filelib:ensure_dir(Hrl),
  807. HrlBody = yeccpre_hrl(),
  808. ok = ec_file:write(Hrl, HrlBody),
  809. RebarConfig = [{yrl_opts, [{includefile, "include/some_header.hrl"}]}],
  810. rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], {ok, [{app, Name}]}),
  811. XrlModTime = filelib:last_modified(XrlErl),
  812. YrlModTime = filelib:last_modified(YrlErl),
  813. XrlBeamModTime = filelib:last_modified(XrlBeam),
  814. YrlBeamModTime = filelib:last_modified(YrlBeam),
  815. timer:sleep(1000),
  816. rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], {ok, [{app, Name}]}),
  817. NewXrlModTime = filelib:last_modified(XrlErl),
  818. NewYrlModTime = filelib:last_modified(YrlErl),
  819. NewXrlBeamModTime = filelib:last_modified(XrlBeam),
  820. NewYrlBeamModTime = filelib:last_modified(YrlBeam),
  821. ?assert(XrlBeamModTime == NewXrlBeamModTime),
  822. ?assert(YrlBeamModTime == NewYrlBeamModTime),
  823. ?assert(XrlModTime == NewXrlModTime),
  824. ?assert(YrlModTime == NewYrlModTime).
  825. delete_beam_if_source_deleted(Config) ->
  826. AppDir = ?config(apps, Config),
  827. Name = rebar_test_utils:create_random_name("app1_"),
  828. Vsn = rebar_test_utils:create_random_vsn(),
  829. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  830. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  831. EbinDir = filename:join([AppDir, "_build", "default", "lib", Name, "ebin"]),
  832. _SrcDir = filename:join([AppDir, "_build", "default", "lib", Name, "src"]),
  833. ?assert(filelib:is_regular(filename:join(EbinDir, "not_a_real_src_" ++ Name ++ ".beam"))),
  834. file:delete(filename:join([AppDir, "src", "not_a_real_src_" ++ Name ++ ".erl"])),
  835. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  836. ?assertNot(filelib:is_regular(filename:join(EbinDir, "not_a_real_src_" ++ Name ++ ".beam"))).
  837. deps_in_path(Config) ->
  838. AppDir = ?config(apps, Config),
  839. StartPaths = code:get_path(),
  840. Name = rebar_test_utils:create_random_name("app1_"),
  841. Vsn = rebar_test_utils:create_random_vsn(),
  842. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  843. DepName = rebar_test_utils:create_random_name("dep1_"),
  844. PkgName = rebar_test_utils:create_random_name("pkg1_"),
  845. mock_git_resource:mock([]),
  846. mock_pkg_resource:mock([
  847. {pkgdeps, [{{iolist_to_binary(PkgName), iolist_to_binary(Vsn)}, []}]}
  848. ]),
  849. RConfFile = rebar_test_utils:create_config(AppDir, [{deps, [
  850. {list_to_atom(DepName), {git, "http://site.com/user/"++DepName++".git", {tag, Vsn}}},
  851. {list_to_atom(PkgName), Vsn}
  852. ]}]),
  853. {ok, RConf} = file:consult(RConfFile),
  854. %% Make sure apps we look for are not visible
  855. %% Hope not to find src name
  856. ?assertEqual([], [Path || Path <- code:get_path(),
  857. {match, _} <- [re:run(Path, DepName)]]),
  858. %% Hope not to find pkg name in there
  859. ?assertEqual([], [Path || Path <- code:get_path(),
  860. {match, _} <- [re:run(Path, PkgName)]]),
  861. %% Build things
  862. {ok, State} = rebar_test_utils:run_and_check(
  863. Config, RConf, ["compile"],
  864. {ok, [{app, Name}, {dep, DepName}, {dep, PkgName}]}
  865. ),
  866. code:add_paths(rebar_state:code_paths(State, all_deps)),
  867. %% Find src name in there
  868. ?assertNotEqual([], [Path || Path <- code:get_path(),
  869. {match, _} <- [re:run(Path, DepName)]]),
  870. %% find pkg name in there
  871. ?assertNotEqual([], [Path || Path <- code:get_path(),
  872. {match, _} <- [re:run(Path, PkgName)]]),
  873. true = code:set_path(lists:filter(fun(P) -> ec_file:exists(P) end, StartPaths)),
  874. %% Make sure apps we look for are not visible again
  875. %% Hope not to find src name
  876. ?assertEqual([], [Path || Path <- code:get_path(),
  877. {match, _} <- [re:run(Path, DepName)]]),
  878. %% Hope not to find pkg name in there
  879. ?assertEqual([], [Path || Path <- code:get_path(),
  880. {match, _} <- [re:run(Path, PkgName)]]),
  881. %% Rebuild
  882. {ok, State1} = rebar_test_utils:run_and_check(
  883. Config, RConf, ["compile"],
  884. {ok, [{app, Name}, {dep, DepName}, {dep, PkgName}]}
  885. ),
  886. %% Find src name in there
  887. code:add_paths(rebar_state:code_paths(State1, all_deps)),
  888. ?assertNotEqual([], [Path || Path <- code:get_path(),
  889. {match, _} <- [re:run(Path, DepName)]]),
  890. %% find pkg name in there
  891. ?assertNotEqual([], [Path || Path <- code:get_path(),
  892. {match, _} <- [re:run(Path, PkgName)]]).
  893. checkout_priority(Config) ->
  894. AppDir = ?config(apps, Config),
  895. CheckoutsDir = ?config(checkouts, Config),
  896. StartPaths = code:get_path(),
  897. Name = rebar_test_utils:create_random_name("app1_"),
  898. Vsn = rebar_test_utils:create_random_vsn(),
  899. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  900. DepName = rebar_test_utils:create_random_name("dep1_"),
  901. PkgName = rebar_test_utils:create_random_name("pkg1_"),
  902. mock_git_resource:mock([]),
  903. mock_pkg_resource:mock([
  904. {pkgdeps, [{{iolist_to_binary(PkgName), iolist_to_binary(Vsn)}, []}]}
  905. ]),
  906. RConfFile = rebar_test_utils:create_config(AppDir, [{deps, [
  907. {list_to_atom(DepName), {git, "http://site.com/user/"++DepName++".git", {tag, Vsn}}},
  908. {list_to_atom(PkgName), Vsn}
  909. ]}]),
  910. {ok, RConf} = file:consult(RConfFile),
  911. %% Build with deps.
  912. rebar_test_utils:run_and_check(
  913. Config, RConf, ["compile"],
  914. {ok, [{app, Name}, {dep, DepName}, {dep, PkgName}]}
  915. ),
  916. %% Build two checkout apps similar to dependencies to be fetched,
  917. %% but on a different version
  918. Vsn2 = rebar_test_utils:create_random_vsn(),
  919. rebar_test_utils:create_app(filename:join([CheckoutsDir,DepName]), DepName, Vsn2, [kernel, stdlib]),
  920. rebar_test_utils:create_app(filename:join([CheckoutsDir,PkgName]), PkgName, Vsn2, [kernel, stdlib]),
  921. %% Rebuild and make sure the checkout apps are in path
  922. code:set_path(StartPaths),
  923. {ok, State} = rebar_test_utils:run_and_check(
  924. Config, RConf, ["compile"],
  925. {ok, [{app, Name}, {checkout, DepName}, {checkout, PkgName}]}
  926. ),
  927. code:add_paths(rebar_state:code_paths(State, all_deps)),
  928. [DepPath] = [Path || Path <- code:get_path(),
  929. {match, _} <- [re:run(Path, DepName)]],
  930. [PkgPath] = [Path || Path <- code:get_path(),
  931. {match, _} <- [re:run(Path, PkgName)]],
  932. {ok, [DepApp]} = file:consult(filename:join([DepPath, DepName ++ ".app"])),
  933. {ok, [PkgApp]} = file:consult(filename:join([PkgPath, PkgName ++ ".app"])),
  934. {application, _, DepProps} = DepApp,
  935. {application, _, PkgProps} = PkgApp,
  936. ?assertEqual(Vsn2, proplists:get_value(vsn, DepProps)),
  937. ?assertEqual(Vsn2, proplists:get_value(vsn, PkgProps)).
  938. highest_version_of_pkg_dep(Config) ->
  939. AppDir = ?config(apps, Config),
  940. Name = rebar_test_utils:create_random_name("app1_"),
  941. Vsn = rebar_test_utils:create_random_vsn(),
  942. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  943. PkgName = rebar_test_utils:create_random_name("pkg1_"),
  944. mock_git_resource:mock([]),
  945. mock_pkg_resource:mock([
  946. {pkgdeps, [{{iolist_to_binary(PkgName), <<"0.1.0">>}, []},
  947. {{iolist_to_binary(PkgName), <<"0.0.1">>}, []},
  948. {{iolist_to_binary(PkgName), <<"0.1.3">>}, []},
  949. {{iolist_to_binary(PkgName), <<"0.1.1">>}, []}]}
  950. ]),
  951. RConfFile = rebar_test_utils:create_config(AppDir, [{deps, [list_to_atom(PkgName)]}]),
  952. {ok, RConf} = file:consult(RConfFile),
  953. %% Build with deps.
  954. rebar_test_utils:run_and_check(
  955. Config, RConf, ["compile"],
  956. {ok, [{app, Name}, {dep, PkgName, <<"0.1.3">>}]}
  957. ).
  958. parse_transform_test(Config) ->
  959. AppDir = ?config(apps, Config),
  960. RebarConfig = [{erl_opts, [{parse_transform, pascal}]}],
  961. Name = rebar_test_utils:create_random_name("app1_"),
  962. Vsn = rebar_test_utils:create_random_vsn(),
  963. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  964. ExtraSrc = <<"-module(pascal). "
  965. "-export([parse_transform/2]). "
  966. "parse_transform(Forms, _Options) -> "
  967. "Forms.">>,
  968. ok = file:write_file(filename:join([AppDir, "src", "pascal.erl"]), ExtraSrc),
  969. rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], {ok, [{app, Name}]}),
  970. EbinDir = filename:join([AppDir, "_build", "default", "lib", Name, "ebin"]),
  971. true = filelib:is_file(filename:join([EbinDir, "pascal.beam"])).
  972. erl_first_files_test(Config) ->
  973. AppDir = ?config(apps, Config),
  974. RebarConfig = [{erl_opts, [{parse_transform, mark_time}]},
  975. {erl_first_files, ["src/mark_time.erl",
  976. "src/b.erl",
  977. "src/d.erl",
  978. "src/a.erl"]}],
  979. Name = rebar_test_utils:create_random_name("app1_"),
  980. Vsn = rebar_test_utils:create_random_vsn(),
  981. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  982. rebar_test_utils:write_src_file(AppDir, "a.erl"),
  983. rebar_test_utils:write_src_file(AppDir, "b.erl"),
  984. rebar_test_utils:write_src_file(AppDir, "d.erl"),
  985. rebar_test_utils:write_src_file(AppDir, "e.erl"),
  986. ExtraSrc = <<"-module(mark_time). "
  987. "-export([parse_transform/2]). "
  988. "parse_transform([Form={attribute,_,module,Mod}|Forms], Options) -> "
  989. " [Form, {attribute,1,number, os:timestamp()} | Forms];"
  990. "parse_transform([Form|Forms], Options) -> "
  991. " [Form | parse_transform(Forms, Options)].">>,
  992. ok = file:write_file(filename:join([AppDir, "src", "mark_time.erl"]), ExtraSrc),
  993. rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], {ok, [{app, Name}]}),
  994. EbinDir = filename:join([AppDir, "_build", "default", "lib", Name, "ebin"]),
  995. true = filelib:is_file(filename:join([EbinDir, "mark_time.beam"])),
  996. code:load_abs(filename:join([EbinDir, "a"])),
  997. code:load_abs(filename:join([EbinDir, "b"])),
  998. code:load_abs(filename:join([EbinDir, "d"])),
  999. code:load_abs(filename:join([EbinDir, "e"])),
  1000. A = proplists:get_value(number, a:module_info(attributes)),
  1001. B = proplists:get_value(number, b:module_info(attributes)),
  1002. D = proplists:get_value(number, d:module_info(attributes)),
  1003. E = proplists:get_value(number, e:module_info(attributes)),
  1004. ?assertEqual([B,D,A,E], lists:sort([A,B,D,E])).
  1005. mib_test(Config) ->
  1006. AppDir = ?config(apps, Config),
  1007. RebarConfig = [{mib_first_files, ["mibs/SIMPLE-MIB.mib"]}],
  1008. Name = rebar_test_utils:create_random_name("app1_"),
  1009. Vsn = rebar_test_utils:create_random_vsn(),
  1010. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  1011. MibsSrc = <<"-- SIMPLE-MIB.\n"
  1012. "-- This is just a simple MIB used for testing!\n"
  1013. "--\n"
  1014. "SIMPLE-MIB DEFINITIONS ::= BEGIN\n"
  1015. "IMPORTS\n"
  1016. " MODULE-IDENTITY, enterprises\n"
  1017. " FROM SNMPv2-SMI;\n"
  1018. "\n"
  1019. "ericsson MODULE-IDENTITY\n"
  1020. " LAST-UPDATED\n"
  1021. " \"201403060000Z\"\n"
  1022. " ORGANIZATION\n"
  1023. " \"rebar\"\n"
  1024. " CONTACT-INFO\n"
  1025. " \"rebar <rebar@example.com>\n"
  1026. " or\n"
  1027. " whoever is currently responsible for the SIMPLE\n"
  1028. " enterprise MIB tree branch (enterprises.999).\"\n"
  1029. " DESCRIPTION\n"
  1030. " \"This very small module is made available\n"
  1031. " for mib-compilation testing.\"\n"
  1032. " ::= { enterprises 999 }\n"
  1033. "END\n">>,
  1034. ok = filelib:ensure_dir(filename:join([AppDir, "mibs", "dummy"])),
  1035. ok = file:write_file(filename:join([AppDir, "mibs", "SIMPLE-MIB.mib"]), MibsSrc),
  1036. rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], {ok, [{app, Name}]}),
  1037. %% check a bin corresponding to the mib in the mibs dir exists in priv/mibs
  1038. PrivMibsDir = filename:join([AppDir, "_build", "default", "lib", Name, "priv", "mibs"]),
  1039. true = filelib:is_file(filename:join([PrivMibsDir, "SIMPLE-MIB.bin"])),
  1040. %% check a hrl corresponding to the mib in the mibs dir exists in include
  1041. true = filelib:is_file(filename:join([AppDir, "include", "SIMPLE-MIB.hrl"])),
  1042. %% check the mibs dir was linked into the _build dir
  1043. true = filelib:is_dir(filename:join([AppDir, "_build", "default", "lib", Name, "mibs"])).
  1044. umbrella_mib_first_test(Config) ->
  1045. AppsDir = ?config(apps, Config),
  1046. Name = rebar_test_utils:create_random_name("app1_"),
  1047. Vsn = rebar_test_utils:create_random_vsn(),
  1048. AppDir = filename:join([AppsDir, "apps", Name]),
  1049. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  1050. BExporterSrc = <<"-- BEXPORTER-MIB.\n"
  1051. "-- This is just a simple MIB used for testing!\n"
  1052. "--\n"
  1053. "BEXPORTER-MIB DEFINITIONS ::= BEGIN\n"
  1054. "IMPORTS\n"
  1055. " TEXTUAL-CONVENTION\n"
  1056. " FROM SNMPv2-TC\n"
  1057. " MODULE-IDENTITY, enterprises\n"
  1058. " FROM SNMPv2-SMI;\n"
  1059. "\n"
  1060. "ericsson MODULE-IDENTITY\n"
  1061. " LAST-UPDATED\n"
  1062. " \"201812050000Z\"\n"
  1063. " ORGANIZATION\n"
  1064. " \"rebar\"\n"
  1065. " CONTACT-INFO\n"
  1066. " \"rebar <rebar@example.com>\n"
  1067. " or\n"
  1068. " whoever is currently responsible for the SIMPLE\n"
  1069. " enterprise MIB tree branch (enterprises.999).\"\n"
  1070. " DESCRIPTION\n"
  1071. " \"This very small module is made available\n"
  1072. " for mib-compilation testing.\"\n"
  1073. " ::= { enterprises 999 }\n"
  1074. "\n"
  1075. "Something ::= TEXTUAL-CONVENTION\n"
  1076. " STATUS current\n"
  1077. " DESCRIPTION \"\"\n"
  1078. " SYNTAX OCTET STRING (SIZE (4))\n"
  1079. "END\n">>,
  1080. AImporterSrc = <<"-- AIMPORTER-MIB.\n"
  1081. "-- This is just a simple MIB used for testing!\n"
  1082. "--\n"
  1083. "AIMPORTER-MIB DEFINITIONS ::= BEGIN\n"
  1084. "IMPORTS\n"
  1085. " Something\n"
  1086. " FROM BEXPORTER-MIB\n"
  1087. " MODULE-IDENTITY, enterprises\n"
  1088. " FROM SNMPv2-SMI;\n"
  1089. "\n"
  1090. "ericsson MODULE-IDENTITY\n"
  1091. " LAST-UPDATED\n"
  1092. " \"201812050000Z\"\n"
  1093. " ORGANIZATION\n"
  1094. " \"rebar\"\n"
  1095. " CONTACT-INFO\n"
  1096. " \"rebar <rebar@example.com>\n"
  1097. " or\n"
  1098. " whoever is currently responsible for the SIMPLE\n"
  1099. " enterprise MIB tree branch (enterprises.999).\"\n"
  1100. " DESCRIPTION\n"
  1101. " \"This very small module is made available\n"
  1102. " for mib-compilation testing.\"\n"
  1103. " ::= { enterprises 1000 }\n"
  1104. "END\n">>,
  1105. ok = filelib:ensure_dir(filename:join([AppDir, "mibs", "dummy"])),
  1106. ok = file:write_file(filename:join([AppDir, "mibs", "AIMPORTER-MIB.mib"]), AImporterSrc),
  1107. ok = file:write_file(filename:join([AppDir, "mibs", "BEXPORTER-MIB.mib"]), BExporterSrc),
  1108. FailureRebarConfig = [{mib_first_files, ["mibs/AIMPORTER-MIB.mib"]}],
  1109. SuccessRebarConfig = [{mib_first_files, ["mibs/BEXPORTER-MIB.mib"]}],
  1110. PrivMibsDir = filename:join([AppsDir, "_build", "default", "lib", Name, "priv", "mibs"]),
  1111. FailureRebarConfig = [{mib_first_files, ["mibs/AIMPORTER-MIB.mib"]}],
  1112. catch (
  1113. rebar_test_utils:run_and_check(Config, FailureRebarConfig, ["compile"], {ok, [{app, Name}]}) ),
  1114. %% check that the bin file was NOT cretated
  1115. false = filelib:is_file(filename:join([PrivMibsDir, "AIMPORTER-MIB.bin"])),
  1116. SuccessRebarConfig = [{mib_first_files, ["mibs/BEXPORTER-MIB.mib"]}],
  1117. rebar_test_utils:run_and_check(Config, SuccessRebarConfig, ["compile"], {ok, [{app, Name}]}),
  1118. %% check a bin corresponding to the mib in the mibs dir exists in priv/mibs
  1119. true = filelib:is_file(filename:join([PrivMibsDir, "AIMPORTER-MIB.bin"])),
  1120. %% check a hrl corresponding to the mib in the mibs dir exists in include
  1121. true = filelib:is_file(filename:join([AppDir, "include", "AIMPORTER-MIB.hrl"])),
  1122. %% check the mibs dir was linked into the _build dir
  1123. true = filelib:is_dir(filename:join([AppsDir, "_build", "default", "lib", Name, "mibs"])).
  1124. deps_mib_test() ->
  1125. [{doc, "reproduces the dependency handling required for the issue "
  1126. "reported in https://github.com/erlang/rebar3/issues/2372"}].
  1127. deps_mib_test(Config) ->
  1128. Priv = ?config(priv_dir, Config),
  1129. CliLvl2Mib =
  1130. "---\n"
  1131. "---\n"
  1132. "---\n"
  1133. "LVL2-MIB DEFINITIONS ::= BEGIN\n"
  1134. "IMPORTS\n"
  1135. " MODULE-IDENTITY, OBJECT-TYPE\n"
  1136. " FROM SNMPv2-SMI\n"
  1137. " lvlModules, lvlApplications\n"
  1138. " FROM LVL0-REG\n"
  1139. " LvlFoo\n"
  1140. " FROM LVL0-TC\n"
  1141. " ;\n"
  1142. "\n"
  1143. "lvl2Module MODULE-IDENTITY\n"
  1144. " LAST-UPDATED \"202009261630Z\"\n"
  1145. " ORGANIZATION \"'some org'\"\n"
  1146. " CONTACT-INFO \"'Contact: some contact'\"\n"
  1147. " DESCRIPTION\n"
  1148. " \" \"\n"
  1149. " ::= { lvlModules 3 }\n"
  1150. "\n\n\n"
  1151. "END",
  1152. CpiLvl0Mib1 =
  1153. " \n"
  1154. "---\n"
  1155. "---\n\n"
  1156. "LVL0-TC DEFINITIONS ::= BEGIN\n"
  1157. "IMPORTS\n"
  1158. " lvlModules\n"
  1159. " FROM LVL0-REG\n"
  1160. " MODULE-IDENTITY\n"
  1161. " FROM SNMPv2-SMI\n"
  1162. " TEXTUAL-CONVENTION\n"
  1163. " FROM SNMPv2-TC\n"
  1164. " ;\n\n"
  1165. "lvlTcModule MODULE-IDENTITY\n"
  1166. " LAST-UPDATED \"202009261630Z\"\n"
  1167. " ORGANIZATION \"'some org'\"\n"
  1168. " CONTACT-INFO \"'Contact: some contact'\"\n"
  1169. " DESCRIPTION\n"
  1170. " \" This MIB is part of the LVL MIB. It defines common\n"
  1171. " Textual Conventions used in other LVL mib modules.\"\n"
  1172. " ::= { lvlModules 2 }\n\n\n"
  1173. "LvlFoo ::= TEXTUAL-CONVENTION\n"
  1174. " DISPLAY-HINT \"512a\"\n"
  1175. " STATUS current\n"
  1176. " DESCRIPTION \"\"\n"
  1177. " SYNTAX OCTET STRING (SIZE (1..512))\n\n"
  1178. "LvlEnum ::= TEXTUAL-CONVENTION\n"
  1179. " STATUS current\n"
  1180. " DESCRIPTION \"\"\n"
  1181. " SYNTAX INTEGER { foo(1), bar(2), baz(3) }\n\n"
  1182. "END\n",
  1183. CpiLvl0Mib2 =
  1184. "---\n---\n---\n\n"
  1185. "LVL0-REG DEFINITIONS ::= BEGIN\n"
  1186. "\n"
  1187. "IMPORTS\n"
  1188. " MODULE-IDENTITY, enterprises\n"
  1189. " FROM SNMPv2-SMI\n"
  1190. " ;\n\n"
  1191. "lvlRegModule MODULE-IDENTITY\n"
  1192. " LAST-UPDATED \"202009261630Z\"\n"
  1193. " ORGANIZATION \"'some org'\"\n"
  1194. " CONTACT-INFO \"'Contact: some contact'\"\n"
  1195. " DESCRIPTION\n"
  1196. " \"The root MIB module for LVL\"\n"
  1197. " ::= { lvlModules 1 }\n\n"
  1198. "-- Example Enterprise Number for Documentation use\n"
  1199. "example OBJECT IDENTIFIER ::= { enterprises 32473 }\n"
  1200. "\n"
  1201. "lvl OBJECT IDENTIFIER ::= { example 1 }\n\n"
  1202. "-- sub-tree for registrations (Modules information)\n"
  1203. "lvlReg OBJECT IDENTIFIER ::= { lvl 1 }\n"
  1204. "lvlModules OBJECT IDENTIFIER ::= { lvlReg 1 }\n"
  1205. "\n"
  1206. "-- the application subtree\n"
  1207. "lvlApplications OBJECT IDENTIFIER ::= { lvl 2 }\n"
  1208. "END\n",
  1209. CliLvl2Path = filename:join([Priv, "deps_mib", "cli_lvl2", "mibs", "LVL2-MIB.mib"]),
  1210. CpiLvl0Path1 = filename:join([Priv, "deps_mib", "cpi_lvl0", "mibs", "LVL0-TC.mib"]),
  1211. CpiLvl0Path2 = filename:join([Priv, "deps_mib", "cpi_lvl0", "mibs", "LVL0-REG.mib"]),
  1212. NprLvl1Path = filename:join([Priv, "deps_mib", "npr_lvl1", "mibs"]), % no mibs dir
  1213. FakeLvl1Path = filename:join([Priv, "deps_mib", "fake_lvl1", "mibs", "no_file"]), % no mibs file
  1214. ok = filelib:ensure_dir(CliLvl2Path),
  1215. ok = filelib:ensure_dir(CpiLvl0Path1), % CpiLvl0Path2 is the same dir
  1216. ok = filelib:ensure_dir(NprLvl1Path),
  1217. ok = filelib:ensure_dir(FakeLvl1Path),
  1218. ok = file:write_file(CliLvl2Path, CliLvl2Mib),
  1219. ok = file:write_file(CpiLvl0Path1, CpiLvl0Mib1),
  1220. ok = file:write_file(CpiLvl0Path2, CpiLvl0Mib2),
  1221. Res = rebar_compiler_mib:dependencies(
  1222. CliLvl2Path,
  1223. filename:dirname(CliLvl2Path),
  1224. [filename:dirname(CliLvl2Path),
  1225. filename:dirname(CpiLvl0Path1),
  1226. NprLvl1Path,
  1227. filename:dirname(FakeLvl1Path)]
  1228. ),
  1229. ?assertEqual(lists:sort([CpiLvl0Path1, CpiLvl0Path2]),
  1230. lists:sort(Res)),
  1231. ok.
  1232. only_default_transitive_deps(Config) ->
  1233. AppDir = ?config(apps, Config),
  1234. Name = rebar_test_utils:create_random_name("app1_"),
  1235. Vsn = rebar_test_utils:create_random_vsn(),
  1236. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  1237. GitDeps = rebar_test_utils:expand_deps(git, [{"a", "1.0.0", []}]),
  1238. PkgName = rebar_test_utils:create_random_name("pkg1_"),
  1239. {SrcDeps, _} = rebar_test_utils:flat_deps(GitDeps),
  1240. mock_git_resource:mock([{deps, SrcDeps},
  1241. {config, [{profiles, [{test, [{deps, [list_to_atom(PkgName)]}]}]}]}]),
  1242. mock_pkg_resource:mock([{pkgdeps, [{{iolist_to_binary(PkgName), <<"0.1.0">>}, []}]}]),
  1243. Deps = rebar_test_utils:top_level_deps(GitDeps),
  1244. RConfFile = rebar_test_utils:create_config(AppDir, [{deps, Deps}]),
  1245. {ok, RConf} = file:consult(RConfFile),
  1246. %% Build with deps.
  1247. rebar_test_utils:run_and_check(
  1248. Config, RConf, ["as", "test", "compile"],
  1249. {ok, [{app, Name}, {dep, "a", <<"1.0.0">>}, {dep_not_exist, PkgName}]}
  1250. ).
  1251. clean_all(Config) ->
  1252. AppDir = ?config(apps, Config),
  1253. Name = rebar_test_utils:create_random_name("app1_"),
  1254. Vsn = rebar_test_utils:create_random_vsn(),
  1255. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  1256. DepName = rebar_test_utils:create_random_name("dep1_"),
  1257. PkgName = rebar_test_utils:create_random_name("pkg1_"),
  1258. mock_git_resource:mock([]),
  1259. mock_pkg_resource:mock([
  1260. {pkgdeps, [{{iolist_to_binary(PkgName), iolist_to_binary(Vsn)}, []}]}
  1261. ]),
  1262. RConfFile = rebar_test_utils:create_config(AppDir, [{deps, [
  1263. {list_to_atom(DepName), {git, "http://site.com/user/"++DepName++".git", {tag, Vsn}}},
  1264. {list_to_atom(PkgName), Vsn}
  1265. ]}]),
  1266. {ok, RConf} = file:consult(RConfFile),
  1267. %% Build things
  1268. rebar_test_utils:run_and_check(
  1269. Config, RConf, ["compile"],
  1270. {ok, [{app, Name}, {app, DepName}, {app, PkgName}]}
  1271. ),
  1272. %% Clean all
  1273. rebar_test_utils:run_and_check(Config, [], ["clean", "--all"],
  1274. {ok, [{app, Name, invalid},
  1275. {app, DepName, invalid},
  1276. {app, PkgName, invalid}]}).
  1277. clean_specific(Config) ->
  1278. AppDir = ?config(apps, Config),
  1279. Name = rebar_test_utils:create_random_name("app1_"),
  1280. Vsn = rebar_test_utils:create_random_vsn(),
  1281. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  1282. DepName = rebar_test_utils:create_random_name("dep1_"),
  1283. PkgName = rebar_test_utils:create_random_name("pkg1_"),
  1284. mock_git_resource:mock([]),
  1285. mock_pkg_resource:mock([
  1286. {pkgdeps, [{{iolist_to_binary(PkgName), iolist_to_binary(Vsn)}, []}]}
  1287. ]),
  1288. RConfFile = rebar_test_utils:create_config(AppDir, [{deps, [
  1289. {list_to_atom(DepName), {git, "http://site.com/user/"++DepName++".git", {tag, Vsn}}},
  1290. {list_to_atom(PkgName), Vsn}
  1291. ]}]),
  1292. {ok, RConf} = file:consult(RConfFile),
  1293. %% Build things
  1294. rebar_test_utils:run_and_check(
  1295. Config, RConf, ["compile"],
  1296. {ok, [{app, Name}, {app, DepName}, {app, PkgName}]}
  1297. ),
  1298. %% Clean all
  1299. rebar_test_utils:run_and_check(Config, [], ["clean", "--apps="++DepName++","++Name],
  1300. {ok, [{app, Name, invalid},
  1301. {app, DepName, invalid},
  1302. {app, PkgName, valid}]}).
  1303. override_deps(Config) ->
  1304. Deps = rebar_test_utils:expand_deps(git, [{"some_dep", "0.0.1", [{"other_dep", "0.0.1", []}]}]),
  1305. TopDeps = rebar_test_utils:top_level_deps(Deps),
  1306. {SrcDeps, _} = rebar_test_utils:flat_deps(Deps),
  1307. mock_git_resource:mock([{deps, SrcDeps}]),
  1308. RebarConfig = [
  1309. {deps, TopDeps},
  1310. {overrides, [
  1311. {override, some_dep, [
  1312. {deps, []}
  1313. ]}
  1314. ]}
  1315. ],
  1316. rebar_test_utils:run_and_check(
  1317. Config, RebarConfig, ["compile"],
  1318. {ok, [{dep, "some_dep"},
  1319. {dep_not_exist, "other_dep"}]}
  1320. ).
  1321. git_subdir_deps(Config) ->
  1322. Deps = rebar_test_utils:expand_deps(git_subdir, [{"some_dep", "0.0.1", [{"other_dep", "0.0.1", []}]}]),
  1323. TopDeps = rebar_test_utils:top_level_deps(Deps),
  1324. {SrcDeps, _} = rebar_test_utils:flat_deps(Deps),
  1325. mock_git_subdir_resource:mock([{deps, SrcDeps}]),
  1326. RebarConfig = [
  1327. {deps, TopDeps}
  1328. ],
  1329. rebar_test_utils:run_and_check(
  1330. Config, RebarConfig, ["compile"],
  1331. {ok, [{subdir_dep, "some_dep"},
  1332. {subdir_dep, "other_dep"}]}
  1333. ).
  1334. override_add_deps(Config) ->
  1335. Deps = rebar_test_utils:expand_deps(git, [{"some_dep", "0.0.1", [{"other_dep", "0.0.1", []}]}]),
  1336. TopDeps = rebar_test_utils:top_level_deps(Deps),
  1337. DepA = {dep_a, "0.0.1", {git, "http://site.com/dep_a.git", {tag, "0.0.1"}}},
  1338. DepB = {dep_b, "0.0.1", {git, "http://site.com/dep_b.git", {tag, "0.0.1"}}},
  1339. DepC = {dep_c, "0.0.1", {git, "http://site.com/dep_c.git", {tag, "0.0.1"}}},
  1340. {SrcDeps, _} = rebar_test_utils:flat_deps(Deps),
  1341. mock_git_resource:mock([{deps, [DepA, DepB, DepC | SrcDeps]}]),
  1342. RebarConfig = [
  1343. {deps, TopDeps},
  1344. {overrides, [
  1345. {add, some_dep, [
  1346. {deps, [DepA, DepB]}
  1347. ]},
  1348. {add, [
  1349. {deps, [DepC]}
  1350. ]}
  1351. ]}
  1352. ],
  1353. rebar_test_utils:run_and_check(
  1354. Config, RebarConfig, ["compile"],
  1355. {ok, [{dep, "some_dep"},
  1356. {dep, "other_dep"},
  1357. {dep, "dep_a"},
  1358. {dep, "dep_b"},
  1359. {dep, "dep_c"}]}
  1360. ).
  1361. override_del_deps(Config) ->
  1362. Deps = rebar_test_utils:expand_deps(git, [{"some_dep", "0.0.1", [{"dep_a", "0.0.1", []},
  1363. {"dep_b", "0.0.1", []},
  1364. {"dep_c", "0.0.1", []}]},
  1365. {"other_dep", "0.0.1", [{"dep_c", "0.0.1", []},
  1366. {"dep_d", "0.0.1", []}]}]),
  1367. TopDeps = rebar_test_utils:top_level_deps(Deps),
  1368. DepA = {dep_a, "0.0.1", {git, "https://example.org/user/dep_a.git", {tag, "0.0.1"}}},
  1369. DepB = {dep_b, "0.0.1", {git, "https://example.org/user/dep_b.git", {tag, "0.0.1"}}},
  1370. DepC = {dep_c, "0.0.1", {git, "https://example.org/user/dep_c.git", {tag, "0.0.1"}}},
  1371. {SrcDeps, _} = rebar_test_utils:flat_deps(Deps),
  1372. mock_git_resource:mock([{deps, SrcDeps}]),
  1373. RebarConfig = [
  1374. {deps, TopDeps},
  1375. {overrides, [
  1376. {del, some_dep, [
  1377. {deps, [DepA, DepB]}
  1378. ]},
  1379. {del, [
  1380. {deps, [DepC]}
  1381. ]}
  1382. ]}
  1383. ],
  1384. rebar_test_utils:run_and_check(
  1385. Config, RebarConfig, ["compile"],
  1386. {ok, [{dep, "some_dep"},
  1387. {dep, "other_dep"},
  1388. {dep_not_exist, "dep_a"},
  1389. {dep_not_exist, "dep_b"},
  1390. {dep_not_exist, "dep_c"},
  1391. {dep, "dep_d"}]}
  1392. ).
  1393. override_del_pkg_deps(Config) ->
  1394. Deps = rebar_test_utils:expand_deps(pkg, [{"some_dep", "0.0.1", [{"other_dep", "0.0.1", []}]}]),
  1395. TopDeps = rebar_test_utils:top_level_deps(Deps),
  1396. {_, PkgDeps} = rebar_test_utils:flat_deps(Deps),
  1397. mock_pkg_resource:mock([{pkgdeps, PkgDeps}]),
  1398. RebarConfig = [
  1399. {deps, TopDeps},
  1400. {overrides, [
  1401. {del, some_dep, [
  1402. {deps, [other_dep]},
  1403. %% regression: a non-existing option deletion
  1404. %% could trigger an infinite loop
  1405. {provider_hooks, [{post, [{compile, xref}]}]}
  1406. ]}
  1407. ]}
  1408. ],
  1409. rebar_test_utils:run_and_check(
  1410. Config, RebarConfig, ["compile"],
  1411. {ok, [{dep, "some_dep"},
  1412. {dep_not_exist, "other_dep"}]}
  1413. ).
  1414. override_opts(Config) ->
  1415. AppsDir = ?config(apps, Config),
  1416. Name = rebar_test_utils:create_random_name("app1_"),
  1417. Vsn = rebar_test_utils:create_random_vsn(),
  1418. AppDir = filename:join([AppsDir, "apps", Name]),
  1419. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  1420. RebarConfig = [
  1421. {erl_opts, [
  1422. compressed,
  1423. warn_missing_spec
  1424. ]},
  1425. {overrides, [
  1426. {override, [
  1427. {erl_opts, [compressed]}
  1428. ]}
  1429. ]}
  1430. ],
  1431. rebar_test_utils:create_config(AppsDir, RebarConfig),
  1432. rebar_test_utils:run_and_check(
  1433. Config, RebarConfig, ["compile"], {ok, [{app, Name}]}),
  1434. Path = filename:join([AppsDir, "_build", "default", "lib", Name, "ebin"]),
  1435. code:add_patha(Path),
  1436. Mod = list_to_atom("not_a_real_src_" ++ Name),
  1437. true = lists:member(compressed, proplists:get_value(options, Mod:module_info(compile), [])),
  1438. false = lists:member(warn_missing_spec, proplists:get_value(options, Mod:module_info(compile), [])).
  1439. %% test for fix of https://github.com/erlang/rebar3/issues/1801
  1440. %% only apply overrides once
  1441. %% verify by having an override add the macro TEST to the dep some_dep
  1442. %% building under `ct` will fail if the `add` is applied more than once
  1443. apply_overrides_exactly_once(Config) ->
  1444. AppDir = ?config(apps, Config),
  1445. Deps = rebar_test_utils:expand_deps(git, [{"some_dep", "0.0.1", [{"other_dep", "0.0.1", []}]}]),
  1446. TopDeps = rebar_test_utils:top_level_deps(Deps),
  1447. {SrcDeps, _} = rebar_test_utils:flat_deps(Deps),
  1448. mock_git_resource:mock([{deps, SrcDeps}]),
  1449. Name = rebar_test_utils:create_random_name("app1_"),
  1450. Vsn = rebar_test_utils:create_random_vsn(),
  1451. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  1452. RebarConfig = [{deps, TopDeps},
  1453. {overrides, [
  1454. {add, some_dep, [
  1455. {erl_opts, [{d, 'TEST'}]}
  1456. ]}
  1457. ]}],
  1458. rebar_test_utils:create_config(AppDir, RebarConfig),
  1459. rebar_test_utils:run_and_check(
  1460. Config, RebarConfig, ["ct", "--compile_only"], {ok, [{app, Name}, {dep, "some_dep"}], "test"}).
  1461. override_only_deps(Config) ->
  1462. AppDir = ?config(apps, Config),
  1463. Name = rebar_test_utils:create_random_name("app1_"),
  1464. Vsn = rebar_test_utils:create_random_vsn(),
  1465. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  1466. RebarConfig = [
  1467. {deps, []}, %% with deps enabled, this test fails
  1468. {overrides, [
  1469. {add, [
  1470. {erl_opts, [{d, bad, a}, {d, bad, b}]}
  1471. ]}
  1472. ]}
  1473. ],
  1474. rebar_test_utils:create_config(AppDir, RebarConfig),
  1475. rebar_test_utils:run_and_check(
  1476. Config, RebarConfig, ["compile"], {ok, [{app, Name}]}),
  1477. ok.
  1478. override_add_opts(Config) ->
  1479. AppsDir = ?config(apps, Config),
  1480. Name = rebar_test_utils:create_random_name("app1_"),
  1481. Vsn = rebar_test_utils:create_random_vsn(),
  1482. AppDir = filename:join([AppsDir, "apps", Name]),
  1483. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  1484. RebarConfig = [
  1485. {erl_opts, [
  1486. warn_missing_spec
  1487. ]},
  1488. {overrides, [
  1489. {add, [
  1490. {erl_opts, [compressed]}
  1491. ]}
  1492. ]}
  1493. ],
  1494. rebar_test_utils:create_config(AppsDir, RebarConfig),
  1495. rebar_test_utils:run_and_check(
  1496. Config, RebarConfig, ["compile"], {ok, [{app, Name}]}),
  1497. Path = filename:join([AppsDir, "_build", "default", "lib", Name, "ebin"]),
  1498. code:add_patha(Path),
  1499. Mod = list_to_atom("not_a_real_src_" ++ Name),
  1500. true = lists:member(compressed, proplists:get_value(options, Mod:module_info(compile), [])),
  1501. true = lists:member(warn_missing_spec, proplists:get_value(options, Mod:module_info(compile), [])).
  1502. override_del_opts(Config) ->
  1503. AppsDir = ?config(apps, Config),
  1504. Name = rebar_test_utils:create_random_name("app1_"),
  1505. Vsn = rebar_test_utils:create_random_vsn(),
  1506. AppDir = filename:join([AppsDir, "apps", Name]),
  1507. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  1508. RebarConfig = [
  1509. {erl_opts, [
  1510. compressed,
  1511. warn_missing_spec
  1512. ]},
  1513. {overrides, [
  1514. {del, [
  1515. {erl_opts, [warn_missing_spec]}
  1516. ]}
  1517. ]}
  1518. ],
  1519. rebar_test_utils:create_config(AppsDir, RebarConfig),
  1520. rebar_test_utils:run_and_check(
  1521. Config, RebarConfig, ["compile"], {ok, [{app, Name}]}),
  1522. Path = filename:join([AppsDir, "_build", "default", "lib", Name, "ebin"]),
  1523. code:add_patha(Path),
  1524. Mod = list_to_atom("not_a_real_src_" ++ Name),
  1525. true = lists:member(compressed, proplists:get_value(options, Mod:module_info(compile), [])),
  1526. false = lists:member(warn_missing_spec, proplists:get_value(options, Mod:module_info(compile), [])),
  1527. ok.
  1528. profile_override_deps(Config) ->
  1529. Deps = rebar_test_utils:expand_deps(git, [{"some_dep", "0.0.1", [{"other_dep", "0.0.1", []}]}]),
  1530. TopDeps = rebar_test_utils:top_level_deps(Deps),
  1531. {SrcDeps, _} = rebar_test_utils:flat_deps(Deps),
  1532. mock_git_resource:mock([{deps, SrcDeps}]),
  1533. RebarConfig = [
  1534. {deps, TopDeps},
  1535. {profiles, [
  1536. {a, [
  1537. {overrides, [
  1538. {override, some_dep, [
  1539. {deps, []}
  1540. ]}
  1541. ]}
  1542. ]}
  1543. ]}],
  1544. rebar_test_utils:run_and_check(
  1545. Config, RebarConfig, ["as", "a", "compile"],
  1546. {ok, [{dep, "some_dep"},
  1547. {dep_not_exist, "other_dep"}]}
  1548. ).
  1549. profile_override_add_deps(Config) ->
  1550. Deps = rebar_test_utils:expand_deps(git, [{"some_dep", "0.0.1", [{"other_dep", "0.0.1", []}]}]),
  1551. TopDeps = rebar_test_utils:top_level_deps(Deps),
  1552. DepA = {dep_a, "0.0.1", {git, "http://site.com/dep_a.git", {tag, "0.0.1"}}},
  1553. DepB = {dep_b, "0.0.1", {git, "http://site.com/dep_b.git", {tag, "0.0.1"}}},
  1554. DepC = {dep_c, "0.0.1", {git, "http://site.com/dep_c.git", {tag, "0.0.1"}}},
  1555. {SrcDeps, _} = rebar_test_utils:flat_deps(Deps),
  1556. mock_git_resource:mock([{deps, [DepA, DepB, DepC | SrcDeps]}]),
  1557. RebarConfig = [
  1558. {deps, TopDeps},
  1559. {profiles, [
  1560. {a, [
  1561. {overrides, [
  1562. {add, some_dep, [
  1563. {deps, [DepA, DepB]}
  1564. ]},
  1565. {add, [
  1566. {deps, [DepC]}
  1567. ]}
  1568. ]}
  1569. ]}
  1570. ]}
  1571. ],
  1572. rebar_test_utils:run_and_check(
  1573. Config, RebarConfig, ["as", "a", "compile"],
  1574. {ok, [{dep, "some_dep"},
  1575. {dep, "other_dep"},
  1576. {dep, "dep_a"},
  1577. {dep, "dep_b"},
  1578. {dep, "dep_c"}]}
  1579. ).
  1580. profile_override_del_deps(Config) ->
  1581. Deps = rebar_test_utils:expand_deps(git, [{"some_dep", "0.0.1", [{"dep_a", "0.0.1", []},
  1582. {"dep_b", "0.0.1", []},
  1583. {"dep_c", "0.0.1", []}]},
  1584. {"other_dep", "0.0.1", [{"dep_c", "0.0.1", []},
  1585. {"dep_d", "0.0.1", []}]}]),
  1586. TopDeps = rebar_test_utils:top_level_deps(Deps),
  1587. DepA = {dep_a, "0.0.1", {git, "https://example.org/user/dep_a.git", {tag, "0.0.1"}}},
  1588. DepB = {dep_b, "0.0.1", {git, "https://example.org/user/dep_b.git", {tag, "0.0.1"}}},
  1589. DepC = {dep_c, "0.0.1", {git, "https://example.org/user/dep_c.git", {tag, "0.0.1"}}},
  1590. {SrcDeps, _} = rebar_test_utils:flat_deps(Deps),
  1591. mock_git_resource:mock([{deps, SrcDeps}]),
  1592. RebarConfig = [
  1593. {deps, TopDeps},
  1594. {profiles, [
  1595. {a, [
  1596. {overrides, [
  1597. {del, some_dep, [
  1598. {deps, [DepA, DepB]}
  1599. ]},
  1600. {del, [
  1601. {deps, [DepC]}
  1602. ]}
  1603. ]}
  1604. ]}
  1605. ]}
  1606. ],
  1607. rebar_test_utils:run_and_check(
  1608. Config, RebarConfig, ["as", "a", "compile"],
  1609. {ok, [{dep, "some_dep"},
  1610. {dep, "other_dep"},
  1611. {dep_not_exist, "dep_a"},
  1612. {dep_not_exist, "dep_b"},
  1613. {dep_not_exist, "dep_c"},
  1614. {dep, "dep_d"}]}
  1615. ).
  1616. profile_override_opts(Config) ->
  1617. AppsDir = ?config(apps, Config),
  1618. Name = rebar_test_utils:create_random_name("app1_"),
  1619. Vsn = rebar_test_utils:create_random_vsn(),
  1620. AppDir = filename:join([AppsDir, "apps", Name]),
  1621. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  1622. RebarConfig = [
  1623. {erl_opts, [
  1624. compressed,
  1625. warn_missing_spec
  1626. ]},
  1627. {profiles, [
  1628. {a, [
  1629. {overrides, [
  1630. {override, [
  1631. {erl_opts, [compressed]}
  1632. ]}
  1633. ]}
  1634. ]}
  1635. ]}
  1636. ],
  1637. rebar_test_utils:create_config(AppsDir, RebarConfig),
  1638. rebar_test_utils:run_and_check(
  1639. Config, RebarConfig, ["as", "a", "compile"], {ok, [{app, Name}]}),
  1640. Path = filename:join([AppsDir, "_build", "a", "lib", Name, "ebin"]),
  1641. code:add_patha(Path),
  1642. Mod = list_to_atom("not_a_real_src_" ++ Name),
  1643. true = lists:member(compressed, proplists:get_value(options, Mod:module_info(compile), [])),
  1644. false = lists:member(warn_missing_spec, proplists:get_value(options, Mod:module_info(compile), [])).
  1645. profile_override_add_opts(Config) ->
  1646. AppsDir = ?config(apps, Config),
  1647. Name = rebar_test_utils:create_random_name("app1_"),
  1648. Vsn = rebar_test_utils:create_random_vsn(),
  1649. AppDir = filename:join([AppsDir, "apps", Name]),
  1650. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  1651. RebarConfig = [
  1652. {erl_opts, [
  1653. warn_missing_spec
  1654. ]},
  1655. {profiles, [
  1656. {a, [
  1657. {overrides, [
  1658. {add, [
  1659. {erl_opts, [compressed]}
  1660. ]}
  1661. ]}
  1662. ]}
  1663. ]}
  1664. ],
  1665. rebar_test_utils:create_config(AppsDir, RebarConfig),
  1666. rebar_test_utils:run_and_check(
  1667. Config, RebarConfig, ["as", "a", "compile"], {ok, [{app, Name}]}),
  1668. Path = filename:join([AppsDir, "_build", "a", "lib", Name, "ebin"]),
  1669. code:add_patha(Path),
  1670. Mod = list_to_atom("not_a_real_src_" ++ Name),
  1671. true = lists:member(compressed, proplists:get_value(options, Mod:module_info(compile), [])),
  1672. true = lists:member(warn_missing_spec, proplists:get_value(options, Mod:module_info(compile), [])).
  1673. profile_override_del_opts(Config) ->
  1674. AppsDir = ?config(apps, Config),
  1675. Name = rebar_test_utils:create_random_name("app1_"),
  1676. Vsn = rebar_test_utils:create_random_vsn(),
  1677. AppDir = filename:join([AppsDir, "apps", Name]),
  1678. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  1679. RebarConfig = [
  1680. {erl_opts, [
  1681. compressed,
  1682. warn_missing_spec
  1683. ]},
  1684. {profiles, [
  1685. {a, [
  1686. {overrides, [
  1687. {del, [
  1688. {erl_opts, [warn_missing_spec]}
  1689. ]}
  1690. ]}
  1691. ]}
  1692. ]}
  1693. ],
  1694. rebar_test_utils:create_config(AppsDir, RebarConfig),
  1695. rebar_test_utils:run_and_check(
  1696. Config, RebarConfig, ["as", "a", "compile"], {ok, [{app, Name}]}),
  1697. Path = filename:join([AppsDir, "_build", "a", "lib", Name, "ebin"]),
  1698. code:add_patha(Path),
  1699. Mod = list_to_atom("not_a_real_src_" ++ Name),
  1700. true = lists:member(compressed, proplists:get_value(options, Mod:module_info(compile), [])),
  1701. false = lists:member(warn_missing_spec, proplists:get_value(options, Mod:module_info(compile), [])),
  1702. ok.
  1703. profile_deps(Config) ->
  1704. Deps = rebar_test_utils:expand_deps(git, [{"some_dep", "0.0.1", [{"other_dep", "0.0.1", []}]}]),
  1705. TopDeps = rebar_test_utils:top_level_deps(Deps),
  1706. {SrcDeps, _} = rebar_test_utils:flat_deps(Deps),
  1707. mock_git_resource:mock([{deps, SrcDeps}]),
  1708. RebarConfig = [
  1709. {deps, TopDeps},
  1710. {profiles, [{a, []}]}],
  1711. rebar_test_utils:run_and_check(
  1712. Config, RebarConfig, ["as", "a", "compile"],
  1713. {ok, [{dep, "some_dep"},{dep, "other_dep"}]}
  1714. ).
  1715. only_deps(Config) ->
  1716. AppDir = ?config(apps, Config),
  1717. Name = rebar_test_utils:create_random_name("app1_"),
  1718. Vsn = rebar_test_utils:create_random_vsn(),
  1719. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  1720. Deps = rebar_test_utils:expand_deps(git, [{"some_dep", "0.0.1", [{"other_dep", "0.0.1", []}]}]),
  1721. TopDeps = rebar_test_utils:top_level_deps(Deps),
  1722. {SrcDeps, _} = rebar_test_utils:flat_deps(Deps),
  1723. mock_git_resource:mock([{deps, SrcDeps}]),
  1724. RConfFile = rebar_test_utils:create_config(AppDir, [{deps, TopDeps}]),
  1725. {ok, RConf} = file:consult(RConfFile),
  1726. rebar_test_utils:run_and_check(
  1727. Config, RConf, ["compile", "--deps_only"],
  1728. {ok, [{app_not_exist, Name}, {dep, "some_dep"},{dep, "other_dep"}]}
  1729. ).
  1730. %% verify a deps prod profile is used
  1731. %% tested by checking prod hooks run and outputs to default profile dir for dep
  1732. %% and prod deps are installed for dep
  1733. deps_build_in_prod(Config) ->
  1734. AppDir = ?config(apps, Config),
  1735. Name = rebar_test_utils:create_random_name("app1_"),
  1736. Vsn = rebar_test_utils:create_random_vsn(),
  1737. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  1738. GitDeps = rebar_test_utils:expand_deps(git, [{"asdf", "1.0.0", []}]),
  1739. PkgName = rebar_test_utils:create_random_name("pkg1_"),
  1740. {SrcDeps, _} = rebar_test_utils:flat_deps(GitDeps),
  1741. mock_git_resource:mock([{deps, SrcDeps},
  1742. {config, [{profiles, [{prod, [{pre_hooks, [{compile, "echo whatsup > randomfile"}]},
  1743. {deps, [list_to_atom(PkgName)]}]}]}]}]),
  1744. mock_pkg_resource:mock([{pkgdeps, [{{iolist_to_binary(PkgName), <<"0.1.0">>}, []}]}]),
  1745. Deps = rebar_test_utils:top_level_deps(GitDeps),
  1746. RConfFile = rebar_test_utils:create_config(AppDir, [{deps, Deps}]),
  1747. {ok, RConf} = file:consult(RConfFile),
  1748. %% Build with deps.
  1749. rebar_test_utils:run_and_check(
  1750. Config, RConf, ["compile"],
  1751. {ok, [{app, Name}, {dep, "asdf", <<"1.0.0">>}, {dep, PkgName},
  1752. {file, filename:join([AppDir, "_build", "default", "lib", "asdf", "randomfile"])}]}
  1753. ).
  1754. %% verify that the proper include path is defined
  1755. %% according the erlang doc which states:
  1756. %% If the filename File is absolute (possibly after variable substitution),
  1757. %% the include file with that name is included. Otherwise, the specified file
  1758. %% is searched for in the following directories, and in this order:
  1759. %% * The current working directory
  1760. %% * The directory where the module is being compiled
  1761. %% * The directories given by the include option
  1762. include_file_relative_to_working_directory(Config) ->
  1763. AppDir = ?config(apps, Config),
  1764. Name = rebar_test_utils:create_random_name("app1_"),
  1765. Vsn = rebar_test_utils:create_random_vsn(),
  1766. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  1767. Src = <<"-module(test).\n"
  1768. "\n"
  1769. "-include(\"include/test.hrl\").\n"
  1770. "\n"
  1771. "test() -> ?TEST_MACRO.\n"
  1772. "\n">>,
  1773. Include = <<"-define(TEST_MACRO, test).\n">>,
  1774. ok = filelib:ensure_dir(filename:join([AppDir, "src", "dummy"])),
  1775. ok = file:write_file(filename:join([AppDir, "src", "test.erl"]), Src),
  1776. ok = filelib:ensure_dir(filename:join([AppDir, "include", "dummy"])),
  1777. ok = file:write_file(filename:join([AppDir, "include", "test.hrl"]), Include),
  1778. RebarConfig = [],
  1779. rebar_test_utils:run_and_check(Config, RebarConfig,
  1780. ["compile"],
  1781. {ok, [{app, Name}]}).
  1782. include_file_in_src(Config) ->
  1783. AppDir = ?config(apps, Config),
  1784. Name = rebar_test_utils:create_random_name("app1_"),
  1785. Vsn = rebar_test_utils:create_random_vsn(),
  1786. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  1787. Src = <<"-module(test).\n"
  1788. "\n"
  1789. "-include(\"test.hrl\").\n"
  1790. "\n"
  1791. "test() -> ?TEST_MACRO.\n"
  1792. "\n">>,
  1793. Include = <<"-define(TEST_MACRO, test).\n">>,
  1794. ok = filelib:ensure_dir(filename:join([AppDir, "src", "dummy"])),
  1795. ok = file:write_file(filename:join([AppDir, "src", "test.erl"]), Src),
  1796. ok = file:write_file(filename:join([AppDir, "src", "test.hrl"]), Include),
  1797. RebarConfig = [],
  1798. rebar_test_utils:run_and_check(Config, RebarConfig,
  1799. ["compile"],
  1800. {ok, [{app, Name}]}).
  1801. %% verify that the proper include path is defined
  1802. %% according the erlang doc which states:
  1803. %% If the filename File is absolute (possibly after variable substitution),
  1804. %% the include file with that name is included. Otherwise, the specified file
  1805. %% is searched for in the following directories, and in this order:
  1806. %% * The current working directory
  1807. %% * The directory where the module is being compiled
  1808. %% * The directories given by the include option
  1809. %%
  1810. %% This test ensures that things keep working when additional directories
  1811. %% are used for apps, such as the test/ directory within the test profile.
  1812. include_file_relative_to_working_directory_test(Config) ->
  1813. AppDir = ?config(apps, Config),
  1814. Name = rebar_test_utils:create_random_name("app1_"),
  1815. Vsn = rebar_test_utils:create_random_vsn(),
  1816. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  1817. Src = <<"-module(test).\n"
  1818. "\n"
  1819. "-include(\"include/test.hrl\").\n"
  1820. "\n"
  1821. "test() -> ?TEST_MACRO.\n"
  1822. "\n">>,
  1823. Include = <<"-define(TEST_MACRO, test).\n">>,
  1824. ok = filelib:ensure_dir(filename:join([AppDir, "src", "dummy"])),
  1825. ok = filelib:ensure_dir(filename:join([AppDir, "test", "dummy"])),
  1826. ok = file:write_file(filename:join([AppDir, "test", "test.erl"]), Src),
  1827. ok = filelib:ensure_dir(filename:join([AppDir, "include", "dummy"])),
  1828. ok = file:write_file(filename:join([AppDir, "include", "test.hrl"]), Include),
  1829. RebarConfig = [],
  1830. rebar_test_utils:run_and_check(Config, RebarConfig,
  1831. ["as", "test", "compile"],
  1832. {ok, [{app, Name}]}).
  1833. %% Same as `include_file_in_src/1' but using the `test/' directory
  1834. %% within the test profile.
  1835. include_file_in_src_test(Config) ->
  1836. AppDir = ?config(apps, Config),
  1837. Name = rebar_test_utils:create_random_name("app1_"),
  1838. Vsn = rebar_test_utils:create_random_vsn(),
  1839. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  1840. Src = <<"-module(test).\n"
  1841. "\n"
  1842. "-include(\"test.hrl\").\n"
  1843. "\n"
  1844. "test() -> ?TEST_MACRO.\n"
  1845. "\n">>,
  1846. Include = <<"-define(TEST_MACRO, test).\n">>,
  1847. ok = filelib:ensure_dir(filename:join([AppDir, "src", "dummy"])),
  1848. ok = filelib:ensure_dir(filename:join([AppDir, "test", "dummy"])),
  1849. ok = file:write_file(filename:join([AppDir, "test", "test.erl"]), Src),
  1850. ok = file:write_file(filename:join([AppDir, "src", "test.hrl"]), Include),
  1851. RebarConfig = [],
  1852. rebar_test_utils:run_and_check(Config, RebarConfig,
  1853. ["as", "test", "compile"],
  1854. {ok, [{app, Name}]}).
  1855. %% Same as `include_file_in_src_test/1' but using multiple top-level
  1856. %% apps as dependencies.
  1857. include_file_in_src_test_multiapp(Config) ->
  1858. Name1 = rebar_test_utils:create_random_name("app2_"),
  1859. Name2 = rebar_test_utils:create_random_name("app1_"),
  1860. AppDir1 = filename:join([?config(apps, Config), "lib", Name1]),
  1861. AppDir2 = filename:join([?config(apps, Config), "lib", Name2]),
  1862. Vsn = rebar_test_utils:create_random_vsn(),
  1863. rebar_test_utils:create_app(AppDir1, Name1, Vsn, [kernel, stdlib, list_to_atom(Name2)]),
  1864. rebar_test_utils:create_app(AppDir2, Name2, Vsn, [kernel, stdlib]),
  1865. Src = "-module(test).\n"
  1866. "\n"
  1867. "-include_lib(\"" ++ Name2 ++ "/include/test.hrl\").\n"
  1868. "\n"
  1869. "test() -> ?TEST_MACRO.\n"
  1870. "\n",
  1871. Include = <<"-define(TEST_MACRO, test).\n">>,
  1872. ok = filelib:ensure_dir(filename:join([AppDir1, "src", "dummy"])),
  1873. ok = filelib:ensure_dir(filename:join([AppDir1, "test", "dummy"])),
  1874. ok = filelib:ensure_dir(filename:join([AppDir2, "src", "dummy"])),
  1875. ok = filelib:ensure_dir(filename:join([AppDir2, "include", "dummy"])),
  1876. ok = file:write_file(filename:join([AppDir1, "test", "test.erl"]), Src),
  1877. ok = file:write_file(filename:join([AppDir2, "include", "test.hrl"]), Include),
  1878. RebarConfig = [],
  1879. rebar_test_utils:run_and_check(Config, RebarConfig,
  1880. ["as", "test", "compile"],
  1881. {ok, [{app, Name1}]}),
  1882. ok.
  1883. %% this test sets the env var, compiles, records the file last modified timestamp,
  1884. %% recompiles and compares the file last modified timestamp to ensure it hasn't
  1885. %% changed. this test should run on 19.x+
  1886. dont_recompile_when_erl_compiler_options_env_does_not_change(Config) ->
  1887. %% save existing env to restore after test
  1888. ExistingEnv = os:getenv("ERL_COMPILER_OPTIONS"),
  1889. AppDir = ?config(apps, Config),
  1890. Name = rebar_test_utils:create_random_name("erl_compiler_options_"),
  1891. Vsn = rebar_test_utils:create_random_vsn(),
  1892. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  1893. true = os:unsetenv("ERL_COMPILER_OPTIONS"),
  1894. true = os:putenv("ERL_COMPILER_OPTIONS", "[{d, some_macro}]"),
  1895. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  1896. EbinDir = filename:join([AppDir, "_build", "default", "lib", Name, "ebin"]),
  1897. {ok, Files} = rebar_utils:list_dir(EbinDir),
  1898. ModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  1899. || F <- Files, filename:extension(F) == ".beam"],
  1900. timer:sleep(1000),
  1901. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  1902. {ok, NewFiles} = rebar_utils:list_dir(EbinDir),
  1903. NewModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  1904. || F <- NewFiles, filename:extension(F) == ".beam"],
  1905. ?assert(ModTime == NewModTime),
  1906. %% restore existing env
  1907. case ExistingEnv of
  1908. false -> ok;
  1909. _ -> os:putenv("ERL_COMPILER_OPTIONS", ExistingEnv)
  1910. end.
  1911. %% this test compiles, records the file last modified timestamp, sets the env
  1912. %% var, recompiles and compares the file last modified timestamp to ensure it
  1913. %% has changed. this test should run on 19.x+
  1914. recompile_when_erl_compiler_options_env_changes(Config) ->
  1915. %% save existing env to restore after test
  1916. ExistingEnv = os:getenv("ERL_COMPILER_OPTIONS"),
  1917. AppDir = ?config(apps, Config),
  1918. Name = rebar_test_utils:create_random_name("erl_compiler_options_"),
  1919. Vsn = rebar_test_utils:create_random_vsn(),
  1920. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  1921. true = os:unsetenv("ERL_COMPILER_OPTIONS"),
  1922. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  1923. EbinDir = filename:join([AppDir, "_build", "default", "lib", Name, "ebin"]),
  1924. {ok, Files} = rebar_utils:list_dir(EbinDir),
  1925. ModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  1926. || F <- Files, filename:extension(F) == ".beam"],
  1927. timer:sleep(1000),
  1928. true = os:putenv("ERL_COMPILER_OPTIONS", "[{d, some_macro}]"),
  1929. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  1930. {ok, NewFiles} = rebar_utils:list_dir(EbinDir),
  1931. NewModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  1932. || F <- NewFiles, filename:extension(F) == ".beam"],
  1933. ?assert(ModTime =/= NewModTime),
  1934. %% restore existing env
  1935. case ExistingEnv of
  1936. false -> ok;
  1937. _ -> os:putenv("ERL_COMPILER_OPTIONS", ExistingEnv)
  1938. end.
  1939. rebar_config_os_var(Config) ->
  1940. AppDir = ?config(apps, Config),
  1941. Name = rebar_test_utils:create_random_name("rebar_config_os_var_"),
  1942. Vsn = rebar_test_utils:create_random_vsn(),
  1943. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  1944. rebar_test_utils:create_config(AppDir, [{erl_opts, []}]),
  1945. AltConfig = filename:join(AppDir, "test.rebar.config"),
  1946. file:write_file(AltConfig, "{erl_opts, [compressed]}."),
  1947. true = os:putenv("REBAR_CONFIG", AltConfig),
  1948. rebar_test_utils:run_and_check(Config, ["compile"], {ok, [{app, Name}]}),
  1949. Path = filename:join([AppDir, "_build", "default", "lib", Name, "ebin"]),
  1950. code:add_patha(Path),
  1951. Mod = list_to_atom("not_a_real_src_" ++ Name),
  1952. true = lists:member(compressed, proplists:get_value(options, Mod:module_info(compile), [])),
  1953. ok.
  1954. %% this test sets the env var, compiles, records the file last modified
  1955. %% timestamp, recompiles and compares the file last modified timestamp to
  1956. %% ensure it has changed. this test should run on 18.x
  1957. always_recompile_when_erl_compiler_options_set(Config) ->
  1958. %% save existing env to restore after test
  1959. ExistingEnv = os:getenv("ERL_COMPILER_OPTIONS"),
  1960. AppDir = ?config(apps, Config),
  1961. Name = rebar_test_utils:create_random_name("erl_compiler_options_"),
  1962. Vsn = rebar_test_utils:create_random_vsn(),
  1963. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  1964. true = os:unsetenv("ERL_COMPILER_OPTIONS"),
  1965. true = os:putenv("ERL_COMPILER_OPTIONS", "[{d, some_macro}]"),
  1966. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  1967. EbinDir = filename:join([AppDir, "_build", "default", "lib", Name, "ebin"]),
  1968. {ok, Files} = rebar_utils:list_dir(EbinDir),
  1969. ModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  1970. || F <- Files, filename:extension(F) == ".beam"],
  1971. timer:sleep(1000),
  1972. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  1973. {ok, NewFiles} = rebar_utils:list_dir(EbinDir),
  1974. NewModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  1975. || F <- NewFiles, filename:extension(F) == ".beam"],
  1976. ?assert(ModTime =/= NewModTime),
  1977. %% restore existing env
  1978. case ExistingEnv of
  1979. false -> ok;
  1980. _ -> os:putenv("ERL_COMPILER_OPTIONS", ExistingEnv)
  1981. end.
  1982. recompile_when_parse_transform_inline_changes(Config) ->
  1983. AppDir = ?config(apps, Config),
  1984. Name = rebar_test_utils:create_random_name("parse_transform_inline_"),
  1985. Vsn = rebar_test_utils:create_random_vsn(),
  1986. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  1987. ok = filelib:ensure_dir(filename:join([AppDir, "src", "dummy"])),
  1988. ModSrc = <<"-module(example).\n"
  1989. "-export([foo/2]).\n"
  1990. "-compile([{parse_transform, example_parse_transform}]).\n"
  1991. "foo(_, _) -> ok.">>,
  1992. ok = file:write_file(filename:join([AppDir, "src", "example.erl"]),
  1993. ModSrc),
  1994. ParseTransform = <<"-module(example_parse_transform).\n"
  1995. "-export([parse_transform/2]).\n"
  1996. "parse_transform(AST, _) -> AST.\n">>,
  1997. ok = file:write_file(filename:join([AppDir, "src", "example_parse_transform.erl"]),
  1998. ParseTransform),
  1999. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  2000. EbinDir = filename:join([AppDir, "_build", "default", "lib", Name, "ebin"]),
  2001. {ok, Files} = rebar_utils:list_dir(EbinDir),
  2002. ModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  2003. || F <- Files, filename:basename(F, ".beam") == "example"],
  2004. timer:sleep(1000),
  2005. NewParseTransform = <<"-module(example_parse_transform).\n"
  2006. "-export([parse_transform/2]).\n"
  2007. "parse_transform(AST, _) -> identity(AST).\n"
  2008. "identity(AST) -> AST.\n">>,
  2009. ok = file:write_file(filename:join([AppDir, "src", "example_parse_transform.erl"]),
  2010. NewParseTransform),
  2011. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  2012. {ok, NewFiles} = rebar_utils:list_dir(EbinDir),
  2013. NewModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  2014. || F <- NewFiles, filename:basename(F, ".beam") == "example"],
  2015. ?assert(ModTime =/= NewModTime).
  2016. recompile_when_parse_transform_as_opt_changes(Config) ->
  2017. AppDir = ?config(apps, Config),
  2018. Name = rebar_test_utils:create_random_name("parse_transform_opt_"),
  2019. Vsn = rebar_test_utils:create_random_vsn(),
  2020. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  2021. ok = filelib:ensure_dir(filename:join([AppDir, "src", "dummy"])),
  2022. ModSrc = <<"-module(example).\n"
  2023. "-export([foo/2]).\n"
  2024. "foo(_, _) -> ok.">>,
  2025. ok = file:write_file(filename:join([AppDir, "src", "example.erl"]),
  2026. ModSrc),
  2027. ParseTransform = <<"-module(example_parse_transform).\n"
  2028. "-export([parse_transform/2]).\n"
  2029. "parse_transform(AST, _) -> AST.">>,
  2030. ok = file:write_file(filename:join([AppDir, "src", "example_parse_transform.erl"]),
  2031. ParseTransform),
  2032. RebarConfig = [{erl_opts, [{parse_transform, example_parse_transform}]}],
  2033. rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], {ok, [{app, Name}]}),
  2034. EbinDir = filename:join([AppDir, "_build", "default", "lib", Name, "ebin"]),
  2035. {ok, Files} = rebar_utils:list_dir(EbinDir),
  2036. ModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  2037. || F <- Files, filename:basename(F, ".beam") == "example"],
  2038. timer:sleep(1000),
  2039. NewParseTransform = <<"-module(example_parse_transform).\n"
  2040. "-export([parse_transform/2]).\n"
  2041. "parse_transform(AST, _) -> identity(AST).\n"
  2042. "identity(AST) -> AST.">>,
  2043. ok = file:write_file(filename:join([AppDir, "src", "example_parse_transform.erl"]),
  2044. NewParseTransform),
  2045. rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], {ok, [{app, Name}]}),
  2046. {ok, NewFiles} = rebar_utils:list_dir(EbinDir),
  2047. NewModTime = [filelib:last_modified(filename:join([EbinDir, F]))
  2048. || F <- NewFiles, filename:basename(F, ".beam") == "example"],
  2049. ?assert(ModTime =/= NewModTime).
  2050. recursive(Config) ->
  2051. AppDir = ?config(apps, Config),
  2052. Name = rebar_test_utils:create_random_name("app1_"),
  2053. Vsn = rebar_test_utils:create_random_vsn(),
  2054. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  2055. rebar_test_utils:write_src_file(filename:join(AppDir,src),"rec.erl"),
  2056. rebar_test_utils:run_and_check(Config, [], ["compile"], {ok, [{app, Name}]}),
  2057. EbinDir = filename:join([AppDir, "_build", "default", "lib", Name, "ebin"]),
  2058. {ok, Files} = rebar_utils:list_dir(EbinDir),
  2059. ?assert(lists:member("rec.beam",Files)),
  2060. %% check that rec is in modules list of .app file
  2061. AppFile = filename:join(EbinDir, Name++".app"),
  2062. {ok, [{application, _, List}]} = file:consult(AppFile),
  2063. {modules, Modules} = lists:keyfind(modules, 1, List),
  2064. ?assert(lists:member(rec, Modules)).
  2065. no_recursive(Config) ->
  2066. AppDir = ?config(apps, Config),
  2067. Name = rebar_test_utils:create_random_name("app1_"),
  2068. Vsn = rebar_test_utils:create_random_vsn(),
  2069. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  2070. rebar_test_utils:write_src_file(filename:join(AppDir,src),"rec.erl"),
  2071. RebarConfig1 = [{erlc_compiler,[{recursive,false}]}],
  2072. rebar_test_utils:run_and_check(Config, RebarConfig1, ["compile"],
  2073. {ok, [{app, Name}]}),
  2074. EbinDir = filename:join([AppDir, "_build", "default", "lib", Name, "ebin"]),
  2075. {ok, Files1} = rebar_utils:list_dir(EbinDir),
  2076. ?assert(false==lists:member("rec.beam",Files1)),
  2077. RebarConfig2 = [{src_dirs,[{"src",[{recursive,false}]}]}],
  2078. rebar_test_utils:run_and_check(Config, RebarConfig2, ["compile"],
  2079. {ok, [{app, Name}]}),
  2080. {ok, Files2} = rebar_utils:list_dir(EbinDir),
  2081. ?assert(false==lists:member("rec.beam",Files2)),
  2082. ok.
  2083. extra_recursion(Config) ->
  2084. AppDir = ?config(apps, Config),
  2085. Name = rebar_test_utils:create_random_name("app1_"),
  2086. Vsn = rebar_test_utils:create_random_vsn(),
  2087. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  2088. rebar_test_utils:write_src_file(filename:join([AppDir, "src", "src2"]), "rec.erl"),
  2089. rebar_test_utils:write_src_file(filename:join([AppDir, "test", "test2"]), "rectest.erl"),
  2090. %% Default for src directories: recursive
  2091. %% default for extra_src directories: non-recursive
  2092. RebarConfig1 = [],
  2093. rebar_test_utils:run_and_check(Config, RebarConfig1, ["as", "test", "compile"],
  2094. {ok, [{app, Name}]}),
  2095. EbinDir = filename:join([AppDir, "_build", "test", "lib", Name, "ebin"]),
  2096. {ok, Files1} = rebar_utils:list_dir(EbinDir),
  2097. ?assert(lists:member("rec.beam", Files1)),
  2098. file:delete(filename:join(EbinDir, "rec.beam")),
  2099. TestEbinDir = filename:join([AppDir, "_build", "test", "lib", Name, "test"]),
  2100. {ok, TestFiles1} = rebar_utils:list_dir(TestEbinDir),
  2101. ?assertNot(lists:member("rectest.beam", TestFiles1)),
  2102. RebarConfig2 = [{src_dirs,[{"src",[{recursive,false}]}]},
  2103. {extra_src_dirs, [{"test", [{recursive, true}]}]}],
  2104. rebar_test_utils:run_and_check(Config, RebarConfig2, ["as", "test", "compile"],
  2105. {ok, [{app, Name}]}),
  2106. {ok, Files2} = rebar_utils:list_dir(EbinDir),
  2107. ?assertNot(lists:member("rec.beam",Files2)),
  2108. {ok, TestFiles2} = rebar_utils:list_dir(TestEbinDir),
  2109. ?assert(lists:member("rectest.beam", TestFiles2)),
  2110. ok.
  2111. regex_filter_skip(Config) ->
  2112. AppDir = ?config(apps, Config),
  2113. Name = rebar_test_utils:create_random_name("regex_skip"),
  2114. Vsn = rebar_test_utils:create_random_vsn(),
  2115. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  2116. rebar_test_utils:write_src_file(filename:join(AppDir,src),"._rec.erl"),
  2117. Expected = filename:join([AppDir, "_build", "default", "lib", Name, "ebin","._rec.beam"]),
  2118. RebarConfig = [],
  2119. try
  2120. rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"],
  2121. {ok, [{file, Expected}]}),
  2122. throw(should_not_be_found)
  2123. catch
  2124. %% the file was not found, as desired!
  2125. error:{assertion_failed,_} -> %% OTP =< 17
  2126. ok;
  2127. error:{assert,_} -> %% OTP >= 18
  2128. ok
  2129. end.
  2130. regex_filter_regression(Config) ->
  2131. AppDir = ?config(apps, Config),
  2132. Name = rebar_test_utils:create_random_name("regex_regression"),
  2133. Vsn = rebar_test_utils:create_random_vsn(),
  2134. rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
  2135. rebar_test_utils:write_src_file(filename:join(AppDir,src),"r_f.erl"),
  2136. Expected = filename:join([AppDir, "_build", "default", "lib", Name, "ebin","r_f.beam"]),
  2137. RebarConfig = [],
  2138. rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"],
  2139. {ok, [{file, Expected}]}),
  2140. ok.
  2141. %% This test could also have existed in rebar_hooks_SUITE but it's more
  2142. %% about compiler implementation details than the hook logic itself,
  2143. %% so it was located here.
  2144. split_project_apps_hooks() ->
  2145. [{doc, "Ensure that a project with multiple project apps runs the "
  2146. "pre-hooks before all the apps are compiled, and the post "
  2147. "hooks after they are all compiled."}].
  2148. split_project_apps_hooks(Config) ->
  2149. BaseDir = ?config(apps, Config),
  2150. Name1 = rebar_test_utils:create_random_name("app2_"),
  2151. Name2 = rebar_test_utils:create_random_name("app1_"),
  2152. AppDir1 = filename:join([BaseDir, "lib", Name1]),
  2153. AppDir2 = filename:join([BaseDir, "lib", Name2]),
  2154. HookDir = filename:join([BaseDir, "hooks"]),
  2155. Vsn = rebar_test_utils:create_random_vsn(),
  2156. rebar_test_utils:create_app(AppDir1, Name1, Vsn, [kernel, stdlib, list_to_atom(Name2)]),
  2157. rebar_test_utils:create_app(AppDir2, Name2, Vsn, [kernel, stdlib]),
  2158. ok = filelib:ensure_dir(filename:join([AppDir1, "src", "dummy"])),
  2159. ok = filelib:ensure_dir(filename:join([AppDir1, "test", "dummy"])),
  2160. ok = filelib:ensure_dir(filename:join([AppDir2, "src", "dummy"])),
  2161. ok = filelib:ensure_dir(filename:join([AppDir2, "include", "dummy"])),
  2162. ok = filelib:ensure_dir(filename:join([HookDir, "dummy"])),
  2163. Cmd = case os:type() of
  2164. {win32, _} -> "dir /B";
  2165. _ -> "ls"
  2166. end,
  2167. Cfg = fun(Name) ->
  2168. [{pre_hooks, [{compile, Cmd++" \""++HookDir++"\" > \""++filename:join(HookDir, "pre-compile-"++Name)++"\""},
  2169. {erlc_compile, Cmd++" \""++HookDir++"\" > \""++filename:join(HookDir, "pre-erlc-"++Name)++"\""},
  2170. {app_compile, Cmd++" \""++HookDir++"\" > \""++filename:join(HookDir, "pre-app-"++Name)++"\""}]},
  2171. {post_hooks, [{compile, Cmd++" \""++HookDir++"\" > \""++filename:join(HookDir, "post-compile-"++Name)++"\""},
  2172. {erlc_compile, Cmd++" \""++HookDir++"\" > \""++filename:join(HookDir, "post-erlc-"++Name)++"\""},
  2173. {app_compile, Cmd++" \""++HookDir++"\" > \""++filename:join(HookDir, "post-app-"++Name)++"\""}]}
  2174. ]
  2175. end,
  2176. ok = file:write_file(filename:join(AppDir1, "rebar.config"),
  2177. io_lib:format("~p.~n~p.", Cfg("app1"))),
  2178. ok = file:write_file(filename:join(AppDir2, "rebar.config"),
  2179. io_lib:format("~p.~n~p.", Cfg("app2"))),
  2180. RebarConfig = Cfg("all"),
  2181. rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"],
  2182. {ok, [{app, Name1}, {app, Name2}]}),
  2183. %% Now for the big party:
  2184. %% - we expect whole pre-hooks to run before either app is compiled
  2185. %% - we don't expect app and erlc hooks on the total run
  2186. %% - we expect app2 to be compiled before app1 (rev alphabetical order)
  2187. %% - we expect all pre-hooks to show up before post-hooks
  2188. %% - the pre-order is: compile->erlc, taking place before any app
  2189. %% is actually compiled, so that analysis can be done on all apps.
  2190. %% - the post-order is more as expected:
  2191. %% - erlc post hook runs right with the app
  2192. %% - app pre hook runs right with the app
  2193. %% - app post hook runs right with the app
  2194. %% - compile post hook runs for each app individually
  2195. %% - we expect app compile post-hooks to show up in order
  2196. %% - we expect whole post-hooks to run last
  2197. CallOrder = [
  2198. "pre-compile-all",
  2199. "pre-compile-app2",
  2200. "pre-compile-app1",
  2201. "pre-erlc-app2",
  2202. "pre-erlc-app1",
  2203. "post-erlc-app2",
  2204. "post-erlc-app1",
  2205. "pre-app-app2",
  2206. "pre-app-app1",
  2207. "post-app-app2",
  2208. "post-app-app1",
  2209. "post-compile-app2",
  2210. "post-compile-app1",
  2211. "post-compile-all"
  2212. ],
  2213. validate_call_order(CallOrder, HookDir),
  2214. ok.
  2215. validate_call_order(Calls, Dir) -> validate_call_order(Calls, Dir, []).
  2216. validate_call_order([], _, _) ->
  2217. ok;
  2218. validate_call_order([Name|T], Dir, Seen) ->
  2219. {ok, Bin} = file:read_file(filename:join(Dir, Name)),
  2220. %% weird list of tokens, but works on lexemes/tokens for backwards compat
  2221. Found = rebar_string:lexemes(binary_to_list(Bin), [$\n, $\r, "\r\n"]),
  2222. NewSeen = [Name|Seen],
  2223. ?assertEqual({Name, Found}, {Name, lists:sort(NewSeen)}),
  2224. validate_call_order(T, Dir, NewSeen).
  2225. app_file_linting(Config) ->
  2226. meck:new(rebar_log, [no_link, passthrough]),
  2227. AppDir = ?config(apps, Config),
  2228. Name = rebar_test_utils:create_random_name("app_file_linting"),
  2229. Vsn = rebar_test_utils:create_random_vsn(),
  2230. rebar_test_utils:create_app(AppDir, Name, Vsn, [foo]),
  2231. _ = rebar_test_utils:run_and_check(Config, [], ["compile"], return),
  2232. History = meck:history(rebar_log),
  2233. Warnings = [{Str, Args} || {_, {rebar_log, log, [warn, Str, Args]}, _} <- History],
  2234. ?assert(none /= proplists:lookup("~p is missing description entry", Warnings)),
  2235. ?assert(none /= proplists:lookup("~p is missing kernel from applications list", Warnings)),
  2236. ?assert(none /= proplists:lookup("~p is missing stdlib from applications list", Warnings)).
  2237. %%
  2238. %% a copy of lib/parsetools/include/yeccpre.hrl so we can test yrl includefile
  2239. yeccpre_hrl() ->
  2240. <<"-type yecc_ret() :: {'error', _} | {'ok', _}.
  2241. -spec parse(Tokens :: list()) -> yecc_ret().
  2242. parse(Tokens) ->
  2243. yeccpars0(Tokens, {no_func, no_line}, 0, [], []).
  2244. -spec parse_and_scan({function() | {atom(), atom()}, [_]}
  2245. | {atom(), atom(), [_]}) -> yecc_ret().
  2246. parse_and_scan({F, A}) ->
  2247. yeccpars0([], {{F, A}, no_line}, 0, [], []);
  2248. parse_and_scan({M, F, A}) ->
  2249. Arity = length(A),
  2250. yeccpars0([], {{fun M:F/Arity, A}, no_line}, 0, [], []).
  2251. -spec format_error(any()) -> [char() | list()].
  2252. format_error(Message) ->
  2253. case io_lib:deep_char_list(Message) of
  2254. true ->
  2255. Message;
  2256. _ ->
  2257. io_lib:write(Message)
  2258. end.
  2259. %% To be used in grammar files to throw an error message to the parser
  2260. %% toplevel. Doesn't have to be exported!
  2261. -compile({nowarn_unused_function, return_error/2}).
  2262. -spec return_error(integer(), any()) -> no_return().
  2263. return_error(Line, Message) ->
  2264. throw({error, {Line, ?MODULE, Message}}).
  2265. -define(CODE_VERSION, \"1.4\").
  2266. yeccpars0(Tokens, Tzr, State, States, Vstack) ->
  2267. try yeccpars1(Tokens, Tzr, State, States, Vstack)
  2268. catch
  2269. error:Error ->
  2270. try yecc_error_type(Error, []) of
  2271. Desc ->
  2272. erlang:raise(error, {yecc_bug, ?CODE_VERSION, Desc},
  2273. [])
  2274. catch _:_ -> erlang:raise(error, Error, [])
  2275. end;
  2276. %% Probably thrown from return_error/2:
  2277. throw: {error, {_Line, ?MODULE, _M}} = Error ->
  2278. Error
  2279. end.
  2280. yecc_error_type(function_clause, _) ->
  2281. not_implemented.
  2282. yeccpars1([Token | Tokens], Tzr, State, States, Vstack) ->
  2283. yeccpars2(State, element(1, Token), States, Vstack, Token, Tokens, Tzr);
  2284. yeccpars1([], {{F, A},_Line}, State, States, Vstack) ->
  2285. case apply(F, A) of
  2286. {ok, Tokens, Endline} ->
  2287. yeccpars1(Tokens, {{F, A}, Endline}, State, States, Vstack);
  2288. {eof, Endline} ->
  2289. yeccpars1([], {no_func, Endline}, State, States, Vstack);
  2290. {error, Descriptor, _Endline} ->
  2291. {error, Descriptor}
  2292. end;
  2293. yeccpars1([], {no_func, no_line}, State, States, Vstack) ->
  2294. Line = 999999,
  2295. yeccpars2(State, '$end', States, Vstack, yecc_end(Line), [],
  2296. {no_func, Line});
  2297. yeccpars1([], {no_func, Endline}, State, States, Vstack) ->
  2298. yeccpars2(State, '$end', States, Vstack, yecc_end(Endline), [],
  2299. {no_func, Endline}).
  2300. %% yeccpars1/7 is called from generated code.
  2301. %%
  2302. %% When using the {includefile, Includefile} option, make sure that
  2303. %% yeccpars1/7 can be found by parsing the file without following
  2304. %% include directives. yecc will otherwise assume that an old
  2305. %% yeccpre.hrl is included (one which defines yeccpars1/5).
  2306. yeccpars1(State1, State, States, Vstack, Token0, [Token | Tokens], Tzr) ->
  2307. yeccpars2(State, element(1, Token), [State1 | States],
  2308. [Token0 | Vstack], Token, Tokens, Tzr);
  2309. yeccpars1(State1, State, States, Vstack, Token0, [], {{_F,_A}, _Line}=Tzr) ->
  2310. yeccpars1([], Tzr, State, [State1 | States], [Token0 | Vstack]);
  2311. yeccpars1(State1, State, States, Vstack, Token0, [], {no_func, no_line}) ->
  2312. Line = yecctoken_end_location(Token0),
  2313. yeccpars2(State, '$end', [State1 | States], [Token0 | Vstack],
  2314. yecc_end(Line), [], {no_func, Line});
  2315. yeccpars1(State1, State, States, Vstack, Token0, [], {no_func, Line}) ->
  2316. yeccpars2(State, '$end', [State1 | States], [Token0 | Vstack],
  2317. yecc_end(Line), [], {no_func, Line}).
  2318. %% For internal use only.
  2319. yecc_end({Line,_Column}) ->
  2320. {'$end', Line};
  2321. yecc_end(Line) ->
  2322. {'$end', Line}.
  2323. yecctoken_end_location(Token) ->
  2324. try erl_anno:end_location(element(2, Token)) of
  2325. undefined -> yecctoken_location(Token);
  2326. Loc -> Loc
  2327. catch _:_ -> yecctoken_location(Token)
  2328. end.
  2329. -compile({nowarn_unused_function, yeccerror/1}).
  2330. yeccerror(Token) ->
  2331. Text = yecctoken_to_string(Token),
  2332. Location = yecctoken_location(Token),
  2333. {error, {Location, ?MODULE, [\"syntax error before: \", Text]}}.
  2334. -compile({nowarn_unused_function, yecctoken_to_string/1}).
  2335. yecctoken_to_string(Token) ->
  2336. try erl_scan:text(Token) of
  2337. undefined -> yecctoken2string(Token);
  2338. Txt -> Txt
  2339. catch _:_ -> yecctoken2string(Token)
  2340. end.
  2341. yecctoken_location(Token) ->
  2342. try erl_scan:location(Token)
  2343. catch _:_ -> element(2, Token)
  2344. end.
  2345. -compile({nowarn_unused_function, yecctoken2string/1}).
  2346. yecctoken2string({atom, _, A}) -> io_lib:write_atom(A);
  2347. yecctoken2string({integer,_,N}) -> io_lib:write(N);
  2348. yecctoken2string({float,_,F}) -> io_lib:write(F);
  2349. yecctoken2string({char,_,C}) -> io_lib:write_char(C);
  2350. yecctoken2string({var,_,V}) -> io_lib:format(\"~s\", [V]);
  2351. yecctoken2string({string,_,S}) -> io_lib:write_string(S);
  2352. yecctoken2string({reserved_symbol, _, A}) -> io_lib:write(A);
  2353. yecctoken2string({_Cat, _, Val}) -> io_lib:format(\"~p\", [Val]);
  2354. yecctoken2string({dot, _}) -> \"'.'\";
  2355. yecctoken2string({'$end', _}) -> [];
  2356. yecctoken2string({Other, _}) when is_atom(Other) ->
  2357. io_lib:write_atom(Other);
  2358. yecctoken2string(Other) ->
  2359. io_lib:format(\"~p\", [Other]).
  2360. ">>.