forked from spacewander/luafilesystem
-
Notifications
You must be signed in to change notification settings - Fork 8
/
lfs_spec.lua
352 lines (304 loc) · 10.3 KB
/
lfs_spec.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
local ffi = require('ffi')
local vanilla_lfs = require('lfs')
local lfs = require('./lfs_ffi')
local eq = assert.are.same
local is_nil = assert.is_nil
local is_not_nil = assert.is_not_nil
local is_true = assert.is_true
local has_error = assert.has_error
local posix = ffi.os ~= 'Windows'
local attr_names = {
'access',
'change',
'dev',
'gid',
'ino',
'mode',
'modification',
'nlink',
'permissions',
'rdev',
'size',
'uid'
}
if posix then
local extra_attrs = {'blksize', 'blocks'}
for i = 1, #extra_attrs do
table.insert(attr_names, extra_attrs[i])
end
end
describe('lfs', function()
describe('#attributes', function()
it('without argument', function()
local info = lfs.attributes('.')
eq(vanilla_lfs.attributes('.'), info)
end)
it('with attribute name', function()
for i = 1, #attr_names do
local attr = attr_names[i]
local info = lfs.attributes('.', attr)
eq(vanilla_lfs.attributes('.', attr), info,
attr..' is not equal')
end
end)
it('with attributes table', function()
local tab = {"table", "for", "attributes"}
local info = lfs.attributes('.', tab)
eq(vanilla_lfs.attributes('.', tab), info)
end)
it('with nonexisted file', function()
local info, err = lfs.attributes('nonexisted')
is_nil(info)
eq('No such file or directory', err)
end)
it('with nonexisted attribute', function()
has_error(function() lfs.attributes('.', 'nonexisted') end,
"invalid attribute name 'nonexisted'")
if not posix then
has_error(function() lfs.attributes('.', 'blocks') end,
"invalid attribute name 'blocks'")
end
end)
end)
describe('#symlinkattributes', function()
local symlink = 'lfs_ffi.lua.link'
it('link failed', function()
if posix then
local res, err = lfs.link('xxx', symlink)
is_nil(res)
eq(err, 'No such file or directory')
end
end)
it('hard link', function()
local _, err = lfs.link('lfs_ffi.lua', symlink)
is_nil(err)
eq(vanilla_lfs.attributes(symlink, 'mode'), 'file')
eq(vanilla_lfs.symlinkattributes(symlink, 'mode'), 'file')
end)
it('soft link', function()
if posix then
local _, err = lfs.link('lfs_ffi.lua', symlink, true)
is_nil(err)
eq(vanilla_lfs.attributes(symlink, 'mode'), 'file')
eq(vanilla_lfs.symlinkattributes(symlink, 'mode'), 'link')
end
end)
it('without argument', function()
lfs.link('lfs_ffi.lua', symlink, true)
local info = lfs.symlinkattributes(symlink)
local expected_info = vanilla_lfs.symlinkattributes(symlink)
for k, v in pairs(expected_info) do
eq(v, info[k], k..'is not equal')
end
end)
it('with attribute name', function()
lfs.link('lfs_ffi.lua', symlink, true)
for i = 1, #attr_names do
local attr = attr_names[i]
local info = lfs.symlinkattributes(symlink, attr)
eq(vanilla_lfs.symlinkattributes(symlink, attr), info,
attr..' is not equal')
end
end)
it('add target field', function()
if posix then
lfs.link('lfs_ffi.lua', symlink, true)
eq('lfs_ffi.lua', lfs.symlinkattributes(symlink, 'target'))
eq('lfs_ffi.lua', lfs.symlinkattributes(symlink).target)
end
end)
after_each(function()
os.remove(symlink)
end)
end)
describe('#setmode', function()
local fh
before_each(function()
fh = io.open('lfs_ffi.lua')
end)
it('setmode', function()
local ok, mode = lfs.setmode(fh, 'binary')
is_true(ok)
if posix then
-- On posix platform, always return 'binary'
eq('binary', mode)
else
eq( 'text', mode)
local _
_, mode = lfs.setmode(fh, 'text')
eq('binary', mode)
end
end)
if not posix then
it('setmode incorrect mode', function()
has_error(function() lfs.setmode(fh, 'bin') end, 'setmode: invalid mode')
end)
it('setmode incorrect file', function()
has_error(function() lfs.setmode('file', 'binary') end, 'setmode: invalid file')
end)
end
end)
describe('#dir', function()
it('mkdir', function()
lfs.mkdir('test')
end)
it('return err if mkdir failed', function()
local res, err = lfs.mkdir('test')
is_nil(res)
eq('File exists', err)
end)
it('raise error if open dir failed', function()
if posix then
has_error(function() lfs.dir('nonexisted') end,
"cannot open nonexisted : No such file or directory")
else
-- Like vanilla lfs, we only check path's length in Windows
local ok, msg = pcall(function() lfs.dir(('12345'):rep(64)) end)
is_true(not ok)
is_not_nil(msg:find('path too long'))
end
end)
if posix or os.getenv('CI') ~= 'True' then
it('iterate dir', function()
local _, dir_obj = lfs.dir('test')
local names = {}
while true do
local name = dir_obj:next()
if not name then break end
names[#names + 1] = name
end
table.sort(names)
eq({'.', '..'}, names)
is_true(dir_obj.closed)
end)
it('iterate dir via iterator', function()
local iter, dir_obj = lfs.dir('test')
local names = {}
while true do
local name = iter(dir_obj)
if not name then break end
names[#names + 1] = name
end
table.sort(names)
eq({'.', '..'}, names)
is_true(dir_obj.closed)
end)
end
it('close', function()
local _, dir_obj = lfs.dir('.')
dir_obj:close()
has_error(function() dir_obj:next() end, "closed directory")
end)
it('chdir and currentdir', function()
lfs.chdir('test')
local cur_dir = lfs.currentdir()
lfs.chdir('..')
assert.is_not_nil(cur_dir:find('test$'))
end)
it('return err if chdir failed', function()
local res, err = lfs.chdir('nonexisted')
is_nil(res)
eq('No such file or directory', err)
end)
it('rmdir', function()
lfs.rmdir('test')
end)
it('return err if rmdir failed', function()
local res, err = lfs.rmdir('test')
is_nil(res)
eq('No such file or directory', err)
end)
end)
describe('#touch', function()
local touched = 'temp'
before_each(function()
local f = io.open(touched, 'w')
f:write('a')
f:close()
end)
after_each(function()
os.remove(touched)
end)
it('touch failed', function()
local _, err = lfs.touch('nonexisted', 1)
eq('No such file or directory', err)
end)
it('set atime', function()
local _, err = lfs.touch(touched, 1)
is_nil(err)
eq(vanilla_lfs.attributes(touched, 'access'), 1)
end)
it('set both atime and mtime', function()
local _, err = lfs.touch(touched, 1, 2)
is_nil(err)
eq(vanilla_lfs.attributes(touched, 'access'), 1)
eq(vanilla_lfs.attributes(touched, 'modification'), 2)
end)
end)
-- Just smoke testing
describe('#lock', function()
local fh
setup(function()
fh = io.open('temp.txt', 'w')
fh:write('1234567890')
fh:close()
end)
before_each(function()
fh = io.open('temp.txt', 'r+')
end)
it('lock', function()
local _, err = lfs.lock(fh, 'r', 2, 8)
is_nil(err)
end)
it('lock exclusively', function()
if posix then
local _, err = lfs.lock(fh, 'w')
is_nil(err)
end
end)
it('lock: invalid mode', function()
has_error(function() lfs.lock('temp.txt', 'u') end, 'lock: invalid mode')
end)
it('lock: invalid file', function()
has_error(function() lfs.lock('temp.txt', 'w') end, 'lock: invalid file')
end)
it('unlock', function()
local _, err = lfs.lock(fh, 'w', 4, 9)
is_nil(err)
if posix then
_, err = lfs.unlock(fh, 3, 11)
is_nil(err)
else
_, err = lfs.unlock(fh, 3, 11)
eq('Permission denied', err)
_, err = lfs.unlock(fh, 4, 9)
is_nil(err)
end
end)
it('unlock: invalid file', function()
has_error(function() lfs.unlock('temp.txt') end, 'unlock: invalid file')
end)
after_each(function()
fh:close()
end)
teardown(function()
os.remove('temp.txt')
end)
end)
describe('#lock_dir', function()
it('lock_dir', function()
if true then
local _, err = lfs.lock_dir('.')
is_nil(err)
_, err = lfs.lock_dir('.')
assert.is_not_nil(err)
end
-- The old lock should be free during gc
collectgarbage()
local lock = lfs.lock_dir('.')
lock:free()
local _, err = lfs.lock_dir('.')
is_nil(err)
end)
end)
end)