aboutsummaryrefslogtreecommitdiff
path: root/lfw/rocks/luafilesystem/1.5.0-1/tests/test.lua
diff options
context:
space:
mode:
Diffstat (limited to 'lfw/rocks/luafilesystem/1.5.0-1/tests/test.lua')
-rw-r--r--lfw/rocks/luafilesystem/1.5.0-1/tests/test.lua130
1 files changed, 130 insertions, 0 deletions
diff --git a/lfw/rocks/luafilesystem/1.5.0-1/tests/test.lua b/lfw/rocks/luafilesystem/1.5.0-1/tests/test.lua
new file mode 100644
index 00000000..71110749
--- /dev/null
+++ b/lfw/rocks/luafilesystem/1.5.0-1/tests/test.lua
@@ -0,0 +1,130 @@
1#!/usr/local/bin/lua5.1
2
3local tmp = "/tmp"
4local sep = "/"
5local upper = ".."
6
7require"lfs"
8print (lfs._VERSION)
9
10function attrdir (path)
11 for file in lfs.dir(path) do
12 if file ~= "." and file ~= ".." then
13 local f = path..sep..file
14 print ("\t=> "..f.." <=")
15 local attr = lfs.attributes (f)
16 assert (type(attr) == "table")
17 if attr.mode == "directory" then
18 attrdir (f)
19 else
20 for name, value in pairs(attr) do
21 print (name, value)
22 end
23 end
24 end
25 end
26end
27
28-- Checking changing directories
29local current = assert (lfs.currentdir())
30local reldir = string.gsub (current, "^.*%"..sep.."([^"..sep.."])$", "%1")
31assert (lfs.chdir (upper), "could not change to upper directory")
32assert (lfs.chdir (reldir), "could not change back to current directory")
33assert (lfs.currentdir() == current, "error trying to change directories")
34assert (lfs.chdir ("this couldn't be an actual directory") == nil, "could change to a non-existent directory")
35
36-- Changing creating and removing directories
37local tmpdir = current..sep.."lfs_tmp_dir"
38local tmpfile = tmpdir..sep.."tmp_file"
39-- Test for existence of a previous lfs_tmp_dir
40-- that may have resulted from an interrupted test execution and remove it
41if lfs.chdir (tmpdir) then
42 assert (lfs.chdir (upper), "could not change to upper directory")
43 assert (os.remove (tmpfile), "could not remove file from previous test")
44 assert (lfs.rmdir (tmpdir), "could not remove directory from previous test")
45end
46
47-- tries to create a directory
48assert (lfs.mkdir (tmpdir), "could not make a new directory")
49local attrib, errmsg = lfs.attributes (tmpdir)
50if not attrib then
51 error ("could not get attributes of file `"..tmpdir.."':\n"..errmsg)
52end
53local f = io.open(tmpfile, "w")
54f:close()
55
56-- Change access time
57local testdate = os.time({ year = 2007, day = 10, month = 2, hour=0})
58assert (lfs.touch (tmpfile, testdate))
59local new_att = assert (lfs.attributes (tmpfile))
60assert (new_att.access == testdate, "could not set access time")
61assert (new_att.modification == testdate, "could not set modification time")
62
63-- Change access and modification time
64local testdate1 = os.time({ year = 2007, day = 10, month = 2, hour=0})
65local testdate2 = os.time({ year = 2007, day = 11, month = 2, hour=0})
66
67assert (lfs.touch (tmpfile, testdate2, testdate1))
68local new_att = assert (lfs.attributes (tmpfile))
69assert (new_att.access == testdate2, "could not set access time")
70assert (new_att.modification == testdate1, "could not set modification time")
71
72local res, err = lfs.symlinkattributes(tmpfile)
73if err ~= "symlinkattributes not supported on this platform" then
74 -- Checking symbolic link information (does not work in Windows)
75 assert (os.execute ("ln -s "..tmpfile.." _a_link_for_test_"))
76 assert (lfs.attributes"_a_link_for_test_".mode == "file")
77 assert (lfs.symlinkattributes"_a_link_for_test_".mode == "link")
78 assert (os.remove"_a_link_for_test_")
79end
80
81if lfs.setmode then
82 -- Checking text/binary modes (works only in Windows)
83 local f = io.open(tmpfile, "w")
84 local result, mode = lfs.setmode(f, "binary")
85 assert((result and mode == "text") or (not result and mode == "setmode not supported on this platform"))
86 result, mode = lfs.setmode(f, "text")
87 assert((result and mode == "binary") or (not result and mode == "setmode not supported on this platform"))
88 f:close()
89end
90
91-- Restore access time to current value
92assert (lfs.touch (tmpfile, attrib.access, attrib.modification))
93new_att = assert (lfs.attributes (tmpfile))
94assert (new_att.access == attrib.access)
95assert (new_att.modification == attrib.modification)
96
97-- Remove new file and directory
98assert (os.remove (tmpfile), "could not remove new file")
99assert (lfs.rmdir (tmpdir), "could not remove new directory")
100assert (lfs.mkdir (tmpdir..sep.."lfs_tmp_dir") == nil, "could create a directory inside a non-existent one")
101
102-- Trying to get attributes of a non-existent file
103assert (lfs.attributes ("this couldn't be an actual file") == nil, "could get attributes of a non-existent file")
104assert (type(lfs.attributes (upper)) == "table", "couldn't get attributes of upper directory")
105
106-- Stressing directory iterator
107count = 0
108for i = 1, 4000 do
109 for file in lfs.dir (tmp) do
110 count = count + 1
111 end
112end
113
114-- Stressing directory iterator, explicit version
115count = 0
116for i = 1, 4000 do
117 local iter, dir = lfs.dir(tmp)
118 local file = dir:next()
119 while file do
120 count = count + 1
121 file = dir:next()
122 end
123 assert(not pcall(dir.next, dir))
124end
125
126-- directory explicit close
127local iter, dir = lfs.dir(tmp)
128dir:close()
129assert(not pcall(dir.next, dir))
130print"Ok!"