summaryrefslogtreecommitdiff
path: root/test/rebar_file_utils_tests.erl
blob: fd9a141c967d7549c4ac930d71bdbfc274027b84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
%% -*- tab-width: 4;erlang-indent-level: 4;indent-tabs-mode: nil -*-
%% ex: ts=4 sw=4 et
%% -------------------------------------------------------------------
%%
%% rebar: Erlang Build Tools
%%
%% Copyright (c) 2009, 2010 Dave Smith (dizzyd@dizzyd.com)
%%
%% Permission is hereby granted, free of charge, to any person obtaining a copy
%% of this software and associated documentation files (the "Software"), to deal
%% in the Software without restriction, including without limitation the rights
%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
%% copies of the Software, and to permit persons to whom the Software is
%% furnished to do so, subject to the following conditions:
%%
%% The above copyright notice and this permission notice shall be included in
%% all copies or substantial portions of the Software.
%%
%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
%% THE SOFTWARE.
%% -------------------------------------------------------------------
%% @author Juhani Rankimies <juhani@juranki.com>
%% @doc Tests functionality of rebar_file_utils module.
%% @copyright 2009, 2010 Dave Smith
%% -------------------------------------------------------------------
-module(rebar_file_utils_tests).

-compile(export_all).

-include_lib("eunit/include/eunit.hrl").

-define(TMP_DIR, "tmp_file_utils").
-define(DIR_TREE, [{d,"source",[{f,"file1"},
                                {f,"file2"}]},
                   {d,"dest",[]}]).
-define(FILE_CONTENT, <<"1234567890">>).

%% ====================================================================
%% delete_each tests
%% ====================================================================

delete_bogus_test_() ->
    {"delete_each survives nonexisting files",
     [?_assertMatch(ok, rebar_file_utils:delete_each(["bogus"])),
      ?_assertMatch(ok, rebar_file_utils:delete_each(["bogus1","bogus2"]))]}.

delete_each_test_() ->
    {"delete_each removes files",
     setup,
     fun() ->
             setup(),
             rebar_file_utils:delete_each(file_list())
     end,
     fun teardown/1,
     [assert_files_not_in("source", file_list())]}.

%% ====================================================================
%% rm_rf tests
%% ====================================================================

rm_rf_wildcard_test_() ->
    {"rm_rf removes files based on wildcard spec",
     setup,
     fun() ->
             setup(),
             rebar_file_utils:rm_rf(filename:join([?TMP_DIR,"source","file*"]))
     end,
     fun teardown/1,
     [assert_files_not_in("source", file_list())]}.

rm_rf_dir_test_() ->
    {"rm_rf removes directory tree",
     setup,
     fun() ->
             setup(),
             rebar_file_utils:rm_rf(filename:join([?TMP_DIR,"source"]))
     end,
     fun teardown/1,
     [?_assertNot(filelib:is_dir(filename:join([?TMP_DIR,"source"])))]}.

%% ====================================================================
%% cp_r tests
%% ====================================================================

cp_r_file_to_file_test_() ->
    {"cp_r copies a file to file",
     setup,
     fun() ->
             setup(),
             rebar_file_utils:cp_r([filename:join([?TMP_DIR,"source","file1"])],
                                   filename:join([?TMP_DIR,"dest","new_file"]))
     end,
     fun teardown/1,
     [?_assert(filelib:is_file(filename:join([?TMP_DIR,"dest","new_file"])))]}.

cp_r_file_to_dir_test_() ->
    {"cp_r copies a file to directory",
     setup,
     fun() ->
             setup(),
             rebar_file_utils:cp_r([filename:join([?TMP_DIR,"source","file1"])],
                                   filename:join([?TMP_DIR,"dest"]))
     end,
     fun teardown/1,
     [?_assert(filelib:is_file(filename:join([?TMP_DIR,"dest","file1"])))]}.

cp_r_dir_to_dir_test_() ->
    {"cp_r copies a directory to directory",
     setup,
     fun() ->
             setup(),
             rebar_file_utils:cp_r([filename:join([?TMP_DIR,"source"])],
                                   filename:join([?TMP_DIR,"dest"]))
     end,
     fun teardown/1,
     [?_assert(filelib:is_dir(filename:join([?TMP_DIR,"dest","source"]))),
      assert_files_in("dest/source",
                      [filename:join([?TMP_DIR,"dest","source",F]) ||
                          F <- ["file1","file2"]])]}.

cp_r_wildcard_file_to_dir_test_() ->
    {"cp_r copies wildcard files to directory",
     setup,
     fun() ->
             setup(),
             rebar_file_utils:cp_r([filename:join([?TMP_DIR,"source","*1"])],
                                   filename:join([?TMP_DIR,"dest"]))
     end,
     fun teardown/1,
     [?_assert(filelib:is_file(filename:join([?TMP_DIR,"dest","file1"])))]}.

cp_r_wildcard_dir_to_dir_test_() ->
    {"cp_r copies wildcard directory to directory",
     setup,
     fun() ->
             setup(),
             rebar_file_utils:cp_r([filename:join([?TMP_DIR,"sour*"])],
                                   filename:join([?TMP_DIR,"dest"]))
     end,
     fun teardown/1,
     [?_assert(filelib:is_dir(filename:join([?TMP_DIR,"dest","source"]))),
      assert_files_in("dest/source",
                      [filename:join([?TMP_DIR,"dest","source",F]) ||
                          F <- ["file1","file2"]])]}.

cp_r_overwrite_file_test_() ->
    {"cp_r overwrites destination file",
     setup,
     fun() ->
             setup(),
             ok = file:write_file(filename:join([?TMP_DIR,"dest","file1"]),
                                  <<"test">>),
             rebar_file_utils:cp_r([filename:join([?TMP_DIR,"source","file1"])],
                                   filename:join([?TMP_DIR,"dest"]))
     end,
     fun teardown/1,
     [?_assertMatch({ok,?FILE_CONTENT},
                    file:read_file(
                      filename:join([?TMP_DIR,"dest","file1"])))]}.

cp_r_overwrite_dir_test_() ->
    {"cp_r overwrites destination file (xcopy case on win32)",
     setup,
     fun() ->
             setup(),
             ok = file:make_dir(filename:join([?TMP_DIR,"dest","source"])),
             ok = file:write_file(
                    filename:join([?TMP_DIR,"dest","source","file1"]),
                    <<"test">>),
             rebar_file_utils:cp_r([filename:join([?TMP_DIR,"source"])],
                                   filename:join([?TMP_DIR,"dest"]))
     end,
     fun teardown/1,
     [?_assertMatch({ok,?FILE_CONTENT},
                    file:read_file(
                      filename:join([?TMP_DIR,"dest","source","file1"])))]}.

cp_r_overwrite_file_fail_test_() ->
    {"cp_r fails to fs permission errors (file:copy/2 case on win32)",
     setup,
     fun() ->
             setup(),
             ok = file:write_file(
                    filename:join([?TMP_DIR,"dest","file1"]),<<"test">>),
             ok = file:change_mode(
                    filename:join([?TMP_DIR,"dest","file1"]),0)
     end,
     fun teardown/1,
     [?_assertError({badmatch,_},
                    rebar_file_utils:cp_r(
                      [filename:join([?TMP_DIR,"source","file1"])],
                      filename:join([?TMP_DIR,"dest"])))]}.

cp_r_overwrite_dir_fail_test_() ->
    {"cp_r fails to fs permission error (xcopy case on win32)",
     setup,
     fun() ->
             setup(),
             ok = file:make_dir(
                    filename:join([?TMP_DIR,"dest","source"])),
             ok = file:write_file(
                    filename:join([?TMP_DIR,"dest","source","file1"]),
                    <<"test">>),
             ok = file:change_mode(
                    filename:join([?TMP_DIR,"dest","source","file1"]),0)
     end,
     fun teardown/1,
     [?_assertError({badmatch,_},
                    rebar_file_utils:cp_r(
                      [filename:join([?TMP_DIR,"source"])],
                      filename:join([?TMP_DIR,"dest"])))]}.

mv_file_test_() ->
    {"move a file to folder",
     setup,
     fun() ->
             setup(),
             rebar_file_utils:mv(filename:join([?TMP_DIR,"source","file1"]),
                                 filename:join([?TMP_DIR,"dest"]))
     end,
     fun teardown/1,
     [?_assert(filelib:is_file(filename:join([?TMP_DIR,"dest","file1"]))),
      ?_assertNot(filelib:is_file(filename:join([?TMP_DIR,"source","file1"])))]}.

%% ====================================================================
%% Utilities
%% ====================================================================

file_list() ->
    [filename:join([?TMP_DIR,"source",F]) || F <- ["file1","file2"]].

%% ====================================================================
%% Setup and Teardown
%% ====================================================================

setup() ->
    file:make_dir(?TMP_DIR),
    make_dir_tree(?TMP_DIR,?DIR_TREE).

make_dir_tree(Parent, [{d,Dir,Contents} | Rest]) ->
    NewDir = filename:join(Parent,Dir),
    ok = file:make_dir(NewDir),
    ok = make_dir_tree(NewDir,Contents),
    ok = make_dir_tree(Parent,Rest);
make_dir_tree(Parent, [{f,File} | Rest]) ->
    ok = file:write_file(filename:join(Parent,File),?FILE_CONTENT),
    ok = make_dir_tree(Parent,Rest);
make_dir_tree(_,[]) ->
    ok.

teardown(_) ->
    case os:type() of
        {unix, _} ->
            os:cmd("rm -rf " ++ ?TMP_DIR ++ " 2>/dev/null");
        {win32, _} ->
            os:cmd("rmdir /S /Q " ++ filename:nativename(?TMP_DIR))
    end.

%% ====================================================================
%% Assert helpers
%% ====================================================================

assert_files_in(Name, [File|T]) ->
    [{Name ++ " has file: " ++ File, ?_assert(filelib:is_file(File))} |
     assert_files_in(Name, T)];
assert_files_in(_, []) -> [].

assert_files_not_in(Name, [File|T]) ->
    [{Name ++ " does not have file: " ++ File,
      ?_assertNot(filelib:is_file(File))} |
     assert_files_not_in(Name, T)];
assert_files_not_in(_, []) -> [].