aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGeorge Roman <george.roman.99@gmail.com>2018-05-18 13:14:33 +0300
committerHisham Muhammad <hisham@gobolinux.org>2018-05-18 12:28:42 -0300
commit158b31967ca4d275db7269991bd5a598c214e307 (patch)
tree919d8a6902cd1b91eacca77cb6b938ef2b89ce66
parent1edae7714d9429ab3cd435f93738a41d86589481 (diff)
downloadluarocks-158b31967ca4d275db7269991bd5a598c214e307.tar.gz
luarocks-158b31967ca4d275db7269991bd5a598c214e307.tar.bz2
luarocks-158b31967ca4d275db7269991bd5a598c214e307.zip
Add more tests for the fs module
-rw-r--r--spec/fs_spec.lua203
1 files changed, 172 insertions, 31 deletions
diff --git a/spec/fs_spec.lua b/spec/fs_spec.lua
index 9681b0e7..4a557629 100644
--- a/spec/fs_spec.lua
+++ b/spec/fs_spec.lua
@@ -12,6 +12,7 @@ describe("Luarocks fs test #whitebox #w_fs", function()
12 if is_win and not path:find(":") then 12 if is_win and not path:find(":") then
13 path = os.getenv("TEMP") .. path 13 path = os.getenv("TEMP") .. path
14 end 14 end
15 os.remove(path)
15 return path 16 return path
16 end 17 end
17 18
@@ -69,6 +70,173 @@ describe("Luarocks fs test #whitebox #w_fs", function()
69 assert.are.same(is_win and [["\\"%" \\\\" \\\\\\"]] or [['\% \\" \\\']], fs.Q([[\% \\" \\\]])) 70 assert.are.same(is_win and [["\\"%" \\\\" \\\\\\"]] or [['\% \\" \\\']], fs.Q([[\% \\" \\\]]))
70 end) 71 end)
71 end) 72 end)
73
74 describe("fs.dir_iterator", function()
75 local tmpfile1
76 local tmpfile2
77 local tmpdir
78 local intdir
79
80 after_each(function()
81 if tmpfile1 then
82 os.remove(tmpfile1)
83 tmpfile1 = nil
84 end
85 if tmpfile2 then
86 os.remove(tmpfile2)
87 tmpfile2 = nil
88 end
89 if intdir then
90 lfs.rmdir(intdir)
91 intdir = nil
92 end
93 if tmpdir then
94 lfs.rmdir(tmpdir)
95 tmpdir = nil
96 end
97 end)
98
99 it("yields all files and directories in the directory given as argument during the iterations", function()
100 tmpdir = get_tmp_path()
101 lfs.mkdir(tmpdir)
102 tmpfile1 = tmpdir .. "/file1"
103 create_file(tmpfile1)
104 tmpfile2 = tmpdir .. "/file2"
105 create_file(tmpfile2)
106 intdir = tmpdir .. "/intdir"
107 lfs.mkdir(intdir)
108 local dirTable = {}
109 local dirCount = 0
110 local crt = coroutine.create(fs.dir_iterator)
111 while coroutine.status(crt) ~= "dead" do
112 local ok, val = coroutine.resume(crt, tmpdir)
113 if ok and val ~= nil then
114 dirTable[val] = true
115 dirCount = dirCount + 1
116 end
117 end
118 assert.same(dirCount, 3)
119 assert.is_not.same(dirTable["file1"], nil)
120 assert.is_not.same(dirTable["file2"], nil)
121 assert.is_not.same(dirTable["intdir"], nil)
122 dirCount = 0
123 crt = coroutine.create(fs.dir_iterator)
124 while coroutine.status(crt) ~= "dead" do
125 local ok, val = coroutine.resume(crt, intdir)
126 if ok and val ~= nil then
127 dirCount = dirCount + 1
128 end
129 end
130 assert.same(dirCount, 0)
131 end)
132
133 it("does nothing if the argument is a file", function()
134 tmpfile1 = get_tmp_path()
135 create_file(tmpfile1)
136 assert.falsy(pcall(fs.dir_iterator, tmpfile1))
137 end)
138
139 it("does nothing if the argument is invalid", function()
140 assert.falsy(pcall(fs.dir_iterator, "/nonexistent"))
141 end)
142 end)
143
144 describe("fs.is_writable", function()
145 local tmpfile
146 local tmpdir
147
148 after_each(function()
149 if tmpfile then
150 os.remove(tmpfile)
151 tmpfile = nil
152 end
153 if tmpdir then
154 lfs.rmdir(tmpdir)
155 tmpdir = nil
156 end
157 end)
158
159 it("returns true if the file given as argument is writable", function()
160 tmpfile = get_tmp_path()
161 create_file(tmpfile)
162 assert.truthy(fs.is_writable(tmpfile))
163 end)
164
165 it("returns true if the directory given as argument is writable", function()
166 tmpdir = get_tmp_path()
167 lfs.mkdir(tmpdir)
168 assert.truthy(fs.is_writable(tmpdir))
169 tmpfile = tmpdir .. "/internalfile"
170 create_file(tmpfile)
171 make_unwritable(tmpfile)
172 assert.truthy(fs.is_writable(tmpdir))
173 end)
174
175 it("returns false if the file given as argument is not writable", function()
176 tmpfile = get_tmp_path()
177 create_file(tmpfile)
178 make_unwritable(tmpfile)
179 assert.falsy(fs.is_writable(tmpfile))
180 end)
181
182 it("returns false if the directory given as argument is not writable", function()
183 tmpdir = get_tmp_path()
184 lfs.mkdir(tmpdir)
185 make_unwritable(tmpdir)
186 assert.falsy(fs.is_writable(tmpdir))
187 end)
188
189 it("returns false if the file or directory given as argument does not exist", function()
190 assert.falsy(fs.is_writable("/nonexistent"))
191 end)
192 end)
193
194 describe("fs.set_time #unix", function()
195 local tmpfile
196 local tmpdir
197 local intdir
198
199 after_each(function()
200 if tmpfile then
201 os.remove(tmpfile)
202 tmpfile = nil
203 end
204 if intdir then
205 os.remove(intdir)
206 intdir = nil
207 end
208 if tmpdir then
209 lfs.rmdir(tmpdir)
210 tmpdir = nil
211 end
212 end)
213
214 it("returns true and modifies the access time of the file given as argument", function()
215 tmpfile = get_tmp_path()
216 create_file(tmpfile)
217 local newtime = os.time() - 100
218 assert.truthy(fs.set_time(tmpfile, newtime))
219 assert.same(lfs.attributes(tmpfile, "access"), newtime)
220 assert.same(lfs.attributes(tmpfile, "modification"), newtime)
221 end)
222
223 it("returns true and modifies the access time of the directory given as argument", function()
224 tmpdir = get_tmp_path()
225 lfs.mkdir(tmpdir)
226 tmpfile = tmpdir .. "/internalfile"
227 create_file(tmpfile)
228 local newtime = os.time() - 100
229 assert.truthy(fs.set_time(tmpdir, newtime))
230 assert.same(lfs.attributes(tmpdir, "access"), newtime)
231 assert.same(lfs.attributes(tmpdir, "modification"), newtime)
232 assert.is_not.same(lfs.attributes(tmpfile, "access"), newtime)
233 assert.is_not.same(lfs.attributes(tmpfile, "modification"), newtime)
234 end)
235
236 it("returns false and does nothing if the file or directory given as arguments doesn't exist", function()
237 assert.falsy(fs.set_time("/nonexistent"))
238 end)
239 end)
72 240
73 describe("fs.set_permissions", function() 241 describe("fs.set_permissions", function()
74 local readfile 242 local readfile
@@ -117,7 +285,6 @@ describe("Luarocks fs test #whitebox #w_fs", function()
117 fd:close() 285 fd:close()
118 286
119 tmpdir = get_tmp_path() 287 tmpdir = get_tmp_path()
120 os.remove(tmpdir)
121 lfs.mkdir(tmpdir) 288 lfs.mkdir(tmpdir)
122 make_unexecutable(tmpdir) 289 make_unexecutable(tmpdir)
123 fd = assert(io.popen("cd " .. fs.Q(tmpdir) .. " 2>&1")) 290 fd = assert(io.popen("cd " .. fs.Q(tmpdir) .. " 2>&1"))
@@ -163,7 +330,6 @@ describe("Luarocks fs test #whitebox #w_fs", function()
163 330
164 it("returns false when the argument exists but is not a file", function() 331 it("returns false when the argument exists but is not a file", function()
165 tmpdir = get_tmp_path() 332 tmpdir = get_tmp_path()
166 os.remove(tmpdir)
167 lfs.mkdir(tmpdir) 333 lfs.mkdir(tmpdir)
168 assert.same(false, fs.is_file("/nonexistent")) 334 assert.same(false, fs.is_file("/nonexistent"))
169 end) 335 end)
@@ -186,7 +352,6 @@ describe("Luarocks fs test #whitebox #w_fs", function()
186 352
187 it("returns true when the argument is a directory", function() 353 it("returns true when the argument is a directory", function()
188 tmpdir = get_tmp_path() 354 tmpdir = get_tmp_path()
189 os.remove(tmpdir)
190 lfs.mkdir(tmpdir) 355 lfs.mkdir(tmpdir)
191 assert.truthy(fs.is_dir(tmpdir)) 356 assert.truthy(fs.is_dir(tmpdir))
192 end) 357 end)
@@ -225,7 +390,6 @@ describe("Luarocks fs test #whitebox #w_fs", function()
225 390
226 it("returns true when the argument is a directory", function() 391 it("returns true when the argument is a directory", function()
227 tmpdir = get_tmp_path() 392 tmpdir = get_tmp_path()
228 os.remove(tmpdir)
229 lfs.mkdir(tmpdir) 393 lfs.mkdir(tmpdir)
230 assert.truthy(fs.exists(tmpdir)) 394 assert.truthy(fs.exists(tmpdir))
231 end) 395 end)
@@ -258,7 +422,6 @@ describe("Luarocks fs test #whitebox #w_fs", function()
258 local currentdir = lfs.currentdir() 422 local currentdir = lfs.currentdir()
259 assert.same(currentdir, fs.current_dir()) 423 assert.same(currentdir, fs.current_dir())
260 tmpdir = get_tmp_path() 424 tmpdir = get_tmp_path()
261 os.remove(tmpdir)
262 lfs.mkdir(tmpdir) 425 lfs.mkdir(tmpdir)
263 assert.truthy(fs.change_dir(tmpdir)) 426 assert.truthy(fs.change_dir(tmpdir))
264 if is_win then 427 if is_win then
@@ -295,7 +458,6 @@ describe("Luarocks fs test #whitebox #w_fs", function()
295 458
296 it("returns true and changes the current working directory if the argument is a directory", function() 459 it("returns true and changes the current working directory if the argument is a directory", function()
297 tmpdir = get_tmp_path() 460 tmpdir = get_tmp_path()
298 os.remove(tmpdir)
299 lfs.mkdir(tmpdir) 461 lfs.mkdir(tmpdir)
300 assert.truthy(fs.change_dir(tmpdir)) 462 assert.truthy(fs.change_dir(tmpdir))
301 if is_win then 463 if is_win then
@@ -338,7 +500,6 @@ describe("Luarocks fs test #whitebox #w_fs", function()
338 500
339 it("returns true and changes the current directory to root if the current directory is valid", function() 501 it("returns true and changes the current directory to root if the current directory is valid", function()
340 tmpdir = get_tmp_path() 502 tmpdir = get_tmp_path()
341 os.remove(tmpdir)
342 lfs.mkdir(tmpdir) 503 lfs.mkdir(tmpdir)
343 assert.truthy(fs.change_dir(tmpdir)) 504 assert.truthy(fs.change_dir(tmpdir))
344 local success = fs.change_dir_to_root() 505 local success = fs.change_dir_to_root()
@@ -350,7 +511,6 @@ describe("Luarocks fs test #whitebox #w_fs", function()
350 511
351 it("returns false and does nothing if the current directory is not valid #unix", function() 512 it("returns false and does nothing if the current directory is not valid #unix", function()
352 tmpdir = get_tmp_path() 513 tmpdir = get_tmp_path()
353 os.remove(tmpdir)
354 lfs.mkdir(tmpdir) 514 lfs.mkdir(tmpdir)
355 lfs.chdir(tmpdir) 515 lfs.chdir(tmpdir)
356 lfs.rmdir(tmpdir) 516 lfs.rmdir(tmpdir)
@@ -379,7 +539,6 @@ describe("Luarocks fs test #whitebox #w_fs", function()
379 539
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() 540 it("returns true and changes the current directory to the previous one in the dir stack if the dir stack is not empty", function()
381 tmpdir = get_tmp_path() 541 tmpdir = get_tmp_path()
382 os.remove(tmpdir)
383 lfs.mkdir(tmpdir) 542 lfs.mkdir(tmpdir)
384 assert.truthy(fs.change_dir(tmpdir)) 543 assert.truthy(fs.change_dir(tmpdir))
385 assert.truthy(fs.pop_dir()) 544 assert.truthy(fs.pop_dir())
@@ -409,14 +568,12 @@ describe("Luarocks fs test #whitebox #w_fs", function()
409 568
410 it("returns true and creates the directory specified by the argument", function() 569 it("returns true and creates the directory specified by the argument", function()
411 tmpdir = get_tmp_path() 570 tmpdir = get_tmp_path()
412 os.remove(tmpdir)
413 assert.truthy(fs.make_dir(tmpdir)) 571 assert.truthy(fs.make_dir(tmpdir))
414 assert.same("directory", lfs.attributes(tmpdir, "mode")) 572 assert.same("directory", lfs.attributes(tmpdir, "mode"))
415 end) 573 end)
416 574
417 it("returns true and creates the directory path specified by the argument", function() 575 it("returns true and creates the directory path specified by the argument", function()
418 tmpdir = get_tmp_path() 576 tmpdir = get_tmp_path()
419 os.remove(tmpdir)
420 intdir = "/internaldir" 577 intdir = "/internaldir"
421 local dirpath = tmpdir .. intdir 578 local dirpath = tmpdir .. intdir
422 assert.truthy(fs.make_dir(dirpath)) 579 assert.truthy(fs.make_dir(dirpath))
@@ -458,7 +615,6 @@ describe("Luarocks fs test #whitebox #w_fs", function()
458 615
459 it("removes the directory specified by the argument if it is empty", function() 616 it("removes the directory specified by the argument if it is empty", function()
460 tmpdir = get_tmp_path() 617 tmpdir = get_tmp_path()
461 os.remove(tmpdir)
462 lfs.mkdir(tmpdir) 618 lfs.mkdir(tmpdir)
463 fs.remove_dir_if_empty(tmpdir) 619 fs.remove_dir_if_empty(tmpdir)
464 assert.falsy(exists_file(tmpdir)) 620 assert.falsy(exists_file(tmpdir))
@@ -466,7 +622,6 @@ describe("Luarocks fs test #whitebox #w_fs", function()
466 622
467 it("does nothing if the directory specified by the argument is not empty", function() 623 it("does nothing if the directory specified by the argument is not empty", function()
468 tmpdir = get_tmp_path() 624 tmpdir = get_tmp_path()
469 os.remove(tmpdir)
470 lfs.mkdir(tmpdir) 625 lfs.mkdir(tmpdir)
471 tmpfile = "/internalfile" 626 tmpfile = "/internalfile"
472 local filepath = tmpdir .. tmpfile 627 local filepath = tmpdir .. tmpfile
@@ -498,7 +653,6 @@ describe("Luarocks fs test #whitebox #w_fs", function()
498 653
499 it("removes the directory path specified by the argument if it is empty", function() 654 it("removes the directory path specified by the argument if it is empty", function()
500 tmpdir = get_tmp_path() 655 tmpdir = get_tmp_path()
501 os.remove(tmpdir)
502 lfs.mkdir(tmpdir) 656 lfs.mkdir(tmpdir)
503 fs.remove_dir_tree_if_empty(tmpdir) 657 fs.remove_dir_tree_if_empty(tmpdir)
504 assert.falsy(exists_file(tmpdir)) 658 assert.falsy(exists_file(tmpdir))
@@ -506,7 +660,6 @@ describe("Luarocks fs test #whitebox #w_fs", function()
506 660
507 it("does nothing if the directory path specified by the argument is not empty", function() 661 it("does nothing if the directory path specified by the argument is not empty", function()
508 tmpdir = get_tmp_path() 662 tmpdir = get_tmp_path()
509 os.remove(tmpdir)
510 lfs.mkdir(tmpdir) 663 lfs.mkdir(tmpdir)
511 intdir = "/internaldir" 664 intdir = "/internaldir"
512 local dirpath = tmpdir .. intdir 665 local dirpath = tmpdir .. intdir
@@ -540,10 +693,9 @@ describe("Luarocks fs test #whitebox #w_fs", function()
540 end) 693 end)
541 694
542 it("returns true and copies the contents and the permissions of the source file to the destination file", function() 695 it("returns true and copies the contents and the permissions of the source file to the destination file", function()
543 srcfile = os.tmpname() 696 srcfile = get_tmp_path()
544 create_file(srcfile, srccontent) 697 create_file(srcfile, srccontent)
545 dstfile = os.tmpname() 698 dstfile = get_tmp_path()
546 os.remove(dstfile)
547 assert.truthy(fs.copy(srcfile, dstfile)) 699 assert.truthy(fs.copy(srcfile, dstfile))
548 fd = assert(io.open(dstfile, "r")) 700 fd = assert(io.open(dstfile, "r"))
549 local dstcontent = fd:read("*a") 701 local dstcontent = fd:read("*a")
@@ -554,10 +706,9 @@ describe("Luarocks fs test #whitebox #w_fs", function()
554 end) 706 end)
555 707
556 it("returns true and copies contents of the source file to the destination file with custom permissions", function() 708 it("returns true and copies contents of the source file to the destination file with custom permissions", function()
557 srcfile = os.tmpname() 709 srcfile = get_tmp_path()
558 create_file(srcfile, srccontent) 710 create_file(srcfile, srccontent)
559 dstfile = os.tmpname() 711 dstfile = get_tmp_path()
560 os.remove(dstfile)
561 assert.truthy(fs.copy(srcfile, dstfile, "exec")) 712 assert.truthy(fs.copy(srcfile, dstfile, "exec"))
562 fd = assert(io.open(dstfile, "r")) 713 fd = assert(io.open(dstfile, "r"))
563 local dstcontent = fd:read("*a") 714 local dstcontent = fd:read("*a")
@@ -566,9 +717,7 @@ describe("Luarocks fs test #whitebox #w_fs", function()
566 717
567 it("returns false and does nothing if the source file does not exist", function() 718 it("returns false and does nothing if the source file does not exist", function()
568 srcfile = get_tmp_path() 719 srcfile = get_tmp_path()
569 os.remove(srcfile)
570 dstfile = get_tmp_path() 720 dstfile = get_tmp_path()
571 os.remove(dstfile)
572 assert.falsy(fs.copy(srcfile, dstfile, nil)) 721 assert.falsy(fs.copy(srcfile, dstfile, nil))
573 assert.falsy(exists_file(dstfile)) 722 assert.falsy(exists_file(dstfile))
574 end) 723 end)
@@ -578,7 +727,6 @@ describe("Luarocks fs test #whitebox #w_fs", function()
578 create_file(srcfile) 727 create_file(srcfile)
579 make_unreadable(srcfile) 728 make_unreadable(srcfile)
580 dstfile = get_tmp_path() 729 dstfile = get_tmp_path()
581 os.remove(dstfile)
582 assert.falsy(fs.copy(srcfile, dstfile, nil)) 730 assert.falsy(fs.copy(srcfile, dstfile, nil))
583 assert.falsy(exists_file(dstfile)) 731 assert.falsy(exists_file(dstfile))
584 end) 732 end)
@@ -587,7 +735,6 @@ describe("Luarocks fs test #whitebox #w_fs", function()
587 srcfile = get_tmp_path() 735 srcfile = get_tmp_path()
588 create_file(srcfile) 736 create_file(srcfile)
589 tmpdir = get_tmp_path() 737 tmpdir = get_tmp_path()
590 os.remove(tmpdir)
591 lfs.mkdir(tmpdir) 738 lfs.mkdir(tmpdir)
592 make_unwritable(tmpdir) 739 make_unwritable(tmpdir)
593 dstfile = tmpdir .. "/dstfile" 740 dstfile = tmpdir .. "/dstfile"
@@ -634,14 +781,12 @@ describe("Luarocks fs test #whitebox #w_fs", function()
634 781
635 local create_dir_tree = function() 782 local create_dir_tree = function()
636 srcdir = get_tmp_path() 783 srcdir = get_tmp_path()
637 os.remove(srcdir)
638 lfs.mkdir(srcdir) 784 lfs.mkdir(srcdir)
639 srcintdir = srcdir .. "/internaldir" 785 srcintdir = srcdir .. "/internaldir"
640 lfs.mkdir(srcintdir) 786 lfs.mkdir(srcintdir)
641 srcfile = srcintdir .. "/internalfile" 787 srcfile = srcintdir .. "/internalfile"
642 create_file(srcfile) 788 create_file(srcfile)
643 dstdir = get_tmp_path() 789 dstdir = get_tmp_path()
644 os.remove(dstdir)
645 end 790 end
646 791
647 it("returns true and copies the contents (with their permissions) of the source dir to the destination dir", function() 792 it("returns true and copies the contents (with their permissions) of the source dir to the destination dir", function()
@@ -673,9 +818,7 @@ describe("Luarocks fs test #whitebox #w_fs", function()
673 818
674 it("returns false and does nothing if the source dir doesn't exist", function() 819 it("returns false and does nothing if the source dir doesn't exist", function()
675 srcdir = get_tmp_path() 820 srcdir = get_tmp_path()
676 os.remove(srcdir)
677 dstdir = get_tmp_path() 821 dstdir = get_tmp_path()
678 os.remove(dstdir)
679 assert.falsy(fs.copy_contents(srcdir, dstdir)) 822 assert.falsy(fs.copy_contents(srcdir, dstdir))
680 assert.falsy(exists_file(dstdir)) 823 assert.falsy(exists_file(dstdir))
681 end) 824 end)
@@ -684,7 +827,6 @@ describe("Luarocks fs test #whitebox #w_fs", function()
684 srcdir = get_tmp_path() 827 srcdir = get_tmp_path()
685 create_file(srcdir) 828 create_file(srcdir)
686 dstdir = get_tmp_path() 829 dstdir = get_tmp_path()
687 os.remove(dstdir)
688 assert.falsy(fs.copy_contents(srcdir, dstdir)) 830 assert.falsy(fs.copy_contents(srcdir, dstdir))
689 assert.falsy(exists_file(dstdir)) 831 assert.falsy(exists_file(dstdir))
690 end) 832 end)
@@ -725,7 +867,6 @@ describe("Luarocks fs test #whitebox #w_fs", function()
725 867
726 local create_dir_tree = function() 868 local create_dir_tree = function()
727 tmpdir = get_tmp_path() 869 tmpdir = get_tmp_path()
728 os.remove(tmpdir)
729 lfs.mkdir(tmpdir) 870 lfs.mkdir(tmpdir)
730 tmpintdir = tmpdir .. "/internaldir" 871 tmpintdir = tmpdir .. "/internaldir"
731 lfs.mkdir(tmpintdir) 872 lfs.mkdir(tmpintdir)