5 |
5 |
# Available under the terms of Creative Commons Zero v1.0 Universal.
|
6 |
6 |
|
7 |
7 |
import pytest
|
|
8 |
import json
|
8 |
9 |
|
9 |
10 |
from tempfile import TemporaryDirectory
|
10 |
11 |
from pathlib import Path
|
|
12 |
from hashlib import sha256, sha1
|
|
13 |
from zipfile import ZipFile
|
11 |
14 |
|
12 |
15 |
here = Path(__file__).resolve().parent
|
13 |
16 |
|
... | ... | |
20 |
23 |
"""Build the sample source package and verify the produced files."""
|
21 |
24 |
from hydrilla_builder.build import Build
|
22 |
25 |
|
23 |
|
build = Build(here / 'source-package-example', Path('index.json'))
|
24 |
|
build.write_package_files(Path(tmpdir))
|
|
26 |
# First, build the package
|
|
27 |
srcdir = here / 'source-package-example'
|
|
28 |
dstdir = Path(tmpdir)
|
25 |
29 |
|
26 |
|
# TODO: verify results
|
|
30 |
build = Build(srcdir, Path('index.json'))
|
|
31 |
build.write_package_files(dstdir)
|
|
32 |
|
|
33 |
# Verify directories under destination directory
|
|
34 |
assert {'file', 'resource', 'mapping', 'source'} == \
|
|
35 |
set([path.name for path in dstdir.iterdir()])
|
|
36 |
|
|
37 |
# Verify files under 'file/'
|
|
38 |
file_dir = dstdir / 'file'
|
|
39 |
js_filenames = ('bye.js', 'hello.js', 'message.js')
|
|
40 |
dist_filenames = (*js_filenames, str(Path('LICENSES') / 'CC0-1.0.txt'))
|
|
41 |
file_contents = {}
|
|
42 |
file_sha256_hashes = {}
|
|
43 |
file_sha1_hashes = {}
|
|
44 |
|
|
45 |
for fn in dist_filenames:
|
|
46 |
with open(srcdir / fn, 'rb') as file_handle:
|
|
47 |
file_contents = file_handle.read()
|
|
48 |
|
|
49 |
file_sha256_hashes[fn] = sha256(file_contents).digest().hex()
|
|
50 |
file_sha1_hashes[fn] = sha1(file_contents).digest().hex()
|
|
51 |
|
|
52 |
dist_file_path = file_dir / f'sha256-{file_sha256_hashes[fn]}'
|
|
53 |
assert dist_file_path.is_file()
|
|
54 |
|
|
55 |
with open(dist_file_path, 'rb') as file_handle:
|
|
56 |
assert file_handle.read() == file_contents
|
|
57 |
|
|
58 |
sha256_hashes = set(file_sha256_hashes.values())
|
|
59 |
spdx_report_sha256 = None
|
|
60 |
spdx_report_checked = False
|
|
61 |
|
|
62 |
for path in file_dir.iterdir():
|
|
63 |
assert path.name.startswith('sha256-')
|
|
64 |
if path.name[7:] in sha256_hashes:
|
|
65 |
continue
|
|
66 |
|
|
67 |
assert spdx_report_sha256 is None
|
|
68 |
|
|
69 |
with open(path, 'rt') as file_handle:
|
|
70 |
spdx_contents = file_handle.read()
|
|
71 |
|
|
72 |
spdx_report_sha256 = sha256(spdx_contents.encode()).digest().hex()
|
|
73 |
assert spdx_report_sha256 == path.name[7:]
|
|
74 |
|
|
75 |
for fn in js_filenames:
|
|
76 |
assert file_sha1_hashes[fn] in spdx_contents
|
|
77 |
|
|
78 |
# Verify files under 'resource/'
|
|
79 |
resource_dir = dstdir / 'resource'
|
|
80 |
|
|
81 |
expected_resource_jsons = [{
|
|
82 |
'api_schema_version': [1, 0, 1],
|
|
83 |
'source_name': 'hello',
|
|
84 |
'source_copyright': [{
|
|
85 |
'file': 'report.spdx',
|
|
86 |
'sha256': spdx_report_sha256
|
|
87 |
}, {
|
|
88 |
'file': 'LICENSES/CC0-1.0.txt',
|
|
89 |
'sha256': file_sha256_hashes['LICENSES/CC0-1.0.txt']
|
|
90 |
}],
|
|
91 |
'type': 'resource',
|
|
92 |
'identifier': 'helloapple',
|
|
93 |
'long_name': 'Hello Apple',
|
|
94 |
'uuid': 'a6754dcb-58d8-4b7a-a245-24fd7ad4cd68',
|
|
95 |
'version': [2021, 11, 10],
|
|
96 |
'revision': 1,
|
|
97 |
'description': 'greets an apple',
|
|
98 |
'dependencies': ['hello-message'],
|
|
99 |
'scripts': [{
|
|
100 |
'file': 'hello.js',
|
|
101 |
'sha256': file_sha256_hashes['hello.js']
|
|
102 |
}, {
|
|
103 |
'file': 'bye.js',
|
|
104 |
'sha256': file_sha256_hashes['bye.js']
|
|
105 |
}]
|
|
106 |
}, {
|
|
107 |
'api_schema_version': [1, 0, 1],
|
|
108 |
'source_name': 'hello',
|
|
109 |
'source_copyright': [{
|
|
110 |
'file': 'report.spdx',
|
|
111 |
'sha256': spdx_report_sha256
|
|
112 |
}, {
|
|
113 |
'file': 'LICENSES/CC0-1.0.txt',
|
|
114 |
'sha256': file_sha256_hashes['LICENSES/CC0-1.0.txt']
|
|
115 |
}],
|
|
116 |
'type': 'resource',
|
|
117 |
'identifier': 'hello-message',
|
|
118 |
'long_name': 'Hello Message',
|
|
119 |
'uuid': '1ec36229-298c-4b35-8105-c4f2e1b9811e',
|
|
120 |
'version': [2021, 11, 10],
|
|
121 |
'revision': 2,
|
|
122 |
'description': 'define messages for saying hello and bye',
|
|
123 |
'dependencies': [],
|
|
124 |
'scripts': [{
|
|
125 |
'file': 'message.js',
|
|
126 |
'sha256': file_sha256_hashes['message.js']
|
|
127 |
}]
|
|
128 |
}]
|
|
129 |
|
|
130 |
assert set([rj['identifier'] for rj in expected_resource_jsons]) == \
|
|
131 |
set([path.name for path in resource_dir.iterdir()])
|
|
132 |
|
|
133 |
for resource_json in expected_resource_jsons:
|
|
134 |
subdir = resource_dir / resource_json['identifier']
|
|
135 |
assert ['2021.11.10'] == [path.name for path in subdir.iterdir()]
|
|
136 |
|
|
137 |
with open(subdir / '2021.11.10', 'rt') as file_handle:
|
|
138 |
assert json.load(file_handle) == resource_json
|
|
139 |
|
|
140 |
# Verify files under 'mapping/'
|
|
141 |
mapping_dir = dstdir / 'mapping'
|
|
142 |
assert ['helloapple'] == [path.name for path in mapping_dir.iterdir()]
|
|
143 |
|
|
144 |
subdir = mapping_dir / 'helloapple'
|
|
145 |
assert ['2021.11.10'] == [path.name for path in subdir.iterdir()]
|
|
146 |
|
|
147 |
expected_mapping_json = {
|
|
148 |
'api_schema_version': [1, 0, 1],
|
|
149 |
'source_name': 'hello',
|
|
150 |
'source_copyright': [{
|
|
151 |
'file': 'report.spdx',
|
|
152 |
'sha256': spdx_report_sha256
|
|
153 |
}, {
|
|
154 |
'file': 'LICENSES/CC0-1.0.txt',
|
|
155 |
'sha256': file_sha256_hashes['LICENSES/CC0-1.0.txt']
|
|
156 |
}],
|
|
157 |
'type': 'mapping',
|
|
158 |
'identifier': 'helloapple',
|
|
159 |
'long_name': 'Hello Apple',
|
|
160 |
'uuid': '54d23bba-472e-42f5-9194-eaa24c0e3ee7',
|
|
161 |
'version': [2021, 11, 10],
|
|
162 |
'description': 'causes apple to get greeted on Hydrillabugs issue tracker',
|
|
163 |
'payloads': {
|
|
164 |
'https://hydrillabugs.koszko.org/***': {
|
|
165 |
'identifier': 'helloapple'
|
|
166 |
},
|
|
167 |
'https://hachettebugs.koszko.org/***': {
|
|
168 |
'identifier': 'helloapple'
|
|
169 |
}
|
|
170 |
}
|
|
171 |
}
|
|
172 |
|
|
173 |
with open(subdir / '2021.11.10', 'rt') as file_handle:
|
|
174 |
assert json.load(file_handle) == expected_mapping_json
|
|
175 |
|
|
176 |
# Verify files under 'source/'
|
|
177 |
source_dir = dstdir / 'source'
|
|
178 |
assert {'hello.json', 'hello.zip'} == \
|
|
179 |
set([path.name for path in source_dir.iterdir()])
|
|
180 |
|
|
181 |
src_filenames = (
|
|
182 |
*dist_filenames,
|
|
183 |
'README.txt', 'README.txt.license', '.reuse/dep5', 'index.json'
|
|
184 |
)
|
|
185 |
zip_filenames = [f'hello/{fn}' for fn in src_filenames]
|
|
186 |
|
|
187 |
with ZipFile(source_dir / 'hello.zip', 'r') as archive:
|
|
188 |
assert set([f.filename for f in archive.filelist]) == set(zip_filenames)
|
|
189 |
|
|
190 |
for zip_fn, src_fn in zip(zip_filenames, src_filenames):
|
|
191 |
src_path = srcdir
|
|
192 |
for segment in src_fn.split('/'):
|
|
193 |
src_path = src_path / segment
|
|
194 |
|
|
195 |
with archive.open(zip_fn, 'r') as zip_file_handle:
|
|
196 |
with open(src_path, 'rb') as disk_file_handle:
|
|
197 |
assert zip_file_handle.read() == disk_file_handle.read()
|
|
198 |
|
|
199 |
with open(source_dir / 'hello.zip', 'rb') as file_handle:
|
|
200 |
zipfile_sha256_hash = sha256(file_handle.read()).digest().hex()
|
|
201 |
|
|
202 |
expected_source_description = {
|
|
203 |
'api_schema_version': [1, 0, 1],
|
|
204 |
'source_name': 'hello',
|
|
205 |
'source_copyright': [{
|
|
206 |
'file': 'report.spdx',
|
|
207 |
'sha256': spdx_report_sha256
|
|
208 |
}, {
|
|
209 |
'file': 'LICENSES/CC0-1.0.txt',
|
|
210 |
'sha256': file_sha256_hashes['LICENSES/CC0-1.0.txt']
|
|
211 |
}],
|
|
212 |
'source_archives': {
|
|
213 |
'zip': {
|
|
214 |
'sha256': zipfile_sha256_hash,
|
|
215 |
}
|
|
216 |
},
|
|
217 |
'upstream_url': 'https://git.koszko.org/hydrilla-source-package-example',
|
|
218 |
'definitions': [{
|
|
219 |
'type': 'resource',
|
|
220 |
'identifier': 'helloapple',
|
|
221 |
'version': [2021, 11, 10],
|
|
222 |
}, {
|
|
223 |
'type': 'resource',
|
|
224 |
'identifier': 'hello-message',
|
|
225 |
'version': [2021, 11, 10],
|
|
226 |
}, {
|
|
227 |
'type': 'mapping',
|
|
228 |
'identifier': 'helloapple',
|
|
229 |
'version': [2021, 11, 10],
|
|
230 |
}]
|
|
231 |
}
|
|
232 |
|
|
233 |
with open(source_dir / 'hello.json', 'rt') as file_handle:
|
|
234 |
assert json.load(file_handle) == expected_source_description
|
|
235 |
|
|
236 |
# TODO: also verify on slightly different examples and check error handling
|
extend the automated test case