-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathtest_support.py
More file actions
89 lines (69 loc) · 2.17 KB
/
test_support.py
File metadata and controls
89 lines (69 loc) · 2.17 KB
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
import os
import shutil
import tempfile
import contextlib
import libarchive.public
_APP_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..')
@contextlib.contextmanager
def temp_path():
path = tempfile.mkdtemp()
original_path = os.getcwd()
os.chdir(path)
try:
yield path
finally:
os.chdir(original_path)
try:
shutil.rmtree(path)
except:
pass
@contextlib.contextmanager
def chdir(path):
original_path = os.getcwd()
os.chdir(path)
try:
yield path
finally:
os.chdir(original_path)
@contextlib.contextmanager
def test_files():
"""
Returns a tuple have all input test files and the output archive path.
:return: Tuple (input_file_paths, output_archive_path)
"""
with chdir(_APP_PATH):
temp_path = tempfile.mkdtemp()
output_filename = 'archive.7z'
output_filepath = os.path.join(temp_path, output_filename)
# Also, write a source file with a unicode name that we can add to
# test internation support.
unicode_test_filepath = \
os.path.join(
temp_path,
"\u0905\u0906\u0907\u0536\u0537\u0538\u0539\u053a\u053b\uac12\uac13\uac14\uac15\uac16")
with open(unicode_test_filepath, 'w') as f:
f.write("test data \uf91f\uf920\uf921\uf922\uf923\uf924\uf925")
try:
files = [
'README.rst',
'libarchive/resources/README.rst',
'libarchive/resources/requirements.txt',
unicode_test_filepath,
]
libarchive.public.create_file(
output_filepath,
libarchive.constants.ARCHIVE_FORMAT_7ZIP,
files)
assert \
os.path.exists(output_filepath) is True, \
"Test archive was not created correctly."
yield (files, output_filepath)
finally:
try:
shutil.rmtree(temp_path)
except:
pass
@contextlib.contextmanager
def test_archive():
with test_files() as (_, archive_path):
yield archive_path