|
1 | 1 | """Unit tests for build_tools/read_kolibri_version.py.""" |
2 | 2 |
|
3 | | -Each caller holds a different entity at its version-read point: an extracted |
4 | | -sdist tree (android), an unpacked wheel tree (desktop-app), the repo checkout |
5 | | -(debian-server CI), a built wheel (the root `pex` target). These pin the |
6 | | -archive-layout differences the helper absorbs. |
7 | | - |
8 | | -Stdlib-only and 3.6-compatible: tox.yml's `unit_test_no_uv` job runs this |
9 | | -directory under a bare `python` in python:3.6-buster and python:3.7-buster. |
10 | | -""" |
11 | | -
|
12 | | -import importlib.util |
13 | | -import io |
14 | 3 | import os |
15 | | -import tarfile |
16 | 4 | import zipfile |
17 | 5 |
|
18 | 6 | import pytest |
19 | 7 |
|
20 | | -_MODULE_PATH = os.path.join( |
21 | | - os.path.dirname(os.path.dirname(os.path.realpath(__file__))), |
22 | | - "build_tools", |
23 | | - "read_kolibri_version.py", |
24 | | -) |
25 | | -_spec = importlib.util.spec_from_file_location("read_kolibri_version", _MODULE_PATH) |
26 | | -read_kolibri_version = importlib.util.module_from_spec(_spec) |
27 | | -_spec.loader.exec_module(read_kolibri_version) |
| 8 | +from build_tools.read_kolibri_version import read_version |
28 | 9 |
|
29 | 10 | VERSION = "0.19.0.dev0+g1234abc" |
30 | 11 |
|
@@ -56,70 +37,14 @@ def test_reads_the_double_quoted_form(tmp_path): |
56 | 37 |
|
57 | 38 |
|
58 | 39 | def test_reads_version_from_a_wheel(tmp_path): |
| 40 | + # A wheel holds the package at the archive root, unlike an extracted tree. |
59 | 41 | whl = os.path.join(str(tmp_path), "kolibri-0.19.0-py2.py3-none-any.whl") |
60 | 42 | with zipfile.ZipFile(whl, "w") as archive: |
61 | 43 | archive.writestr("kolibri/_version.py", SCM_VERSION_PY) |
62 | 44 | archive.writestr("kolibri-0.19.0.dist-info/METADATA", "Name: kolibri\n") |
63 | 45 | assert read_version(whl) == VERSION |
64 | 46 |
|
65 | 47 |
|
66 | | -def test_reads_version_from_an_sdist(tmp_path): |
67 | | - # The root component is the one android strips with --strip-components=1. |
68 | | - sdist = os.path.join(str(tmp_path), "kolibri-0.19.0.tar.gz") |
69 | | - payload = SCM_VERSION_PY.encode("utf-8") |
70 | | - member = tarfile.TarInfo("kolibri-0.19.0/kolibri/_version.py") |
71 | | - member.size = len(payload) |
72 | | - with tarfile.open(sdist, "w:gz") as archive: |
73 | | - archive.addfile(member, io.BytesIO(payload)) |
74 | | - assert read_kolibri_version.read_version(sdist) == VERSION |
75 | | -
|
76 | | -
|
77 | | -def test_wheel_without_a_version_file_raises(tmp_path): |
78 | | - whl = os.path.join(str(tmp_path), "kolibri-0.19.0-py2.py3-none-any.whl") |
79 | | - with zipfile.ZipFile(whl, "w") as archive: |
80 | | - archive.writestr("kolibri-0.19.0.dist-info/METADATA", "Name: kolibri\n") |
81 | | - with pytest.raises(ValueError) as excinfo: |
82 | | - read_kolibri_version.read_version(whl) |
83 | | - assert whl in str(excinfo.value) |
84 | | -
|
85 | | -
|
86 | | -def test_sdist_without_a_version_file_raises(tmp_path): |
87 | | - sdist = os.path.join(str(tmp_path), "kolibri-0.19.0.tar.gz") |
88 | | - member = tarfile.TarInfo("kolibri-0.19.0/kolibri/__init__.py") |
89 | | - member.size = 0 |
90 | | - with tarfile.open(sdist, "w:gz") as archive: |
91 | | - archive.addfile(member, io.BytesIO(b"")) |
92 | | - with pytest.raises(ValueError) as excinfo: |
93 | | - read_kolibri_version.read_version(sdist) |
94 | | - assert sdist in str(excinfo.value) |
95 | | -
|
96 | | -
|
97 | | -def test_tree_without_a_version_file_raises(tmp_path): |
98 | | - with pytest.raises(ValueError) as excinfo: |
99 | | - read_kolibri_version.read_version(str(tmp_path)) |
100 | | - assert str(tmp_path) in str(excinfo.value) |
101 | | -
|
102 | | -
|
103 | | -def test_unrecognized_suffix_raises(tmp_path): |
104 | | - # raspberry-pi reads a .deb filename instead, deliberately. Anyone pointing |
105 | | - # this helper at one should get an error, not a silent misparse. |
106 | | - deb = os.path.join(str(tmp_path), "kolibri_0.19.0-0ubuntu1_all.deb") |
107 | | - with open(deb, "wb") as f: |
108 | | - f.write(b"") |
109 | | - with pytest.raises(ValueError) as excinfo: |
110 | | - read_kolibri_version.read_version(deb) |
111 | | - assert deb in str(excinfo.value) |
112 | | -
|
113 | | -
|
114 | | -def test_missing_entity_raises(tmp_path): |
115 | | - # The likeliest failure in every caller: nothing has been staged yet, so |
116 | | - # kolibrisrc/ or tar/extracted/ simply is not there. |
117 | | - missing = os.path.join(str(tmp_path), "kolibrisrc") |
118 | | - with pytest.raises(ValueError) as excinfo: |
119 | | - read_kolibri_version.read_version(missing) |
120 | | - assert missing in str(excinfo.value) |
121 | | -
|
122 | | -
|
123 | 48 | def test_version_alias_alone_raises(tmp_path): |
124 | 49 | with pytest.raises(ValueError): |
125 | 50 | read_version(_write_tree(tmp_path, "version = '%s'\n" % VERSION)) |
0 commit comments