Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

256 Zeilen
12 KiB

  1. -module(rebar_dir_SUITE).
  2. -export([all/0, init_per_testcase/2, end_per_testcase/2]).
  3. -export([default_src_dirs/1, default_extra_src_dirs/1, default_all_src_dirs/1]).
  4. -export([src_dirs/1, src_dirs_with_opts/1, extra_src_dirs/1, all_src_dirs/1]).
  5. -export([src_dir_opts/1, recursive/1]).
  6. -export([profile_src_dirs/1, profile_extra_src_dirs/1, profile_all_src_dirs/1]).
  7. -export([profile_src_dir_opts/1]).
  8. -export([retarget_path/1, alt_base_dir_abs/1, alt_base_dir_rel/1]).
  9. -export([global_cache_dir/1, default_global_cache_dir/1, overwrite_default_global_cache_dir/1]).
  10. -include_lib("common_test/include/ct.hrl").
  11. -include_lib("eunit/include/eunit.hrl").
  12. -include_lib("kernel/include/file.hrl").
  13. all() -> [default_src_dirs, default_extra_src_dirs, default_all_src_dirs,
  14. src_dirs, extra_src_dirs, all_src_dirs, src_dir_opts, recursive,
  15. profile_src_dirs, profile_extra_src_dirs, profile_all_src_dirs,
  16. profile_src_dir_opts,
  17. retarget_path, alt_base_dir_abs, alt_base_dir_rel, global_cache_dir,
  18. default_global_cache_dir, overwrite_default_global_cache_dir].
  19. init_per_testcase(default_global_cache_dir, Config) ->
  20. [{apps, AppsDir}, {checkouts, CheckoutsDir}, {state, _State} | Config] = rebar_test_utils:init_rebar_state(Config),
  21. NewState = rebar_state:new([{base_dir, filename:join([AppsDir, "_build"])}
  22. ,{root_dir, AppsDir}]),
  23. [{apps, AppsDir}, {checkouts, CheckoutsDir}, {state, NewState} | Config];
  24. init_per_testcase(overwrite_default_global_cache_dir, Config) ->
  25. os:putenv("REBAR_CACHE_DIR", ?config(priv_dir, Config)),
  26. [{apps, AppsDir}, {checkouts, CheckoutsDir}, {state, _State} | Config] = rebar_test_utils:init_rebar_state(Config),
  27. NewState = rebar_state:new([{base_dir, filename:join([AppsDir, "_build"])}
  28. ,{root_dir, AppsDir}]),
  29. [{apps, AppsDir}, {checkouts, CheckoutsDir}, {state, NewState} | Config];
  30. init_per_testcase(_, Config) ->
  31. C = rebar_test_utils:init_rebar_state(Config),
  32. AppDir = ?config(apps, C),
  33. Name1 = rebar_test_utils:create_random_name("app1_"),
  34. Vsn1 = rebar_test_utils:create_random_vsn(),
  35. rebar_test_utils:create_app(filename:join([AppDir,"apps",Name1]), Name1, Vsn1, [kernel, stdlib]),
  36. Name2 = rebar_test_utils:create_random_name("app2_"),
  37. Vsn2 = rebar_test_utils:create_random_vsn(),
  38. rebar_test_utils:create_app(filename:join([AppDir,"apps",Name2]), Name2, Vsn2, [kernel, stdlib]),
  39. [{app_one, Name1}, {app_two, Name2}] ++ C.
  40. end_per_testcase(_, _Config) -> ok.
  41. default_src_dirs(Config) ->
  42. {ok, State} = rebar_test_utils:run_and_check(Config, [], ["compile"], return),
  43. [] = rebar_dir:src_dirs(rebar_state:opts(State)),
  44. ["src"] = rebar_dir:src_dirs(rebar_state:opts(State), ["src"]).
  45. default_extra_src_dirs(Config) ->
  46. {ok, State} = rebar_test_utils:run_and_check(Config, [], ["compile"], return),
  47. [] = rebar_dir:extra_src_dirs(rebar_state:opts(State)),
  48. ["src"] = rebar_dir:extra_src_dirs(rebar_state:opts(State), ["src"]).
  49. default_all_src_dirs(Config) ->
  50. {ok, State} = rebar_test_utils:run_and_check(Config, [], ["compile"], return),
  51. [] = rebar_dir:all_src_dirs(rebar_state:opts(State)),
  52. ["src", "test"] = rebar_dir:all_src_dirs(rebar_state:opts(State), ["src"], ["test"]).
  53. src_dirs(Config) ->
  54. RebarConfig = [{erl_opts, [{src_dirs, ["foo", "bar", "baz"]}]}],
  55. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  56. ["bar", "baz", "foo"] = rebar_dir:src_dirs(rebar_state:opts(State)).
  57. src_dirs_with_opts(Config) ->
  58. RebarConfig = [{erl_opts, [{src_dirs, ["foo", "bar", "baz"]},
  59. {src_dirs, [{"foo",[{recursive,false}]}, "qux"]}]}],
  60. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  61. ["bar", "baz", "foo", "qux"] = rebar_dir:src_dirs(rebar_state:opts(State)).
  62. extra_src_dirs(Config) ->
  63. RebarConfig = [{erl_opts, [{extra_src_dirs, ["foo", "bar", "baz"]}]}],
  64. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  65. ["bar", "baz", "foo"] = rebar_dir:extra_src_dirs(rebar_state:opts(State)).
  66. all_src_dirs(Config) ->
  67. RebarConfig = [{erl_opts, [{src_dirs, ["foo", "bar"]}, {extra_src_dirs, ["baz", "qux"]}, {src_dirs, [{"foo", [{recursive,false}]}]}]}],
  68. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  69. ["bar", "baz", "foo", "qux"] = rebar_dir:all_src_dirs(rebar_state:opts(State)).
  70. src_dir_opts(Config) ->
  71. RebarConfig =
  72. [{erl_opts, [{src_dirs, [{"foo",[{recursive,true}]}, "bar"]},
  73. {extra_src_dirs, ["baz", {"foo", [{recursive,false}]}]},
  74. {src_dirs, [{"foo", [{recursive,false}]}]}]}],
  75. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig,
  76. ["compile"], return),
  77. [{recursive,true}] = rebar_dir:src_dir_opts(rebar_state:opts(State), "foo"),
  78. [] = rebar_dir:src_dir_opts(rebar_state:opts(State), "bar"),
  79. [] = rebar_dir:src_dir_opts(rebar_state:opts(State), "nonexisting").
  80. recursive(Config) ->
  81. RebarConfig1 =
  82. [{erl_opts, [{src_dirs, ["foo", "bar"]},
  83. {extra_src_dirs, ["baz", {"foo", [{recursive,true}]}]},
  84. {src_dirs, [{"foo", [{recursive,false}]}]}]}],
  85. {ok, State1} = rebar_test_utils:run_and_check(Config, RebarConfig1,
  86. ["compile"], return),
  87. false = rebar_dir:recursive(rebar_state:opts(State1), "foo"),
  88. true = rebar_dir:recursive(rebar_state:opts(State1), "bar"),
  89. RebarConfig2 = [{erlc_compiler,[{recursive,false}]},
  90. {erl_opts,[{src_dirs,["foo",{"bar",[{recursive,true}]}]}]}],
  91. {ok, State2} = rebar_test_utils:run_and_check(Config, RebarConfig2,
  92. ["compile"], return),
  93. false = rebar_dir:recursive(rebar_state:opts(State2), "foo"),
  94. true = rebar_dir:recursive(rebar_state:opts(State2), "bar"),
  95. ok.
  96. profile_src_dirs(Config) ->
  97. RebarConfig = [
  98. {erl_opts, [{src_dirs, ["foo", "bar"]}]},
  99. {profiles, [
  100. {more, [{erl_opts, [{src_dirs, ["baz", "qux"]}]}]}
  101. ]}
  102. ],
  103. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["as", "more", "compile"], return),
  104. R = lists:sort(["foo", "bar", "baz", "qux"]),
  105. R = rebar_dir:src_dirs(rebar_state:opts(State)).
  106. profile_extra_src_dirs(Config) ->
  107. RebarConfig = [
  108. {erl_opts, [{extra_src_dirs, ["foo", "bar"]}]},
  109. {profiles, [
  110. {more, [{erl_opts, [{extra_src_dirs, ["baz", "qux"]}]}]}
  111. ]}
  112. ],
  113. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["as", "more", "compile"], return),
  114. R = lists:sort(["foo", "bar", "baz", "qux"]),
  115. R = rebar_dir:extra_src_dirs(rebar_state:opts(State)).
  116. profile_all_src_dirs(Config) ->
  117. RebarConfig = [
  118. {erl_opts, [{src_dirs, ["foo"]}, {extra_src_dirs, ["bar"]}]},
  119. {profiles, [
  120. {more, [{erl_opts, [{src_dirs, ["baz"]}, {extra_src_dirs, ["qux"]}]}]}
  121. ]}
  122. ],
  123. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["as", "more", "compile"], return),
  124. R = lists:sort(["foo", "bar", "baz", "qux"]),
  125. R = rebar_dir:all_src_dirs(rebar_state:opts(State)).
  126. profile_src_dir_opts(Config) ->
  127. RebarConfig = [
  128. {erl_opts, [{src_dirs, ["foo"]},
  129. {extra_src_dirs, [{"bar",[recursive]}]}]},
  130. {profiles, [
  131. {more, [{erl_opts, [{src_dirs, [{"bar",[{recursive,false}]}]},
  132. {extra_src_dirs, ["qux"]}]}]}
  133. ]}
  134. ],
  135. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig,
  136. ["as", "more", "compile"],
  137. return),
  138. [{recursive,false}] = rebar_dir:src_dir_opts(rebar_state:opts(State),"bar"),
  139. {ok, State1} = rebar_test_utils:run_and_check(Config, RebarConfig,
  140. ["compile"], return),
  141. [{recursive,true}] = rebar_dir:src_dir_opts(rebar_state:opts(State1),"bar").
  142. retarget_path(Config) ->
  143. {ok, State} = rebar_test_utils:run_and_check(Config, [], ["compile"], return),
  144. BaseDir = rebar_dir:base_dir(State),
  145. Name1 = ?config(app_one, Config),
  146. Name2 = ?config(app_two, Config),
  147. ?assertEqual(filename:join([BaseDir, "lib", Name1, "test"]),
  148. rebar_dir:retarget_path(State, filename:join([rebar_dir:root_dir(State), "apps", Name1, "test"]))),
  149. ?assertEqual(filename:join([BaseDir, "lib", Name2, "test"]),
  150. rebar_dir:retarget_path(State, filename:join([rebar_dir:root_dir(State), "apps", Name2, "test"]))),
  151. ?assertEqual(filename:join([BaseDir, "lib", Name1, "more_test"]),
  152. rebar_dir:retarget_path(State, filename:join([rebar_dir:root_dir(State), "apps", Name1, "more_test"]))),
  153. ?assertEqual(filename:join([BaseDir, "test"]),
  154. rebar_dir:retarget_path(State, filename:join([rebar_dir:root_dir(State), "test"]))),
  155. ?assertEqual(filename:join([BaseDir, "some_other_dir"]),
  156. rebar_dir:retarget_path(State, filename:join([rebar_dir:root_dir(State), "some_other_dir"]))),
  157. ?assertEqual("/somewhere/outside/the/project",
  158. rebar_dir:retarget_path(State, "/somewhere/outside/the/project")).
  159. alt_base_dir_abs(Config) ->
  160. AltName = lists:flatten(io_lib:format("~p", [os:timestamp()])),
  161. AltBaseDir = filename:join(?config(priv_dir, Config), AltName),
  162. RebarConfig = [{base_dir, AltBaseDir}],
  163. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  164. BaseDir = rebar_dir:base_dir(State),
  165. ?assertEqual(filename:join(AltBaseDir, "default"), BaseDir),
  166. Name1 = ?config(app_one, Config),
  167. Name2 = ?config(app_two, Config),
  168. ?assert(filelib:is_dir(filename:join([BaseDir, "lib", Name1, "ebin"]))),
  169. ?assert(filelib:is_file(filename:join([BaseDir, "lib", Name1, "ebin", Name1++".app"]))),
  170. ?assert(filelib:is_file(filename:join([BaseDir, "lib", Name1, "ebin", Name1++".beam"]))),
  171. ?assert(filelib:is_dir(filename:join([BaseDir, "lib", Name2, "ebin"]))),
  172. ?assert(filelib:is_file(filename:join([BaseDir, "lib", Name2, "ebin", Name2++".app"]))),
  173. ?assert(filelib:is_file(filename:join([BaseDir, "lib", Name2, "ebin", Name2++".beam"]))).
  174. alt_base_dir_rel(Config) ->
  175. AltName = lists:flatten(io_lib:format("~p", [os:timestamp()])),
  176. AltBaseDir = filename:join("..", AltName),
  177. RebarConfig = [{base_dir, AltBaseDir}],
  178. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  179. BaseDir = rebar_dir:base_dir(State),
  180. Name1 = ?config(app_one, Config),
  181. Name2 = ?config(app_two, Config),
  182. ?assert(filelib:is_dir(filename:join([BaseDir, "lib", Name1, "ebin"]))),
  183. ?assert(filelib:is_file(filename:join([BaseDir, "lib", Name1, "ebin", Name1++".app"]))),
  184. ?assert(filelib:is_file(filename:join([BaseDir, "lib", Name1, "ebin", Name1++".beam"]))),
  185. ?assert(filelib:is_dir(filename:join([BaseDir, "lib", Name2, "ebin"]))),
  186. ?assert(filelib:is_file(filename:join([BaseDir, "lib", Name2, "ebin", Name2++".app"]))),
  187. ?assert(filelib:is_file(filename:join([BaseDir, "lib", Name2, "ebin", Name2++".beam"]))).
  188. global_cache_dir(Config) ->
  189. RebarConfig = [{erl_opts, []}],
  190. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  191. DataDir = ?config(priv_dir, Config),
  192. Expected = filename:join([DataDir, "cache"]),
  193. ?assertEqual(Expected, rebar_dir:global_cache_dir(rebar_state:opts(State))).
  194. default_global_cache_dir(Config) ->
  195. RebarConfig = [{erl_opts, []}],
  196. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  197. Expected = filename:join([rebar_dir:home_dir(), ".cache", "rebar3"]),
  198. ?assertEqual(Expected, rebar_dir:global_cache_dir(rebar_state:opts(State))).
  199. overwrite_default_global_cache_dir(Config) ->
  200. RebarConfig = [{erl_opts, []}],
  201. {ok, State} = rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], return),
  202. Expected = ?config(priv_dir, Config),
  203. ?assertEqual(Expected, rebar_dir:global_cache_dir(rebar_state:opts(State))).