25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2454 lines
95 KiB

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