diff options
author | George Roman <george.roman.99@gmail.com> | 2018-05-16 19:07:04 +0300 |
---|---|---|
committer | Hisham Muhammad <hisham@gobolinux.org> | 2018-05-17 18:30:29 -0300 |
commit | ac1cb9b178fd5bd7bbd909062034fdb60434092a (patch) | |
tree | 53a24283dcc9dae78e60de3a92d8186ef4ec3933 | |
parent | 42b85be332ebaabdbc7f457c484aeedd361d0528 (diff) | |
download | luarocks-ac1cb9b178fd5bd7bbd909062034fdb60434092a.tar.gz luarocks-ac1cb9b178fd5bd7bbd909062034fdb60434092a.tar.bz2 luarocks-ac1cb9b178fd5bd7bbd909062034fdb60434092a.zip |
Update fs module tests to use fs.set_permissions
-rw-r--r-- | spec/fs_spec.lua | 316 |
1 files changed, 186 insertions, 130 deletions
diff --git a/spec/fs_spec.lua b/spec/fs_spec.lua index a74980f3..9681b0e7 100644 --- a/spec/fs_spec.lua +++ b/spec/fs_spec.lua | |||
@@ -4,8 +4,58 @@ test_env.unload_luarocks() | |||
4 | local fs = require("luarocks.fs") | 4 | local fs = require("luarocks.fs") |
5 | local lfs = require("lfs") | 5 | local lfs = require("lfs") |
6 | local is_win = test_env.TEST_TARGET_OS == "windows" | 6 | local is_win = test_env.TEST_TARGET_OS == "windows" |
7 | local posix_ok = pcall(require, "posix") | ||
7 | 8 | ||
8 | describe("Luarocks fs test #whitebox #w_fs", function() | 9 | describe("Luarocks fs test #whitebox #w_fs", function() |
10 | local get_tmp_path = function() | ||
11 | local path = os.tmpname() | ||
12 | if is_win and not path:find(":") then | ||
13 | path = os.getenv("TEMP") .. path | ||
14 | end | ||
15 | return path | ||
16 | end | ||
17 | |||
18 | local exists_file = function(path) | ||
19 | local ok, err, code = os.rename(path, path) | ||
20 | if not ok and code == 13 then | ||
21 | return true | ||
22 | end | ||
23 | return ok | ||
24 | end | ||
25 | |||
26 | local create_file = function(path, content) | ||
27 | local fd = assert(io.open(path, "w")) | ||
28 | if not content then | ||
29 | content = "foo" | ||
30 | end | ||
31 | assert(fd:write(content)) | ||
32 | fd:close() | ||
33 | end | ||
34 | |||
35 | local make_unreadable = function(path) | ||
36 | if is_win then | ||
37 | fs.execute("icacls " .. fs.Q(path) .. " /deny %USERNAME%:(RD)") | ||
38 | else | ||
39 | fs.execute("chmod -r " .. fs.Q(path)) | ||
40 | end | ||
41 | end | ||
42 | |||
43 | local make_unwritable = function(path) | ||
44 | if is_win then | ||
45 | fs.execute("icacls " .. fs.Q(path) .. " /deny %USERNAME%:(WD,AD)") | ||
46 | else | ||
47 | fs.execute("chmod -w " .. fs.Q(path)) | ||
48 | end | ||
49 | end | ||
50 | |||
51 | local make_unexecutable = function(path) | ||
52 | if is_win then | ||
53 | fs.execute("icacls " .. fs.Q(path) .. " /deny %USERNAME%:(X)") | ||
54 | else | ||
55 | fs.execute("chmod -x " .. fs.Q(path)) | ||
56 | end | ||
57 | end | ||
58 | |||
9 | describe("fs.Q", function() | 59 | describe("fs.Q", function() |
10 | it("simple argument", function() | 60 | it("simple argument", function() |
11 | assert.are.same(is_win and '"foo"' or "'foo'", fs.Q("foo")) | 61 | assert.are.same(is_win and '"foo"' or "'foo'", fs.Q("foo")) |
@@ -20,6 +70,72 @@ describe("Luarocks fs test #whitebox #w_fs", function() | |||
20 | end) | 70 | end) |
21 | end) | 71 | end) |
22 | 72 | ||
73 | describe("fs.set_permissions", function() | ||
74 | local readfile | ||
75 | local execfile | ||
76 | local tmpdir | ||
77 | |||
78 | after_each(function() | ||
79 | if readfile then | ||
80 | os.remove(readfile) | ||
81 | readfile = nil | ||
82 | end | ||
83 | if execfile then | ||
84 | os.remove(execfile) | ||
85 | execfile = nil | ||
86 | end | ||
87 | if tmpdir then | ||
88 | lfs.rmdir(tmpdir) | ||
89 | tmpdir = nil | ||
90 | end | ||
91 | end) | ||
92 | |||
93 | it("returns true and sets the permissions of the argument accordingly", function() | ||
94 | readfile = get_tmp_path() | ||
95 | create_file(readfile) | ||
96 | make_unreadable(readfile) | ||
97 | assert.falsy(io.open(readfile, "r")) | ||
98 | assert.truthy(fs.set_permissions(readfile, "read", "user")) | ||
99 | assert.truthy(io.open(readfile, "r")) | ||
100 | |||
101 | if is_win then | ||
102 | execfile = get_tmp_path() .. ".exe" | ||
103 | create_file(execfile) | ||
104 | else | ||
105 | execfile = get_tmp_path() .. ".sh" | ||
106 | create_file(execfile, "#!/bin/bash") | ||
107 | end | ||
108 | make_unexecutable(execfile) | ||
109 | local fd = assert(io.popen(execfile .. " 2>&1")) | ||
110 | local result = assert(fd:read("*a")) | ||
111 | assert.truthy(result:match("denied")) | ||
112 | fd:close() | ||
113 | assert.truthy(fs.set_permissions(execfile, "exec", "user")) | ||
114 | fd = assert(io.popen(execfile .. " 2>&1")) | ||
115 | result = assert(fd:read("*a")) | ||
116 | assert.falsy(result:match("denied")) | ||
117 | fd:close() | ||
118 | |||
119 | tmpdir = get_tmp_path() | ||
120 | os.remove(tmpdir) | ||
121 | lfs.mkdir(tmpdir) | ||
122 | make_unexecutable(tmpdir) | ||
123 | fd = assert(io.popen("cd " .. fs.Q(tmpdir) .. " 2>&1")) | ||
124 | result = assert(fd:read("*a")) | ||
125 | assert.truthy(result:match("denied") or result:match("can't cd")) | ||
126 | fd:close() | ||
127 | assert.truthy(fs.set_permissions(tmpdir, "exec", "user")) | ||
128 | fd = assert(io.popen("cd " .. fs.Q(tmpdir) .. " 2>&1")) | ||
129 | result = assert(fd:read("*a")) | ||
130 | assert.falsy(result:match("denied") or result:match("can't cd")) | ||
131 | fd:close() | ||
132 | end) | ||
133 | |||
134 | it("returns false and does nothing if the argument is nonexistent", function() | ||
135 | assert.falsy(fs.set_permissions("/nonexistent", "read", "user")) | ||
136 | end) | ||
137 | end) | ||
138 | |||
23 | describe("fs.is_file", function() | 139 | describe("fs.is_file", function() |
24 | local tmpfile | 140 | local tmpfile |
25 | local tmpdir | 141 | local tmpdir |
@@ -36,10 +152,8 @@ describe("Luarocks fs test #whitebox #w_fs", function() | |||
36 | end) | 152 | end) |
37 | 153 | ||
38 | it("returns true when the argument is a file", function() | 154 | it("returns true when the argument is a file", function() |
39 | tmpfile = os.tmpname() | 155 | tmpfile = get_tmp_path() |
40 | local fd = assert(io.open(tmpfile, "w")) | 156 | create_file(tmpfile) |
41 | assert(fd:write("foo")) | ||
42 | fd:close() | ||
43 | assert.same(true, fs.is_file(tmpfile)) | 157 | assert.same(true, fs.is_file(tmpfile)) |
44 | end) | 158 | end) |
45 | 159 | ||
@@ -48,7 +162,7 @@ describe("Luarocks fs test #whitebox #w_fs", function() | |||
48 | end) | 162 | end) |
49 | 163 | ||
50 | it("returns false when the argument exists but is not a file", function() | 164 | it("returns false when the argument exists but is not a file", function() |
51 | tmpdir = os.tmpname() | 165 | tmpdir = get_tmp_path() |
52 | os.remove(tmpdir) | 166 | os.remove(tmpdir) |
53 | lfs.mkdir(tmpdir) | 167 | lfs.mkdir(tmpdir) |
54 | assert.same(false, fs.is_file("/nonexistent")) | 168 | assert.same(false, fs.is_file("/nonexistent")) |
@@ -71,17 +185,15 @@ describe("Luarocks fs test #whitebox #w_fs", function() | |||
71 | end) | 185 | end) |
72 | 186 | ||
73 | it("returns true when the argument is a directory", function() | 187 | it("returns true when the argument is a directory", function() |
74 | tmpdir = os.tmpname() | 188 | tmpdir = get_tmp_path() |
75 | os.remove(tmpdir) | 189 | os.remove(tmpdir) |
76 | lfs.mkdir(tmpdir) | 190 | lfs.mkdir(tmpdir) |
77 | assert.truthy(fs.is_dir(tmpdir)) | 191 | assert.truthy(fs.is_dir(tmpdir)) |
78 | end) | 192 | end) |
79 | 193 | ||
80 | it("returns false when the argument is a file", function() | 194 | it("returns false when the argument is a file", function() |
81 | tmpfile = os.tmpname() | 195 | tmpfile = get_tmp_path() |
82 | local fd = assert(io.open(tmpfile, "w")) | 196 | create_file(tmpfile) |
83 | assert(fd:write("foo")) | ||
84 | fd:close() | ||
85 | assert.falsy(fs.is_dir(tmpfile)) | 197 | assert.falsy(fs.is_dir(tmpfile)) |
86 | end) | 198 | end) |
87 | 199 | ||
@@ -106,15 +218,13 @@ describe("Luarocks fs test #whitebox #w_fs", function() | |||
106 | end) | 218 | end) |
107 | 219 | ||
108 | it("returns true when the argument is a file", function() | 220 | it("returns true when the argument is a file", function() |
109 | tmpfile = os.tmpname() | 221 | tmpfile = get_tmp_path() |
110 | local fd = assert(io.open(tmpfile, "w")) | 222 | create_file(tmpfile) |
111 | assert(fd:write("foo")) | ||
112 | fd:close() | ||
113 | assert.truthy(fs.exists(tmpfile)) | 223 | assert.truthy(fs.exists(tmpfile)) |
114 | end) | 224 | end) |
115 | 225 | ||
116 | it("returns true when the argument is a directory", function() | 226 | it("returns true when the argument is a directory", function() |
117 | tmpdir = os.tmpname() | 227 | tmpdir = get_tmp_path() |
118 | os.remove(tmpdir) | 228 | os.remove(tmpdir) |
119 | lfs.mkdir(tmpdir) | 229 | lfs.mkdir(tmpdir) |
120 | assert.truthy(fs.exists(tmpdir)) | 230 | assert.truthy(fs.exists(tmpdir)) |
@@ -147,7 +257,7 @@ describe("Luarocks fs test #whitebox #w_fs", function() | |||
147 | it("returns the current working directory", function() | 257 | it("returns the current working directory", function() |
148 | local currentdir = lfs.currentdir() | 258 | local currentdir = lfs.currentdir() |
149 | assert.same(currentdir, fs.current_dir()) | 259 | assert.same(currentdir, fs.current_dir()) |
150 | tmpdir = os.tmpname() | 260 | tmpdir = get_tmp_path() |
151 | os.remove(tmpdir) | 261 | os.remove(tmpdir) |
152 | lfs.mkdir(tmpdir) | 262 | lfs.mkdir(tmpdir) |
153 | assert.truthy(fs.change_dir(tmpdir)) | 263 | assert.truthy(fs.change_dir(tmpdir)) |
@@ -184,7 +294,7 @@ describe("Luarocks fs test #whitebox #w_fs", function() | |||
184 | end) | 294 | end) |
185 | 295 | ||
186 | it("returns true and changes the current working directory if the argument is a directory", function() | 296 | it("returns true and changes the current working directory if the argument is a directory", function() |
187 | tmpdir = os.tmpname() | 297 | tmpdir = get_tmp_path() |
188 | os.remove(tmpdir) | 298 | os.remove(tmpdir) |
189 | lfs.mkdir(tmpdir) | 299 | lfs.mkdir(tmpdir) |
190 | assert.truthy(fs.change_dir(tmpdir)) | 300 | assert.truthy(fs.change_dir(tmpdir)) |
@@ -196,10 +306,8 @@ describe("Luarocks fs test #whitebox #w_fs", function() | |||
196 | end) | 306 | end) |
197 | 307 | ||
198 | it("returns false and does nothing when the argument is a file", function() | 308 | it("returns false and does nothing when the argument is a file", function() |
199 | tmpfile = os.tmpname() | 309 | tmpfile = get_tmp_path() |
200 | local fd = assert(io.open(tmpfile, "w")) | 310 | create_file(tmpfile) |
201 | assert(fd:write("foo")) | ||
202 | fd:close() | ||
203 | assert.falsy(fs.change_dir(tmpfile)) | 311 | assert.falsy(fs.change_dir(tmpfile)) |
204 | assert.same(olddir, lfs.currentdir()) | 312 | assert.same(olddir, lfs.currentdir()) |
205 | end) | 313 | end) |
@@ -229,7 +337,7 @@ describe("Luarocks fs test #whitebox #w_fs", function() | |||
229 | end) | 337 | end) |
230 | 338 | ||
231 | it("returns true and changes the current directory to root if the current directory is valid", function() | 339 | it("returns true and changes the current directory to root if the current directory is valid", function() |
232 | tmpdir = os.tmpname() | 340 | tmpdir = get_tmp_path() |
233 | os.remove(tmpdir) | 341 | os.remove(tmpdir) |
234 | lfs.mkdir(tmpdir) | 342 | lfs.mkdir(tmpdir) |
235 | assert.truthy(fs.change_dir(tmpdir)) | 343 | assert.truthy(fs.change_dir(tmpdir)) |
@@ -241,7 +349,7 @@ describe("Luarocks fs test #whitebox #w_fs", function() | |||
241 | end) | 349 | end) |
242 | 350 | ||
243 | it("returns false and does nothing if the current directory is not valid #unix", function() | 351 | it("returns false and does nothing if the current directory is not valid #unix", function() |
244 | tmpdir = os.tmpname() | 352 | tmpdir = get_tmp_path() |
245 | os.remove(tmpdir) | 353 | os.remove(tmpdir) |
246 | lfs.mkdir(tmpdir) | 354 | lfs.mkdir(tmpdir) |
247 | lfs.chdir(tmpdir) | 355 | lfs.chdir(tmpdir) |
@@ -270,7 +378,7 @@ describe("Luarocks fs test #whitebox #w_fs", function() | |||
270 | end) | 378 | end) |
271 | 379 | ||
272 | it("returns true and changes the current directory to the previous one in the dir stack if the dir stack is not empty", function() | 380 | it("returns true and changes the current directory to the previous one in the dir stack if the dir stack is not empty", function() |
273 | tmpdir = os.tmpname() | 381 | tmpdir = get_tmp_path() |
274 | os.remove(tmpdir) | 382 | os.remove(tmpdir) |
275 | lfs.mkdir(tmpdir) | 383 | lfs.mkdir(tmpdir) |
276 | assert.truthy(fs.change_dir(tmpdir)) | 384 | assert.truthy(fs.change_dir(tmpdir)) |
@@ -300,14 +408,14 @@ describe("Luarocks fs test #whitebox #w_fs", function() | |||
300 | end) | 408 | end) |
301 | 409 | ||
302 | it("returns true and creates the directory specified by the argument", function() | 410 | it("returns true and creates the directory specified by the argument", function() |
303 | tmpdir = os.tmpname() | 411 | tmpdir = get_tmp_path() |
304 | os.remove(tmpdir) | 412 | os.remove(tmpdir) |
305 | assert.truthy(fs.make_dir(tmpdir)) | 413 | assert.truthy(fs.make_dir(tmpdir)) |
306 | assert.same("directory", lfs.attributes(tmpdir, "mode")) | 414 | assert.same("directory", lfs.attributes(tmpdir, "mode")) |
307 | end) | 415 | end) |
308 | 416 | ||
309 | it("returns true and creates the directory path specified by the argument", function() | 417 | it("returns true and creates the directory path specified by the argument", function() |
310 | tmpdir = os.tmpname() | 418 | tmpdir = get_tmp_path() |
311 | os.remove(tmpdir) | 419 | os.remove(tmpdir) |
312 | intdir = "/internaldir" | 420 | intdir = "/internaldir" |
313 | local dirpath = tmpdir .. intdir | 421 | local dirpath = tmpdir .. intdir |
@@ -317,7 +425,7 @@ describe("Luarocks fs test #whitebox #w_fs", function() | |||
317 | end) | 425 | end) |
318 | 426 | ||
319 | it("returns false and does nothing if the argument is not valid (file in the path)", function() | 427 | it("returns false and does nothing if the argument is not valid (file in the path)", function() |
320 | tmpfile = os.tmpname() | 428 | tmpfile = get_tmp_path() |
321 | local fd = assert(io.open(tmpfile, "w")) | 429 | local fd = assert(io.open(tmpfile, "w")) |
322 | assert(fd:write("foo")) | 430 | assert(fd:write("foo")) |
323 | fd:close() | 431 | fd:close() |
@@ -327,22 +435,12 @@ describe("Luarocks fs test #whitebox #w_fs", function() | |||
327 | end) | 435 | end) |
328 | 436 | ||
329 | it("returns false and does nothing if the argument already exists", function() | 437 | it("returns false and does nothing if the argument already exists", function() |
330 | tmpfile = os.tmpname() | 438 | tmpfile = get_tmp_path() |
331 | local fd = assert(io.open(tmpfile, "w")) | 439 | create_file(tmpfile) |
332 | assert(fd:write("foo")) | ||
333 | fd:close() | ||
334 | assert.falsy(fs.make_dir(tmpfile)) | 440 | assert.falsy(fs.make_dir(tmpfile)) |
335 | end) | 441 | end) |
336 | end) | 442 | end) |
337 | 443 | ||
338 | local exists_file = function(path) | ||
339 | local ok, err, code = os.rename(path, path) | ||
340 | if not ok and code == 13 then | ||
341 | return true | ||
342 | end | ||
343 | return ok | ||
344 | end | ||
345 | |||
346 | describe("fs.remove_dir_if_empty", function() | 444 | describe("fs.remove_dir_if_empty", function() |
347 | local tmpfile | 445 | local tmpfile |
348 | local tmpdir | 446 | local tmpdir |
@@ -359,7 +457,7 @@ describe("Luarocks fs test #whitebox #w_fs", function() | |||
359 | end) | 457 | end) |
360 | 458 | ||
361 | it("removes the directory specified by the argument if it is empty", function() | 459 | it("removes the directory specified by the argument if it is empty", function() |
362 | tmpdir = os.tmpname() | 460 | tmpdir = get_tmp_path() |
363 | os.remove(tmpdir) | 461 | os.remove(tmpdir) |
364 | lfs.mkdir(tmpdir) | 462 | lfs.mkdir(tmpdir) |
365 | fs.remove_dir_if_empty(tmpdir) | 463 | fs.remove_dir_if_empty(tmpdir) |
@@ -367,15 +465,12 @@ describe("Luarocks fs test #whitebox #w_fs", function() | |||
367 | end) | 465 | end) |
368 | 466 | ||
369 | it("does nothing if the directory specified by the argument is not empty", function() | 467 | it("does nothing if the directory specified by the argument is not empty", function() |
370 | tmpdir = os.tmpname() | 468 | tmpdir = get_tmp_path() |
371 | os.remove(tmpdir) | 469 | os.remove(tmpdir) |
372 | lfs.mkdir(tmpdir) | 470 | lfs.mkdir(tmpdir) |
373 | tmpfile = "/internalfile" | 471 | tmpfile = "/internalfile" |
374 | local filepath = tmpdir .. tmpfile | 472 | local filepath = tmpdir .. tmpfile |
375 | lfs.touch(filepath) | 473 | create_file(filepath) |
376 | local fd = assert(io.open(filepath, "w")) | ||
377 | assert(fd:write("foo")) | ||
378 | fd:close() | ||
379 | fs.remove_dir_if_empty(tmpdir) | 474 | fs.remove_dir_if_empty(tmpdir) |
380 | assert.truthy(exists_file(tmpdir)) | 475 | assert.truthy(exists_file(tmpdir)) |
381 | end) | 476 | end) |
@@ -402,7 +497,7 @@ describe("Luarocks fs test #whitebox #w_fs", function() | |||
402 | end) | 497 | end) |
403 | 498 | ||
404 | it("removes the directory path specified by the argument if it is empty", function() | 499 | it("removes the directory path specified by the argument if it is empty", function() |
405 | tmpdir = os.tmpname() | 500 | tmpdir = get_tmp_path() |
406 | os.remove(tmpdir) | 501 | os.remove(tmpdir) |
407 | lfs.mkdir(tmpdir) | 502 | lfs.mkdir(tmpdir) |
408 | fs.remove_dir_tree_if_empty(tmpdir) | 503 | fs.remove_dir_tree_if_empty(tmpdir) |
@@ -410,7 +505,7 @@ describe("Luarocks fs test #whitebox #w_fs", function() | |||
410 | end) | 505 | end) |
411 | 506 | ||
412 | it("does nothing if the directory path specified by the argument is not empty", function() | 507 | it("does nothing if the directory path specified by the argument is not empty", function() |
413 | tmpdir = os.tmpname() | 508 | tmpdir = get_tmp_path() |
414 | os.remove(tmpdir) | 509 | os.remove(tmpdir) |
415 | lfs.mkdir(tmpdir) | 510 | lfs.mkdir(tmpdir) |
416 | intdir = "/internaldir" | 511 | intdir = "/internaldir" |
@@ -418,7 +513,6 @@ describe("Luarocks fs test #whitebox #w_fs", function() | |||
418 | lfs.mkdir(dirpath) | 513 | lfs.mkdir(dirpath) |
419 | tmpfile = "/internalfile" | 514 | tmpfile = "/internalfile" |
420 | local filepath = dirpath .. tmpfile | 515 | local filepath = dirpath .. tmpfile |
421 | lfs.touch(filepath) | ||
422 | fs.remove_dir_tree_if_empty(tmpdir) | 516 | fs.remove_dir_tree_if_empty(tmpdir) |
423 | assert.truthy(exists_file(dirpath)) | 517 | assert.truthy(exists_file(dirpath)) |
424 | assert.truthy(exists_file(tmpdir)) | 518 | assert.truthy(exists_file(tmpdir)) |
@@ -447,71 +541,58 @@ describe("Luarocks fs test #whitebox #w_fs", function() | |||
447 | 541 | ||
448 | it("returns true and copies the contents and the permissions of the source file to the destination file", function() | 542 | it("returns true and copies the contents and the permissions of the source file to the destination file", function() |
449 | srcfile = os.tmpname() | 543 | srcfile = os.tmpname() |
450 | local fd = assert(io.open(srcfile, "w")) | 544 | create_file(srcfile, srccontent) |
451 | local srccontent = "foo" | ||
452 | assert(fd:write(srccontent)) | ||
453 | fd:close() | ||
454 | dstfile = os.tmpname() | 545 | dstfile = os.tmpname() |
455 | os.remove(dstfile) | 546 | os.remove(dstfile) |
456 | assert.truthy(fs.copy(srcfile, dstfile, nil)) | 547 | assert.truthy(fs.copy(srcfile, dstfile)) |
457 | fd = assert(io.open(dstfile, "r")) | 548 | fd = assert(io.open(dstfile, "r")) |
458 | local dstcontent = fd:read("*a") | 549 | local dstcontent = fd:read("*a") |
459 | assert.same(srccontent, dstcontent) | 550 | assert.same("foo", dstcontent) |
460 | if not is_win then | 551 | if posix_ok then |
461 | assert.same(lfs.attributes(srcfile, "permissions"), lfs.attributes(dstfile, "permissions")) | 552 | assert.same(lfs.attributes(srcfile, "permissions"), lfs.attributes(dstfile, "permissions")) |
462 | end | 553 | end |
463 | end) | 554 | end) |
464 | 555 | ||
465 | it("returns true and copies contents of the source file to the destination file with custom permissions", function() | 556 | it("returns true and copies contents of the source file to the destination file with custom permissions", function() |
466 | srcfile = os.tmpname() | 557 | srcfile = os.tmpname() |
467 | local fd = assert(io.open(srcfile, "w")) | 558 | create_file(srcfile, srccontent) |
468 | local srccontent = "foo" | ||
469 | assert(fd:write(srccontent)) | ||
470 | fd:close() | ||
471 | dstfile = os.tmpname() | 559 | dstfile = os.tmpname() |
472 | os.remove(dstfile) | 560 | os.remove(dstfile) |
473 | assert.truthy(fs.copy(srcfile, dstfile, "755")) | 561 | assert.truthy(fs.copy(srcfile, dstfile, "exec")) |
474 | fd = assert(io.open(dstfile, "r")) | 562 | fd = assert(io.open(dstfile, "r")) |
475 | local dstcontent = fd:read("*a") | 563 | local dstcontent = fd:read("*a") |
476 | assert.same(srccontent, dstcontent) | 564 | assert.same("foo", dstcontent) |
477 | if not is_win then | ||
478 | assert.same("rwxr-xr-x", lfs.attributes(dstfile, "permissions")) | ||
479 | end | ||
480 | end) | 565 | end) |
481 | 566 | ||
482 | it("returns false and does nothing if the source file does not exist", function() | 567 | it("returns false and does nothing if the source file does not exist", function() |
483 | srcfile = os.tmpname() | 568 | srcfile = get_tmp_path() |
484 | os.remove(srcfile) | 569 | os.remove(srcfile) |
485 | dstfile = os.tmpname() | 570 | dstfile = get_tmp_path() |
486 | os.remove(dstfile) | 571 | os.remove(dstfile) |
487 | assert.falsy(fs.copy(srcfile, dstfile, nil)) | 572 | assert.falsy(fs.copy(srcfile, dstfile, nil)) |
488 | assert.falsy(exists_file(dstfile)) | 573 | assert.falsy(exists_file(dstfile)) |
489 | end) | 574 | end) |
490 | 575 | ||
491 | it("returns false and does nothing if the source file doesn't have the proper permissions #unix", function() | 576 | it("returns false and does nothing if the source file doesn't have the proper permissions", function() |
492 | srcfile = os.tmpname() | 577 | srcfile = get_tmp_path() |
493 | local fd = assert(io.open(srcfile, "w")) | 578 | create_file(srcfile) |
494 | assert(fd:write("foo")) | 579 | make_unreadable(srcfile) |
495 | fd:close() | 580 | dstfile = get_tmp_path() |
496 | assert(fs.chmod(srcfile, "333")) | ||
497 | dstfile = os.tmpname() | ||
498 | os.remove(dstfile) | 581 | os.remove(dstfile) |
499 | assert.falsy(fs.copy(srcfile, dstfile, nil)) | 582 | assert.falsy(fs.copy(srcfile, dstfile, nil)) |
500 | assert.falsy(exists_file(dstfile)) | 583 | assert.falsy(exists_file(dstfile)) |
501 | end) | 584 | end) |
502 | 585 | ||
503 | it("returns false and does nothing if the destination file directory doesn't have the proper permissions #unix", function() | 586 | it("returns false and does nothing if the destination file directory doesn't have the proper permissions", function() |
504 | srcfile = os.tmpname() | 587 | srcfile = get_tmp_path() |
505 | local fd = assert(io.open(srcfile, "w")) | 588 | create_file(srcfile) |
506 | assert(fd:write("foo")) | 589 | tmpdir = get_tmp_path() |
507 | fd:close() | ||
508 | tmpdir = os.tmpname() | ||
509 | os.remove(tmpdir) | 590 | os.remove(tmpdir) |
510 | lfs.mkdir(tmpdir) | 591 | lfs.mkdir(tmpdir) |
511 | assert(fs.chmod(tmpdir, "666")) | 592 | make_unwritable(tmpdir) |
512 | dstfile = tmpdir .. "/dstfile" | 593 | dstfile = tmpdir .. "/dstfile" |
513 | assert.falsy(fs.copy(srcfile, dstfile, nil)) | 594 | assert.falsy(fs.copy(srcfile, dstfile, nil)) |
514 | assert(fs.chmod(tmpdir, "777")) | 595 | assert(fs.set_permissions(tmpdir, "exec", "all")) |
515 | assert.falsy(exists_file(dstfile)) | 596 | assert.falsy(exists_file(dstfile)) |
516 | end) | 597 | end) |
517 | end) | 598 | end) |
@@ -552,23 +633,20 @@ describe("Luarocks fs test #whitebox #w_fs", function() | |||
552 | end) | 633 | end) |
553 | 634 | ||
554 | local create_dir_tree = function() | 635 | local create_dir_tree = function() |
555 | srcdir = os.tmpname() | 636 | srcdir = get_tmp_path() |
556 | os.remove(srcdir) | 637 | os.remove(srcdir) |
557 | lfs.mkdir(srcdir) | 638 | lfs.mkdir(srcdir) |
558 | srcintdir = srcdir .. "/internaldir" | 639 | srcintdir = srcdir .. "/internaldir" |
559 | lfs.mkdir(srcintdir) | 640 | lfs.mkdir(srcintdir) |
560 | srcfile = srcintdir .. "/internalfile" | 641 | srcfile = srcintdir .. "/internalfile" |
561 | lfs.touch(srcfile) | 642 | create_file(srcfile) |
562 | local fd = assert(io.open(srcfile, "w")) | 643 | dstdir = get_tmp_path() |
563 | assert(fd:write("foo")) | ||
564 | fd:close() | ||
565 | dstdir = os.tmpname() | ||
566 | os.remove(dstdir) | 644 | os.remove(dstdir) |
567 | end | 645 | end |
568 | 646 | ||
569 | it("returns true and copies the contents (with their permissions) of the source dir to the destination dir", function() | 647 | it("returns true and copies the contents (with their permissions) of the source dir to the destination dir", function() |
570 | create_dir_tree() | 648 | create_dir_tree() |
571 | assert.truthy(fs.copy_contents(srcdir, dstdir, nil)) | 649 | assert.truthy(fs.copy_contents(srcdir, dstdir)) |
572 | assert.truthy(exists_file(dstdir)) | 650 | assert.truthy(exists_file(dstdir)) |
573 | dstintdir = dstdir .. "/internaldir" | 651 | dstintdir = dstdir .. "/internaldir" |
574 | assert.truthy(exists_file(dstintdir)) | 652 | assert.truthy(exists_file(dstintdir)) |
@@ -576,14 +654,14 @@ describe("Luarocks fs test #whitebox #w_fs", function() | |||
576 | local fd = assert(io.open(dstfile, "r")) | 654 | local fd = assert(io.open(dstfile, "r")) |
577 | local dstfilecontent = fd:read("*a") | 655 | local dstfilecontent = fd:read("*a") |
578 | assert.same("foo", dstfilecontent) | 656 | assert.same("foo", dstfilecontent) |
579 | if not is_win then | 657 | if posix_ok then |
580 | assert.same(lfs.attributes(srcfile, "permissions"), lfs.attributes(dstfile, "permissions")) | 658 | assert.same(lfs.attributes(srcfile, "permissions"), lfs.attributes(dstfile, "permissions")) |
581 | end | 659 | end |
582 | end) | 660 | end) |
583 | 661 | ||
584 | it("returns true and copies the contents of the source dir to the destination dir with custom permissions", function() | 662 | it("returns true and copies the contents of the source dir to the destination dir with custom permissions", function() |
585 | create_dir_tree() | 663 | create_dir_tree() |
586 | assert.truthy(fs.copy_contents(srcdir, dstdir, "755")) | 664 | assert.truthy(fs.copy_contents(srcdir, dstdir, "read")) |
587 | assert.truthy(exists_file(dstdir)) | 665 | assert.truthy(exists_file(dstdir)) |
588 | dstintdir = dstdir .. "/internaldir" | 666 | dstintdir = dstdir .. "/internaldir" |
589 | assert.truthy(exists_file(dstintdir)) | 667 | assert.truthy(exists_file(dstintdir)) |
@@ -591,36 +669,32 @@ describe("Luarocks fs test #whitebox #w_fs", function() | |||
591 | local fd = assert(io.open(dstfile, "r")) | 669 | local fd = assert(io.open(dstfile, "r")) |
592 | local dstfilecontent = fd:read("*a") | 670 | local dstfilecontent = fd:read("*a") |
593 | assert.same("foo", dstfilecontent) | 671 | assert.same("foo", dstfilecontent) |
594 | if not is_win then | ||
595 | assert.same("rwxr-xr-x", lfs.attributes(dstfile, "permissions")) | ||
596 | end | ||
597 | end) | 672 | end) |
598 | 673 | ||
599 | it("returns false and does nothing if the source dir doesn't exist", function() | 674 | it("returns false and does nothing if the source dir doesn't exist", function() |
600 | srcdir = os.tmpname() | 675 | srcdir = get_tmp_path() |
601 | os.remove(srcdir) | 676 | os.remove(srcdir) |
602 | dstdir = os.tmpname() | 677 | dstdir = get_tmp_path() |
603 | os.remove(dstdir) | 678 | os.remove(dstdir) |
604 | assert.falsy(fs.copy_contents(srcdir, dstdir, nil)) | 679 | assert.falsy(fs.copy_contents(srcdir, dstdir)) |
605 | assert.falsy(exists_file(dstdir)) | 680 | assert.falsy(exists_file(dstdir)) |
606 | end) | 681 | end) |
607 | 682 | ||
608 | it("returns false if the source argument is a file", function() | 683 | it("returns false if the source argument is a file", function() |
609 | srcdir = os.tmpname() | 684 | srcdir = get_tmp_path() |
610 | local fd = assert(io.open(srcdir, "w")) | 685 | create_file(srcdir) |
611 | assert(fd:write("foo")) | 686 | dstdir = get_tmp_path() |
612 | fd:close() | ||
613 | dstdir = os.tmpname() | ||
614 | os.remove(dstdir) | 687 | os.remove(dstdir) |
615 | assert.falsy(fs.copy_contents(srcdir, dstdir, nil)) | 688 | assert.falsy(fs.copy_contents(srcdir, dstdir)) |
616 | assert.falsy(exists_file(dstdir)) | 689 | assert.falsy(exists_file(dstdir)) |
617 | end) | 690 | end) |
618 | 691 | ||
619 | it("returns false and does nothing if the source dir doesn't have the proper permissions #unix", function() | 692 | it("returns false and does nothing if the source dir doesn't have the proper permissions", function() |
620 | create_dir_tree() | 693 | create_dir_tree() |
621 | assert(fs.chmod(srcdir, "333")) | 694 | make_unreadable(srcdir) |
622 | assert.falsy(fs.copy_contents(srcdir, dstdir, nil)) | 695 | assert.falsy(fs.copy_contents(srcdir, dstdir)) |
623 | assert.falsy(exists_file(dstdir)) | 696 | assert.falsy(exists_file(dstdir .. "/internaldir")) |
697 | assert.falsy(exists_file(dstdir .. "/internalfile")) | ||
624 | end) | 698 | end) |
625 | end) | 699 | end) |
626 | 700 | ||
@@ -650,26 +724,20 @@ describe("Luarocks fs test #whitebox #w_fs", function() | |||
650 | end) | 724 | end) |
651 | 725 | ||
652 | local create_dir_tree = function() | 726 | local create_dir_tree = function() |
653 | tmpdir = os.tmpname() | 727 | tmpdir = get_tmp_path() |
654 | os.remove(tmpdir) | 728 | os.remove(tmpdir) |
655 | lfs.mkdir(tmpdir) | 729 | lfs.mkdir(tmpdir) |
656 | tmpintdir = tmpdir .. "/internaldir" | 730 | tmpintdir = tmpdir .. "/internaldir" |
657 | lfs.mkdir(tmpintdir) | 731 | lfs.mkdir(tmpintdir) |
658 | tmpfile1 = tmpdir .. "/internalfile1" | 732 | tmpfile1 = tmpdir .. "/internalfile1" |
659 | lfs.touch(tmpfile1) | 733 | create_file(tmpfile1) |
660 | local fd = assert(io.open(tmpfile1, "w")) | 734 | tmpfile2 = tmpdir .. "/internalfile2" |
661 | assert(fd:write("foo")) | 735 | create_file(tmpfile2) |
662 | fd:close() | ||
663 | tmpfile2 = tmpintdir .. "/internalfile2" | ||
664 | lfs.touch(tmpfile2) | ||
665 | fd = assert(io.open(tmpfile2, "w")) | ||
666 | assert(fd:write("foo")) | ||
667 | fd:close() | ||
668 | end | 736 | end |
669 | 737 | ||
670 | it("deletes the file specified by the argument", function() | 738 | it("deletes the file specified by the argument", function() |
671 | tmpfile1 = os.tmpname() | 739 | tmpfile1 = get_tmp_path() |
672 | tmpfile2 = os.tmpname() | 740 | tmpfile2 = get_tmp_path() |
673 | fs.delete(tmpfile1) | 741 | fs.delete(tmpfile1) |
674 | fs.delete(tmpfile2) | 742 | fs.delete(tmpfile2) |
675 | assert.falsy(exists_file(tmpfile1)) | 743 | assert.falsy(exists_file(tmpfile1)) |
@@ -684,17 +752,5 @@ describe("Luarocks fs test #whitebox #w_fs", function() | |||
684 | assert.falsy(exists_file(tmpfile1)) | 752 | assert.falsy(exists_file(tmpfile1)) |
685 | assert.falsy(exists_file(tmpdir)) | 753 | assert.falsy(exists_file(tmpdir)) |
686 | end) | 754 | end) |
687 | |||
688 | it("does nothing if the parent directory of the argument doesn't have the proper permissions #unix", function() | ||
689 | create_dir_tree() | ||
690 | assert(fs.chmod(tmpdir, "000")) | ||
691 | fs.delete(tmpfile1) | ||
692 | fs.delete(tmpfile2) | ||
693 | fs.delete(tmpintdir) | ||
694 | assert.truthy(exists_file(tmpfile2)) | ||
695 | assert.truthy(exists_file(tmpintdir)) | ||
696 | assert.truthy(exists_file(tmpfile1)) | ||
697 | assert.truthy(exists_file(tmpdir)) | ||
698 | end) | ||
699 | end) | 755 | end) |
700 | end) | 756 | end) |