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

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