aboutsummaryrefslogtreecommitdiff
path: root/spec/unit/fs_spec.lua
diff options
context:
space:
mode:
Diffstat (limited to 'spec/unit/fs_spec.lua')
-rw-r--r--spec/unit/fs_spec.lua1607
1 files changed, 1607 insertions, 0 deletions
diff --git a/spec/unit/fs_spec.lua b/spec/unit/fs_spec.lua
new file mode 100644
index 00000000..aea86af3
--- /dev/null
+++ b/spec/unit/fs_spec.lua
@@ -0,0 +1,1607 @@
1local test_env = require("spec.util.test_env")
2
3test_env.unload_luarocks()
4test_env.setup_specs()
5local fs = require("luarocks.fs")
6local path = require("luarocks.path")
7local cfg = require("luarocks.core.cfg")
8local lfs = require("lfs")
9local is_win = test_env.TEST_TARGET_OS == "windows"
10local posix_ok = pcall(require, "posix")
11local testing_paths = test_env.testing_paths
12local get_tmp_path = test_env.get_tmp_path
13local write_file = test_env.write_file
14local P = test_env.P
15
16-- A chdir that works in both full and minimal mode, setting
17-- both the real process current dir and the LuaRocks internal stack in minimal mode
18local function chdir(d)
19 lfs.chdir(d)
20 fs.change_dir(d)
21end
22
23describe("luarocks.fs #unit", function()
24 local exists_file = function(path)
25 local ok, err, code = os.rename(path, path)
26 if not ok and code == 13 then
27 return true
28 end
29 return ok
30 end
31
32 local create_file = function(path, content)
33 local fd = assert(io.open(path, "w"))
34 if not content then
35 content = "foo"
36 end
37 assert(fd:write(content))
38 fd:close()
39 end
40
41 local make_unreadable = function(path)
42 if is_win then
43 fs.execute("icacls " .. fs.Q(path) .. " /inheritance:d /deny \"%USERNAME%\":(R)")
44 else
45 fs.execute("chmod -r " .. fs.Q(path))
46 end
47 end
48
49 local make_unwritable = function(path)
50 if is_win then
51 fs.execute("icacls " .. fs.Q(path) .. " /inheritance:d /deny \"%USERNAME%\":(W,M)")
52 else
53 fs.execute("chmod -w " .. fs.Q(path))
54 end
55 end
56
57 local make_unexecutable = function(path)
58 if is_win then
59 fs.execute("icacls " .. fs.Q(path) .. " /inheritance:d /deny \"%USERNAME%\":(X)")
60 else
61 fs.execute("chmod -x " .. fs.Q(path))
62 end
63 end
64
65 local runner
66
67 setup(function()
68 cfg.init()
69 fs.init()
70 runner = require("luacov.runner")
71 runner.init(testing_paths.testrun_dir .. "/luacov.config")
72 runner.tick = true
73 end)
74
75 teardown(function()
76 runner.shutdown()
77 end)
78
79 describe("fs.Q", function()
80 it("simple argument", function()
81 assert.are.same(is_win and '"foo"' or "'foo'", fs.Q("foo"))
82 end)
83
84 it("argument with quotes", function()
85 assert.are.same(is_win and [["it's \"quoting\""]] or [['it'\''s "quoting"']], fs.Q([[it's "quoting"]]))
86 end)
87
88 it("argument with special characters", function()
89 assert.are.same(is_win and [["\\"%" \\\\" \\\\\\"]] or [['\% \\" \\\']], fs.Q([[\% \\" \\\]]))
90 end)
91 end)
92
93 describe("fs.absolute_name", function()
94 it("unchanged if already absolute", function()
95 if is_win then
96 assert.are.same(P"c:\\foo\\bar", fs.absolute_name("\"c:\\foo\\bar\""))
97 assert.are.same(P"c:\\foo\\bar", fs.absolute_name("c:\\foo\\bar"))
98 assert.are.same(P"d:\\foo\\bar", fs.absolute_name("d:\\foo\\bar"))
99 assert.are.same(P"\\foo\\bar", fs.absolute_name("\\foo\\bar"))
100 else
101 assert.are.same(P"/foo/bar", fs.absolute_name("/foo/bar"))
102 end
103 end)
104
105 it("converts to absolute if relative", function()
106 local cur = fs.current_dir()
107 if is_win then
108 assert.are.same(P(cur .. "/foo\\bar"), fs.absolute_name("\"foo\\bar\""))
109 assert.are.same(P(cur .. "/foo\\bar"), fs.absolute_name("foo\\bar"))
110 else
111 assert.are.same(P(cur .. "/foo/bar"), fs.absolute_name("foo/bar"))
112 end
113 end)
114
115 it("converts a relative to specified base if given", function()
116 if is_win then
117 assert.are.same(P"c:\\bla/foo\\bar", fs.absolute_name("\"foo\\bar\"", "c:\\bla"))
118 assert.are.same(P"c:\\bla/foo\\bar", fs.absolute_name("foo/bar", "c:\\bla"))
119 assert.are.same(P"c:\\bla/foo\\bar", fs.absolute_name("foo\\bar", "c:\\bla\\"))
120 else
121 assert.are.same(P"/bla/foo/bar", fs.absolute_name("foo/bar", "/bla"))
122 assert.are.same(P"/bla/foo/bar", fs.absolute_name("foo/bar", "/bla/"))
123 end
124 end)
125 end)
126
127 describe("fs.execute_string", function()
128 local tmpdir
129
130 after_each(function()
131 if tmpdir then
132 lfs.rmdir(tmpdir)
133 tmpdir = nil
134 end
135 end)
136
137 it("returns the status code and runs the command given in the argument", function()
138 tmpdir = get_tmp_path()
139 assert.truthy(fs.execute_string("mkdir " .. fs.Q(tmpdir)))
140 assert.truthy(fs.is_dir(tmpdir))
141 assert.falsy(fs.execute_string("invalidcommand"))
142 end)
143 end)
144
145 describe("fs.dir_iterator", function()
146 local tmpfile1
147 local tmpfile2
148 local tmpdir
149 local intdir
150
151 after_each(function()
152 if tmpfile1 then
153 os.remove(tmpfile1)
154 tmpfile1 = nil
155 end
156 if tmpfile2 then
157 os.remove(tmpfile2)
158 tmpfile2 = nil
159 end
160 if intdir then
161 lfs.rmdir(intdir)
162 intdir = nil
163 end
164 if tmpdir then
165 lfs.rmdir(tmpdir)
166 tmpdir = nil
167 end
168 end)
169
170 it("yields all files and directories in the directory given as argument during the iterations", function()
171 tmpdir = get_tmp_path()
172 lfs.mkdir(tmpdir)
173 tmpfile1 = tmpdir .. "/file1"
174 create_file(tmpfile1)
175 tmpfile2 = tmpdir .. "/file2"
176 create_file(tmpfile2)
177 intdir = tmpdir .. "/intdir"
178 lfs.mkdir(intdir)
179 local dirTable = {}
180 local dirCount = 0
181 local crt = coroutine.create(fs.dir_iterator)
182 while coroutine.status(crt) ~= "dead" do
183 local ok, val = coroutine.resume(crt, tmpdir)
184 if ok and val ~= nil then
185 dirTable[val] = true
186 dirCount = dirCount + 1
187 end
188 end
189 assert.same(dirCount, 3)
190 assert.is_not.same(dirTable["file1"], nil)
191 assert.is_not.same(dirTable["file2"], nil)
192 assert.is_not.same(dirTable["intdir"], nil)
193 dirCount = 0
194 crt = coroutine.create(fs.dir_iterator)
195 while coroutine.status(crt) ~= "dead" do
196 local ok, val = coroutine.resume(crt, intdir)
197 if ok and val ~= nil then
198 dirCount = dirCount + 1
199 end
200 end
201 assert.same(dirCount, 0)
202 end)
203
204 it("does nothing if the argument is a file", function()
205 tmpfile1 = get_tmp_path()
206 create_file(tmpfile1)
207 local crt = coroutine.create(fs.dir_iterator)
208 while coroutine.status(crt) ~= "dead" do
209 local ok, val = coroutine.resume(crt, tmpfile1)
210 assert.falsy(ok and res)
211 end
212 end)
213
214 it("does nothing if the argument is invalid", function()
215 local crt = coroutine.create(fs.dir_iterator)
216 while coroutine.status(crt) ~= "dead" do
217 local ok, val = coroutine.resume(crt, "/nonexistent")
218 assert.falsy(ok and res)
219 end
220 end)
221 end)
222
223 describe("fs.is_writable", function()
224 local tmpfile
225 local tmpdir
226
227 after_each(function()
228 if tmpfile then
229 os.remove(tmpfile)
230 tmpfile = nil
231 end
232 if tmpdir then
233 lfs.rmdir(tmpdir)
234 tmpdir = nil
235 end
236 end)
237
238 it("returns true if the file given as argument is writable", function()
239 tmpfile = get_tmp_path()
240 create_file(tmpfile)
241 assert.truthy(fs.is_writable(tmpfile))
242 end)
243
244 it("returns true if the directory given as argument is writable", function()
245 tmpdir = get_tmp_path()
246 lfs.mkdir(tmpdir)
247 assert.truthy(fs.is_writable(tmpdir))
248 tmpfile = tmpdir .. "/internalfile"
249 create_file(tmpfile)
250 make_unwritable(tmpfile)
251 assert.truthy(fs.is_writable(tmpdir))
252 end)
253
254 it("returns false if the file given as argument is not writable", function()
255 tmpfile = get_tmp_path()
256 create_file(tmpfile)
257 make_unwritable(tmpfile)
258 assert.falsy(fs.is_writable(tmpfile))
259 end)
260
261 it("returns false if the directory given as argument is not writable", function()
262 tmpdir = get_tmp_path()
263 lfs.mkdir(tmpdir)
264 make_unwritable(tmpdir)
265 assert.falsy(fs.is_writable(tmpdir))
266 end)
267
268 it("returns false if the file or directory given as argument does not exist", function()
269 assert.falsy(fs.is_writable("/nonexistent"))
270 end)
271 end)
272
273 describe("fs.set_time #unix", function()
274 local tmpfile
275 local tmpdir
276 local intdir
277
278 after_each(function()
279 if tmpfile then
280 os.remove(tmpfile)
281 tmpfile = nil
282 end
283 if intdir then
284 os.remove(intdir)
285 intdir = nil
286 end
287 if tmpdir then
288 lfs.rmdir(tmpdir)
289 tmpdir = nil
290 end
291 end)
292
293 it("returns true and modifies the access time of the file given as argument", function()
294 tmpfile = get_tmp_path()
295 create_file(tmpfile)
296 local newtime = os.time() - 100
297 assert.truthy(fs.set_time(tmpfile, newtime))
298 assert.same(lfs.attributes(tmpfile, "access"), newtime)
299 assert.same(lfs.attributes(tmpfile, "modification"), newtime)
300 end)
301
302 it("returns true and modifies the access time of the directory given as argument", function()
303 tmpdir = get_tmp_path()
304 lfs.mkdir(tmpdir)
305 tmpfile = tmpdir .. "/internalfile"
306 create_file(tmpfile)
307 local newtime = os.time() - 100
308 assert.truthy(fs.set_time(tmpdir, newtime))
309 assert.same(lfs.attributes(tmpdir, "access"), newtime)
310 assert.same(lfs.attributes(tmpdir, "modification"), newtime)
311 assert.is_not.same(lfs.attributes(tmpfile, "access"), newtime)
312 assert.is_not.same(lfs.attributes(tmpfile, "modification"), newtime)
313 end)
314
315 it("returns false and does nothing if the file or directory given as arguments doesn't exist", function()
316 assert.falsy(fs.set_time("/nonexistent"))
317 end)
318 end)
319
320 describe("fs.set_permissions", function()
321 local readfile
322 local execfile
323 local tmpdir
324
325 after_each(function()
326 if readfile then
327 os.remove(readfile)
328 readfile = nil
329 end
330 if execfile then
331 os.remove(execfile)
332 execfile = nil
333 end
334 if tmpdir then
335 lfs.rmdir(tmpdir)
336 tmpdir = nil
337 end
338 end)
339
340 it("returns true and sets the permissions of the argument accordingly", function()
341 readfile = get_tmp_path()
342 create_file(readfile)
343 make_unreadable(readfile)
344 assert.falsy(io.open(readfile, "r"))
345 assert.truthy(fs.set_permissions(readfile, "read", "user"))
346 assert.truthy(io.open(readfile, "r"))
347
348 if is_win then
349 execfile = get_tmp_path() .. ".exe"
350 create_file(execfile)
351 else
352 execfile = get_tmp_path() .. ".sh"
353 create_file(execfile, "#!/bin/bash")
354 end
355 make_unexecutable(execfile)
356 local fd = assert(io.popen(execfile .. " 2>&1"))
357 local result = assert(fd:read("*a"))
358 assert.truthy(result:match("denied"))
359 fd:close()
360 assert.truthy(fs.set_permissions(execfile, "exec", "user"))
361 fd = assert(io.popen(execfile .. " 2>&1"))
362 result = assert(fd:read("*a"))
363 assert.falsy(result:match("denied"))
364 fd:close()
365
366 tmpdir = get_tmp_path()
367 lfs.mkdir(tmpdir)
368 make_unexecutable(tmpdir)
369 fd = assert(io.popen("cd " .. fs.Q(tmpdir) .. " 2>&1"))
370 result = assert(fd:read("*a"))
371 assert.truthy(result:match("denied") or result:match("can't cd"))
372 fd:close()
373 assert.truthy(fs.set_permissions(tmpdir, "exec", "user"))
374 fd = assert(io.popen("cd " .. fs.Q(tmpdir) .. " 2>&1"))
375 result = assert(fd:read("*a"))
376 assert.falsy(result:match("denied") or result:match("can't cd"))
377 fd:close()
378 end)
379
380 it("returns false and does nothing if the argument is nonexistent", function()
381 assert.falsy(fs.set_permissions("/nonexistent", "read", "user"))
382 end)
383 end)
384
385 describe("fs.is_file", function()
386 local tmpfile
387 local tmpdir
388
389 after_each(function()
390 if tmpfile then
391 os.remove(tmpfile)
392 tmpfile = nil
393 end
394 if tmpdir then
395 lfs.rmdir(tmpdir)
396 tmpdir = nil
397 end
398 end)
399
400 it("returns true when the argument is a file", function()
401 tmpfile = get_tmp_path()
402 create_file(tmpfile)
403 assert.same(true, fs.is_file(tmpfile))
404 end)
405
406 it("returns false when the argument does not exist", function()
407 assert.same(false, fs.is_file("/nonexistent"))
408 end)
409
410 it("returns false when the argument exists but is not a file", function()
411 tmpdir = get_tmp_path()
412 lfs.mkdir(tmpdir)
413 assert.same(false, fs.is_file("/nonexistent"))
414 end)
415
416 it("#unix returns false when the argument is a symlink to a directory", function()
417 tmpdir = get_tmp_path()
418 lfs.mkdir(tmpdir)
419 local linkname = tmpdir .. "/symlink"
420 finally(function() os.remove(linkname) end)
421 lfs.link(tmpdir, linkname, true)
422 assert.falsy(fs.is_file(linkname))
423 end)
424
425 it("#unix returns true when the argument is a symlink to a file", function()
426 tmpfile = get_tmp_path()
427 create_file(tmpfile)
428 local linkname = tmpfile .. "_symlink"
429 finally(function() os.remove(linkname) end)
430 lfs.link(tmpfile, linkname, true)
431 assert.truthy(fs.is_file(linkname))
432 end)
433 end)
434
435 describe("fs.is_dir", function()
436 local tmpfile
437 local tmpdir
438
439 after_each(function()
440 if tmpfile then
441 os.remove(tmpfile)
442 tmpfile = nil
443 end
444 if tmpdir then
445 lfs.rmdir(tmpdir)
446 tmpdir = nil
447 end
448 end)
449
450 it("returns true when the argument is a directory", function()
451 tmpdir = get_tmp_path()
452 lfs.mkdir(tmpdir)
453 assert.truthy(fs.is_dir(tmpdir))
454 end)
455
456 it("returns false when the argument is a file", function()
457 tmpfile = get_tmp_path()
458 create_file(tmpfile)
459 assert.falsy(fs.is_dir(tmpfile))
460 end)
461
462 it("#unix returns true when the argument is a symlink to a directory", function()
463 tmpdir = get_tmp_path()
464 lfs.mkdir(tmpdir)
465 local linkname = tmpdir .. "/symlink"
466 finally(function() os.remove(linkname) end)
467 lfs.link(tmpdir, linkname, true)
468 assert.truthy(fs.is_dir(linkname))
469 end)
470
471 it("#unix returns false when the argument is a symlink to a file", function()
472 tmpfile = get_tmp_path()
473 create_file(tmpfile)
474 local linkname = tmpfile .. "_symlink"
475 finally(function() os.remove(linkname) end)
476 lfs.link(tmpfile, linkname, true)
477 assert.falsy(fs.is_dir(linkname))
478 end)
479
480 it("returns false when the argument does not exist", function()
481 assert.falsy(fs.is_dir("/nonexistent"))
482 end)
483 end)
484
485 describe("fs.exists", function()
486 local tmpfile
487 local tmpdir
488
489 after_each(function()
490 if tmpfile then
491 os.remove(tmpfile)
492 tmpfile = nil
493 end
494 if tmpdir then
495 lfs.rmdir(tmpdir)
496 tmpdir = nil
497 end
498 end)
499
500 it("returns true when the argument is a file", function()
501 tmpfile = get_tmp_path()
502 create_file(tmpfile)
503 assert.truthy(fs.exists(tmpfile))
504 end)
505
506 it("returns true when the argument is a directory", function()
507 tmpdir = get_tmp_path()
508 lfs.mkdir(tmpdir)
509 assert.truthy(fs.exists(tmpdir))
510 end)
511
512 it("returns false when the argument does not exist", function()
513 assert.falsy(fs.exists("/nonexistent"))
514 end)
515 end)
516
517 describe("fs.current_dir", function()
518 local tmpdir
519 local olddir
520
521 before_each(function()
522 olddir = lfs.currentdir()
523 end)
524
525 after_each(function()
526 if tmpdir then
527 lfs.rmdir(tmpdir)
528 tmpdir = nil
529 end
530 if olddir then
531 chdir(olddir)
532 olddir = nil
533 end
534 end)
535
536 it("returns the current working directory", function()
537 local currentdir = lfs.currentdir()
538 assert.same(currentdir, fs.current_dir())
539 tmpdir = get_tmp_path()
540 lfs.mkdir(tmpdir)
541 assert.truthy(fs.change_dir(tmpdir))
542 if is_win then
543 assert.same(tmpdir, fs.current_dir())
544 else
545 assert.same(lfs.attributes(tmpdir).ino, lfs.attributes(fs.current_dir()).ino)
546 end
547 end)
548 end)
549
550 describe("fs.change_dir", function()
551 local tmpfile
552 local tmpdir
553 local olddir
554
555 before_each(function()
556 olddir = lfs.currentdir()
557 end)
558
559 after_each(function()
560 if tmpfile then
561 os.remove(tmpfile)
562 tmpfile = nil
563 end
564 if tmpdir then
565 lfs.rmdir(tmpdir)
566 tmpdir = nil
567 end
568 if olddir then
569 chdir(olddir)
570 olddir = nil
571 end
572 end)
573
574 it("returns true and changes the current working directory if the argument is a directory", function()
575 tmpdir = get_tmp_path()
576 lfs.mkdir(tmpdir)
577 assert.truthy(fs.change_dir(tmpdir))
578 if is_win then
579 assert.same(tmpdir, fs.current_dir())
580 else
581 assert.same(lfs.attributes(tmpdir).ino, lfs.attributes(lfs.currentdir()).ino)
582 end
583 end)
584
585 it("returns false and does nothing when the argument is a file", function()
586 tmpfile = get_tmp_path()
587 create_file(tmpfile)
588 assert.falsy(fs.change_dir(tmpfile))
589 assert.same(olddir, lfs.currentdir())
590 end)
591
592 it("returns false and does nothing when the argument does not exist", function()
593 assert.falsy(fs.change_dir("/nonexistent"))
594 assert.same(olddir, lfs.currentdir())
595 end)
596 end)
597
598 describe("fs.change_dir_to_root", function()
599 local tmpdir
600 local olddir
601
602 before_each(function()
603 olddir = lfs.currentdir()
604 end)
605
606 after_each(function()
607 if tmpdir then
608 lfs.rmdir(tmpdir)
609 tmpdir = nil
610 end
611 if olddir then
612 chdir(olddir)
613 end
614 end)
615
616 it("returns true and changes the current directory to root if the current directory is valid", function()
617 tmpdir = get_tmp_path()
618 lfs.mkdir(tmpdir)
619 assert.truthy(fs.change_dir(tmpdir))
620 assert.truthy(fs.change_dir_to_root())
621 if is_win then
622 local curr_dir = fs.current_dir()
623 assert.truthy(curr_dir == "C:\\" or curr_dir == P"/")
624 else
625 assert.same(P"/", fs.current_dir())
626 end
627 end)
628
629 it("returns false and does nothing if the current directory is not valid #unix", function()
630 tmpdir = get_tmp_path()
631 lfs.mkdir(tmpdir)
632 chdir(tmpdir)
633 lfs.rmdir(tmpdir)
634 assert.falsy(fs.change_dir_to_root())
635 assert.is_not.same("/", lfs.currentdir())
636 end)
637 end)
638
639 describe("fs.pop_dir", function()
640 local tmpdir
641 local olddir
642
643 before_each(function()
644 olddir = lfs.currentdir()
645 end)
646
647 after_each(function()
648 if tmpdir then
649 lfs.rmdir(tmpdir)
650 tmpdir = nil
651 end
652 if olddir then
653 chdir(olddir)
654 end
655 end)
656
657 it("returns true and changes the current directory to the previous one in the dir stack if the dir stack is not empty", function()
658 tmpdir = get_tmp_path()
659 lfs.mkdir(tmpdir)
660 assert.truthy(fs.change_dir(tmpdir))
661 assert.truthy(fs.pop_dir())
662 assert.same(olddir, lfs.currentdir())
663 end)
664 end)
665
666 describe("fs.make_dir", function()
667 local tmpfile
668 local tmpdir
669 local intdir
670
671 after_each(function()
672 if tmpfile then
673 os.remove(tmpfile)
674 tmpfile = nil
675 end
676 if intdir then
677 lfs.rmdir(intdir)
678 intdir = nil
679 end
680 if tmpdir then
681 lfs.rmdir(tmpdir)
682 tmpdir = nil
683 end
684 end)
685
686 it("returns true and creates the directory specified by the argument", function()
687 tmpdir = get_tmp_path()
688 assert.truthy(fs.make_dir(tmpdir))
689 assert.same("directory", lfs.attributes(tmpdir, "mode"))
690 end)
691
692 it("returns true and creates the directory path specified by the argument", function()
693 tmpdir = get_tmp_path()
694 intdir = "/internaldir"
695 local dirpath = tmpdir .. intdir
696 assert.truthy(fs.make_dir(dirpath))
697 assert.same("directory", lfs.attributes(tmpdir, "mode"))
698 assert.same("directory", lfs.attributes(dirpath, "mode"))
699 end)
700
701 it("returns false and does nothing if the argument is not valid (file in the path)", function()
702 tmpfile = get_tmp_path()
703 local fd = assert(io.open(tmpfile, "w"))
704 assert(fd:write("foo"))
705 fd:close()
706 intdir = "/internaldir"
707 local dirpath = tmpfile .. intdir
708 assert.falsy(fs.make_dir(dirpath))
709 end)
710
711 it("returns false and does nothing if the argument already exists", function()
712 tmpfile = get_tmp_path()
713 create_file(tmpfile)
714 assert.falsy(fs.make_dir(tmpfile))
715 end)
716 end)
717
718 describe("fs.remove_dir_if_empty", function()
719 local tmpfile
720 local tmpdir
721
722 after_each(function()
723 if tmpfile then
724 os.remove(tmpfile)
725 tmpfile = nil
726 end
727 if tmpdir then
728 lfs.rmdir(tmpdir)
729 tmpdir = nil
730 end
731 end)
732
733 it("removes the directory specified by the argument if it is empty", function()
734 tmpdir = get_tmp_path()
735 lfs.mkdir(tmpdir)
736 fs.remove_dir_if_empty(tmpdir)
737 assert.falsy(exists_file(tmpdir))
738 end)
739
740 it("does nothing if the directory specified by the argument is not empty", function()
741 tmpdir = get_tmp_path()
742 lfs.mkdir(tmpdir)
743 tmpfile = "/internalfile"
744 local filepath = tmpdir .. tmpfile
745 create_file(filepath)
746 fs.remove_dir_if_empty(tmpdir)
747 assert.truthy(exists_file(tmpdir))
748 end)
749 end)
750
751 describe("fs.remove_dir_tree_if_empty", function()
752 local tmpfile
753 local tmpdir
754 local intdir
755
756 after_each(function()
757 if tmpfile then
758 os.remove(tmpfile)
759 tmpfile = nil
760 end
761 if intdir then
762 lfs.rmdir(intdir)
763 intdir = nil
764 end
765 if tmpdir then
766 lfs.rmdir(tmpdir)
767 tmpdir = nil
768 end
769 end)
770
771 it("removes the directory path specified by the argument if it is empty", function()
772 tmpdir = get_tmp_path()
773 lfs.mkdir(tmpdir)
774 fs.remove_dir_tree_if_empty(tmpdir)
775 assert.falsy(exists_file(tmpdir))
776 end)
777
778 it("does nothing if the directory path specified by the argument is not empty", function()
779 tmpdir = get_tmp_path()
780 lfs.mkdir(tmpdir)
781 intdir = "/internaldir"
782 local dirpath = tmpdir .. intdir
783 lfs.mkdir(dirpath)
784 tmpfile = "/internalfile"
785 local filepath = dirpath .. tmpfile
786 fs.remove_dir_tree_if_empty(tmpdir)
787 assert.truthy(exists_file(dirpath))
788 assert.truthy(exists_file(tmpdir))
789 end)
790 end)
791
792 describe("fs.list_dir", function()
793 local intfile1
794 local intfile2
795 local intdir
796 local tmpdir
797
798 before_each(function()
799 if intfile1 then
800 os.remove(intfile1)
801 intfile1 = nil
802 end
803 if intfile2 then
804 os.remove(intfile2)
805 intfile2 = nil
806 end
807 if intdir then
808 lfs.rmdir(intdir)
809 intdir = nil
810 end
811 if tmpdir then
812 lfs.rmdir(tmpdir)
813 tmpdir = nil
814 end
815 end)
816
817 it("returns a table with the contents of the given directory", function()
818 tmpdir = get_tmp_path()
819 lfs.mkdir(tmpdir)
820 intfile1 = tmpdir .. "/intfile1"
821 create_file(intfile1)
822 intdir = tmpdir .. "/intdir"
823 lfs.mkdir(intdir)
824 intfile2 = intdir .. "/intfile2"
825 create_file(intfile2)
826 local result = fs.list_dir(tmpdir)
827 assert.same(#result, 2)
828 assert.truthy(result[1] == "intfile1" or result[1] == "intdir")
829 assert.truthy(result[2] == "intfile1" or result[2] == "intdir")
830 assert.is_not.same(result[1], result[2])
831 end)
832
833 it("returns an empty table if the argument is a file", function()
834 intfile1 = get_tmp_path()
835 create_file(intfile1)
836 local result = fs.list_dir(intfile1)
837 assert.same(#result, 0)
838 end)
839
840 it("does nothing if the argument is nonexistent", function()
841 assert.same(fs.list_dir("/nonexistent"), {})
842 end)
843
844 it("does nothing if the argument doesn't have the proper permissions", function()
845 tmpdir = get_tmp_path()
846 lfs.mkdir(tmpdir)
847 make_unreadable(tmpdir)
848 assert.same(fs.list_dir(tmpdir), {})
849 end)
850 end)
851
852 describe("fs.copy", function()
853 local srcfile
854 local dstfile
855 local tmpdir
856
857 after_each(function()
858 if srcfile then
859 os.remove(srcfile)
860 srcfile = nil
861 end
862 if dstfile then
863 os.remove(dstfile)
864 dstfile = nil
865 end
866 if tmpdir then
867 lfs.rmdir(tmpdir)
868 tmpdir = nil
869 end
870 end)
871
872 it("returns true and copies the contents and the permissions of the source file to the destination file", function()
873 srcfile = get_tmp_path()
874 create_file(srcfile, srccontent)
875 dstfile = get_tmp_path()
876 assert.truthy(fs.copy(srcfile, dstfile))
877 local fd = assert(io.open(dstfile, "r"))
878 local dstcontent = fd:read("*a")
879 assert.same("foo", dstcontent)
880 if posix_ok then
881 assert.same(lfs.attributes(srcfile, "permissions"), lfs.attributes(dstfile, "permissions"))
882 end
883 end)
884
885 it("returns true and copies contents of the source file to the destination file with custom permissions", function()
886 srcfile = get_tmp_path()
887 create_file(srcfile, srccontent)
888 dstfile = get_tmp_path()
889 assert.truthy(fs.copy(srcfile, dstfile, "exec"))
890 local fd = assert(io.open(dstfile, "r"))
891 local dstcontent = fd:read("*a")
892 assert.same("foo", dstcontent)
893 end)
894
895 it("returns false and does nothing if the source file does not exist", function()
896 srcfile = get_tmp_path()
897 dstfile = get_tmp_path()
898 local ok, err = fs.copy(srcfile, dstfile, nil)
899 assert.falsy(ok)
900 assert.not_match("are the same file", err)
901 assert.falsy(exists_file(dstfile))
902 end)
903
904 it("returns false and does nothing if the source file doesn't have the proper permissions", function()
905 srcfile = get_tmp_path()
906 create_file(srcfile)
907 make_unreadable(srcfile)
908 dstfile = get_tmp_path()
909 assert.falsy(fs.copy(srcfile, dstfile, nil))
910 assert.falsy(exists_file(dstfile))
911 end)
912
913 it("returns false and does nothing if the destination file directory doesn't have the proper permissions", function()
914 srcfile = get_tmp_path()
915 create_file(srcfile)
916 tmpdir = get_tmp_path()
917 lfs.mkdir(tmpdir)
918 make_unwritable(tmpdir)
919 dstfile = tmpdir .. "/dstfile"
920 assert.falsy(fs.copy(srcfile, dstfile, nil))
921 assert(fs.set_permissions(tmpdir, "exec", "all"))
922 assert.falsy(exists_file(dstfile))
923 end)
924 end)
925
926 describe("fs.copy_contents", function()
927 local srcfile
928 local dstfile
929 local srcintdir
930 local dstintdir
931 local srcdir
932 local dstdir
933
934 after_each(function()
935 if srcfile then
936 os.remove(srcfile)
937 srcfile = nil
938 end
939 if dstfile then
940 os.remove(dstfile)
941 dstfile = nil
942 end
943 if srcintdir then
944 lfs.rmdir(srcintdir)
945 srcintdir = nil
946 end
947 if dstintdir then
948 lfs.rmdir(dstintdir)
949 dstintdir = nil
950 end
951 if srcdir then
952 lfs.rmdir(srcdir)
953 srcdir = nil
954 end
955 if dstdir then
956 lfs.rmdir(dstdir)
957 dstdir = nil
958 end
959 end)
960
961 local create_dir_tree = function()
962 srcdir = get_tmp_path()
963 lfs.mkdir(srcdir)
964 srcintdir = srcdir .. "/internaldir"
965 lfs.mkdir(srcintdir)
966 srcfile = srcintdir .. "/internalfile"
967 create_file(srcfile)
968 dstdir = get_tmp_path()
969 end
970
971 it("returns true and copies the contents (with their permissions) of the source dir to the destination dir", function()
972 create_dir_tree()
973 assert.truthy(fs.copy_contents(srcdir, dstdir))
974 assert.truthy(exists_file(dstdir))
975 dstintdir = dstdir .. "/internaldir"
976 assert.truthy(exists_file(dstintdir))
977 dstfile = dstdir .. "/internaldir/internalfile"
978 local fd = assert(io.open(dstfile, "r"))
979 local dstfilecontent = fd:read("*a")
980 assert.same("foo", dstfilecontent)
981 if posix_ok then
982 assert.same(lfs.attributes(srcfile, "permissions"), lfs.attributes(dstfile, "permissions"))
983 end
984 end)
985
986 it("returns true and copies the contents of the source dir to the destination dir with custom permissions", function()
987 create_dir_tree()
988 assert.truthy(fs.copy_contents(srcdir, dstdir, "read"))
989 assert.truthy(exists_file(dstdir))
990 dstintdir = dstdir .. "/internaldir"
991 assert.truthy(exists_file(dstintdir))
992 dstfile = dstdir .. "/internaldir/internalfile"
993 local fd = assert(io.open(dstfile, "r"))
994 local dstfilecontent = fd:read("*a")
995 assert.same("foo", dstfilecontent)
996 end)
997
998 it("returns false and does nothing if the source dir doesn't exist", function()
999 srcdir = get_tmp_path()
1000 dstdir = get_tmp_path()
1001 assert.falsy(fs.copy_contents(srcdir, dstdir))
1002 assert.falsy(exists_file(dstdir))
1003 end)
1004
1005 it("returns false if the source argument is a file", function()
1006 srcdir = get_tmp_path()
1007 create_file(srcdir)
1008 dstdir = get_tmp_path()
1009 assert.falsy(fs.copy_contents(srcdir, dstdir))
1010 assert.falsy(exists_file(dstdir))
1011 end)
1012
1013 it("returns false and does nothing if the source dir doesn't have the proper permissions", function()
1014 create_dir_tree()
1015 make_unreadable(srcdir)
1016 assert.falsy(fs.copy_contents(srcdir, dstdir))
1017 assert.falsy(exists_file(dstdir .. "/internaldir"))
1018 assert.falsy(exists_file(dstdir .. "/internalfile"))
1019 end)
1020 end)
1021
1022 describe("fs.find", function()
1023 local tmpdir
1024 local intdir
1025 local intfile1
1026 local intfile2
1027
1028 after_each(function()
1029 if intfile1 then
1030 os.remove(intfile1)
1031 intfile1 = nil
1032 end
1033 if intfile2 then
1034 os.remove(intfile2)
1035 intfile2 = nil
1036 end
1037 if intdir then
1038 lfs.rmdir(intdir)
1039 intdir = nil
1040 end
1041 if tmpdir then
1042 lfs.rmdir(tmpdir)
1043 tmpdir = nil
1044 end
1045 end)
1046
1047 local create_dir_tree = function()
1048 tmpdir = get_tmp_path()
1049 lfs.mkdir(tmpdir)
1050 intfile1 = tmpdir .. "/intfile1"
1051 create_file(intfile1)
1052 intdir = tmpdir .. "/intdir"
1053 lfs.mkdir(intdir)
1054 intfile2 = intdir .. "/intfile2"
1055 create_file(intfile2)
1056 end
1057
1058 it("returns a table of all the contents in the directory given as argument", function()
1059 create_dir_tree()
1060 local contents = {}
1061 local count = 0
1062 for _, file in pairs(fs.find(tmpdir)) do
1063 contents[file] = true
1064 count = count + 1
1065 end
1066 assert.same(count, 3)
1067 assert.is_not.same(contents[tmpdir], true)
1068 assert.same(contents["intfile1"], true)
1069 assert.same(contents["intdir"], true)
1070 assert.same(contents["intdir/intfile2"], true)
1071 end)
1072
1073 it("uses the current working directory if the argument is nil", function()
1074 create_dir_tree()
1075 local olddir = fs.current_dir()
1076 fs.change_dir(intdir)
1077 local contents = {}
1078 local count = 0
1079 for _, file in pairs(fs.find()) do
1080 contents[file] = true
1081 count = count + 1
1082 end
1083 assert.same(count, 1)
1084 assert.is_not.same(contents["intfile1"], true)
1085 assert.is_not.same(contents["intdir"], true)
1086 assert.same(contents["intfile2"], true)
1087 fs.change_dir(olddir)
1088 end)
1089
1090 it("returns an empty table if the argument is nonexistent", function()
1091 local contents = fs.find("/nonexistent")
1092 local count = 0
1093 for _, file in pairs(contents) do
1094 count = count + 1
1095 end
1096 assert.same(count, 0)
1097 end)
1098
1099 it("returns an empty table if the argument is a file", function()
1100 intfile1 = get_tmp_path()
1101 create_file(intfile1)
1102 local contents = fs.find(intfile1)
1103 local count = 0
1104 for _, file in pairs(contents) do
1105 count = count + 1
1106 end
1107 assert.same(count, 0)
1108 end)
1109
1110 it("does nothing if the argument doesn't have the proper permissions", function()
1111 tmpdir = get_tmp_path()
1112 lfs.mkdir(tmpdir)
1113 make_unreadable(tmpdir)
1114 assert.same(fs.find(tmpdir), {})
1115 end)
1116 end)
1117
1118 describe("fs.move", function()
1119 local srcfile
1120 local dstfile
1121 local tmpdir
1122
1123 after_each(function()
1124 if srcfile then
1125 os.remove(srcfile)
1126 srcfile = nil
1127 end
1128 if dstfile then
1129 os.remove(dstfile)
1130 dstfile = nil
1131 end
1132 if tmpdir then
1133 lfs.rmdir(tmpdir)
1134 tmpdir = nil
1135 end
1136 end)
1137
1138 it("returns true and moves the source (together with its permissions) to the destination", function()
1139 srcfile = get_tmp_path()
1140 create_file(srcfile)
1141 dstfile = get_tmp_path()
1142 local oldperms = lfs.attributes(srcfile, "permissions")
1143 assert.truthy(fs.move(srcfile, dstfile))
1144 assert.truthy(fs.exists(dstfile))
1145 assert.falsy(fs.exists(srcfile))
1146 local fd = assert(io.open(dstfile, "r"))
1147 local dstcontents = assert(fd:read("*a"))
1148 assert.same(dstcontents, "foo")
1149 if posix_ok then
1150 assert.same(oldperms, lfs.attributes(dstfile, "permissions"))
1151 end
1152 end)
1153
1154 it("returns true and moves the source (with custom permissions) to the destination", function()
1155 srcfile = get_tmp_path()
1156 create_file(srcfile)
1157 dstfile = get_tmp_path()
1158 assert.truthy(fs.move(srcfile, dstfile, "read"))
1159 assert.truthy(fs.exists(dstfile))
1160 assert.falsy(fs.exists(srcfile))
1161 local fd = assert(io.open(dstfile, "r"))
1162 local dstcontents = assert(fd:read("*a"))
1163 assert.same(dstcontents, "foo")
1164 end)
1165
1166 it("returns false and does nothing if the source doesn't exist", function()
1167 dstfile = get_tmp_path()
1168 assert.falsy(fs.move("/nonexistent", dstfile))
1169 assert.falsy(fs.exists(dstfile))
1170 end)
1171
1172 it("returns false and does nothing if the destination already exists", function()
1173 srcfile = get_tmp_path()
1174 create_file(srcfile)
1175 dstfile = get_tmp_path()
1176 create_file(dstfile, "bar")
1177 assert.falsy(fs.move(srcfile, dstfile))
1178 assert.truthy(fs.exists(srcfile))
1179 local fd = assert(io.open(dstfile, "r"))
1180 local dstcontents = assert(fd:read("*a"))
1181 assert.same(dstcontents, "bar")
1182 end)
1183
1184 it("returns false and does nothing if the destination path doesn't have the proper permissions", function()
1185 srcfile = get_tmp_path()
1186 create_file(srcfile)
1187 tmpdir = get_tmp_path()
1188 lfs.mkdir(tmpdir)
1189 make_unwritable(tmpdir)
1190 assert.falsy(fs.move(srcfile, tmpdir .. "/dstfile"))
1191 assert.falsy(fs.exists(tmpdir .. "/dstfile"))
1192 end)
1193 end)
1194
1195 describe("fs.is_lua", function()
1196 local tmpfile
1197
1198 after_each(function()
1199 if tmpfile then
1200 os.remove(tmpfile)
1201 tmpfile = nil
1202 end
1203 end)
1204
1205 it("returns true if the argument is a valid lua script", function()
1206 tmpfile = get_tmp_path()
1207 create_file(tmpfile, "print(\"foo\")")
1208 assert.truthy(fs.is_lua(tmpfile))
1209 end)
1210
1211 it("returns true if the argument is a valid lua script with shebang", function()
1212 tmpfile = get_tmp_path()
1213 create_file(tmpfile, "#!/usr/bin/env lua\n\nprint(\"foo\")")
1214 assert.truthy(fs.is_lua(tmpfile))
1215 end)
1216
1217 it("returns false if the argument is not a valid lua script", function()
1218 tmpfile = os.tmpname()
1219 create_file(tmpfile)
1220 assert.falsy(fs.is_lua(tmpfile))
1221 end)
1222
1223 it("returns false if the argument is a valid lua script but doesn't have the proper permissions", function()
1224 tmpfile = get_tmp_path()
1225 create_file(tmpfile, "print(\"foo\")")
1226 make_unreadable(tmpfile)
1227 assert.falsy(fs.is_lua(tmpfile))
1228 end)
1229 end)
1230
1231 describe("fs.delete", function()
1232 local tmpfile1
1233 local tmpfile2
1234 local tmpintdir
1235 local tmpdir
1236
1237 after_each(function()
1238 if tmpfile1 then
1239 os.remove(tmpfile1)
1240 tmpfile1 = nil
1241 end
1242 if tmpfile2 then
1243 os.remove(tmpfile2)
1244 tmpfile2 = nil
1245 end
1246 if tmpintdir then
1247 lfs.rmdir(tmpintdir)
1248 tmpintdir = nil
1249 end
1250 if tmpdir then
1251 lfs.rmdir(tmpdir)
1252 tmpdir = nil
1253 end
1254 end)
1255
1256 local create_dir_tree = function()
1257 tmpdir = get_tmp_path()
1258 lfs.mkdir(tmpdir)
1259 tmpintdir = tmpdir .. "/internaldir"
1260 lfs.mkdir(tmpintdir)
1261 tmpfile1 = tmpdir .. "/internalfile1"
1262 create_file(tmpfile1)
1263 tmpfile2 = tmpdir .. "/internalfile2"
1264 create_file(tmpfile2)
1265 end
1266
1267 it("deletes the file specified by the argument", function()
1268 tmpfile1 = get_tmp_path()
1269 tmpfile2 = get_tmp_path()
1270 fs.delete(tmpfile1)
1271 fs.delete(tmpfile2)
1272 assert.falsy(exists_file(tmpfile1))
1273 assert.falsy(exists_file(tmpfile2))
1274 end)
1275
1276 it("deletes the contents of the directory specified by the argument", function()
1277 create_dir_tree()
1278 fs.delete(tmpdir)
1279 assert.falsy(exists_file(tmpfile2))
1280 assert.falsy(exists_file(tmpintdir))
1281 assert.falsy(exists_file(tmpfile1))
1282 assert.falsy(exists_file(tmpdir))
1283 end)
1284 end)
1285
1286 describe("fs.download #mock", function()
1287 local tmpfile
1288 local tmpdir
1289
1290 setup(function()
1291 test_env.mock_server_init()
1292 end)
1293
1294 teardown(function()
1295 test_env.mock_server_done()
1296 end)
1297
1298 after_each(function()
1299 if tmpfile then
1300 os.remove(tmpfile)
1301 tmpfile = nil
1302 end
1303 if tmpdir then
1304 lfs.rmdir(tmpdir)
1305 tmpdir = nil
1306 end
1307 end)
1308
1309 it("returns true and fetches the url argument into the specified filename", function()
1310 tmpfile = get_tmp_path()
1311 assert.truthy(fs.download("http://localhost:8080/file/a_rock.lua", tmpfile))
1312 local fd = assert(io.open(tmpfile, "r"))
1313 local downloadcontent = assert(fd:read("*a"))
1314 fd:close()
1315 fd = assert(io.open(testing_paths.fixtures_dir .. "/a_rock.lua", "r"))
1316 local originalcontent = assert(fd:read("*a"))
1317 fd:close()
1318 assert.same(downloadcontent, originalcontent)
1319 end)
1320
1321 it("returns true and fetches the url argument into a file whose name matches the basename of the url if the filename argument is not given", function()
1322 tmpdir = get_tmp_path()
1323 lfs.mkdir(tmpdir)
1324 fs.change_dir(tmpdir)
1325 assert.truthy(fs.download("http://localhost:8080/file/a_rock.lua"))
1326 tmpfile = tmpdir .. "/a_rock.lua"
1327 local fd = assert(io.open(tmpfile, "r"))
1328 local downloadcontent = assert(fd:read("*a"))
1329 fd:close()
1330 fd = assert(io.open(testing_paths.fixtures_dir .. "/a_rock.lua", "r"))
1331 local originalcontent = assert(fd:read("*a"))
1332 fd:close()
1333 assert.same(downloadcontent, originalcontent)
1334 fs.pop_dir()
1335 end)
1336
1337 it("returns false and does nothing if the url argument contains a nonexistent file", function()
1338 tmpfile = get_tmp_path()
1339 assert.falsy(fs.download("http://localhost:8080/file/nonexistent", tmpfile))
1340 end)
1341
1342 it("returns false and does nothing if the url argument is invalid", function()
1343 assert.falsy(fs.download("invalidurl"))
1344 end)
1345 end)
1346
1347 describe("fs.zip", function()
1348 local tmpdir
1349 local olddir
1350
1351 before_each(function()
1352 olddir = lfs.currentdir()
1353 tmpdir = get_tmp_path()
1354 lfs.mkdir(tmpdir)
1355 chdir(tmpdir)
1356
1357 write_file("file1", "content1", finally)
1358 write_file("file2", "content2", finally)
1359 lfs.mkdir("dir")
1360 write_file("dir/file3", "content3", finally)
1361 end)
1362
1363 after_each(function()
1364 if olddir then
1365 chdir(olddir)
1366 if tmpdir then
1367 lfs.rmdir(tmpdir .. "/dir")
1368 lfs.rmdir(tmpdir)
1369 end
1370 end
1371 end)
1372
1373 it("returns true and creates a zip archive of the given files", function()
1374 assert.truthy(fs.zip("archive.zip", "file1", "file2", "dir"))
1375 assert.truthy(exists_file("archive.zip"))
1376 end)
1377
1378 it("returns false and does nothing if the files specified in the arguments are invalid", function()
1379 assert.falsy(fs.zip("archive.zip", "nonexistent"))
1380 assert.falsy(exists_file("nonexistent"))
1381 end)
1382 end)
1383
1384 describe("fs.bunzip2", function()
1385
1386 it("uncompresses a .bz2 file", function()
1387 local input = testing_paths.fixtures_dir .. "/abc.bz2"
1388 local output = os.tmpname()
1389 assert.truthy(fs.bunzip2(input, output))
1390 local fd = io.open(output, "r")
1391 local content = fd:read("*a")
1392 fd:close()
1393 assert.same(300000, #content)
1394 local abc = ("a"):rep(100000)..("b"):rep(100000)..("c"):rep(100000)
1395 assert.same(abc, content)
1396 end)
1397
1398 end)
1399
1400 describe("fs.unzip", function()
1401 local tmpdir
1402 local olddir
1403
1404 before_each(function()
1405 olddir = lfs.currentdir()
1406 tmpdir = get_tmp_path()
1407 lfs.mkdir(tmpdir)
1408 chdir(tmpdir)
1409
1410 write_file("file1", "content1", finally)
1411 write_file("file2", "content2", finally)
1412 lfs.mkdir("dir")
1413 write_file("dir/file3", "content3", finally)
1414 end)
1415
1416 after_each(function()
1417 if olddir then
1418 chdir(olddir)
1419 if tmpdir then
1420 lfs.rmdir(tmpdir .. "/dir")
1421 lfs.rmdir(tmpdir)
1422 end
1423 end
1424 end)
1425
1426 it("returns true and unzips the given zip archive", function()
1427 assert.truthy(fs.zip("archive.zip", "file1", "file2", "dir"))
1428 os.remove("file1")
1429 os.remove("file2")
1430 lfs.rmdir("dir")
1431
1432 assert.truthy(fs.unzip("archive.zip"))
1433 assert.truthy(exists_file("file1"))
1434 assert.truthy(exists_file("file2"))
1435 assert.truthy(exists_file("dir/file3"))
1436
1437 local fd
1438
1439 fd = assert(io.open("file1", "r"))
1440 assert.same(fd:read("*a"), "content1")
1441 fd:close()
1442
1443 fd = assert(io.open("file2", "r"))
1444 assert.same(fd:read("*a"), "content2")
1445 fd:close()
1446
1447 fd = assert(io.open("dir/file3", "r"))
1448 assert.same(fd:read("*a"), "content3")
1449 fd:close()
1450 end)
1451
1452 it("does nothing if the given archive is invalid", function()
1453 assert.falsy(fs.unzip("archive.zip"))
1454 end)
1455 end)
1456
1457 describe("fs.wrap_script", function()
1458 local tmpdir
1459 local olddir
1460
1461 before_each(function()
1462 olddir = lfs.currentdir()
1463 tmpdir = get_tmp_path()
1464 lfs.mkdir(tmpdir)
1465 chdir(tmpdir)
1466 end)
1467
1468 after_each(function()
1469 if olddir then
1470 chdir(olddir)
1471 if tmpdir then
1472 lfs.rmdir(tmpdir)
1473 end
1474 end
1475 end)
1476
1477 it("produces a wrapper for a Lua script", function()
1478 write_file("my_script", "io.write('Hello ' .. arg[1])", finally)
1479 path.use_tree(testing_paths.testing_tree)
1480 local wrapper_name = fs.absolute_name("wrapper") .. test_env.wrapper_extension
1481 fs.wrap_script("my_script", wrapper_name, "one", nil, nil, "World")
1482 local pd = assert(io.popen(wrapper_name))
1483 local data = pd:read("*a")
1484 pd:close()
1485 assert.same("Hello World", data)
1486 end)
1487 end)
1488
1489 describe("fs.copy_binary", function()
1490 local tmpdir
1491 local olddir
1492
1493 before_each(function()
1494 olddir = lfs.currentdir()
1495 tmpdir = get_tmp_path()
1496 lfs.mkdir(tmpdir)
1497 chdir(tmpdir)
1498
1499 write_file("test.exe", "", finally)
1500 end)
1501
1502 after_each(function()
1503 if olddir then
1504 chdir(olddir)
1505 if tmpdir then
1506 lfs.rmdir(tmpdir)
1507 end
1508 end
1509 end)
1510
1511 it("returns true and copies the given binary file to the file specified in the dest argument", function()
1512 assert.truthy(fs.copy_binary("test.exe", lfs.currentdir() .. "/copy.exe"))
1513 assert.truthy(exists_file("copy.exe"))
1514 if is_win then
1515 assert.truthy(exists_file("test.lua"))
1516 local fd = assert(io.open("test.lua", "r"))
1517 local content = assert(fd:read("*a"))
1518 assert.truthy(content:find("package.path", 1, true))
1519 assert.truthy(content:find("package.cpath", 1, true))
1520 fd:close()
1521 end
1522 end)
1523
1524 it("returns false and does nothing if the source file is invalid", function()
1525 assert.falsy(fs.copy_binary("invalid.exe", "copy.exe"))
1526 end)
1527 end)
1528
1529 describe("fs.modules", function()
1530 local tmpdir
1531 local olddir
1532 local oldpath
1533
1534 before_each(function()
1535 olddir = lfs.currentdir()
1536 tmpdir = get_tmp_path()
1537 lfs.mkdir(tmpdir)
1538 chdir(tmpdir)
1539 lfs.mkdir("lib")
1540 write_file("lib/module1.lua", "", finally)
1541 write_file("lib/module2.lua", "", finally)
1542 write_file("lib/module1.LuA", "", finally)
1543 write_file("lib/non_lua", "", finally)
1544 lfs.mkdir("lib/internal")
1545 write_file("lib/internal/module11.lua", "", finally)
1546 write_file("lib/internal/module22.lua", "", finally)
1547
1548 oldpath = package.path
1549 package.path = package.path .. tmpdir .. "/?.lua;"
1550 end)
1551
1552 after_each(function()
1553 if olddir then
1554 chdir(olddir)
1555 if tmpdir then
1556 lfs.rmdir(tmpdir .. "/lib/internal")
1557 lfs.rmdir(tmpdir .. "/lib")
1558 lfs.rmdir(tmpdir)
1559 end
1560 end
1561 if oldpath then
1562 package.path = oldpath
1563 end
1564 end)
1565
1566 it("returns a table of the lua modules at a specific require path", function()
1567 local result
1568
1569 result = fs.modules("lib")
1570 assert.same(#result, 2)
1571 assert.truthy(result[1] == "module1" or result[2] == "module1")
1572 assert.truthy(result[1] == "module2" or result[2] == "module2")
1573
1574 result = fs.modules("lib.internal")
1575 assert.same(#result, 2)
1576 assert.truthy(result[1] == "module11" or result[2] == "module11")
1577 assert.truthy(result[1] == "module22" or result[2] == "module22")
1578 end)
1579
1580 it("returns an empty table if the modules couldn't be found in package.path", function()
1581 package.path = ""
1582 assert.same(fs.modules("lib"), {})
1583 end)
1584 end)
1585
1586 describe("#unix fs._unix_rwx_to_number", function()
1587
1588 it("converts permissions in rwx notation to numeric ones", function()
1589 assert.same(tonumber("0644", 8), fs._unix_rwx_to_number("rw-r--r--"))
1590 assert.same(tonumber("0755", 8), fs._unix_rwx_to_number("rwxr-xr-x"))
1591 assert.same(tonumber("0000", 8), fs._unix_rwx_to_number("---------"))
1592 assert.same(tonumber("0777", 8), fs._unix_rwx_to_number("rwxrwxrwx"))
1593 assert.same(tonumber("0700", 8), fs._unix_rwx_to_number("rwx------"))
1594 assert.same(tonumber("0600", 8), fs._unix_rwx_to_number("rw-------"))
1595 end)
1596
1597 it("produces a negated mask if asked to", function()
1598 assert.same(tonumber("0133", 8), fs._unix_rwx_to_number("rw-r--r--", true))
1599 assert.same(tonumber("0022", 8), fs._unix_rwx_to_number("rwxr-xr-x", true))
1600 assert.same(tonumber("0777", 8), fs._unix_rwx_to_number("---------", true))
1601 assert.same(tonumber("0000", 8), fs._unix_rwx_to_number("rwxrwxrwx", true))
1602 assert.same(tonumber("0077", 8), fs._unix_rwx_to_number("rwx------", true))
1603 assert.same(tonumber("0177", 8), fs._unix_rwx_to_number("rw-------", true))
1604 end)
1605 end)
1606
1607end)