/
gambiarra_file.lua
210 lines (174 loc) · 5.02 KB
/
gambiarra_file.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
local test = require('gambiarra')
-- set the output handler
local function TERMINAL_HANDLER(e, test, msg, errormsg)
if errormsg then
errormsg = ": "..errormsg
else
errormsg = ""
end
if e == 'pass' then
print(" "..e.." "..test..': '..msg)
elseif e == 'fail' then
print(" ==> "..e.." "..test..': '..msg..errormsg)
elseif e == 'except' then
print(" ==> "..e.." "..test..': '..msg..errormsg)
else
print(e.." "..test)
end
end
-- set the output handler
-- test(TERMINAL_HANDLER)
local function cleanup()
file.remove("testfile")
file.remove("testfile2")
local testfiles = {"testfile1&", "testFILE2"}
for _, n in ipairs(testfiles) do
file.remove(n,n)
end
end
local function buildfn(fn, ...)
params = {...}
local fnp = function() fn(unpack(params)) end
return fnp
end
test('exist', function()
cleanup()
ok(not file.exists("non existing file"), "non existing file")
file.putcontents("testfile", "testcontents")
ok(file.exists("testfile"), "existing file")
end)
test('fscfg', function()
cleanup()
local start, size = file.fscfg()
ok(start, "start")
ok(size, "size")
end)
test('fsinfo', function()
cleanup()
local remaining, used, total = file.fsinfo()
ok(remaining, "remaining")
ok(used, "used")
ok(total, "total")
ok(eq(remaining+used, total), "size maths")
end)
test('getcontents', function()
cleanup()
local testcontent = "some content \0 and more"
file.putcontents("testfile", testcontent)
local content = file.getcontents("testfile")
ok(testcontent, content)
end)
test('getcontents non existent file', function()
cleanup()
nok(file.getcontents("non existing file"), "non existent file")
end)
test('getcontents more than 1K', function()
cleanup()
local f = file.open("testfile", "w")
local i
for i = 1,100 do
f:write("some text to test")
end
f:close()
content = file.getcontents("testfile")
eq(#content, 1700, "partial read")
end)
test('list', function()
cleanup()
local files
local function count(files)
local filecount = 0
for k,v in pairs(files) do filecount = filecount+1 end
return filecount
end
local function testfile(name)
ok(eq(files[name],#name), "length matches name length")
end
local testfiles = {"testfile1&", "testFILE2"}
for _, n in ipairs(testfiles) do
file.putcontents(n,n)
end
files = file.list("testfile%.*")
ok(eq(count(files), 1), "found file")
testfile("testfile1&")
files = file.list("^%l*%u+%d%.-")
ok(eq(count(files), 1), "found file regexp")
testfile("testFILE2")
files = file.list()
ok(count(files) >= 2, "several files found")
end)
test('open non existing', function()
cleanup()
local function testopen(test, filename, mode)
test(file.open(filename, mode), mode)
file.close()
file.remove(filename)
end
testopen(nok, "testfile", "r")
testopen(ok, "testfile", "w")
testopen(ok, "testfile", "a")
testopen(nok, "testfile", "r+")
testopen(ok, "testfile", "w+")
testopen(ok, "testfile", "a+")
fail(buildfn(file.open, "testfile"), "errormsg", "x") -- shouldn't this fail?
end)
test('open existing', function()
cleanup()
local function testopen(mode, position)
file.putcontents("testfile", "testcontent")
ok(file.open("testfile", mode), mode)
file.write("")
ok(eq(file.seek(), position), "seek check")
file.close()
end
testopen("r", 0)
testopen("w", 0)
testopen("a", 11)
testopen("r+", 0)
testopen("w+", 0)
testopen("a+", 11)
fail(buildfn(file.open, "testfile"), "errormsg", "x") -- shouldn't this fail?
end)
test('remove', function()
cleanup()
file.putcontents("testfile", "testfile")
ok(file.remove("testfile") == nil, "existing file")
ok(file.remove("testfile") == nil, "non existing file")
end)
test('rename', function()
cleanup()
file.putcontents("testfile", "testfile")
ok(file.rename("testfile", "testfile2"), "rename existing")
nok(file.exists("testfile"), "old file removed")
ok(file.exists("testfile2"), "new file exists")
nok(file.rename("testfile", "testfile3"), "rename non existing")
file.putcontents("testfile", "testfile")
nok(file.rename("testfile", "testfile2"), "rename to existing")
ok(file.exists("testfile"), "from file exists")
ok(file.exists("testfile2"), "to file exists")
end)
test('stat existing file', function()
cleanup()
file.putcontents("testfile", "testfile")
local stat = file.stat("testfile")
nok(stat == nil, "stat existing")
ok(eq(stat.size, 8), "size")
ok(eq(stat.name, "testfile"), "name")
nok(stat.time == nil, "no time")
ok(eq(stat.time.year, 1970), "year")
ok(eq(stat.time.mon, 01), "mon")
ok(eq(stat.time.day, 01), "day")
ok(eq(stat.time.hour, 0), "hour")
ok(eq(stat.time.min, 0), "min")
ok(eq(stat.time.sec, 0), "sec")
nok(stat.is_dir, "is_dir")
nok(stat.is_rdonly, "is_rdonly")
nok(stat.is_hidden, "is_hidden")
nok(stat.is_sys, "is_sys")
nok(stat.is_arch, "is_arch")
end)
test('stat non existing file', function()
cleanup()
local stat = file.stat("not existing file")
ok(stat == nil, "stat empty")
end)