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
|
import os
from collections.abc import Sequence
from pathlib import Path
from tempfile import TemporaryDirectory
from typing import Any
from unittest import TestCase
from dhpython.interpreter import Interpreter
from dhpython.fs import Scan, merge_WHEEL, share_files
from dhpython.options import DHPythonOptions
from dhpython.version import Version
class FSTestCase(TestCase):
files: dict[str, Sequence[str]] = {}
def setUp(self) -> None:
self.tempdir = TemporaryDirectory() # pylint: disable=consider-using-with
self.addCleanup(self.tempdir.cleanup)
temp_path = Path(self.tempdir.name)
for fn, contents in self.files.items():
path = temp_path / fn
setattr(self, path.name, path)
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text("\n".join(contents) + "\n")
def assertFileContents(self, path: Path, contents: str | Sequence[str]) -> None:
"""Assert that the contents of path is contents
Contents may be specified as a list of strings, one per line, without
line-breaks.
"""
if isinstance(contents, (list, tuple)):
contents = "\n".join(contents) + "\n"
assert isinstance(contents, str)
with path.open("r") as f:
self.assertMultiLineEqual(contents, f.read())
class MergeTagsTest(FSTestCase):
a: Path
b: Path
files = {
"a": ("foo", "Tag: A"),
"b": ("foo", "Tag: B"),
}
def test_merge_wheel(self) -> None:
merge_WHEEL(self.a, self.b)
self.assertFileContents(self.b, ("foo", "Tag: B", "Tag: A"))
class ShareFilesTestCase(FSTestCase):
impl = "cpython3"
options: dict[str, Any] = {}
def setUp(self) -> None:
super().setUp()
self.destdir = TemporaryDirectory() # pylint: disable=consider-using-with
self.addCleanup(self.destdir.cleanup)
self.interpreter = Interpreter(self.impl)
def share_files(self, options: dict[str, Any] | None = None) -> None:
if options is None:
options = self.options
share_files(
self.tempdir.name,
self.destdir.name,
self.interpreter,
DHPythonOptions(**options),
)
def destPath(self, name: str) -> Path:
return Path(self.destdir.name) / name
class HatchlingLicenseTest(ShareFilesTestCase):
files = {
"foo.dist-info/license_files/LICENSE.txt": ("foo"),
"foo.dist-info/licenses/COPYING": ("foo"),
"foo.dist-info/RECORD": (
"foo.dist-info/license_files/LICENSE.txt,sha256=2c26b46b68ffc68ff99"
"b453c1d30413413422d706483bfa0f98a5e886266e7ae,4",
"foo.dist-info/licenses/COPYING,sha256=2c26b46b68ffc68ff99b453c1d30"
"413413422d706483bfa0f98a5e886266e7ae,4",
"foo.dist-info/WHEEL,sha256=447fb61fa39a067229e1cce8fc0953bfced53ea"
"c85d1844f5940f51c1fcba725,6",
),
"foo.dist-info/WHEEL": ("foo"),
}
def test_removes_license_files(self) -> None:
self.share_files()
self.assertFalse(
self.destPath("foo.dist-info/license_files/LICENSE.txt").exists()
)
self.assertFalse(self.destPath("foo.dist-info/licenses/COPYING").exists())
class FlitLicenseTest(ShareFilesTestCase):
files = {
"foo.dist-info/COPYING": ("foo"),
"foo.dist-info/COPYING.LESSER": ("foo"),
"foo.dist-info/RECORD": (
"foo.dist-info/COPYING,sha256=2c26b46b68ffc68ff99b453c1d30413413422"
"d706483bfa0f98a5e886266e7ae,4",
"foo.dist-info/COPYING.LESSER,sha256=2c26b46b68ffc68ff99b453c1d3041"
"3413422d706483bfa0f98a5e886266e7ae,4",
"foo.dist-info/WHEEL,sha256=447fb61fa39a067229e1cce8fc0953bfced53ea"
"c85d1844f5940f51c1fcba725,6",
),
"foo.dist-info/WHEEL": ("foo"),
}
def test_removes_license_files(self) -> None:
self.share_files()
self.assertFalse(self.destPath("foo.dist-info/COPYING.LESSER").exists())
self.assertFalse(self.destPath("foo.dist-info/COPYING").exists())
class CExtensionsRenameTest(ShareFilesTestCase):
def setUp(self) -> None:
ver = Interpreter(self.impl).default_version
self.files = {
f"usr/lib/python{ver}/foo.so": ("binary-data"),
}
self.ext_filename = f"usr/lib/python{ver}/foo.so"
super().setUp()
def test_python_extensions(self) -> None:
self.share_files()
self.assertFalse(self.destPath(self.ext_filename).exists())
expected_name = self.interpreter.check_extname(self.ext_filename)
self.assertTrue(expected_name)
assert expected_name
self.assertTrue(self.destPath(expected_name).exists())
def test_no_ext_rename(self) -> None:
self.share_files(options={"no_ext_rename": True})
self.assertTrue(self.destPath(self.ext_filename).exists())
class ScanTestCase(FSTestCase):
impl = "cpython3"
options: dict[str, Any] = {}
package = "python3-test"
def setUp(self) -> None:
super().setUp()
pwd = os.getcwd()
try:
os.chdir(self.tempdir.name)
self.scan = Scan(
interpreter=Interpreter(self.impl),
package=self.package,
options=DHPythonOptions(**self.options),
)
finally:
os.chdir(pwd)
class UnversionedExtensionScanTest(ScanTestCase):
files = {
"debian/python3-test/usr/lib/python3/dist-packages/foo.so": "contents",
}
options = {
"no_ext_rename": True,
}
def test_scan(self) -> None:
self.assertEqual(self.scan.result["public_vers"], {Version("3")})
self.assertEqual(self.scan.result["ext_vers"], set())
self.assertEqual(
self.scan.result["ext_no_version"],
{"debian/python3-test/usr/lib/python3/dist-packages/foo.so"},
)
self.assertEqual(self.scan.result["ext_stableabi"], set())
class UnversionedExtensionRenamedScanTest(ScanTestCase):
files = {
"debian/python3-test/usr/lib/python3/dist-packages/foo.so": "contents",
}
def test_scan(self) -> None:
version = Interpreter(self.impl).default_version
self.assertEqual(self.scan.result["public_vers"], {Version("3")})
self.assertEqual(self.scan.result["ext_vers"], {version})
self.assertEqual(self.scan.result["ext_no_version"], set())
self.assertEqual(self.scan.result["ext_stableabi"], set())
class StableABIExtensionDependenciesTest(ScanTestCase):
files = {
(
"debian/python3-test/usr/lib/python3/dist-packages/"
"foo.abi3-x86_64-linux-gnu.so"
): "contents",
}
def test_scan(self) -> None:
self.assertEqual(self.scan.result["public_vers"], {Version("3")})
self.assertEqual(self.scan.result["ext_vers"], set())
self.assertEqual(self.scan.result["ext_no_version"], set())
self.assertEqual(
self.scan.result["ext_stableabi"],
{
"debian/python3-test/usr/lib/python3/dist-packages/"
"foo.abi3-x86_64-linux-gnu.so"
},
)
class VersionedExtensionDependenciesTest(ScanTestCase):
files = {
(
"debian/python3-test/usr/lib/python3/dist-packages/"
"foo.cpython-314-x86_64-linux-gnu.so"
): "contents",
}
def test_scan(self) -> None:
self.assertEqual(self.scan.result["public_vers"], {Version("3")})
self.assertEqual(self.scan.result["ext_vers"], {Version("3.14")})
self.assertEqual(self.scan.result["ext_no_version"], set())
self.assertEqual(self.scan.result["ext_stableabi"], set())
|