test_dist.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. """Tests for distutils.dist."""
  2. import email
  3. import email.generator
  4. import email.policy
  5. import functools
  6. import io
  7. import os
  8. import sys
  9. import textwrap
  10. import unittest.mock as mock
  11. import warnings
  12. from distutils.cmd import Command
  13. from distutils.dist import Distribution, fix_help_options
  14. from distutils.tests import support
  15. from typing import ClassVar
  16. import jaraco.path
  17. import pytest
  18. pydistutils_cfg = '.' * (os.name == 'posix') + 'pydistutils.cfg'
  19. class test_dist(Command):
  20. """Sample distutils extension command."""
  21. user_options: ClassVar[list[tuple[str, str, str]]] = [
  22. ("sample-option=", "S", "help text"),
  23. ]
  24. def initialize_options(self):
  25. self.sample_option = None
  26. class TestDistribution(Distribution):
  27. """Distribution subclasses that avoids the default search for
  28. configuration files.
  29. The ._config_files attribute must be set before
  30. .parse_config_files() is called.
  31. """
  32. def find_config_files(self):
  33. return self._config_files
  34. @pytest.fixture
  35. def clear_argv():
  36. del sys.argv[1:]
  37. @support.combine_markers
  38. @pytest.mark.usefixtures('save_env')
  39. @pytest.mark.usefixtures('save_argv')
  40. class TestDistributionBehavior(support.TempdirManager):
  41. def create_distribution(self, configfiles=()):
  42. d = TestDistribution()
  43. d._config_files = configfiles
  44. d.parse_config_files()
  45. d.parse_command_line()
  46. return d
  47. def test_command_packages_unspecified(self, clear_argv):
  48. sys.argv.append("build")
  49. d = self.create_distribution()
  50. assert d.get_command_packages() == ["distutils.command"]
  51. def test_command_packages_cmdline(self, clear_argv):
  52. from distutils.tests.test_dist import test_dist
  53. sys.argv.extend([
  54. "--command-packages",
  55. "foo.bar,distutils.tests",
  56. "test_dist",
  57. "-Ssometext",
  58. ])
  59. d = self.create_distribution()
  60. # let's actually try to load our test command:
  61. assert d.get_command_packages() == [
  62. "distutils.command",
  63. "foo.bar",
  64. "distutils.tests",
  65. ]
  66. cmd = d.get_command_obj("test_dist")
  67. assert isinstance(cmd, test_dist)
  68. assert cmd.sample_option == "sometext"
  69. @pytest.mark.skipif(
  70. 'distutils' not in Distribution.parse_config_files.__module__,
  71. reason='Cannot test when virtualenv has monkey-patched Distribution',
  72. )
  73. def test_venv_install_options(self, tmp_path, clear_argv):
  74. sys.argv.append("install")
  75. file = str(tmp_path / 'file')
  76. fakepath = '/somedir'
  77. jaraco.path.build({
  78. file: f"""
  79. [install]
  80. install-base = {fakepath}
  81. install-platbase = {fakepath}
  82. install-lib = {fakepath}
  83. install-platlib = {fakepath}
  84. install-purelib = {fakepath}
  85. install-headers = {fakepath}
  86. install-scripts = {fakepath}
  87. install-data = {fakepath}
  88. prefix = {fakepath}
  89. exec-prefix = {fakepath}
  90. home = {fakepath}
  91. user = {fakepath}
  92. root = {fakepath}
  93. """,
  94. })
  95. # Base case: Not in a Virtual Environment
  96. with mock.patch.multiple(sys, prefix='/a', base_prefix='/a'):
  97. d = self.create_distribution([file])
  98. option_tuple = (file, fakepath)
  99. result_dict = {
  100. 'install_base': option_tuple,
  101. 'install_platbase': option_tuple,
  102. 'install_lib': option_tuple,
  103. 'install_platlib': option_tuple,
  104. 'install_purelib': option_tuple,
  105. 'install_headers': option_tuple,
  106. 'install_scripts': option_tuple,
  107. 'install_data': option_tuple,
  108. 'prefix': option_tuple,
  109. 'exec_prefix': option_tuple,
  110. 'home': option_tuple,
  111. 'user': option_tuple,
  112. 'root': option_tuple,
  113. }
  114. assert sorted(d.command_options.get('install').keys()) == sorted(
  115. result_dict.keys()
  116. )
  117. for key, value in d.command_options.get('install').items():
  118. assert value == result_dict[key]
  119. # Test case: In a Virtual Environment
  120. with mock.patch.multiple(sys, prefix='/a', base_prefix='/b'):
  121. d = self.create_distribution([file])
  122. for key in result_dict.keys():
  123. assert key not in d.command_options.get('install', {})
  124. def test_command_packages_configfile(self, tmp_path, clear_argv):
  125. sys.argv.append("build")
  126. file = str(tmp_path / "file")
  127. jaraco.path.build({
  128. file: """
  129. [global]
  130. command_packages = foo.bar, splat
  131. """,
  132. })
  133. d = self.create_distribution([file])
  134. assert d.get_command_packages() == ["distutils.command", "foo.bar", "splat"]
  135. # ensure command line overrides config:
  136. sys.argv[1:] = ["--command-packages", "spork", "build"]
  137. d = self.create_distribution([file])
  138. assert d.get_command_packages() == ["distutils.command", "spork"]
  139. # Setting --command-packages to '' should cause the default to
  140. # be used even if a config file specified something else:
  141. sys.argv[1:] = ["--command-packages", "", "build"]
  142. d = self.create_distribution([file])
  143. assert d.get_command_packages() == ["distutils.command"]
  144. def test_empty_options(self, request):
  145. # an empty options dictionary should not stay in the
  146. # list of attributes
  147. # catching warnings
  148. warns = []
  149. def _warn(msg):
  150. warns.append(msg)
  151. request.addfinalizer(
  152. functools.partial(setattr, warnings, 'warn', warnings.warn)
  153. )
  154. warnings.warn = _warn
  155. dist = Distribution(
  156. attrs={
  157. 'author': 'xxx',
  158. 'name': 'xxx',
  159. 'version': 'xxx',
  160. 'url': 'xxxx',
  161. 'options': {},
  162. }
  163. )
  164. assert len(warns) == 0
  165. assert 'options' not in dir(dist)
  166. def test_finalize_options(self):
  167. attrs = {'keywords': 'one,two', 'platforms': 'one,two'}
  168. dist = Distribution(attrs=attrs)
  169. dist.finalize_options()
  170. # finalize_option splits platforms and keywords
  171. assert dist.metadata.platforms == ['one', 'two']
  172. assert dist.metadata.keywords == ['one', 'two']
  173. attrs = {'keywords': 'foo bar', 'platforms': 'foo bar'}
  174. dist = Distribution(attrs=attrs)
  175. dist.finalize_options()
  176. assert dist.metadata.platforms == ['foo bar']
  177. assert dist.metadata.keywords == ['foo bar']
  178. def test_get_command_packages(self):
  179. dist = Distribution()
  180. assert dist.command_packages is None
  181. cmds = dist.get_command_packages()
  182. assert cmds == ['distutils.command']
  183. assert dist.command_packages == ['distutils.command']
  184. dist.command_packages = 'one,two'
  185. cmds = dist.get_command_packages()
  186. assert cmds == ['distutils.command', 'one', 'two']
  187. def test_announce(self):
  188. # make sure the level is known
  189. dist = Distribution()
  190. with pytest.raises(TypeError):
  191. dist.announce('ok', level='ok2')
  192. def test_find_config_files_disable(self, temp_home):
  193. # Ticket #1180: Allow user to disable their home config file.
  194. jaraco.path.build({pydistutils_cfg: '[distutils]\n'}, temp_home)
  195. d = Distribution()
  196. all_files = d.find_config_files()
  197. d = Distribution(attrs={'script_args': ['--no-user-cfg']})
  198. files = d.find_config_files()
  199. # make sure --no-user-cfg disables the user cfg file
  200. assert len(all_files) - 1 == len(files)
  201. def test_script_args_list_coercion(self):
  202. d = Distribution(attrs={'script_args': ('build', '--no-user-cfg')})
  203. # make sure script_args is a list even if it started as a different iterable
  204. assert d.script_args == ['build', '--no-user-cfg']
  205. @pytest.mark.skipif(
  206. 'platform.system() == "Windows"',
  207. reason='Windows does not honor chmod 000',
  208. )
  209. def test_find_config_files_permission_error(self, fake_home):
  210. """
  211. Finding config files should not fail when directory is inaccessible.
  212. """
  213. fake_home.joinpath(pydistutils_cfg).write_text('', encoding='utf-8')
  214. fake_home.chmod(0o000)
  215. Distribution().find_config_files()
  216. @pytest.mark.usefixtures('save_env')
  217. @pytest.mark.usefixtures('save_argv')
  218. class TestMetadata(support.TempdirManager):
  219. def format_metadata(self, dist):
  220. sio = io.StringIO()
  221. dist.metadata.write_pkg_file(sio)
  222. return sio.getvalue()
  223. def test_simple_metadata(self):
  224. attrs = {"name": "package", "version": "1.0"}
  225. dist = Distribution(attrs)
  226. meta = self.format_metadata(dist)
  227. assert "Metadata-Version: 1.0" in meta
  228. assert "provides:" not in meta.lower()
  229. assert "requires:" not in meta.lower()
  230. assert "obsoletes:" not in meta.lower()
  231. def test_provides(self):
  232. attrs = {
  233. "name": "package",
  234. "version": "1.0",
  235. "provides": ["package", "package.sub"],
  236. }
  237. dist = Distribution(attrs)
  238. assert dist.metadata.get_provides() == ["package", "package.sub"]
  239. assert dist.get_provides() == ["package", "package.sub"]
  240. meta = self.format_metadata(dist)
  241. assert "Metadata-Version: 1.1" in meta
  242. assert "requires:" not in meta.lower()
  243. assert "obsoletes:" not in meta.lower()
  244. def test_provides_illegal(self):
  245. with pytest.raises(ValueError):
  246. Distribution(
  247. {"name": "package", "version": "1.0", "provides": ["my.pkg (splat)"]},
  248. )
  249. def test_requires(self):
  250. attrs = {
  251. "name": "package",
  252. "version": "1.0",
  253. "requires": ["other", "another (==1.0)"],
  254. }
  255. dist = Distribution(attrs)
  256. assert dist.metadata.get_requires() == ["other", "another (==1.0)"]
  257. assert dist.get_requires() == ["other", "another (==1.0)"]
  258. meta = self.format_metadata(dist)
  259. assert "Metadata-Version: 1.1" in meta
  260. assert "provides:" not in meta.lower()
  261. assert "Requires: other" in meta
  262. assert "Requires: another (==1.0)" in meta
  263. assert "obsoletes:" not in meta.lower()
  264. def test_requires_illegal(self):
  265. with pytest.raises(ValueError):
  266. Distribution(
  267. {"name": "package", "version": "1.0", "requires": ["my.pkg (splat)"]},
  268. )
  269. def test_requires_to_list(self):
  270. attrs = {"name": "package", "requires": iter(["other"])}
  271. dist = Distribution(attrs)
  272. assert isinstance(dist.metadata.requires, list)
  273. def test_obsoletes(self):
  274. attrs = {
  275. "name": "package",
  276. "version": "1.0",
  277. "obsoletes": ["other", "another (<1.0)"],
  278. }
  279. dist = Distribution(attrs)
  280. assert dist.metadata.get_obsoletes() == ["other", "another (<1.0)"]
  281. assert dist.get_obsoletes() == ["other", "another (<1.0)"]
  282. meta = self.format_metadata(dist)
  283. assert "Metadata-Version: 1.1" in meta
  284. assert "provides:" not in meta.lower()
  285. assert "requires:" not in meta.lower()
  286. assert "Obsoletes: other" in meta
  287. assert "Obsoletes: another (<1.0)" in meta
  288. def test_obsoletes_illegal(self):
  289. with pytest.raises(ValueError):
  290. Distribution(
  291. {"name": "package", "version": "1.0", "obsoletes": ["my.pkg (splat)"]},
  292. )
  293. def test_obsoletes_to_list(self):
  294. attrs = {"name": "package", "obsoletes": iter(["other"])}
  295. dist = Distribution(attrs)
  296. assert isinstance(dist.metadata.obsoletes, list)
  297. def test_classifier(self):
  298. attrs = {
  299. 'name': 'Boa',
  300. 'version': '3.0',
  301. 'classifiers': ['Programming Language :: Python :: 3'],
  302. }
  303. dist = Distribution(attrs)
  304. assert dist.get_classifiers() == ['Programming Language :: Python :: 3']
  305. meta = self.format_metadata(dist)
  306. assert 'Metadata-Version: 1.1' in meta
  307. def test_classifier_invalid_type(self, caplog):
  308. attrs = {
  309. 'name': 'Boa',
  310. 'version': '3.0',
  311. 'classifiers': ('Programming Language :: Python :: 3',),
  312. }
  313. d = Distribution(attrs)
  314. # should have warning about passing a non-list
  315. assert 'should be a list' in caplog.messages[0]
  316. # should be converted to a list
  317. assert isinstance(d.metadata.classifiers, list)
  318. assert d.metadata.classifiers == list(attrs['classifiers'])
  319. def test_keywords(self):
  320. attrs = {
  321. 'name': 'Monty',
  322. 'version': '1.0',
  323. 'keywords': ['spam', 'eggs', 'life of brian'],
  324. }
  325. dist = Distribution(attrs)
  326. assert dist.get_keywords() == ['spam', 'eggs', 'life of brian']
  327. def test_keywords_invalid_type(self, caplog):
  328. attrs = {
  329. 'name': 'Monty',
  330. 'version': '1.0',
  331. 'keywords': ('spam', 'eggs', 'life of brian'),
  332. }
  333. d = Distribution(attrs)
  334. # should have warning about passing a non-list
  335. assert 'should be a list' in caplog.messages[0]
  336. # should be converted to a list
  337. assert isinstance(d.metadata.keywords, list)
  338. assert d.metadata.keywords == list(attrs['keywords'])
  339. def test_platforms(self):
  340. attrs = {
  341. 'name': 'Monty',
  342. 'version': '1.0',
  343. 'platforms': ['GNU/Linux', 'Some Evil Platform'],
  344. }
  345. dist = Distribution(attrs)
  346. assert dist.get_platforms() == ['GNU/Linux', 'Some Evil Platform']
  347. def test_platforms_invalid_types(self, caplog):
  348. attrs = {
  349. 'name': 'Monty',
  350. 'version': '1.0',
  351. 'platforms': ('GNU/Linux', 'Some Evil Platform'),
  352. }
  353. d = Distribution(attrs)
  354. # should have warning about passing a non-list
  355. assert 'should be a list' in caplog.messages[0]
  356. # should be converted to a list
  357. assert isinstance(d.metadata.platforms, list)
  358. assert d.metadata.platforms == list(attrs['platforms'])
  359. def test_download_url(self):
  360. attrs = {
  361. 'name': 'Boa',
  362. 'version': '3.0',
  363. 'download_url': 'http://example.org/boa',
  364. }
  365. dist = Distribution(attrs)
  366. meta = self.format_metadata(dist)
  367. assert 'Metadata-Version: 1.1' in meta
  368. def test_long_description(self):
  369. long_desc = textwrap.dedent(
  370. """\
  371. example::
  372. We start here
  373. and continue here
  374. and end here."""
  375. )
  376. attrs = {"name": "package", "version": "1.0", "long_description": long_desc}
  377. dist = Distribution(attrs)
  378. meta = self.format_metadata(dist)
  379. meta = meta.replace('\n' + 8 * ' ', '\n')
  380. assert long_desc in meta
  381. def test_custom_pydistutils(self, temp_home):
  382. """
  383. pydistutils.cfg is found
  384. """
  385. jaraco.path.build({pydistutils_cfg: ''}, temp_home)
  386. config_path = temp_home / pydistutils_cfg
  387. assert str(config_path) in Distribution().find_config_files()
  388. def test_extra_pydistutils(self, monkeypatch, tmp_path):
  389. jaraco.path.build({'overrides.cfg': ''}, tmp_path)
  390. filename = tmp_path / 'overrides.cfg'
  391. monkeypatch.setenv('DIST_EXTRA_CONFIG', str(filename))
  392. assert str(filename) in Distribution().find_config_files()
  393. def test_fix_help_options(self):
  394. help_tuples = [('a', 'b', 'c', 'd'), (1, 2, 3, 4)]
  395. fancy_options = fix_help_options(help_tuples)
  396. assert fancy_options[0] == ('a', 'b', 'c')
  397. assert fancy_options[1] == (1, 2, 3)
  398. def test_show_help(self, request, capsys):
  399. # smoke test, just makes sure some help is displayed
  400. dist = Distribution()
  401. sys.argv = []
  402. dist.help = True
  403. dist.script_name = 'setup.py'
  404. dist.parse_command_line()
  405. output = [
  406. line for line in capsys.readouterr().out.split('\n') if line.strip() != ''
  407. ]
  408. assert output
  409. def test_read_metadata(self):
  410. attrs = {
  411. "name": "package",
  412. "version": "1.0",
  413. "long_description": "desc",
  414. "description": "xxx",
  415. "download_url": "http://example.com",
  416. "keywords": ['one', 'two'],
  417. "requires": ['foo'],
  418. }
  419. dist = Distribution(attrs)
  420. metadata = dist.metadata
  421. # write it then reloads it
  422. PKG_INFO = io.StringIO()
  423. metadata.write_pkg_file(PKG_INFO)
  424. PKG_INFO.seek(0)
  425. metadata.read_pkg_file(PKG_INFO)
  426. assert metadata.name == "package"
  427. assert metadata.version == "1.0"
  428. assert metadata.description == "xxx"
  429. assert metadata.download_url == 'http://example.com'
  430. assert metadata.keywords == ['one', 'two']
  431. assert metadata.platforms is None
  432. assert metadata.obsoletes is None
  433. assert metadata.requires == ['foo']
  434. def test_round_trip_through_email_generator(self):
  435. """
  436. In pypa/setuptools#4033, it was shown that once PKG-INFO is
  437. re-generated using ``email.generator.Generator``, some control
  438. characters might cause problems.
  439. """
  440. # Given a PKG-INFO file ...
  441. attrs = {
  442. "name": "package",
  443. "version": "1.0",
  444. "long_description": "hello\x0b\nworld\n",
  445. }
  446. dist = Distribution(attrs)
  447. metadata = dist.metadata
  448. with io.StringIO() as buffer:
  449. metadata.write_pkg_file(buffer)
  450. msg = buffer.getvalue()
  451. # ... when it is read and re-written using stdlib's email library,
  452. orig = email.message_from_string(msg)
  453. policy = email.policy.EmailPolicy(
  454. utf8=True,
  455. mangle_from_=False,
  456. max_line_length=0,
  457. )
  458. with io.StringIO() as buffer:
  459. email.generator.Generator(buffer, policy=policy).flatten(orig)
  460. buffer.seek(0)
  461. regen = email.message_from_file(buffer)
  462. # ... then it should be the same as the original
  463. # (except for the specific line break characters)
  464. orig_desc = set(orig["Description"].splitlines())
  465. regen_desc = set(regen["Description"].splitlines())
  466. assert regen_desc == orig_desc