|
1
|
|
|
import os |
|
|
|
|
|
|
2
|
|
|
import unittest |
|
3
|
|
|
import zipfile |
|
4
|
|
|
from tempfile import NamedTemporaryFile |
|
5
|
|
|
|
|
6
|
|
|
from foil.paths import (match_files, match_file_listing, |
|
7
|
|
|
get_file_listing_sha, |
|
8
|
|
|
match_zipfile_members, |
|
9
|
|
|
find_zipfile_member, |
|
10
|
|
|
strip_full_path, merge_paths, |
|
11
|
|
|
strip_file_listing, unique_listing_directories) |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
def mock_file_listing(): |
|
15
|
|
|
return ['ABC_124.txt', 'RANDOM.txt', 'ABC_123.txt'] |
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
def mock_pattern(): |
|
19
|
|
|
return r'ABC_[0-9]+.txt' |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
def mock_pattern_match(): |
|
23
|
|
|
return ['ABC_124.txt', 'ABC_123.txt'] |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
class TestMatchFiles(unittest.TestCase): |
|
27
|
|
|
|
|
28
|
|
|
def setUp(self): |
|
29
|
|
|
self.pattern = mock_pattern() |
|
30
|
|
|
|
|
31
|
|
|
def test_match_files(self): |
|
32
|
|
|
files = mock_file_listing() |
|
33
|
|
|
|
|
34
|
|
|
expected = set(mock_pattern_match()) |
|
35
|
|
|
result = set(match_files(files, self.pattern)) |
|
36
|
|
|
|
|
37
|
|
|
self.assertCountEqual(expected, result) |
|
38
|
|
|
|
|
39
|
|
|
def test_match_listings(self): |
|
40
|
|
|
paths = ['/home/ABC_124.txt', 'home/away/RANDOM.txt', 'ABC_123.txt'] |
|
41
|
|
|
|
|
42
|
|
|
expected = set(['/home/ABC_124.txt', 'ABC_123.txt']) |
|
43
|
|
|
result = set(match_file_listing(paths, self.pattern)) |
|
44
|
|
|
|
|
45
|
|
|
self.assertEqual(expected, result) |
|
46
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
class TestZipPaths(unittest.TestCase): |
|
49
|
|
|
|
|
50
|
|
|
@classmethod |
|
51
|
|
|
def setUpClass(cls): |
|
52
|
|
|
filenames = mock_file_listing() |
|
53
|
|
|
|
|
54
|
|
|
with NamedTemporaryFile(prefix='zip_path_', suffix='.zip', delete=False) as tmp: |
|
55
|
|
|
with zipfile.ZipFile(tmp.name, mode='w') as myzip: |
|
56
|
|
|
for file in filenames: |
|
57
|
|
|
myzip.writestr(file, b'') |
|
58
|
|
|
cls.zip_path = tmp.name |
|
59
|
|
|
|
|
60
|
|
|
def setUp(self): |
|
61
|
|
|
self.pattern = mock_pattern() |
|
62
|
|
|
|
|
63
|
|
|
def test_match_zipfile_members(self): |
|
64
|
|
|
|
|
65
|
|
|
expected = set(mock_pattern_match()) |
|
66
|
|
|
result = set(match_zipfile_members(self.zip_path, self.pattern)) |
|
67
|
|
|
|
|
68
|
|
|
self.assertEqual(expected, result) |
|
69
|
|
|
|
|
70
|
|
|
def test_find_zipfile_member(self): |
|
71
|
|
|
|
|
72
|
|
|
expected = mock_pattern_match()[0] |
|
73
|
|
|
result = find_zipfile_member(self.zip_path, self.pattern) |
|
74
|
|
|
|
|
75
|
|
|
self.assertEqual(expected, result) |
|
76
|
|
|
|
|
77
|
|
|
@classmethod |
|
78
|
|
|
def tearDownClass(cls): |
|
79
|
|
|
if os.path.exists(cls.zip_path): |
|
80
|
|
|
os.unlink(cls.zip_path) |
|
81
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
class TestSHA(unittest.TestCase): |
|
84
|
|
|
|
|
85
|
|
|
def test_get_file_listing_sha(self): |
|
86
|
|
|
from hashlib import sha256 |
|
87
|
|
|
file_listings = ['/xyz/file2.zip', '/abc/file1.zip'] |
|
88
|
|
|
|
|
89
|
|
|
expected = sha256(b'/abc/file1.zip' + b'/xyz/file2.zip').hexdigest() |
|
90
|
|
|
result = get_file_listing_sha(file_listings) |
|
91
|
|
|
|
|
92
|
|
|
self.assertEqual(expected, result) |
|
93
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
class TestPathFormatters(unittest.TestCase): |
|
96
|
|
|
|
|
97
|
|
|
def setUp(self): |
|
98
|
|
|
self.directory = 'blah' + os.sep |
|
99
|
|
|
self.base_path = os.sep + self.directory |
|
100
|
|
|
|
|
101
|
|
|
def test_strip_full_path(self): |
|
102
|
|
|
|
|
103
|
|
|
expected = self.directory |
|
104
|
|
|
result = strip_full_path(self.base_path) |
|
105
|
|
|
|
|
106
|
|
|
self.assertEqual(expected, result) |
|
107
|
|
|
|
|
108
|
|
|
def test_merge_paths(self): |
|
109
|
|
|
directory_name = 'blah_sub_directory' |
|
110
|
|
|
sub_directory = os.sep + directory_name |
|
111
|
|
|
|
|
112
|
|
|
path2 = os.path.join(self.base_path, sub_directory) |
|
113
|
|
|
|
|
114
|
|
|
expected = os.path.join(self.base_path, directory_name) |
|
115
|
|
|
result = merge_paths(self.base_path, path2) |
|
116
|
|
|
|
|
117
|
|
|
self.assertEqual(expected, result) |
|
118
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
class TestFileDirectoryListing(unittest.TestCase): |
|
121
|
|
|
|
|
122
|
|
|
def setUp(self): |
|
123
|
|
|
self.paths = ['/foobar/bar.zip', '/foobar/foo.txt', '/foobar/bar/foo.txt'] |
|
124
|
|
|
|
|
125
|
|
|
def test_strip_file_listing(self): |
|
126
|
|
|
expected = ['/foobar', '/foobar', '/foobar/bar'] |
|
127
|
|
|
result = list(strip_file_listing(self.paths)) |
|
128
|
|
|
|
|
129
|
|
|
self.assertEqual(expected, result) |
|
130
|
|
|
|
|
131
|
|
|
def test_unique_listing_directories(self): |
|
132
|
|
|
expected = set(['/foobar', '/foobar/bar']) |
|
133
|
|
|
result = unique_listing_directories(self.paths) |
|
134
|
|
|
|
|
135
|
|
|
self.assertEqual(expected, result) |
|
136
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.