-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathversion_test.py
More file actions
402 lines (350 loc) · 12.5 KB
/
version_test.py
File metadata and controls
402 lines (350 loc) · 12.5 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
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
"""Test version functions"""
import json
import os
from pathlib import Path
from tempfile import TemporaryDirectory
import pytest
from constants import (
FILE_VERSION,
NPM_VERSION,
PYTHON_VERSION,
)
from repo_info import RepoInfo
from version import (
get_commit_oneline_message,
get_version_tag,
get_project_version,
UpdateVersionException,
update_version,
update_python_version_in_file,
update_npm_version,
update_version_file,
)
pytestmark = pytest.mark.asyncio
@pytest.mark.parametrize("readonly", [True, False])
async def test_update_python_version_settings(test_repo, test_repo_directory, readonly):
"""
update_python_version should return the old version and replace the appropriate file's text with the new version
"""
new_version = "9.9.99"
path = os.path.join(test_repo_directory, "ccxcon/settings.py")
with open(path, "r", encoding="utf-8") as f:
old_lines = f.readlines()
old_version = await update_version(
repo_info=test_repo,
new_version=new_version,
working_dir=test_repo_directory,
readonly=readonly,
)
assert old_version == "0.2.0"
with open(path, "r", encoding="utf-8") as f:
new_lines = f.readlines()
assert len(old_lines) == len(new_lines)
diff_count = 0
for old_line, new_line in zip(old_lines, new_lines):
if old_line != new_line:
diff_count += 1
assert diff_count == (0 if readonly else 1)
found_new_version = False
with open(path, "r", encoding="utf-8") as f:
for line in f.readlines():
if line == f'VERSION = "{new_version}"\n':
found_new_version = True
break
assert found_new_version is not readonly
@pytest.mark.parametrize("readonly", [True, False])
async def test_update_python_version_init(test_repo_directory, test_repo, readonly):
"""If we detect a version in a __init__.py file we should update it properly"""
old_version = "1.2.3"
test_repo_directory = Path(test_repo_directory)
os.unlink(test_repo_directory / "ccxcon" / "settings.py")
with open(
test_repo_directory / "ccxcon" / "__init__.py", "w", encoding="utf-8"
) as f:
f.write(f"__version__ = '{old_version}'")
new_version = "4.5.6"
assert (
await update_version(
repo_info=test_repo,
new_version=new_version,
working_dir=test_repo_directory,
readonly=readonly,
)
== old_version
)
found_new_version = False
with open(
test_repo_directory / "ccxcon" / "__init__.py", "r", encoding="utf-8"
) as f:
for line in f.readlines():
if line.strip() == f'__version__ = "{new_version}"':
found_new_version = True
break
assert found_new_version is not readonly
@pytest.mark.parametrize("readonly", [True, False])
async def test_update_python_version_setup(test_repo_directory, test_repo, readonly):
"""If we detect a version in setup.py we should update it properly, if readonly is set"""
old_version = "0.2.0"
os.unlink(os.path.join(test_repo_directory, "ccxcon/settings.py"))
with open(
os.path.join(test_repo_directory, "setup.py"), "w", encoding="utf-8"
) as f:
f.write(
"""
setup(
name='pylmod',
version='0.2.0',
license='BSD',
author='MIT ODL Engineering',
zip_safe=True,
) """
)
new_version = "4.5.6"
assert (
await update_version(
repo_info=test_repo,
new_version=new_version,
working_dir=test_repo_directory,
readonly=readonly,
)
== old_version
)
found_new_version = False
with open(
os.path.join(test_repo_directory, "setup.py"), "r", encoding="utf-8"
) as f:
for line in f.readlines():
if line.strip() == f"version='{new_version}',":
found_new_version = True
break
assert found_new_version is not readonly
@pytest.mark.parametrize("readonly", [True, False])
async def test_update_python_version_missing(test_repo_directory, test_repo, readonly):
"""If there is no version we should return None"""
os.unlink(os.path.join(test_repo_directory, "ccxcon/settings.py"))
contents = """
setup(
name='pylmod',
) """
with open(
os.path.join(test_repo_directory, "setup.py"), "w", encoding="utf-8"
) as f:
f.write(contents)
with pytest.raises(UpdateVersionException) as ex:
await update_version(
repo_info=test_repo,
new_version="4.5.6",
working_dir=test_repo_directory,
readonly=readonly,
)
assert ex.value.args[0] == "Unable to find previous version number"
@pytest.mark.parametrize("readonly", [True, False])
async def test_update_python_version_duplicate(
test_repo_directory, test_repo, readonly
):
"""If there are two detected versions in different files we should raise an exception"""
contents = """
setup(
name='pylmod',
version='1.2.3',
) """
with open(
os.path.join(test_repo_directory, "setup.py"), "w", encoding="utf-8"
) as f:
f.write(contents)
with pytest.raises(UpdateVersionException) as ex:
await update_version(
repo_info=test_repo,
new_version="4.5.6",
working_dir=test_repo_directory,
readonly=readonly,
)
assert (
ex.value.args[0]
== "Found at least two files with updatable versions: settings.py and setup.py"
)
@pytest.mark.parametrize("readonly", [True, False])
async def test_update_python_version_duplicate_same_file(
test_repo_directory, test_repo, readonly
):
"""If there are two detected versions in the same file we should raise an exception"""
contents = """
setup(
name='pylmod',
version='1.2.3',
version='4.5.6',
) """
with open(
os.path.join(test_repo_directory, "setup.py"), "w", encoding="utf-8"
) as f:
f.write(contents)
with pytest.raises(UpdateVersionException) as ex:
await update_version(
repo_info=test_repo,
new_version="4.5.6",
working_dir=test_repo_directory,
readonly=readonly,
)
assert ex.value.args[0] == "Expected only one version for setup.py but found 2"
async def test_get_version_tag(mocker):
"""
get_version_tag should return the git hash of the directory
"""
a_hash = b"hash"
mocker.async_patch("version.check_output", return_value=a_hash)
assert (
await get_version_tag(
github_access_token="github",
repo_url="http://github.com/mitodl/doof.git",
commit_hash="commit",
)
== a_hash.decode()
)
async def test_get_commit_oneline_message(mocker):
"""
get_commit_oneline_message should return the commit message and a piece of the commit hash
"""
message = b"abc123 A useful pull request was merged (#143)"
mocker.async_patch("version.check_output", return_value=message)
assert (
await get_commit_oneline_message(
github_access_token="github",
repo_url="http://github.com/mitodl/doof.git",
commit_hash="commit",
)
== message.decode()
)
@pytest.mark.parametrize("readonly", [True, False])
@pytest.mark.parametrize(
"filename,line, expected_output",
[
("settings.py", 'VERSION = "0.34.56"\n', 'VERSION = "0.123.456"\n'),
("__init__.py", "__version__ = '0.34.56'\n", '__version__ = "0.123.456"\n'),
("setup.py", ' version="0.34.56",\n', ' version="0.123.456",\n'),
],
)
async def test_update_python_version_in_file(filename, line, expected_output, readonly):
"""update_python_version_in_file should update the version in the file and return the old version, if found"""
old_version = "0.34.56"
new_version = "0.123.456"
with TemporaryDirectory() as base:
path = Path(base) / filename
with open(path, "w", encoding="utf-8") as f:
f.write("text")
retrieved_version = update_python_version_in_file(
root=base, filename=filename, new_version=new_version, readonly=readonly
)
assert retrieved_version is None
with open(path, "w", encoding="utf-8") as f:
f.write(line)
retrieved_version = update_python_version_in_file(
root=base, filename=filename, new_version=new_version, readonly=readonly
)
assert retrieved_version == old_version
with open(path, "r", encoding="utf-8") as f:
lines = f.readlines()
if readonly:
assert new_version not in "\n".join(lines)
else:
version_line = [line for line in lines if new_version in line][0]
assert version_line == expected_output
@pytest.mark.parametrize("readonly", [True, False])
async def test_update_npm_version(readonly):
"""update version for an npm package"""
old_version = "0.76.54"
new_version = "0.99.99"
with TemporaryDirectory() as working_dir:
package_json_path = Path(working_dir) / "package.json"
with open(package_json_path, "w", encoding="utf-8") as f:
json.dump({"version": old_version}, f)
received = await update_npm_version(
new_version=new_version, working_dir=working_dir, readonly=readonly
)
assert received == old_version
with open(package_json_path, "r", encoding="utf-8") as f:
assert json.load(f) == {"version": old_version if readonly else new_version}
@pytest.mark.parametrize("readonly", [True, False])
async def test_update_version_file(readonly):
"""update version for a version file"""
old_version = "0.76.54"
new_version = "0.99.99"
with TemporaryDirectory() as working_dir:
version_file_path = Path(working_dir) / "VERSION"
with open(version_file_path, "w", encoding="utf-8") as f:
f.write(f"{old_version}\n")
received = await update_version_file(
new_version=new_version, working_dir=working_dir, readonly=readonly
)
assert received == old_version
with open(version_file_path, "r", encoding="utf-8") as f:
assert f.readline().strip() == old_version if readonly else new_version
@pytest.mark.parametrize("readonly", [True, False])
@pytest.mark.parametrize(
"versioning_strategy, expected_python, expected_js, expected_version_file",
[
[PYTHON_VERSION, True, False, False],
[NPM_VERSION, False, True, False],
[FILE_VERSION, False, False, True],
],
)
async def test_update_version(
mocker,
test_repo,
versioning_strategy,
expected_python,
expected_js,
expected_version_file,
readonly,
):
"""Call update_version on project"""
repo_info = RepoInfo(
**{
**test_repo._asdict(),
"versioning_strategy": versioning_strategy,
}
)
new_version = "12.34.56"
working_dir = "/tmp/a/directory"
update_py_mock = mocker.patch("version.update_python_version")
update_js_mock = mocker.async_patch("version.update_npm_version")
update_version_file_mock = mocker.async_patch("version.update_version_file")
await update_version(
repo_info=repo_info,
new_version=new_version,
working_dir=working_dir,
readonly=readonly,
)
if expected_python:
update_py_mock.assert_called_once_with(
new_version=new_version,
working_dir=working_dir,
readonly=readonly,
)
else:
assert update_py_mock.called is False
if expected_js:
update_js_mock.assert_called_once_with(
new_version=new_version,
working_dir=working_dir,
readonly=readonly,
)
else:
assert update_js_mock.called is False
if expected_version_file:
update_version_file_mock.assert_called_once_with(
new_version=new_version,
working_dir=working_dir,
readonly=readonly,
)
else:
assert update_version_file_mock.called is False
async def test_get_project_version(mocker, test_repo):
"""
get_project_version should return the latest version without making modifications
"""
update_version_mock = mocker.async_patch("version.update_version")
working_dir = "/tmp"
await get_project_version(repo_info=test_repo, working_dir=working_dir)
update_version_mock.assert_called_once_with(
repo_info=test_repo, new_version="9.9.9", working_dir=working_dir, readonly=True
)