aboutsummaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorGeorge Roman <george.roman.99@gmail.com>2018-03-05 21:49:03 +0200
committerHisham Muhammad <hisham@gobolinux.org>2018-04-19 11:37:53 -0300
commit28b0940125d24f448895c5a68d52eafc798cc547 (patch)
treee338c72b87603e71ff4b525aec2f46ec97fe072b /spec
parentd7c9f4b6d6d44d3b6b668fa2e3084a01a09fd80a (diff)
downloadluarocks-28b0940125d24f448895c5a68d52eafc798cc547.tar.gz
luarocks-28b0940125d24f448895c5a68d52eafc798cc547.tar.bz2
luarocks-28b0940125d24f448895c5a68d52eafc798cc547.zip
Add tests for some filesystem functions
Diffstat (limited to 'spec')
-rw-r--r--spec/fs_spec.lua662
1 files changed, 658 insertions, 4 deletions
diff --git a/spec/fs_spec.lua b/spec/fs_spec.lua
index a6622f16..b36fded3 100644
--- a/spec/fs_spec.lua
+++ b/spec/fs_spec.lua
@@ -23,7 +23,7 @@ describe("Luarocks fs test #whitebox #w_fs", function()
23 describe("fs.is_file", function() 23 describe("fs.is_file", function()
24 local tmpfile 24 local tmpfile
25 local tmpdir 25 local tmpdir
26 26
27 after_each(function() 27 after_each(function()
28 if tmpfile then 28 if tmpfile then
29 os.remove(tmpfile) 29 os.remove(tmpfile)
@@ -34,11 +34,11 @@ describe("Luarocks fs test #whitebox #w_fs", function()
34 tmpdir = nil 34 tmpdir = nil
35 end 35 end
36 end) 36 end)
37 37
38 it("returns true when the argument is a file", function() 38 it("returns true when the argument is a file", function()
39 tmpfile = os.tmpname() 39 tmpfile = os.tmpname()
40 local fd = assert(io.open(tmpfile, "w")) 40 local fd = assert(io.open(tmpfile, "w"))
41 fd:write("foo") 41 assert(fd:write("foo"))
42 fd:close() 42 fd:close()
43 assert.same(true, fs.is_file(tmpfile)) 43 assert.same(true, fs.is_file(tmpfile))
44 end) 44 end)
@@ -47,11 +47,665 @@ describe("Luarocks fs test #whitebox #w_fs", function()
47 assert.same(false, fs.is_file("/nonexistent")) 47 assert.same(false, fs.is_file("/nonexistent"))
48 end) 48 end)
49 49
50 it("returns false when arguments exists but is not a file", function() 50 it("returns false when the argument exists but is not a file", function()
51 tmpdir = os.tmpname() 51 tmpdir = os.tmpname()
52 os.remove(tmpdir) 52 os.remove(tmpdir)
53 lfs.mkdir(tmpdir) 53 lfs.mkdir(tmpdir)
54 assert.same(false, fs.is_file("/nonexistent")) 54 assert.same(false, fs.is_file("/nonexistent"))
55 end) 55 end)
56 end) 56 end)
57
58 describe("fs.is_dir", function()
59 local tmpfile
60 local tmpdir
61
62 after_each(function()
63 if tmpfile then
64 os.remove(tmpfile)
65 tmpfile = nil
66 end
67 if tmpdir then
68 lfs.rmdir(tmpdir)
69 tmpdir = nil
70 end
71 end)
72
73 it("returns true when the argument is a directory", function()
74 tmpdir = os.tmpname()
75 os.remove(tmpdir)
76 lfs.mkdir(tmpdir)
77 assert.truthy(fs.is_dir(tmpdir))
78 end)
79
80 it("returns false when the argument is a file", function()
81 tmpfile = os.tmpname()
82 local fd = assert(io.open(tmpfile, "w"))
83 assert(fd:write("foo"))
84 fd:close()
85 assert.falsy(fs.is_dir(tmpfile))
86 end)
87
88 it("returns false when the argument does not exist", function()
89 assert.falsy(fs.is_dir("/nonexistent"))
90 end)
91 end)
92
93 describe("fs.exists", function()
94 local tmpfile
95 local tmpdir
96
97 after_each(function()
98 if tmpfile then
99 os.remove(tmpfile)
100 tmpfile = nil
101 end
102 if tmpdir then
103 lfs.rmdir(tmpdir)
104 tmpdir = nil
105 end
106 end)
107
108 it("returns true when the argument is a file", function()
109 tmpfile = os.tmpname()
110 local fd = assert(io.open(tmpfile, "w"))
111 assert(fd:write("foo"))
112 fd:close()
113 assert.truthy(fs.exists(tmpfile))
114 end)
115
116 it("returns true when the argument is a directory", function()
117 tmpdir = os.tmpname()
118 os.remove(tmpdir)
119 lfs.mkdir(tmpdir)
120 assert.truthy(fs.exists(tmpdir))
121 end)
122
123 it("returns false when the argument does not exist", function()
124 assert.falsy(fs.exists("/nonexistent"))
125 end)
126 end)
127
128 describe("fs.current_dir", function()
129 local tmpdir
130 local olddir
131
132 before_each(function()
133 olddir = lfs.currentdir()
134 end)
135
136 after_each(function()
137 if tmpdir then
138 lfs.rmdir(tmpdir)
139 tmpdir = nil
140 end
141 if olddir then
142 lfs.chdir(olddir)
143 olddir = nil
144 end
145 end)
146
147 it("returns the current working directory", function()
148 local currentdir = lfs.currentdir()
149 assert.same(currentdir, fs.current_dir())
150 tmpdir = os.tmpname()
151 os.remove(tmpdir)
152 lfs.mkdir(tmpdir)
153 assert.truthy(fs.change_dir(tmpdir))
154 if is_win then
155 assert.same(tmpdir, fs.current_dir())
156 else
157 assert.same(lfs.attributes(tmpdir).ino, lfs.attributes(fs.current_dir()).ino)
158 end
159 end)
160 end)
161
162 describe("fs.change_dir", function()
163 local tmpfile
164 local tmpdir
165 local olddir
166
167 before_each(function()
168 olddir = lfs.currentdir()
169 end)
170
171 after_each(function()
172 if tmpfile then
173 os.remove(tmpfile)
174 tmpfile = nil
175 end
176 if tmpdir then
177 lfs.rmdir(tmpdir)
178 tmpdir = nil
179 end
180 if olddir then
181 lfs.chdir(olddir)
182 olddir = nil
183 end
184 end)
185
186 it("returns true and changes the current working directory if the argument is a directory", function()
187 tmpdir = os.tmpname()
188 os.remove(tmpdir)
189 lfs.mkdir(tmpdir)
190 assert.truthy(fs.change_dir(tmpdir))
191 if is_win then
192 assert.same(tmpdir, fs.current_dir())
193 else
194 assert.same(lfs.attributes(tmpdir).ino, lfs.attributes(lfs.currentdir()).ino)
195 end
196 end)
197
198 it("returns false and does nothing when the argument is a file", function()
199 tmpfile = os.tmpname()
200 local fd = assert(io.open(tmpfile, "w"))
201 assert(fd:write("foo"))
202 fd:close()
203 assert.falsy(fs.change_dir(tmpfile))
204 assert.same(olddir, lfs.currentdir())
205 end)
206
207 it("returns false and does nothing when the argument does not exist", function()
208 assert.falsy(fs.change_dir("/nonexistent"))
209 assert.same(olddir, lfs.currentdir())
210 end)
211 end)
212
213 describe("fs.change_dir_to_root", function()
214 local tmpdir
215 local olddir
216
217 before_each(function()
218 olddir = lfs.currentdir()
219 end)
220
221 after_each(function()
222 if tmpdir then
223 lfs.rmdir(tmpdir)
224 tmpdir = nil
225 end
226 if olddir then
227 lfs.chdir(olddir)
228 end
229 end)
230
231 it("returns true and changes the current directory to root if the current directory is valid", function()
232 tmpdir = os.tmpname()
233 os.remove(tmpdir)
234 lfs.mkdir(tmpdir)
235 assert.truthy(fs.change_dir(tmpdir))
236 local success = fs.change_dir_to_root()
237 if not is_win then
238 assert.truthy(success)
239 end
240 assert.same("/", fs.current_dir())
241 end)
242
243 it("returns false and does nothing if the current directory is not valid #unix", function()
244 tmpdir = os.tmpname()
245 os.remove(tmpdir)
246 lfs.mkdir(tmpdir)
247 lfs.chdir(tmpdir)
248 lfs.rmdir(tmpdir)
249 assert.falsy(fs.change_dir_to_root())
250 assert.is_not.same("/", lfs.currentdir())
251 end)
252 end)
253
254 describe("fs.pop_dir", function()
255 local tmpdir
256 local olddir
257
258 before_each(function()
259 olddir = lfs.currentdir()
260 end)
261
262 after_each(function()
263 if tmpdir then
264 lfs.rmdir(tmpdir)
265 tmpdir = nil
266 end
267 if olddir then
268 lfs.chdir(olddir)
269 end
270 end)
271
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()
273 tmpdir = os.tmpname()
274 os.remove(tmpdir)
275 lfs.mkdir(tmpdir)
276 assert.truthy(fs.change_dir(tmpdir))
277 assert.truthy(fs.pop_dir())
278 assert.same(olddir, lfs.currentdir())
279 end)
280 end)
281
282 describe("fs.make_dir", function()
283 local tmpfile
284 local tmpdir
285 local intdir
286
287 after_each(function()
288 if tmpfile then
289 os.remove(tmpfile)
290 tmpfile = nil
291 end
292 if intdir then
293 lfs.rmdir(intdir)
294 intdir = nil
295 end
296 if tmpdir then
297 lfs.rmdir(tmpdir)
298 tmpdir = nil
299 end
300 end)
301
302 it("returns true and creates the directory specified by the argument", function()
303 tmpdir = os.tmpname()
304 os.remove(tmpdir)
305 assert.truthy(fs.make_dir(tmpdir))
306 assert.same("directory", lfs.attributes(tmpdir, "mode"))
307 end)
308
309 it("returns true and creates the directory path specified by the argument", function()
310 tmpdir = os.tmpname()
311 os.remove(tmpdir)
312 intdir = "/internaldir"
313 local dirpath = tmpdir .. intdir
314 assert.truthy(fs.make_dir(dirpath))
315 assert.same("directory", lfs.attributes(tmpdir, "mode"))
316 assert.same("directory", lfs.attributes(dirpath, "mode"))
317 end)
318
319 it("returns false and does nothing if the argument is not valid (file in the path)", function()
320 tmpfile = os.tmpname()
321 local fd = assert(io.open(tmpfile, "w"))
322 assert(fd:write("foo"))
323 fd:close()
324 intdir = "/internaldir"
325 local dirpath = tmpfile .. intdir
326 assert.falsy(fs.make_dir(dirpath))
327 end)
328
329 it("returns false and does nothing if the argument already exists", function()
330 tmpfile = os.tmpname()
331 local fd = assert(io.open(tmpfile, "w"))
332 assert(fd:write("foo"))
333 fd:close()
334 assert.falsy(fs.make_dir(tmpfile))
335 end)
336 end)
337
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()
347 local tmpfile
348 local tmpdir
349
350 after_each(function()
351 if tmpfile then
352 os.remove(tmpfile)
353 tmpfile = nil
354 end
355 if tmpdir then
356 lfs.rmdir(tmpdir)
357 tmpdir = nil
358 end
359 end)
360
361 it("removes the directory specified by the argument if it is empty", function()
362 tmpdir = os.tmpname()
363 os.remove(tmpdir)
364 lfs.mkdir(tmpdir)
365 fs.remove_dir_if_empty(tmpdir)
366 assert.falsy(exists_file(tmpdir))
367 end)
368
369 it("does nothing if the directory specified by the argument is not empty", function()
370 tmpdir = os.tmpname()
371 os.remove(tmpdir)
372 lfs.mkdir(tmpdir)
373 tmpfile = "/internalfile"
374 local filepath = tmpdir .. tmpfile
375 lfs.touch(filepath)
376 local fd = assert(io.open(filepath, "w"))
377 assert(fd:write("foo"))
378 fd:close()
379 fs.remove_dir_if_empty(tmpdir)
380 assert.truthy(exists_file(tmpdir))
381 end)
382 end)
383
384 describe("fs.remove_dir_tree_if_empty", function()
385 local tmpfile
386 local tmpdir
387 local intdir
388
389 after_each(function()
390 if tmpfile then
391 os.remove(tmpfile)
392 tmpfile = nil
393 end
394 if intdir then
395 lfs.rmdir(intdir)
396 intdir = nil
397 end
398 if tmpdir then
399 lfs.rmdir(tmpdir)
400 tmpdir = nil
401 end
402 end)
403
404 it("removes the directory path specified by the argument if it is empty", function()
405 tmpdir = os.tmpname()
406 os.remove(tmpdir)
407 lfs.mkdir(tmpdir)
408 fs.remove_dir_tree_if_empty(tmpdir)
409 assert.falsy(exists_file(tmpdir))
410 end)
411
412 it("does nothing if the directory path specified by the argument is not empty", function()
413 tmpdir = os.tmpname()
414 os.remove(tmpdir)
415 lfs.mkdir(tmpdir)
416 intdir = "/internaldir"
417 local dirpath = tmpdir .. intdir
418 lfs.mkdir(dirpath)
419 tmpfile = "/internalfile"
420 local filepath = dirpath .. tmpfile
421 lfs.touch(filepath)
422 fs.remove_dir_tree_if_empty(tmpdir)
423 assert.truthy(exists_file(dirpath))
424 assert.truthy(exists_file(tmpdir))
425 end)
426 end)
427
428 describe("fs.copy", function()
429 local srcfile
430 local dstfile
431 local tmpdir
432
433 after_each(function()
434 if srcfile then
435 os.remove(srcfile)
436 srcfile = nil
437 end
438 if dstfile then
439 os.remove(dstfile)
440 dstfile = nil
441 end
442 if tmpdir then
443 lfs.rmdir(tmpdir)
444 tmpdir = nil
445 end
446 end)
447
448 it("returns true and copies the contents and the permissions of the source file to the destination file", function()
449 srcfile = os.tmpname()
450 local fd = assert(io.open(srcfile, "w"))
451 local srccontent = "foo"
452 assert(fd:write(srccontent))
453 fd:close()
454 dstfile = os.tmpname()
455 os.remove(dstfile)
456 assert.truthy(fs.copy(srcfile, dstfile, nil))
457 fd = assert(io.open(dstfile, "r"))
458 local dstcontent = fd:read("*a")
459 assert.same(srccontent, dstcontent)
460 if not is_win then
461 assert.same(lfs.attributes(srcfile, "permissions"), lfs.attributes(dstfile, "permissions"))
462 end
463 end)
464
465 it("returns true and copies contents of the source file to the destination file with custom permissions", function()
466 srcfile = os.tmpname()
467 local fd = assert(io.open(srcfile, "w"))
468 local srccontent = "foo"
469 assert(fd:write(srccontent))
470 fd:close()
471 dstfile = os.tmpname()
472 os.remove(dstfile)
473 assert.truthy(fs.copy(srcfile, dstfile, "755"))
474 fd = assert(io.open(dstfile, "r"))
475 local dstcontent = fd:read("*a")
476 assert.same(srccontent, dstcontent)
477 if not is_win then
478 assert.same("rwxr-xr-x", lfs.attributes(dstfile, "permissions"))
479 end
480 end)
481
482 it("returns false and does nothing if the source file does not exist", function()
483 srcfile = os.tmpname()
484 os.remove(srcfile)
485 dstfile = os.tmpname()
486 os.remove(dstfile)
487 assert.falsy(fs.copy(srcfile, dstfile, nil))
488 assert.falsy(exists_file(dstfile))
489 end)
490
491 it("returns false and does nothing if the source file doesn't have the proper permissions #unix", function()
492 srcfile = os.tmpname()
493 local fd = assert(io.open(srcfile, "w"))
494 assert(fd:write("foo"))
495 fd:close()
496 assert(fs.chmod(srcfile, "333"))
497 dstfile = os.tmpname()
498 os.remove(dstfile)
499 assert.falsy(fs.copy(srcfile, dstfile, nil))
500 assert.falsy(exists_file(dstfile))
501 end)
502
503 it("returns false and does nothing if the destination file directory doesn't have the proper permissions #unix", function()
504 srcfile = os.tmpname()
505 local fd = assert(io.open(srcfile, "w"))
506 assert(fd:write("foo"))
507 fd:close()
508 tmpdir = os.tmpname()
509 os.remove(tmpdir)
510 lfs.mkdir(tmpdir)
511 assert(fs.chmod(tmpdir, "666"))
512 dstfile = tmpdir .. "/dstfile"
513 assert.falsy(fs.copy(srcfile, dstfile, nil))
514 assert(fs.chmod(tmpdir, "777"))
515 assert.falsy(exists_file(dstfile))
516 end)
517 end)
518
519 describe("fs.copy_contents", function()
520 local srcfile
521 local dstfile
522 local srcintdir
523 local dstintdir
524 local srcdir
525 local dstdir
526
527 after_each(function()
528 if srcfile then
529 os.remove(srcfile)
530 srcfile = nil
531 end
532 if dstfile then
533 os.remove(dstfile)
534 dstfile = nil
535 end
536 if srcintdir then
537 lfs.rmdir(srcintdir)
538 srcintdir = nil
539 end
540 if dstintdir then
541 lfs.rmdir(dstintdir)
542 dstintdir = nil
543 end
544 if srcdir then
545 lfs.rmdir(srcdir)
546 srcdir = nil
547 end
548 if dstdir then
549 lfs.rmdir(dstdir)
550 dstdir = nil
551 end
552 end)
553
554 local create_dir_tree = function()
555 srcdir = os.tmpname()
556 os.remove(srcdir)
557 lfs.mkdir(srcdir)
558 srcintdir = srcdir .. "/internaldir"
559 lfs.mkdir(srcintdir)
560 srcfile = srcintdir .. "/internalfile"
561 lfs.touch(srcfile)
562 local fd = assert(io.open(srcfile, "w"))
563 assert(fd:write("foo"))
564 fd:close()
565 dstdir = os.tmpname()
566 os.remove(dstdir)
567 if is_win then
568 lfs.mkdir(dstdir)
569 end
570 end
571
572 it("returns true and copies the contents (with their permissions) of the source dir to the destination dir", function()
573 create_dir_tree()
574 assert.truthy(fs.copy_contents(srcdir, dstdir, nil))
575 assert.truthy(exists_file(dstdir))
576 dstintdir = dstdir .. "/internaldir"
577 assert.truthy(exists_file(dstintdir))
578 dstfile = dstdir .. "/internaldir/internalfile"
579 local fd = assert(io.open(dstfile, "r"))
580 local dstfilecontent = fd:read("*a")
581 assert.same("foo", dstfilecontent)
582 if not is_win then
583 assert.same(lfs.attributes(srcfile, "permissions"), lfs.attributes(dstfile, "permissions"))
584 end
585 end)
586
587 it("returns true and copies the contents of the source dir to the destination dir with custom permissions", function()
588 create_dir_tree()
589 assert.truthy(fs.copy_contents(srcdir, dstdir, "755"))
590 assert.truthy(exists_file(dstdir))
591 dstintdir = dstdir .. "/internaldir"
592 assert.truthy(exists_file(dstintdir))
593 dstfile = dstdir .. "/internaldir/internalfile"
594 local fd = assert(io.open(dstfile, "r"))
595 local dstfilecontent = fd:read("*a")
596 assert.same("foo", dstfilecontent)
597 if not is_win then
598 assert.same("rwxr-xr-x", lfs.attributes(dstfile, "permissions"))
599 end
600 end)
601
602 it("returns false and does nothing if the source dir doesn't exist", function()
603 srcdir = os.tmpname()
604 os.remove(srcdir)
605 dstdir = os.tmpname()
606 os.remove(dstdir)
607 if is_win then
608 fs.copy_contents(srcdir, dstdir, nil)
609 else
610 assert.falsy(pcall(fs.copy_contents, srcdir, dstdir, nil))
611 end
612 assert.falsy(exists_file(dstdir))
613 end)
614
615 it("returns false if the source argument is a file", function()
616 srcdir = os.tmpname()
617 local fd = assert(io.open(srcdir, "w"))
618 assert(fd:write("foo"))
619 fd:close()
620 dstdir = os.tmpname()
621 os.remove(dstdir)
622 if is_win then
623 fs.copy_contents(srcdir, dstdir, nil)
624 else
625 assert.falsy(pcall(fs.copy_contents, srcdir, dstdir, nil))
626 end
627 assert.falsy(exists_file(dstdir))
628 end)
629
630 it("returns false and does nothing if the source dir doesn't have the proper permissions #unix", function()
631 create_dir_tree()
632 assert(fs.chmod(srcdir, "333"))
633 assert.falsy(pcall(fs.copy_contents, srcdir, dstdir, nil))
634 assert.falsy(exists_file(dstdir))
635 end)
636 end)
637
638 describe("fs.delete", function()
639 local tmpfile1
640 local tmpfile2
641 local tmpintdir
642 local tmpdir
643
644 after_each(function()
645 if tmpfile1 then
646 os.remove(tmpfile1)
647 tmpfile1 = nil
648 end
649 if tmpfile2 then
650 os.remove(tmpfile2)
651 tmpfile2 = nil
652 end
653 if tmpintdir then
654 lfs.rmdir(tmpintdir)
655 tmpintdir = nil
656 end
657 if tmpdir then
658 lfs.rmdir(tmpdir)
659 tmpdir = nil
660 end
661 end)
662
663 local create_dir_tree = function()
664 tmpdir = os.tmpname()
665 os.remove(tmpdir)
666 lfs.mkdir(tmpdir)
667 tmpintdir = tmpdir .. "/internaldir"
668 lfs.mkdir(tmpintdir)
669 tmpfile1 = tmpdir .. "/internalfile1"
670 lfs.touch(tmpfile1)
671 local fd = assert(io.open(tmpfile1, "w"))
672 assert(fd:write("foo"))
673 fd:close()
674 tmpfile2 = tmpintdir .. "/internalfile2"
675 lfs.touch(tmpfile2)
676 fd = assert(io.open(tmpfile2, "w"))
677 assert(fd:write("foo"))
678 fd:close()
679 end
680
681 it("deletes the file specified by the argument", function()
682 tmpfile1 = os.tmpname()
683 tmpfile2 = os.tmpname()
684 fs.delete(tmpfile1)
685 fs.delete(tmpfile2)
686 assert.falsy(exists_file(tmpfile1))
687 assert.falsy(exists_file(tmpfile2))
688 end)
689
690 it("deletes the contents of the directory specified by the argument", function()
691 create_dir_tree()
692 fs.delete(tmpdir)
693 assert.falsy(exists_file(tmpfile2))
694 assert.falsy(exists_file(tmpintdir))
695 assert.falsy(exists_file(tmpfile1))
696 assert.falsy(exists_file(tmpdir))
697 end)
698
699 it("does nothing if the parent directory of the argument doesn't have the proper permissions #unix", function()
700 create_dir_tree()
701 assert(fs.chmod(tmpdir, "000"))
702 fs.delete(tmpfile1)
703 fs.delete(tmpfile2)
704 fs.delete(tmpintdir)
705 assert.truthy(exists_file(tmpfile2))
706 assert.truthy(exists_file(tmpintdir))
707 assert.truthy(exists_file(tmpfile1))
708 assert.truthy(exists_file(tmpdir))
709 end)
710 end)
57end) 711end)