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

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