-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnon_member_functions.lua
371 lines (286 loc) · 10.2 KB
/
non_member_functions.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
local test = require( "test" )
local fs = require( "filesystem" )
local function _current_test_path( str )
return tostring( fs.current_path():append( str ):make_preferred() )
end
local function _absolute()
local p1 = fs.path( "." )
local p2 = fs.absolute( p1 )
test.is_true( fs.equivalent( p1, p2 ) )
end
local function _canonical()
local p1 = fs.path( "." )
local p2 = fs.canonical( p1 )
test.is_true( fs.equivalent( p1, p2 ) )
end
local function _weakly_canonical()
local p1 = fs.path( "." )
local p2 = fs.weakly_canonical( p1 )
test.is_true( fs.equivalent( p1, p2 ) )
local p3 = fs.path( "./foo" )
local p4 = fs.weakly_canonical( p1 ) -- Should not emit an error
end
local function _relative()
local p1 = fs.current_path()
local p2 = fs.relative( p1 )
local p3 = fs.relative( p1, p2 )
test.is_same( p2, fs.path( "." ) )
test.is_same( p2, p2 )
end
local function _proximate()
local p1 = fs.current_path()
local p2 = fs.proximate( p1 )
local p3 = fs.proximate( p1, p2 )
test.is_same( p2, fs.path( "." ) )
test.is_same( p2, p2 )
end
local function _copy()
local src = "./test/tests/foo"
local dst = fs.path( "./test/tests/bar" )
fs.copy( src, dst )
test.is_true( fs.exists( fs.path( dst ):append( "file.txt" ) ) )
fs.remove_all( dst )
fs.copy( src, dst, fs.copy_options.recursive | fs.copy_options.directories_only | fs.copy_options.overwrite_existing )
test.is_true( fs.exists( fs.path( dst ):append( "bar/buz" ) ) )
fs.remove_all( dst )
end
local function _copy_file()
local src = "./test/tests/foo/file.txt"
local dst = fs.path( "./test/tests/file.txt" )
test.is_true( fs.copy_file( src, dst ) )
test.is_true( fs.exists( dst ) )
test.is_false( fs.copy_file( src, dst, fs.copy_options.skip_existing ) )
fs.remove( dst )
end
local function _create_copy_read_symlink_status()
local src = fs.path( "./test/tests/foo/file.txt" )
local dst = "./test/tests/file.txt"
local cpy = "./test/tests/copy.txt"
fs.create_symlink( src, dst )
test.is_true( fs.is_symlink( dst ) )
fs.copy_symlink( dst, cpy );
test.is_true( fs.is_symlink( cpy ) )
local target = fs.read_symlink( dst )
test.is_same( src, target )
local is_ok, err = pcall( fs.read_symlink, src )
test.is_false( is_ok )
local perm, t = fs.symlink_status( dst )
test.is_same( t, fs.file_type.symlink )
fs.remove( dst )
fs.remove( cpy )
end
local function _create_directory()
local dir = "./test/tests/bar/"
test.is_true( fs.create_directory( dir ) )
test.is_true( fs.exists( dir ) )
fs.remove( dir )
test.is_false( fs.exists( dir ) )
end
local function _create_directories_remove_all()
local dir = _current_test_path( "test/tests/bar/baz" )
test.is_true( fs.create_directories( dir ) )
test.is_true( fs.exists( dir ) )
fs.remove_all( _current_test_path( "./test/tests/bar" ) )
test.is_false( fs.exists( dir ) )
end
local function _create_hard_link_and_count()
local src = "./test/tests/foo/file.txt"
local dst = "./test/tests/file.txt"
test.is_same( fs.hard_link_count( src ), 1 )
fs.create_hard_link( src, dst )
test.is_true( fs.exists( dst ) )
test.is_same( fs.hard_link_count( src ), 2 )
fs.remove( dst )
test.is_false( fs.exists( dst ) )
end
local function _create_directory_symlink()
local src = "./test/tests/foo"
local dst = "./test/tests/bar"
fs.create_directory_symlink( src, dst )
test.is_true( fs.is_symlink( dst ) )
fs.remove( dst )
end
local function _current_path()
local p1 = fs.current_path()
fs.current_path( p1 )
local p2 = fs.current_path()
test.is_same( p1, p2 )
end
local function _equivalent()
local p1 = fs.path( _current_test_path( "test/tests/foo/file.txt" ) )
local p2 = _current_test_path( "././test/../test/tests/foo/file.txt" )
test.is_true( fs.equivalent( p1, p2 ) )
test.is_false( fs.equivalent( p1, _current_test_path( "test/tests/foo/bar" ) ) )
end
local function _file_size_resize()
local p = "./test/tests/foo/file.txt"
local size = fs.file_size( p )
test.is_true( math.type( size ) == "integer" )
test.is_true( size >= 0 )
local new_size = size + 42
fs.resize_file( p, new_size )
test.is_same( fs.file_size( p ), new_size )
fs.resize_file( p, size )
end
local function _status_permissions()
local p = fs.path( "./test/tests/foo/file.txt" )
local perm1, t1 = fs.status( p )
test.is_same( t1, fs.file_type.regular )
local new_perm = perm1 | fs.perms.owner_exec
fs.permissions( p, new_perm )
local perm2, t2 = fs.status( p )
test.is_same( new_perm, perm2 )
fs.permissions( p, fs.perms.owner_exec, fs.perm_options.remove )
local perm3, t3 = fs.status( p )
test.is_same( perm1, perm3 )
end
local function _rename()
local p1 = fs.path( "./test/tests/foo/file.txt" )
local p2 = "./test/tests/foo/elif.xtx"
test.is_true( fs.exists( p1 ) )
fs.rename( p1, p2 )
test.is_true( fs.exists( p2 ) )
fs.rename( p2, p1 )
test.is_true( fs.exists( p1 ) )
end
local function _space()
local p = fs.path( "./test/tests/foo" )
local free, available, capacity = fs.space( p )
test.is_not_nil( free )
test.is_not_nil( available )
test.is_not_nil( capacity )
end
local function _temp_directory_path()
local tmp = fs.temp_directory_path()
test.is_false( tmp:empty() )
end
local function _last_write_time()
local str = _current_test_path( "test/tests/foo/file.txt" )
local p = fs.path( str )
local ft1 = fs.last_write_time( str )
local ft2 = fs.last_write_time( p )
test.is_not_nil( ft1 )
test.is_same( ft1, ft2 )
local ft3 = fs.file_time_now()
fs.last_write_time( p, ft3 )
test.is_not_same( fs.last_write_time( p ), ft2 )
test.is_same( fs.last_write_time( p ), ft3 )
end
local function _file_time()
local ft1 = fs.file_time_now()
local ft2 = ft1 + 1
local ft3 = ft1 - 1
test.is_true( ft2 > ft1 )
test.is_true( ft2 >= ft1 )
test.is_true( ft3 < ft1 )
test.is_true( ft3 <= ft1 )
test.is_not_same( ft2 ~= ft3 )
local ft4 = ft1 + 1.0
local ft5 = ft1 - 1.0
test.is_true( ft4 > ft1 )
test.is_true( ft4 >= ft1 )
test.is_true( ft5 < ft1 )
test.is_true( ft5 <= ft1 )
test.is_not_same( ft4, ft5 )
local dif = ft4 - ft5
local ft6 = dif + ft1
local ft7 = ft1 + dif
local ft8 = ft1 - dif
test.is_same( ft6, ft7 )
test.is_true( ft8 < ft1 )
end
local function _file_time_now()
local ft1 = fs.file_time_now()
local ft2 = fs.last_write_time( "./test/tests/foo/bar/file.txt" )
test.is_true( ft1 > ft2 )
test.is_true( ft1 >= ft2 )
test.is_false( ft1 < ft2 )
test.is_false( ft1 <= ft2 )
end
local function _file_time_duraion()
local ft1 = fs.file_time_now()
local ft2 = fs.last_write_time( "./test/tests/foo/bar/file.txt" )
local diff1 = ft1 - ft2
test.is_same( ft1, ft2 + diff1 )
test.is_same( ft1, diff1 + ft2 )
test.is_same( ft2, ft1 - diff1 )
local diff2 = ft2 - ft1
test.is_same( ft2, ft1 + diff2 )
test.is_same( ft2, diff2 + ft1 )
test.is_same( ft1, ft2 + diff1 )
local diff3 = diff1 + 1.0
local diff4 = diff1 + 1
local diff5 = 1.0 + diff1
local diff6 = 1 + diff1
test.is_true( diff3 > diff1 )
test.is_true( diff3 >= diff1 )
test.is_true( diff4 > diff1 )
test.is_true( diff4 >= diff1 )
test.is_true( diff5 > diff1 )
test.is_true( diff6 > diff1 )
test.is_same( diff3, diff4 )
test.is_same( diff3, diff5 )
test.is_same( diff3, diff6 )
local diff7 = diff1 - 1.0
local diff8 = diff1 - 1
test.is_true( diff7 < diff1 )
test.is_true( diff7 <= diff1 )
test.is_true( diff8 < diff1 )
test.is_true( diff8 <= diff1 )
test.is_not_same( diff1, diff8 )
test.is_same( diff7, diff8 )
local diff9 = diff1 - diff8
local diff10 = diff9 + diff1
local diff11 = diff1 + diff9
local seconds = diff9:seconds()
test.is_same( diff10, diff11 )
test.is_same( seconds, 1 )
end
local function _is_xyz()
local p1 = _current_test_path( "test/tests/foo/" )
test.is_false( fs.is_block_file( p1 ) )
test.is_false( fs.is_character_file( p1 ) )
test.is_true( fs.is_directory( p1 ) )
test.is_false( fs.is_fifo( p1 ) )
test.is_false( fs.is_other( p1 ) )
local p2 = _current_test_path( "./test/tests/foo/file.txt" )
test.is_true( fs.is_regular_file( p2 ) )
test.is_false( fs.is_socket( p2 ) )
test.is_false( fs.is_symlink( p2 ) )
end
local function _enum_binary_operators()
local all = fs.perms.all
local none = all & ~all
test.is_same( none, fs.perms.none )
test.is_same( none | all, fs.perms.all )
end
local tests =
{
absolute = _absolute,
canonical = _canonical,
weakly_canonical = _weakly_canonical,
relative = _relative,
proximate = _proximate,
copy = _copy,
copy_file = _copy_file,
create_copy_read_symlink_status = _create_copy_read_symlink_status,
create_directory = _create_directory,
create_directories_remove_all = _create_directories_remove_all,
create_hard_link_and_count = _create_hard_link_and_count,
create_directory_symlink = _create_directory_symlink,
current_path = _current_path,
equivalent = _equivalent,
file_size_resize = _file_size_resize,
permissions = _permissions,
status_permissions = _status_permissions,
rename = _rename,
space = _space,
temp_directory_path = _temp_directory_path,
last_write_time = _last_write_time,
file_time = _file_time,
file_time_now = _file_time_now,
file_time_duraion = _file_time_duraion,
is_xyzz = _is_xyz,
enum_binary_operators = _enum_binary_operators
}
return tests