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

277 lines
10 KiB

  1. %% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
  2. %% ex: ts=4 sw=4 et
  3. %% -------------------------------------------------------------------
  4. %%
  5. %% rebar: Erlang Build Tools
  6. %%
  7. %% Copyright (c) 2009, 2010 Dave Smith (dizzyd@dizzyd.com)
  8. %%
  9. %% Permission is hereby granted, free of charge, to any person obtaining a copy
  10. %% of this software and associated documentation files (the "Software"), to deal
  11. %% in the Software without restriction, including without limitation the rights
  12. %% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. %% copies of the Software, and to permit persons to whom the Software is
  14. %% furnished to do so, subject to the following conditions:
  15. %%
  16. %% The above copyright notice and this permission notice shall be included in
  17. %% all copies or substantial portions of the Software.
  18. %%
  19. %% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. %% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. %% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. %% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. %% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. %% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. %% THE SOFTWARE.
  26. %% -------------------------------------------------------------------
  27. %% @author Juhani Rankimies <juhani@juranki.com>
  28. %% @doc Tests functionality of rebar_file_utils module.
  29. %% @copyright 2009, 2010 Dave Smith
  30. %% -------------------------------------------------------------------
  31. -module(rebar_file_utils_tests).
  32. -compile(export_all).
  33. -include_lib("eunit/include/eunit.hrl").
  34. -define(TMP_DIR, "tmp_file_utils").
  35. -define(DIR_TREE, [{d,"source",[{f,"file1"},
  36. {f,"file2"}]},
  37. {d,"dest",[]}]).
  38. -define(FILE_CONTENT, <<"1234567890">>).
  39. %% ====================================================================
  40. %% delete_each tests
  41. %% ====================================================================
  42. delete_bogus_test_() ->
  43. {"delete_each survives nonexisting files",
  44. [?_assertMatch(ok, rebar_file_utils:delete_each(["bogus"])),
  45. ?_assertMatch(ok, rebar_file_utils:delete_each(["bogus1","bogus2"]))]}.
  46. delete_each_test_() ->
  47. {"delete_each removes files",
  48. setup,
  49. fun() ->
  50. setup(),
  51. rebar_file_utils:delete_each(file_list())
  52. end,
  53. fun teardown/1,
  54. [assert_files_not_in("source", file_list())]}.
  55. %% ====================================================================
  56. %% rm_rf tests
  57. %% ====================================================================
  58. rm_rf_wildcard_test_() ->
  59. {"rm_rf removes files based on wildcard spec",
  60. setup,
  61. fun() ->
  62. setup(),
  63. rebar_file_utils:rm_rf(filename:join([?TMP_DIR,"source","file*"]))
  64. end,
  65. fun teardown/1,
  66. [assert_files_not_in("source", file_list())]}.
  67. rm_rf_dir_test_() ->
  68. {"rm_rf removes directory tree",
  69. setup,
  70. fun() ->
  71. setup(),
  72. rebar_file_utils:rm_rf(filename:join([?TMP_DIR,"source"]))
  73. end,
  74. fun teardown/1,
  75. [?_assertNot(filelib:is_dir(filename:join([?TMP_DIR,"source"])))]}.
  76. %% ====================================================================
  77. %% cp_r tests
  78. %% ====================================================================
  79. cp_r_file_to_file_test_() ->
  80. {"cp_r copies a file to file",
  81. setup,
  82. fun() ->
  83. setup(),
  84. rebar_file_utils:cp_r([filename:join([?TMP_DIR,"source","file1"])],
  85. filename:join([?TMP_DIR,"dest","new_file"]))
  86. end,
  87. fun teardown/1,
  88. [?_assert(filelib:is_regular(filename:join([?TMP_DIR,"dest","new_file"])))]}.
  89. cp_r_file_to_dir_test_() ->
  90. {"cp_r copies a file to directory",
  91. setup,
  92. fun() ->
  93. setup(),
  94. rebar_file_utils:cp_r([filename:join([?TMP_DIR,"source","file1"])],
  95. filename:join([?TMP_DIR,"dest"]))
  96. end,
  97. fun teardown/1,
  98. [?_assert(filelib:is_regular(filename:join([?TMP_DIR,"dest","file1"])))]}.
  99. cp_r_dir_to_dir_test_() ->
  100. {"cp_r copies a directory to directory",
  101. setup,
  102. fun() ->
  103. setup(),
  104. rebar_file_utils:cp_r([filename:join([?TMP_DIR,"source"])],
  105. filename:join([?TMP_DIR,"dest"]))
  106. end,
  107. fun teardown/1,
  108. [?_assert(filelib:is_dir(filename:join([?TMP_DIR,"dest","source"]))),
  109. assert_files_in("dest/source",
  110. [filename:join([?TMP_DIR,"dest","source",F]) ||
  111. F <- ["file1","file2"]])]}.
  112. cp_r_wildcard_file_to_dir_test_() ->
  113. {"cp_r copies wildcard files to directory",
  114. setup,
  115. fun() ->
  116. setup(),
  117. rebar_file_utils:cp_r([filename:join([?TMP_DIR,"source","*1"])],
  118. filename:join([?TMP_DIR,"dest"]))
  119. end,
  120. fun teardown/1,
  121. [?_assert(filelib:is_regular(filename:join([?TMP_DIR,"dest","file1"])))]}.
  122. cp_r_wildcard_dir_to_dir_test_() ->
  123. {"cp_r copies wildcard directory to directory",
  124. setup,
  125. fun() ->
  126. setup(),
  127. rebar_file_utils:cp_r([filename:join([?TMP_DIR,"sour*"])],
  128. filename:join([?TMP_DIR,"dest"]))
  129. end,
  130. fun teardown/1,
  131. [?_assert(filelib:is_dir(filename:join([?TMP_DIR,"dest","source"]))),
  132. assert_files_in("dest/source",
  133. [filename:join([?TMP_DIR,"dest","source",F]) ||
  134. F <- ["file1","file2"]])]}.
  135. cp_r_overwrite_file_test_() ->
  136. {"cp_r overwrites destination file",
  137. setup,
  138. fun() ->
  139. setup(),
  140. ok = file:write_file(filename:join([?TMP_DIR,"dest","file1"]),
  141. <<"test">>),
  142. rebar_file_utils:cp_r([filename:join([?TMP_DIR,"source","file1"])],
  143. filename:join([?TMP_DIR,"dest"]))
  144. end,
  145. fun teardown/1,
  146. [?_assertMatch({ok,?FILE_CONTENT},
  147. file:read_file(
  148. filename:join([?TMP_DIR,"dest","file1"])))]}.
  149. cp_r_overwrite_dir_test_() ->
  150. {"cp_r overwrites destination file (xcopy case on win32)",
  151. setup,
  152. fun() ->
  153. setup(),
  154. ok = file:make_dir(filename:join([?TMP_DIR,"dest","source"])),
  155. ok = file:write_file(
  156. filename:join([?TMP_DIR,"dest","source","file1"]),
  157. <<"test">>),
  158. rebar_file_utils:cp_r([filename:join([?TMP_DIR,"source"])],
  159. filename:join([?TMP_DIR,"dest"]))
  160. end,
  161. fun teardown/1,
  162. [?_assertMatch({ok,?FILE_CONTENT},
  163. file:read_file(
  164. filename:join([?TMP_DIR,"dest","source","file1"])))]}.
  165. cp_r_overwrite_file_fail_test_() ->
  166. {"cp_r fails to fs permission errors (file:copy/2 case on win32)",
  167. setup,
  168. fun() ->
  169. setup(),
  170. ok = file:write_file(
  171. filename:join([?TMP_DIR,"dest","file1"]),<<"test">>),
  172. ok = file:change_mode(
  173. filename:join([?TMP_DIR,"dest","file1"]),0)
  174. end,
  175. fun teardown/1,
  176. [?_assertError({badmatch,_},
  177. rebar_file_utils:cp_r(
  178. [filename:join([?TMP_DIR,"source","file1"])],
  179. filename:join([?TMP_DIR,"dest"])))]}.
  180. cp_r_overwrite_dir_fail_test_() ->
  181. {"cp_r fails to fs permission error (xcopy case on win32)",
  182. setup,
  183. fun() ->
  184. setup(),
  185. ok = file:make_dir(
  186. filename:join([?TMP_DIR,"dest","source"])),
  187. ok = file:write_file(
  188. filename:join([?TMP_DIR,"dest","source","file1"]),
  189. <<"test">>),
  190. ok = file:change_mode(
  191. filename:join([?TMP_DIR,"dest","source","file1"]),0)
  192. end,
  193. fun teardown/1,
  194. [?_assertError({badmatch,_},
  195. rebar_file_utils:cp_r(
  196. [filename:join([?TMP_DIR,"source"])],
  197. filename:join([?TMP_DIR,"dest"])))]}.
  198. mv_file_test_() ->
  199. {"move a file to folder",
  200. setup,
  201. fun() ->
  202. setup(),
  203. rebar_file_utils:mv(filename:join([?TMP_DIR,"source","file1"]),
  204. filename:join([?TMP_DIR,"dest"]))
  205. end,
  206. fun teardown/1,
  207. [?_assert(filelib:is_regular(filename:join([?TMP_DIR,"dest","file1"]))),
  208. ?_assertNot(filelib:is_regular(filename:join([?TMP_DIR,"source","file1"])))]}.
  209. %% ====================================================================
  210. %% Utilities
  211. %% ====================================================================
  212. file_list() ->
  213. [filename:join([?TMP_DIR,"source",F]) || F <- ["file1","file2"]].
  214. %% ====================================================================
  215. %% Setup and Teardown
  216. %% ====================================================================
  217. setup() ->
  218. file:make_dir(?TMP_DIR),
  219. make_dir_tree(?TMP_DIR,?DIR_TREE).
  220. make_dir_tree(Parent, [{d,Dir,Contents} | Rest]) ->
  221. NewDir = filename:join(Parent,Dir),
  222. ok = file:make_dir(NewDir),
  223. ok = make_dir_tree(NewDir,Contents),
  224. ok = make_dir_tree(Parent,Rest);
  225. make_dir_tree(Parent, [{f,File} | Rest]) ->
  226. ok = file:write_file(filename:join(Parent,File),?FILE_CONTENT),
  227. ok = make_dir_tree(Parent,Rest);
  228. make_dir_tree(_,[]) ->
  229. ok.
  230. teardown(_) ->
  231. case os:type() of
  232. {unix, _} ->
  233. os:cmd("rm -rf " ++ ?TMP_DIR ++ " 2>/dev/null");
  234. {win32, _} ->
  235. os:cmd("rmdir /S /Q " ++ filename:nativename(?TMP_DIR))
  236. end.
  237. %% ====================================================================
  238. %% Assert helpers
  239. %% ====================================================================
  240. assert_files_in(Name, [File|T]) ->
  241. [{Name ++ " has file: " ++ File, ?_assert(filelib:is_regular(File))} |
  242. assert_files_in(Name, T)];
  243. assert_files_in(_, []) -> [].
  244. assert_files_not_in(Name, [File|T]) ->
  245. [{Name ++ " does not have file: " ++ File,
  246. ?_assertNot(filelib:is_regular(File))} |
  247. assert_files_not_in(Name, T)];
  248. assert_files_not_in(_, []) -> [].