-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathtest_tools_filetools.py
445 lines (322 loc) · 13.5 KB
/
test_tools_filetools.py
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
import os
import platform
import pytest
import rsgislib.tools.filetools
DATA_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "data")
FILETOOLS_DATA_DIR = os.path.join(DATA_DIR, "tools/filetools")
unzip_cmd_avail = rsgislib.tools.filetools.is_cmd_tool_avail("unzip")
tar_cmd_avail = rsgislib.tools.filetools.is_cmd_tool_avail("tar")
on_windows = platform.system() == "Windows"
@pytest.mark.parametrize(
"input, expected",
[("hello.txt", "hello"), ("world/hello.txt", "hello"), ("hello.tar.gz", "hello")],
)
def test_get_file_basename(input, expected):
assert rsgislib.tools.filetools.get_file_basename(input) == expected
def test_get_file_basename_rm_n_exts_1():
assert (
rsgislib.tools.filetools.get_file_basename("hello.tar.gz", rm_n_exts=1)
== "hello.tar"
)
def test_get_file_basename_check_valid():
assert (
rsgislib.tools.filetools.get_file_basename("hello!.txt", check_valid=True)
== "hello"
)
def test_get_file_basename_check_valid_1():
assert (
rsgislib.tools.filetools.get_file_basename(
"hello_world_rsgislib.txt", n_comps=1
)
== "hello"
)
def test_get_file_basename_check_valid_2():
assert (
rsgislib.tools.filetools.get_file_basename(
"hello_world_rsgislib.txt", n_comps=2
)
== "hello_world"
)
def test_is_path_valid_true():
input_img = os.path.join(DATA_DIR, "sen2_20210527_aber.kea")
assert rsgislib.tools.filetools.is_path_valid(input_img)
def test_is_path_valid_false(tmp_path):
input_file = os.path.join(tmp_path, "sen2_20210527_aber\x00.kea")
assert not rsgislib.tools.filetools.is_path_valid(input_file)
def test_is_path_sibling_creatable_true(tmp_path):
input_file = os.path.join(tmp_path, "sen2_20210527_aber.kea")
assert rsgislib.tools.filetools.is_path_sibling_creatable(input_file)
def test_is_path_sibling_creatable_false(tmp_path):
input_file = os.path.join(tmp_path, "hello", "world", "sen2_20210527_aber.kea")
assert not rsgislib.tools.filetools.is_path_sibling_creatable(input_file)
def test_does_path_exists_or_creatable_true(tmp_path):
input_file = os.path.join(tmp_path, "sen2_20210527_aber.kea")
assert rsgislib.tools.filetools.does_path_exists_or_creatable(input_file)
def test_does_path_exists_or_creatable_false_file_name(tmp_path):
input_file = os.path.join(tmp_path, "sen2_20210527_aber\x00.kea")
assert not rsgislib.tools.filetools.does_path_exists_or_creatable(input_file)
def test_does_path_exists_or_creatable_false_path(tmp_path):
input_file = os.path.join(tmp_path, "hello", "world", "sen2_20210527_aber.kea")
assert not rsgislib.tools.filetools.does_path_exists_or_creatable(input_file)
def test_does_path_exists_or_creatable_false_path_and_name(tmp_path):
input_file = os.path.join(tmp_path, "hello", "world", "sen2_20210527_aber\x00.kea")
assert not rsgislib.tools.filetools.does_path_exists_or_creatable(input_file)
def test_get_file_size_bytes():
input_img = os.path.join(DATA_DIR, "sen2_20210527_aber.kea")
assert rsgislib.tools.filetools.get_file_size(input_img) == 7749324
def test_get_file_size_kb():
input_img = os.path.join(DATA_DIR, "sen2_20210527_aber.kea")
assert (
abs(rsgislib.tools.filetools.get_file_size(input_img, unit="kb") - 7567.699)
< 0.1
)
def test_get_file_size_mb():
input_img = os.path.join(DATA_DIR, "sen2_20210527_aber.kea")
assert (
abs(rsgislib.tools.filetools.get_file_size(input_img, unit="mb") - 7.39) < 0.01
)
def test_get_file_size_gb():
input_img = os.path.join(DATA_DIR, "sen2_20210527_aber.kea")
assert (
abs(rsgislib.tools.filetools.get_file_size(input_img, unit="gb") - 0.00722)
< 0.0001
)
def test_get_file_size_tb():
input_img = os.path.join(DATA_DIR, "sen2_20210527_aber.kea")
assert (
abs(rsgislib.tools.filetools.get_file_size(input_img, unit="tb") - 7.04797e-06)
< 0.0000001
)
def test_convert_file_size_units_bytes_kb():
out_size = rsgislib.tools.filetools.convert_file_size_units(
1000000, in_unit="bytes", out_unit="kb"
)
assert abs(out_size - 976.56) < 0.1
def test_convert_file_size_units_bytes_mb():
import rsgislib.tools.filetools
out_size = rsgislib.tools.filetools.convert_file_size_units(
1000000, in_unit="bytes", out_unit="mb"
)
assert abs(out_size - 0.954) < 0.1
def test_convert_file_size_units_bytes_gb():
out_size = rsgislib.tools.filetools.convert_file_size_units(
1000000, in_unit="bytes", out_unit="gb"
)
assert abs(out_size - 0.000931) < 0.0001
def test_convert_file_size_units_bytes_tb():
out_size = rsgislib.tools.filetools.convert_file_size_units(
1000000, in_unit="bytes", out_unit="tb"
)
assert abs(out_size - 9.095e-07) < 0.0000001
def test_convert_file_size_units_kb_bytes():
out_size = rsgislib.tools.filetools.convert_file_size_units(
1000, in_unit="kb", out_unit="bytes"
)
assert abs(out_size - 1024000.0) < 1
def test_convert_file_size_units_kb_mb():
out_size = rsgislib.tools.filetools.convert_file_size_units(
1000, in_unit="kb", out_unit="mb"
)
assert abs(out_size - 0.977) < 0.1
def test_convert_file_size_units_kb_gb():
out_size = rsgislib.tools.filetools.convert_file_size_units(
1000, in_unit="kb", out_unit="gb"
)
assert abs(out_size - 0.000954) < 0.0001
def test_convert_file_size_units_kb_tb():
out_size = rsgislib.tools.filetools.convert_file_size_units(
1000, in_unit="kb", out_unit="tb"
)
assert abs(out_size - 9.313e-07) < 0.0000001
@pytest.mark.skipif((not tar_cmd_avail), reason="tar command not available")
def test_untar_file_gen_arch_dir_true(tmp_path):
input_file = os.path.join(FILETOOLS_DATA_DIR, "test_file.tar")
rsgislib.tools.filetools.untar_file(input_file, tmp_path)
hello_file = os.path.join(tmp_path, "test_file", "hello.txt")
world_file = os.path.join(tmp_path, "test_file", "world.txt")
rsgislib_file = os.path.join(tmp_path, "test_file", "rsgislib.txt")
success = (
os.path.exists(hello_file)
and os.path.exists(world_file)
and os.path.exists(rsgislib_file)
)
assert success
@pytest.mark.skipif((not tar_cmd_avail), reason="tar command not available")
def test_untar_file_gen_arch_dir_false(tmp_path):
input_file = os.path.join(FILETOOLS_DATA_DIR, "test_file.tar")
rsgislib.tools.filetools.untar_file(input_file, tmp_path, gen_arch_dir=False)
hello_file = os.path.join(tmp_path, "hello.txt")
world_file = os.path.join(tmp_path, "world.txt")
rsgislib_file = os.path.join(tmp_path, "rsgislib.txt")
success = (
os.path.exists(hello_file)
and os.path.exists(world_file)
and os.path.exists(rsgislib_file)
)
assert success
@pytest.mark.skipif((not tar_cmd_avail), reason="tar command not available")
def test_untar_gz_file_gen_arch_dir_true(tmp_path):
input_file = os.path.join(FILETOOLS_DATA_DIR, "test_file.tar.gz")
rsgislib.tools.filetools.untar_gz_file(input_file, tmp_path)
hello_file = os.path.join(tmp_path, "test_file", "hello.txt")
world_file = os.path.join(tmp_path, "test_file", "world.txt")
rsgislib_file = os.path.join(tmp_path, "test_file", "rsgislib.txt")
success = (
os.path.exists(hello_file)
and os.path.exists(world_file)
and os.path.exists(rsgislib_file)
)
assert success
@pytest.mark.skipif((not tar_cmd_avail), reason="tar command not available")
def test_untar_gz_file_gen_arch_dir_false(tmp_path):
input_file = os.path.join(FILETOOLS_DATA_DIR, "test_file.tar.gz")
rsgislib.tools.filetools.untar_gz_file(input_file, tmp_path, gen_arch_dir=False)
hello_file = os.path.join(tmp_path, "hello.txt")
world_file = os.path.join(tmp_path, "world.txt")
rsgislib_file = os.path.join(tmp_path, "rsgislib.txt")
success = (
os.path.exists(hello_file)
and os.path.exists(world_file)
and os.path.exists(rsgislib_file)
)
assert success
@pytest.mark.skipif(
on_windows or (not tar_cmd_avail), reason="tar command not available or on Windows"
)
def test_untar_bz_file_gen_arch_dir_true(tmp_path):
input_file = os.path.join(FILETOOLS_DATA_DIR, "test_file.tar.bz")
rsgislib.tools.filetools.untar_bz_file(input_file, tmp_path)
hello_file = os.path.join(tmp_path, "test_file", "hello.txt")
world_file = os.path.join(tmp_path, "test_file", "world.txt")
rsgislib_file = os.path.join(tmp_path, "test_file", "rsgislib.txt")
success = (
os.path.exists(hello_file)
and os.path.exists(world_file)
and os.path.exists(rsgislib_file)
)
assert success
@pytest.mark.skipif(
on_windows or (not tar_cmd_avail), reason="tar command not available or on Windows"
)
def test_untar_bz_file_gen_arch_dir_false(tmp_path):
input_file = os.path.join(FILETOOLS_DATA_DIR, "test_file.tar.bz")
rsgislib.tools.filetools.untar_bz_file(input_file, tmp_path, gen_arch_dir=False)
hello_file = os.path.join(tmp_path, "hello.txt")
world_file = os.path.join(tmp_path, "world.txt")
rsgislib_file = os.path.join(tmp_path, "rsgislib.txt")
success = (
os.path.exists(hello_file)
and os.path.exists(world_file)
and os.path.exists(rsgislib_file)
)
assert success
@pytest.mark.skipif((not unzip_cmd_avail), reason="unzip command not available")
def test_unzip_file_gen_arch_dir_true(tmp_path):
input_file = os.path.join(FILETOOLS_DATA_DIR, "test_file.zip")
rsgislib.tools.filetools.unzip_file(input_file, tmp_path)
hello_file = os.path.join(tmp_path, "test_file", "hello.txt")
world_file = os.path.join(tmp_path, "test_file", "world.txt")
rsgislib_file = os.path.join(tmp_path, "test_file", "rsgislib.txt")
success = (
os.path.exists(hello_file)
and os.path.exists(world_file)
and os.path.exists(rsgislib_file)
)
assert success
@pytest.mark.skipif((not unzip_cmd_avail), reason="unzip command not available")
def test_unzip_file_gen_arch_dir_false(tmp_path):
input_file = os.path.join(FILETOOLS_DATA_DIR, "test_file.zip")
rsgislib.tools.filetools.unzip_file(input_file, tmp_path, gen_arch_dir=False)
hello_file = os.path.join(tmp_path, "hello.txt")
world_file = os.path.join(tmp_path, "world.txt")
rsgislib_file = os.path.join(tmp_path, "rsgislib.txt")
success = (
os.path.exists(hello_file)
and os.path.exists(world_file)
and os.path.exists(rsgislib_file)
)
assert success
def test_delete_file_silent(tmp_path):
import pathlib
file_to_del = os.path.join(tmp_path, "hello.txt")
# Create a file to be deleted.
pathlib.Path(file_to_del).touch()
if not os.path.exists(file_to_del):
success = False # Something went wrong and file wasn't created.
else:
rsgislib.tools.filetools.delete_file_silent(file_to_del)
# File should no longer exist
success = not os.path.exists(file_to_del)
assert success
def test_delete_file_with_basename(tmp_path):
import pathlib
# Create some file names where the base is the same but extension is different
file_1_to_del = os.path.join(tmp_path, "hello.shp")
file_2_to_del = os.path.join(tmp_path, "hello.shx")
file_3_to_del = os.path.join(tmp_path, "hello.txt")
# Create files to be deleted.
pathlib.Path(file_1_to_del).touch()
pathlib.Path(file_2_to_del).touch()
pathlib.Path(file_3_to_del).touch()
if (
(not os.path.exists(file_1_to_del))
or (not os.path.exists(file_2_to_del))
or (not os.path.exists(file_3_to_del))
):
success = False # Something went wrong and file wasn't created.
else:
rsgislib.tools.filetools.delete_file_with_basename(file_1_to_del)
# Files should no longer exist
success = (
(not os.path.exists(file_1_to_del))
and (not os.path.exists(file_2_to_del))
and (not os.path.exists(file_3_to_del))
)
assert success
def test_split_path_all_abs():
import rsgislib.tools.filetools
parts = rsgislib.tools.filetools.split_path_all("/a/b/c/d")
assert (
(parts[0] == "/")
and (parts[1] == "a")
and (parts[2] == "b")
and (parts[3] == "c")
and (parts[4] == "d")
)
def test_split_path_all_rel():
import rsgislib.tools.filetools
parts = rsgislib.tools.filetools.split_path_all("a/b/c/d")
assert (
(parts[0] == "a")
and (parts[1] == "b")
and (parts[2] == "c")
and (parts[3] == "d")
)
def test_split_path_all_file_abs():
import rsgislib.tools.filetools
parts = rsgislib.tools.filetools.split_path_all("/a/b/c/hello.txt")
assert (
(parts[0] == "/")
and (parts[1] == "a")
and (parts[2] == "b")
and (parts[3] == "c")
and (parts[4] == "hello.txt")
)
def test_split_path_all_file_rel():
import rsgislib.tools.filetools
parts = rsgislib.tools.filetools.split_path_all("a/b/c/hello.txt")
assert (
(parts[0] == "a")
and (parts[1] == "b")
and (parts[2] == "c")
and (parts[3] == "hello.txt")
)
def test_get_dir_name_file():
import rsgislib.tools.filetools
input_img = os.path.join(DATA_DIR, "sen2_20210527_aber.kea")
dir_name = rsgislib.tools.filetools.get_dir_name(input_img)
assert dir_name == "data"
def test_get_dir_name_dir():
import rsgislib.tools.filetools
dir_name = rsgislib.tools.filetools.get_dir_name(DATA_DIR)
assert dir_name == "data"