Debian Codegraph @

texk/web2c/luatexdir/luafilesystem/tests/test.lua

raw symbols SCIP
1 #!/usr/bin/env lua5.1
2
3 local tmp = "/tmp"
4 local sep = string.match (package.config, "[^\n]+")
5 local upper = ".."
6
7 local lfs = require"lfs"
8 print (lfs._VERSION)
9
10 io.write(".")
11 io.flush()
12
13 function attrdir (path)
14 for file in lfs.dir(path) do
15 if file ~= "." and file ~= ".." then
16 local f = path..sep..file
17 print ("\t=> "..f.." <=")
18 local attr = lfs.attributes (f)
19 assert (type(attr) == "table")
20 if attr.mode == "directory" then
21 attrdir (f)
22 else
23 for name, value in pairs(attr) do
24 print (name, value)
25 end
26 end
27 end
28 end
29 end
30
31 -- Checking changing directories
32 local current = assert (lfs.currentdir())
33 local reldir = string.gsub (current, "^.*%"..sep.."([^"..sep.."])$", "%1")
34 assert (lfs.chdir (upper), "could not change to upper directory")
35 assert (lfs.chdir (reldir), "could not change back to current directory")
36 assert (lfs.currentdir() == current, "error trying to change directories")
37 assert (lfs.chdir ("this couldn't be an actual directory") == nil, "could change to a non-existent directory")
38
39 io.write(".")
40 io.flush()
41
42 -- Changing creating and removing directories
43 local tmpdir = current..sep.."lfs_tmp_dir"
44 local tmpfile = tmpdir..sep.."tmp_file"
45 -- Test for existence of a previous lfs_tmp_dir
46 -- that may have resulted from an interrupted test execution and remove it
47 if lfs.chdir (tmpdir) then
48 assert (lfs.chdir (upper), "could not change to upper directory")
49 assert (os.remove (tmpfile), "could not remove file from previous test")
50 assert (lfs.rmdir (tmpdir), "could not remove directory from previous test")
51 end
52
53 io.write(".")
54 io.flush()
55
56 -- tries to create a directory
57 assert (lfs.mkdir (tmpdir), "could not make a new directory")
58 local attrib, errmsg = lfs.attributes (tmpdir)
59 if not attrib then
60 error ("could not get attributes of file `"..tmpdir.."':\n"..errmsg)
61 end
62 local f = io.open(tmpfile, "w")
63 f:close()
64
65 io.write(".")
66 io.flush()
67
68 -- Change access time
69 local testdate = os.time({ year = 2007, day = 10, month = 2, hour=0})
70 assert (lfs.touch (tmpfile, testdate))
71 local new_att = assert (lfs.attributes (tmpfile))
72 assert (new_att.access == testdate, "could not set access time")
73 assert (new_att.modification == testdate, "could not set modification time")
74
75 io.write(".")
76 io.flush()
77
78 -- Change access and modification time
79 local testdate1 = os.time({ year = 2007, day = 10, month = 2, hour=0})
80 local testdate2 = os.time({ year = 2007, day = 11, month = 2, hour=0})
81
82 assert (lfs.touch (tmpfile, testdate2, testdate1))
83 local new_att = assert (lfs.attributes (tmpfile))
84 assert (new_att.access == testdate2, "could not set access time")
85 assert (new_att.modification == testdate1, "could not set modification time")
86
87 io.write(".")
88 io.flush()
89
90 -- Checking link (does not work on Windows)
91 if lfs.link (tmpfile, "_a_link_for_test_", true) then
92 assert (lfs.attributes"_a_link_for_test_".mode == "file")
93 assert (lfs.symlinkattributes"_a_link_for_test_".mode == "link")
94 assert (lfs.symlinkattributes"_a_link_for_test_".target == tmpfile)
95 assert (lfs.symlinkattributes("_a_link_for_test_", "target") == tmpfile)
96 assert (lfs.link (tmpfile, "_a_hard_link_for_test_"))
97 assert (lfs.attributes (tmpfile, "nlink") == 2)
98 assert (os.remove"_a_link_for_test_")
99 assert (os.remove"_a_hard_link_for_test_")
100 end
101
102 io.write(".")
103 io.flush()
104
105 -- Checking text/binary modes (only has an effect in Windows)
106 local f = io.open(tmpfile, "w")
107 local result, mode = lfs.setmode(f, "binary")
108 assert(result) -- on non-Windows platforms, mode is always returned as "binary"
109 result, mode = lfs.setmode(f, "text")
110 assert(result and mode == "binary")
111 f:close()
112 local ok, err = pcall(lfs.setmode, f, "binary")
113 assert(not ok, "could setmode on closed file")
114 assert(err:find("closed file"), "bad error message for setmode on closed file")
115
116 io.write(".")
117 io.flush()
118
119 -- Restore access time to current value
120 assert (lfs.touch (tmpfile, attrib.access, attrib.modification))
121 new_att = assert (lfs.attributes (tmpfile))
122 assert (new_att.access == attrib.access)
123 assert (new_att.modification == attrib.modification)
124
125 io.write(".")
126 io.flush()
127
128 -- Check consistency of lfs.attributes values
129 local attr = lfs.attributes (tmpfile)
130 for key, value in pairs(attr) do
131 assert (value == lfs.attributes (tmpfile, key),
132 "lfs.attributes values not consistent")
133 end
134
135 -- Check that lfs.attributes accepts a table as second argument
136 local attr2 = {}
137 lfs.attributes(tmpfile, attr2)
138 for key, value in pairs(attr2) do
139 assert (value == lfs.attributes (tmpfile, key),
140 "lfs.attributes values with table argument not consistent")
141 end
142
143 -- Check that extra arguments are ignored
144 lfs.attributes(tmpfile, attr2, nil)
145
146 -- Remove new file and directory
147 assert (os.remove (tmpfile), "could not remove new file")
148 assert (lfs.rmdir (tmpdir), "could not remove new directory")
149 assert (lfs.mkdir (tmpdir..sep.."lfs_tmp_dir") == nil, "could create a directory inside a non-existent one")
150
151 io.write(".")
152 io.flush()
153
154 -- Trying to get attributes of a non-existent file
155 local attr_ok, err, errno = lfs.attributes("this couldn't be an actual file")
156 assert(attr_ok == nil, "could get attributes of a non-existent file")
157 assert(type(err) == "string", "failed lfs.attributes did not return an error message")
158 assert(type(errno) == "number", "failed lfs.attributes did not return error code")
159 assert (type(lfs.attributes (upper)) == "table", "couldn't get attributes of upper directory")
160
161 io.write(".")
162 io.flush()
163
164 -- Stressing directory iterator
165 count = 0
166 for i = 1, 4000 do
167 for file in lfs.dir (tmp) do
168 count = count + 1
169 end
170 end
171
172 io.write(".")
173 io.flush()
174
175 -- Stressing directory iterator, explicit version
176 count = 0
177 for i = 1, 4000 do
178 local iter, dir = lfs.dir(tmp)
179 local file = dir:next()
180 while file do
181 count = count + 1
182 file = dir:next()
183 end
184 assert(not pcall(dir.next, dir))
185 end
186
187 io.write(".")
188 io.flush()
189
190 -- directory explicit close
191 local iter, dir = lfs.dir(tmp)
192 dir:close()
193 assert(not pcall(dir.next, dir))
194 print"Ok!"