test_pkg_resources.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. from __future__ import annotations
  2. import builtins
  3. import datetime
  4. import inspect
  5. import os
  6. import plistlib
  7. import stat
  8. import subprocess
  9. import sys
  10. import tempfile
  11. import zipfile
  12. from unittest import mock
  13. import pytest
  14. import pkg_resources
  15. from pkg_resources import DistInfoDistribution, Distribution, EggInfoDistribution
  16. import distutils.command.install_egg_info
  17. import distutils.dist
  18. class EggRemover(str):
  19. def __call__(self):
  20. if self in sys.path:
  21. sys.path.remove(self)
  22. if os.path.exists(self):
  23. os.remove(self)
  24. class TestZipProvider:
  25. finalizers: list[EggRemover] = []
  26. ref_time = datetime.datetime(2013, 5, 12, 13, 25, 0)
  27. "A reference time for a file modification"
  28. @classmethod
  29. def setup_class(cls):
  30. "create a zip egg and add it to sys.path"
  31. egg = tempfile.NamedTemporaryFile(suffix='.egg', delete=False)
  32. zip_egg = zipfile.ZipFile(egg, 'w')
  33. zip_info = zipfile.ZipInfo()
  34. zip_info.filename = 'mod.py'
  35. zip_info.date_time = cls.ref_time.timetuple()
  36. zip_egg.writestr(zip_info, 'x = 3\n')
  37. zip_info = zipfile.ZipInfo()
  38. zip_info.filename = 'data.dat'
  39. zip_info.date_time = cls.ref_time.timetuple()
  40. zip_egg.writestr(zip_info, 'hello, world!')
  41. zip_info = zipfile.ZipInfo()
  42. zip_info.filename = 'subdir/mod2.py'
  43. zip_info.date_time = cls.ref_time.timetuple()
  44. zip_egg.writestr(zip_info, 'x = 6\n')
  45. zip_info = zipfile.ZipInfo()
  46. zip_info.filename = 'subdir/data2.dat'
  47. zip_info.date_time = cls.ref_time.timetuple()
  48. zip_egg.writestr(zip_info, 'goodbye, world!')
  49. zip_egg.close()
  50. egg.close()
  51. sys.path.append(egg.name)
  52. subdir = os.path.join(egg.name, 'subdir')
  53. sys.path.append(subdir)
  54. cls.finalizers.append(EggRemover(subdir))
  55. cls.finalizers.append(EggRemover(egg.name))
  56. @classmethod
  57. def teardown_class(cls):
  58. for finalizer in cls.finalizers:
  59. finalizer()
  60. def test_resource_listdir(self):
  61. import mod # pyright: ignore[reportMissingImports] # Temporary package for test
  62. zp = pkg_resources.ZipProvider(mod)
  63. expected_root = ['data.dat', 'mod.py', 'subdir']
  64. assert sorted(zp.resource_listdir('')) == expected_root
  65. expected_subdir = ['data2.dat', 'mod2.py']
  66. assert sorted(zp.resource_listdir('subdir')) == expected_subdir
  67. assert sorted(zp.resource_listdir('subdir/')) == expected_subdir
  68. assert zp.resource_listdir('nonexistent') == []
  69. assert zp.resource_listdir('nonexistent/') == []
  70. import mod2 # pyright: ignore[reportMissingImports] # Temporary package for test
  71. zp2 = pkg_resources.ZipProvider(mod2)
  72. assert sorted(zp2.resource_listdir('')) == expected_subdir
  73. assert zp2.resource_listdir('subdir') == []
  74. assert zp2.resource_listdir('subdir/') == []
  75. def test_resource_filename_rewrites_on_change(self):
  76. """
  77. If a previous call to get_resource_filename has saved the file, but
  78. the file has been subsequently mutated with different file of the
  79. same size and modification time, it should not be overwritten on a
  80. subsequent call to get_resource_filename.
  81. """
  82. import mod # pyright: ignore[reportMissingImports] # Temporary package for test
  83. manager = pkg_resources.ResourceManager()
  84. zp = pkg_resources.ZipProvider(mod)
  85. filename = zp.get_resource_filename(manager, 'data.dat')
  86. actual = datetime.datetime.fromtimestamp(os.stat(filename).st_mtime)
  87. assert actual == self.ref_time
  88. f = open(filename, 'w', encoding="utf-8")
  89. f.write('hello, world?')
  90. f.close()
  91. ts = self.ref_time.timestamp()
  92. os.utime(filename, (ts, ts))
  93. filename = zp.get_resource_filename(manager, 'data.dat')
  94. with open(filename, encoding="utf-8") as f:
  95. assert f.read() == 'hello, world!'
  96. manager.cleanup_resources()
  97. class TestResourceManager:
  98. def test_get_cache_path(self):
  99. mgr = pkg_resources.ResourceManager()
  100. path = mgr.get_cache_path('foo')
  101. type_ = str(type(path))
  102. message = "Unexpected type from get_cache_path: " + type_
  103. assert isinstance(path, str), message
  104. def test_get_cache_path_race(self, tmpdir):
  105. # Patch to os.path.isdir to create a race condition
  106. def patched_isdir(dirname, unpatched_isdir=pkg_resources.isdir):
  107. patched_isdir.dirnames.append(dirname)
  108. was_dir = unpatched_isdir(dirname)
  109. if not was_dir:
  110. os.makedirs(dirname)
  111. return was_dir
  112. patched_isdir.dirnames = []
  113. # Get a cache path with a "race condition"
  114. mgr = pkg_resources.ResourceManager()
  115. mgr.set_extraction_path(str(tmpdir))
  116. archive_name = os.sep.join(('foo', 'bar', 'baz'))
  117. with mock.patch.object(pkg_resources, 'isdir', new=patched_isdir):
  118. mgr.get_cache_path(archive_name)
  119. # Because this test relies on the implementation details of this
  120. # function, these assertions are a sentinel to ensure that the
  121. # test suite will not fail silently if the implementation changes.
  122. called_dirnames = patched_isdir.dirnames
  123. assert len(called_dirnames) == 2
  124. assert called_dirnames[0].split(os.sep)[-2:] == ['foo', 'bar']
  125. assert called_dirnames[1].split(os.sep)[-1:] == ['foo']
  126. """
  127. Tests to ensure that pkg_resources runs independently from setuptools.
  128. """
  129. def test_setuptools_not_imported(self):
  130. """
  131. In a separate Python environment, import pkg_resources and assert
  132. that action doesn't cause setuptools to be imported.
  133. """
  134. lines = (
  135. 'import pkg_resources',
  136. 'import sys',
  137. ('assert "setuptools" not in sys.modules, "setuptools was imported"'),
  138. )
  139. cmd = [sys.executable, '-c', '; '.join(lines)]
  140. subprocess.check_call(cmd)
  141. def make_test_distribution(metadata_path, metadata):
  142. """
  143. Make a test Distribution object, and return it.
  144. :param metadata_path: the path to the metadata file that should be
  145. created. This should be inside a distribution directory that should
  146. also be created. For example, an argument value might end with
  147. "<project>.dist-info/METADATA".
  148. :param metadata: the desired contents of the metadata file, as bytes.
  149. """
  150. dist_dir = os.path.dirname(metadata_path)
  151. os.mkdir(dist_dir)
  152. with open(metadata_path, 'wb') as f:
  153. f.write(metadata)
  154. dists = list(pkg_resources.distributions_from_metadata(dist_dir))
  155. (dist,) = dists
  156. return dist
  157. def test_get_metadata__bad_utf8(tmpdir):
  158. """
  159. Test a metadata file with bytes that can't be decoded as utf-8.
  160. """
  161. filename = 'METADATA'
  162. # Convert the tmpdir LocalPath object to a string before joining.
  163. metadata_path = os.path.join(str(tmpdir), 'foo.dist-info', filename)
  164. # Encode a non-ascii string with the wrong encoding (not utf-8).
  165. metadata = 'née'.encode('iso-8859-1')
  166. dist = make_test_distribution(metadata_path, metadata=metadata)
  167. with pytest.raises(UnicodeDecodeError) as excinfo:
  168. dist.get_metadata(filename)
  169. exc = excinfo.value
  170. actual = str(exc)
  171. expected = (
  172. # The error message starts with "'utf-8' codec ..." However, the
  173. # spelling of "utf-8" can vary (e.g. "utf8") so we don't include it
  174. "codec can't decode byte 0xe9 in position 1: "
  175. 'invalid continuation byte in METADATA file at path: '
  176. )
  177. assert expected in actual, f'actual: {actual}'
  178. assert actual.endswith(metadata_path), f'actual: {actual}'
  179. def make_distribution_no_version(tmpdir, basename):
  180. """
  181. Create a distribution directory with no file containing the version.
  182. """
  183. dist_dir = tmpdir / basename
  184. dist_dir.ensure_dir()
  185. # Make the directory non-empty so distributions_from_metadata()
  186. # will detect it and yield it.
  187. dist_dir.join('temp.txt').ensure()
  188. dists = list(pkg_resources.distributions_from_metadata(dist_dir))
  189. assert len(dists) == 1
  190. (dist,) = dists
  191. return dist, dist_dir
  192. @pytest.mark.parametrize(
  193. ("suffix", "expected_filename", "expected_dist_type"),
  194. [
  195. ('egg-info', 'PKG-INFO', EggInfoDistribution),
  196. ('dist-info', 'METADATA', DistInfoDistribution),
  197. ],
  198. )
  199. @pytest.mark.xfail(
  200. sys.version_info[:2] == (3, 12) and sys.version_info.releaselevel != 'final',
  201. reason="https://github.com/python/cpython/issues/103632",
  202. )
  203. def test_distribution_version_missing(
  204. tmpdir, suffix, expected_filename, expected_dist_type
  205. ):
  206. """
  207. Test Distribution.version when the "Version" header is missing.
  208. """
  209. basename = f'foo.{suffix}'
  210. dist, dist_dir = make_distribution_no_version(tmpdir, basename)
  211. expected_text = (
  212. f"Missing 'Version:' header and/or {expected_filename} file at path: "
  213. )
  214. metadata_path = os.path.join(dist_dir, expected_filename)
  215. # Now check the exception raised when the "version" attribute is accessed.
  216. with pytest.raises(ValueError) as excinfo:
  217. dist.version
  218. err = str(excinfo.value)
  219. # Include a string expression after the assert so the full strings
  220. # will be visible for inspection on failure.
  221. assert expected_text in err, str((expected_text, err))
  222. # Also check the args passed to the ValueError.
  223. msg, dist = excinfo.value.args
  224. assert expected_text in msg
  225. # Check that the message portion contains the path.
  226. assert metadata_path in msg, str((metadata_path, msg))
  227. assert type(dist) is expected_dist_type
  228. @pytest.mark.xfail(
  229. sys.version_info[:2] == (3, 12) and sys.version_info.releaselevel != 'final',
  230. reason="https://github.com/python/cpython/issues/103632",
  231. )
  232. def test_distribution_version_missing_undetected_path():
  233. """
  234. Test Distribution.version when the "Version" header is missing and
  235. the path can't be detected.
  236. """
  237. # Create a Distribution object with no metadata argument, which results
  238. # in an empty metadata provider.
  239. dist = Distribution('/foo')
  240. with pytest.raises(ValueError) as excinfo:
  241. dist.version
  242. msg, dist = excinfo.value.args
  243. expected = (
  244. "Missing 'Version:' header and/or PKG-INFO file at path: [could not detect]"
  245. )
  246. assert msg == expected
  247. @pytest.mark.parametrize('only', [False, True])
  248. def test_dist_info_is_not_dir(tmp_path, only):
  249. """Test path containing a file with dist-info extension."""
  250. dist_info = tmp_path / 'foobar.dist-info'
  251. dist_info.touch()
  252. assert not pkg_resources.dist_factory(str(tmp_path), str(dist_info), only)
  253. def test_macos_vers_fallback(monkeypatch, tmp_path):
  254. """Regression test for pkg_resources._macos_vers"""
  255. orig_open = builtins.open
  256. # Pretend we need to use the plist file
  257. monkeypatch.setattr('platform.mac_ver', mock.Mock(return_value=('', (), '')))
  258. # Create fake content for the fake plist file
  259. with open(tmp_path / 'fake.plist', 'wb') as fake_file:
  260. plistlib.dump({"ProductVersion": "11.4"}, fake_file)
  261. # Pretend the fake file exists
  262. monkeypatch.setattr('os.path.exists', mock.Mock(return_value=True))
  263. def fake_open(file, *args, **kwargs):
  264. return orig_open(tmp_path / 'fake.plist', *args, **kwargs)
  265. # Ensure that the _macos_vers works correctly
  266. with mock.patch('builtins.open', mock.Mock(side_effect=fake_open)) as m:
  267. pkg_resources._macos_vers.cache_clear()
  268. assert pkg_resources._macos_vers() == ["11", "4"]
  269. pkg_resources._macos_vers.cache_clear()
  270. m.assert_called()
  271. class TestDeepVersionLookupDistutils:
  272. @pytest.fixture
  273. def env(self, tmpdir):
  274. """
  275. Create a package environment, similar to a virtualenv,
  276. in which packages are installed.
  277. """
  278. class Environment(str):
  279. pass
  280. env = Environment(tmpdir)
  281. tmpdir.chmod(stat.S_IRWXU)
  282. subs = 'home', 'lib', 'scripts', 'data', 'egg-base'
  283. env.paths = dict((dirname, str(tmpdir / dirname)) for dirname in subs)
  284. list(map(os.mkdir, env.paths.values()))
  285. return env
  286. def create_foo_pkg(self, env, version):
  287. """
  288. Create a foo package installed (distutils-style) to env.paths['lib']
  289. as version.
  290. """
  291. ld = "This package has unicode metadata! ❄"
  292. attrs = dict(name='foo', version=version, long_description=ld)
  293. dist = distutils.dist.Distribution(attrs)
  294. iei_cmd = distutils.command.install_egg_info.install_egg_info(dist)
  295. iei_cmd.initialize_options()
  296. iei_cmd.install_dir = env.paths['lib']
  297. iei_cmd.finalize_options()
  298. iei_cmd.run()
  299. def test_version_resolved_from_egg_info(self, env):
  300. version = '1.11.0.dev0+2329eae'
  301. self.create_foo_pkg(env, version)
  302. # this requirement parsing will raise a VersionConflict unless the
  303. # .egg-info file is parsed (see #419 on BitBucket)
  304. req = pkg_resources.Requirement.parse('foo>=1.9')
  305. dist = pkg_resources.WorkingSet([env.paths['lib']]).find(req)
  306. assert dist.version == version
  307. @pytest.mark.parametrize(
  308. ("unnormalized", "normalized"),
  309. [
  310. ('foo', 'foo'),
  311. ('foo/', 'foo'),
  312. ('foo/bar', 'foo/bar'),
  313. ('foo/bar/', 'foo/bar'),
  314. ],
  315. )
  316. def test_normalize_path_trailing_sep(self, unnormalized, normalized):
  317. """Ensure the trailing slash is cleaned for path comparison.
  318. See pypa/setuptools#1519.
  319. """
  320. result_from_unnormalized = pkg_resources.normalize_path(unnormalized)
  321. result_from_normalized = pkg_resources.normalize_path(normalized)
  322. assert result_from_unnormalized == result_from_normalized
  323. @pytest.mark.skipif(
  324. os.path.normcase('A') != os.path.normcase('a'),
  325. reason='Testing case-insensitive filesystems.',
  326. )
  327. @pytest.mark.parametrize(
  328. ("unnormalized", "normalized"),
  329. [
  330. ('MiXeD/CasE', 'mixed/case'),
  331. ],
  332. )
  333. def test_normalize_path_normcase(self, unnormalized, normalized):
  334. """Ensure mixed case is normalized on case-insensitive filesystems."""
  335. result_from_unnormalized = pkg_resources.normalize_path(unnormalized)
  336. result_from_normalized = pkg_resources.normalize_path(normalized)
  337. assert result_from_unnormalized == result_from_normalized
  338. @pytest.mark.skipif(
  339. os.path.sep != '\\',
  340. reason='Testing systems using backslashes as path separators.',
  341. )
  342. @pytest.mark.parametrize(
  343. ("unnormalized", "expected"),
  344. [
  345. ('forward/slash', 'forward\\slash'),
  346. ('forward/slash/', 'forward\\slash'),
  347. ('backward\\slash\\', 'backward\\slash'),
  348. ],
  349. )
  350. def test_normalize_path_backslash_sep(self, unnormalized, expected):
  351. """Ensure path seps are cleaned on backslash path sep systems."""
  352. result = pkg_resources.normalize_path(unnormalized)
  353. assert result.endswith(expected)
  354. class TestWorkdirRequire:
  355. def fake_site_packages(self, tmp_path, monkeypatch, dist_files):
  356. site_packages = tmp_path / "site-packages"
  357. site_packages.mkdir()
  358. for file, content in self.FILES.items():
  359. path = site_packages / file
  360. path.parent.mkdir(exist_ok=True, parents=True)
  361. path.write_text(inspect.cleandoc(content), encoding="utf-8")
  362. monkeypatch.setattr(sys, "path", [site_packages])
  363. return os.fspath(site_packages)
  364. FILES = {
  365. "pkg1_mod-1.2.3.dist-info/METADATA": """
  366. Metadata-Version: 2.4
  367. Name: pkg1.mod
  368. Version: 1.2.3
  369. """,
  370. "pkg2.mod-0.42.dist-info/METADATA": """
  371. Metadata-Version: 2.1
  372. Name: pkg2.mod
  373. Version: 0.42
  374. """,
  375. "pkg3_mod.egg-info/PKG-INFO": """
  376. Name: pkg3.mod
  377. Version: 1.2.3.4
  378. """,
  379. "pkg4.mod.egg-info/PKG-INFO": """
  380. Name: pkg4.mod
  381. Version: 0.42.1
  382. """,
  383. }
  384. @pytest.mark.parametrize(
  385. ("version", "requirement"),
  386. [
  387. ("1.2.3", "pkg1.mod>=1"),
  388. ("0.42", "pkg2.mod>=0.4"),
  389. ("1.2.3.4", "pkg3.mod<=2"),
  390. ("0.42.1", "pkg4.mod>0.2,<1"),
  391. ],
  392. )
  393. def test_require_non_normalised_name(
  394. self, tmp_path, monkeypatch, version, requirement
  395. ):
  396. # https://github.com/pypa/setuptools/issues/4853
  397. site_packages = self.fake_site_packages(tmp_path, monkeypatch, self.FILES)
  398. ws = pkg_resources.WorkingSet([site_packages])
  399. for req in [requirement, requirement.replace(".", "-")]:
  400. [dist] = ws.require(req)
  401. assert dist.version == version
  402. assert os.path.samefile(
  403. os.path.commonpath([dist.location, site_packages]), site_packages
  404. )