You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2864 lines
112 KiB

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