test_setupcfg.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  1. import configparser
  2. import contextlib
  3. import inspect
  4. import re
  5. import sys
  6. from pathlib import Path
  7. from unittest.mock import Mock, patch
  8. import pytest
  9. from packaging.requirements import InvalidRequirement
  10. from setuptools.config.setupcfg import ConfigHandler, Target, read_configuration
  11. from setuptools.dist import Distribution, _Distribution
  12. from setuptools.warnings import SetuptoolsDeprecationWarning
  13. from ..textwrap import DALS
  14. from distutils.errors import DistutilsFileError, DistutilsOptionError
  15. IS_PYPY = '__pypy__' in sys.builtin_module_names
  16. class ErrConfigHandler(ConfigHandler[Target]):
  17. """Erroneous handler. Fails to implement required methods."""
  18. section_prefix = "**err**"
  19. def make_package_dir(name, base_dir, ns=False):
  20. dir_package = base_dir
  21. for dir_name in name.split('/'):
  22. dir_package = dir_package.mkdir(dir_name)
  23. init_file = None
  24. if not ns:
  25. init_file = dir_package.join('__init__.py')
  26. init_file.write('')
  27. return dir_package, init_file
  28. def fake_env(
  29. tmpdir, setup_cfg, setup_py=None, encoding='ascii', package_path='fake_package'
  30. ):
  31. if setup_py is None:
  32. setup_py = 'from setuptools import setup\nsetup()\n'
  33. tmpdir.join('setup.py').write(setup_py)
  34. config = tmpdir.join('setup.cfg')
  35. config.write(setup_cfg.encode(encoding), mode='wb')
  36. package_dir, init_file = make_package_dir(package_path, tmpdir)
  37. init_file.write(
  38. 'VERSION = (1, 2, 3)\n'
  39. '\n'
  40. 'VERSION_MAJOR = 1'
  41. '\n'
  42. 'def get_version():\n'
  43. ' return [3, 4, 5, "dev"]\n'
  44. '\n'
  45. )
  46. return package_dir, config
  47. @contextlib.contextmanager
  48. def get_dist(tmpdir, kwargs_initial=None, parse=True):
  49. kwargs_initial = kwargs_initial or {}
  50. with tmpdir.as_cwd():
  51. dist = Distribution(kwargs_initial)
  52. dist.script_name = 'setup.py'
  53. parse and dist.parse_config_files()
  54. yield dist
  55. def test_parsers_implemented():
  56. with pytest.raises(NotImplementedError):
  57. handler = ErrConfigHandler(None, {}, False, Mock())
  58. handler.parsers
  59. class TestConfigurationReader:
  60. def test_basic(self, tmpdir):
  61. _, config = fake_env(
  62. tmpdir,
  63. '[metadata]\n'
  64. 'version = 10.1.1\n'
  65. 'keywords = one, two\n'
  66. '\n'
  67. '[options]\n'
  68. 'scripts = bin/a.py, bin/b.py\n',
  69. )
  70. config_dict = read_configuration(str(config))
  71. assert config_dict['metadata']['version'] == '10.1.1'
  72. assert config_dict['metadata']['keywords'] == ['one', 'two']
  73. assert config_dict['options']['scripts'] == ['bin/a.py', 'bin/b.py']
  74. def test_no_config(self, tmpdir):
  75. with pytest.raises(DistutilsFileError):
  76. read_configuration(str(tmpdir.join('setup.cfg')))
  77. def test_ignore_errors(self, tmpdir):
  78. _, config = fake_env(
  79. tmpdir,
  80. '[metadata]\nversion = attr: none.VERSION\nkeywords = one, two\n',
  81. )
  82. with pytest.raises(ImportError):
  83. read_configuration(str(config))
  84. config_dict = read_configuration(str(config), ignore_option_errors=True)
  85. assert config_dict['metadata']['keywords'] == ['one', 'two']
  86. assert 'version' not in config_dict['metadata']
  87. config.remove()
  88. class TestMetadata:
  89. def test_basic(self, tmpdir):
  90. fake_env(
  91. tmpdir,
  92. '[metadata]\n'
  93. 'version = 10.1.1\n'
  94. 'description = Some description\n'
  95. 'long_description_content_type = text/something\n'
  96. 'long_description = file: README\n'
  97. 'name = fake_name\n'
  98. 'keywords = one, two\n'
  99. 'provides = package, package.sub\n'
  100. 'license = otherlic\n'
  101. 'download_url = http://test.test.com/test/\n'
  102. 'maintainer_email = test@test.com\n',
  103. )
  104. tmpdir.join('README').write('readme contents\nline2')
  105. meta_initial = {
  106. # This will be used so `otherlic` won't replace it.
  107. 'license': 'BSD 3-Clause License',
  108. }
  109. with get_dist(tmpdir, meta_initial) as dist:
  110. metadata = dist.metadata
  111. assert metadata.version == '10.1.1'
  112. assert metadata.description == 'Some description'
  113. assert metadata.long_description_content_type == 'text/something'
  114. assert metadata.long_description == 'readme contents\nline2'
  115. assert metadata.provides == ['package', 'package.sub']
  116. assert metadata.license == 'BSD 3-Clause License'
  117. assert metadata.name == 'fake_name'
  118. assert metadata.keywords == ['one', 'two']
  119. assert metadata.download_url == 'http://test.test.com/test/'
  120. assert metadata.maintainer_email == 'test@test.com'
  121. def test_license_cfg(self, tmpdir):
  122. fake_env(
  123. tmpdir,
  124. DALS(
  125. """
  126. [metadata]
  127. name=foo
  128. version=0.0.1
  129. license=Apache 2.0
  130. """
  131. ),
  132. )
  133. with get_dist(tmpdir) as dist:
  134. metadata = dist.metadata
  135. assert metadata.name == "foo"
  136. assert metadata.version == "0.0.1"
  137. assert metadata.license == "Apache 2.0"
  138. def test_file_mixed(self, tmpdir):
  139. fake_env(
  140. tmpdir,
  141. '[metadata]\nlong_description = file: README.rst, CHANGES.rst\n\n',
  142. )
  143. tmpdir.join('README.rst').write('readme contents\nline2')
  144. tmpdir.join('CHANGES.rst').write('changelog contents\nand stuff')
  145. with get_dist(tmpdir) as dist:
  146. assert dist.metadata.long_description == (
  147. 'readme contents\nline2\nchangelog contents\nand stuff'
  148. )
  149. def test_file_sandboxed(self, tmpdir):
  150. tmpdir.ensure("README")
  151. project = tmpdir.join('depth1', 'depth2')
  152. project.ensure(dir=True)
  153. fake_env(project, '[metadata]\nlong_description = file: ../../README\n')
  154. with get_dist(project, parse=False) as dist:
  155. with pytest.raises(DistutilsOptionError):
  156. dist.parse_config_files() # file: out of sandbox
  157. def test_aliases(self, tmpdir):
  158. fake_env(
  159. tmpdir,
  160. '[metadata]\n'
  161. 'author_email = test@test.com\n'
  162. 'home_page = http://test.test.com/test/\n'
  163. 'summary = Short summary\n'
  164. 'platform = a, b\n'
  165. 'classifier =\n'
  166. ' Framework :: Django\n'
  167. ' Programming Language :: Python :: 3.5\n',
  168. )
  169. with get_dist(tmpdir) as dist:
  170. metadata = dist.metadata
  171. assert metadata.author_email == 'test@test.com'
  172. assert metadata.url == 'http://test.test.com/test/'
  173. assert metadata.description == 'Short summary'
  174. assert metadata.platforms == ['a', 'b']
  175. assert metadata.classifiers == [
  176. 'Framework :: Django',
  177. 'Programming Language :: Python :: 3.5',
  178. ]
  179. def test_multiline(self, tmpdir):
  180. fake_env(
  181. tmpdir,
  182. '[metadata]\n'
  183. 'name = fake_name\n'
  184. 'keywords =\n'
  185. ' one\n'
  186. ' two\n'
  187. 'classifiers =\n'
  188. ' Framework :: Django\n'
  189. ' Programming Language :: Python :: 3.5\n',
  190. )
  191. with get_dist(tmpdir) as dist:
  192. metadata = dist.metadata
  193. assert metadata.keywords == ['one', 'two']
  194. assert metadata.classifiers == [
  195. 'Framework :: Django',
  196. 'Programming Language :: Python :: 3.5',
  197. ]
  198. def test_dict(self, tmpdir):
  199. fake_env(
  200. tmpdir,
  201. '[metadata]\n'
  202. 'project_urls =\n'
  203. ' Link One = https://example.com/one/\n'
  204. ' Link Two = https://example.com/two/\n',
  205. )
  206. with get_dist(tmpdir) as dist:
  207. metadata = dist.metadata
  208. assert metadata.project_urls == {
  209. 'Link One': 'https://example.com/one/',
  210. 'Link Two': 'https://example.com/two/',
  211. }
  212. def test_version(self, tmpdir):
  213. package_dir, config = fake_env(
  214. tmpdir, '[metadata]\nversion = attr: fake_package.VERSION\n'
  215. )
  216. sub_a = package_dir.mkdir('subpkg_a')
  217. sub_a.join('__init__.py').write('')
  218. sub_a.join('mod.py').write('VERSION = (2016, 11, 26)')
  219. sub_b = package_dir.mkdir('subpkg_b')
  220. sub_b.join('__init__.py').write('')
  221. sub_b.join('mod.py').write(
  222. 'import third_party_module\nVERSION = (2016, 11, 26)'
  223. )
  224. with get_dist(tmpdir) as dist:
  225. assert dist.metadata.version == '1.2.3'
  226. config.write('[metadata]\nversion = attr: fake_package.get_version\n')
  227. with get_dist(tmpdir) as dist:
  228. assert dist.metadata.version == '3.4.5.dev'
  229. config.write('[metadata]\nversion = attr: fake_package.VERSION_MAJOR\n')
  230. with get_dist(tmpdir) as dist:
  231. assert dist.metadata.version == '1'
  232. config.write('[metadata]\nversion = attr: fake_package.subpkg_a.mod.VERSION\n')
  233. with get_dist(tmpdir) as dist:
  234. assert dist.metadata.version == '2016.11.26'
  235. config.write('[metadata]\nversion = attr: fake_package.subpkg_b.mod.VERSION\n')
  236. with get_dist(tmpdir) as dist:
  237. assert dist.metadata.version == '2016.11.26'
  238. def test_version_file(self, tmpdir):
  239. fake_env(tmpdir, '[metadata]\nversion = file: fake_package/version.txt\n')
  240. tmpdir.join('fake_package', 'version.txt').write('1.2.3\n')
  241. with get_dist(tmpdir) as dist:
  242. assert dist.metadata.version == '1.2.3'
  243. tmpdir.join('fake_package', 'version.txt').write('1.2.3\n4.5.6\n')
  244. with pytest.raises(DistutilsOptionError):
  245. with get_dist(tmpdir) as dist:
  246. dist.metadata.version
  247. def test_version_with_package_dir_simple(self, tmpdir):
  248. fake_env(
  249. tmpdir,
  250. '[metadata]\n'
  251. 'version = attr: fake_package_simple.VERSION\n'
  252. '[options]\n'
  253. 'package_dir =\n'
  254. ' = src\n',
  255. package_path='src/fake_package_simple',
  256. )
  257. with get_dist(tmpdir) as dist:
  258. assert dist.metadata.version == '1.2.3'
  259. def test_version_with_package_dir_rename(self, tmpdir):
  260. fake_env(
  261. tmpdir,
  262. '[metadata]\n'
  263. 'version = attr: fake_package_rename.VERSION\n'
  264. '[options]\n'
  265. 'package_dir =\n'
  266. ' fake_package_rename = fake_dir\n',
  267. package_path='fake_dir',
  268. )
  269. with get_dist(tmpdir) as dist:
  270. assert dist.metadata.version == '1.2.3'
  271. def test_version_with_package_dir_complex(self, tmpdir):
  272. fake_env(
  273. tmpdir,
  274. '[metadata]\n'
  275. 'version = attr: fake_package_complex.VERSION\n'
  276. '[options]\n'
  277. 'package_dir =\n'
  278. ' fake_package_complex = src/fake_dir\n',
  279. package_path='src/fake_dir',
  280. )
  281. with get_dist(tmpdir) as dist:
  282. assert dist.metadata.version == '1.2.3'
  283. def test_unknown_meta_item(self, tmpdir):
  284. fake_env(tmpdir, '[metadata]\nname = fake_name\nunknown = some\n')
  285. with get_dist(tmpdir, parse=False) as dist:
  286. dist.parse_config_files() # Skip unknown.
  287. def test_usupported_section(self, tmpdir):
  288. fake_env(tmpdir, '[metadata.some]\nkey = val\n')
  289. with get_dist(tmpdir, parse=False) as dist:
  290. with pytest.raises(DistutilsOptionError):
  291. dist.parse_config_files()
  292. def test_classifiers(self, tmpdir):
  293. expected = set([
  294. 'Framework :: Django',
  295. 'Programming Language :: Python :: 3',
  296. 'Programming Language :: Python :: 3.5',
  297. ])
  298. # From file.
  299. _, config = fake_env(tmpdir, '[metadata]\nclassifiers = file: classifiers\n')
  300. tmpdir.join('classifiers').write(
  301. 'Framework :: Django\n'
  302. 'Programming Language :: Python :: 3\n'
  303. 'Programming Language :: Python :: 3.5\n'
  304. )
  305. with get_dist(tmpdir) as dist:
  306. assert set(dist.metadata.classifiers) == expected
  307. # From list notation
  308. config.write(
  309. '[metadata]\n'
  310. 'classifiers =\n'
  311. ' Framework :: Django\n'
  312. ' Programming Language :: Python :: 3\n'
  313. ' Programming Language :: Python :: 3.5\n'
  314. )
  315. with get_dist(tmpdir) as dist:
  316. assert set(dist.metadata.classifiers) == expected
  317. def test_interpolation(self, tmpdir):
  318. fake_env(tmpdir, '[metadata]\ndescription = %(message)s\n')
  319. with pytest.raises(configparser.InterpolationMissingOptionError):
  320. with get_dist(tmpdir):
  321. pass
  322. def test_non_ascii_1(self, tmpdir):
  323. fake_env(tmpdir, '[metadata]\ndescription = éàïôñ\n', encoding='utf-8')
  324. with get_dist(tmpdir):
  325. pass
  326. def test_non_ascii_3(self, tmpdir):
  327. fake_env(tmpdir, '\n# -*- coding: invalid\n')
  328. with get_dist(tmpdir):
  329. pass
  330. def test_non_ascii_4(self, tmpdir):
  331. fake_env(
  332. tmpdir,
  333. '# -*- coding: utf-8\n[metadata]\ndescription = éàïôñ\n',
  334. encoding='utf-8',
  335. )
  336. with get_dist(tmpdir) as dist:
  337. assert dist.metadata.description == 'éàïôñ'
  338. def test_not_utf8(self, tmpdir):
  339. """
  340. Config files encoded not in UTF-8 will fail
  341. """
  342. fake_env(
  343. tmpdir,
  344. '# vim: set fileencoding=iso-8859-15 :\n[metadata]\ndescription = éàïôñ\n',
  345. encoding='iso-8859-15',
  346. )
  347. with pytest.raises(UnicodeDecodeError):
  348. with get_dist(tmpdir):
  349. pass
  350. @pytest.mark.parametrize(
  351. ("error_msg", "config", "invalid"),
  352. [
  353. (
  354. "Invalid dash-separated key 'author-email' in 'metadata' (setup.cfg)",
  355. DALS(
  356. """
  357. [metadata]
  358. author-email = test@test.com
  359. maintainer_email = foo@foo.com
  360. """
  361. ),
  362. {"author-email": "test@test.com"},
  363. ),
  364. (
  365. "Invalid uppercase key 'Name' in 'metadata' (setup.cfg)",
  366. DALS(
  367. """
  368. [metadata]
  369. Name = foo
  370. description = Some description
  371. """
  372. ),
  373. {"Name": "foo"},
  374. ),
  375. ],
  376. )
  377. def test_invalid_options_previously_deprecated(
  378. self, tmpdir, error_msg, config, invalid
  379. ):
  380. # This test and related methods can be removed when no longer needed.
  381. # Deprecation postponed due to push-back from the community in
  382. # https://github.com/pypa/setuptools/issues/4910
  383. fake_env(tmpdir, config)
  384. with pytest.warns(SetuptoolsDeprecationWarning, match=re.escape(error_msg)):
  385. dist = get_dist(tmpdir).__enter__()
  386. tmpdir.join('setup.cfg').remove()
  387. for field, value in invalid.items():
  388. attr = field.replace("-", "_").lower()
  389. assert getattr(dist.metadata, attr) == value
  390. class TestOptions:
  391. def test_basic(self, tmpdir):
  392. fake_env(
  393. tmpdir,
  394. '[options]\n'
  395. 'zip_safe = True\n'
  396. 'include_package_data = yes\n'
  397. 'package_dir = b=c, =src\n'
  398. 'packages = pack_a, pack_b.subpack\n'
  399. 'namespace_packages = pack1, pack2\n'
  400. 'scripts = bin/one.py, bin/two.py\n'
  401. 'eager_resources = bin/one.py, bin/two.py\n'
  402. 'install_requires = docutils>=0.3; pack ==1.1, ==1.3; hey\n'
  403. 'setup_requires = docutils>=0.3; spack ==1.1, ==1.3; there\n'
  404. 'dependency_links = http://some.com/here/1, '
  405. 'http://some.com/there/2\n'
  406. 'python_requires = >=1.0, !=2.8\n'
  407. 'py_modules = module1, module2\n',
  408. )
  409. deprec = pytest.warns(SetuptoolsDeprecationWarning, match="namespace_packages")
  410. with deprec, get_dist(tmpdir) as dist:
  411. assert dist.zip_safe
  412. assert dist.include_package_data
  413. assert dist.package_dir == {'': 'src', 'b': 'c'}
  414. assert dist.packages == ['pack_a', 'pack_b.subpack']
  415. assert dist.namespace_packages == ['pack1', 'pack2']
  416. assert dist.scripts == ['bin/one.py', 'bin/two.py']
  417. assert dist.dependency_links == ([
  418. 'http://some.com/here/1',
  419. 'http://some.com/there/2',
  420. ])
  421. assert dist.install_requires == ([
  422. 'docutils>=0.3',
  423. 'pack==1.1,==1.3',
  424. 'hey',
  425. ])
  426. assert dist.setup_requires == ([
  427. 'docutils>=0.3',
  428. 'spack ==1.1, ==1.3',
  429. 'there',
  430. ])
  431. assert dist.python_requires == '>=1.0, !=2.8'
  432. assert dist.py_modules == ['module1', 'module2']
  433. def test_multiline(self, tmpdir):
  434. fake_env(
  435. tmpdir,
  436. '[options]\n'
  437. 'package_dir = \n'
  438. ' b=c\n'
  439. ' =src\n'
  440. 'packages = \n'
  441. ' pack_a\n'
  442. ' pack_b.subpack\n'
  443. 'namespace_packages = \n'
  444. ' pack1\n'
  445. ' pack2\n'
  446. 'scripts = \n'
  447. ' bin/one.py\n'
  448. ' bin/two.py\n'
  449. 'eager_resources = \n'
  450. ' bin/one.py\n'
  451. ' bin/two.py\n'
  452. 'install_requires = \n'
  453. ' docutils>=0.3\n'
  454. ' pack ==1.1, ==1.3\n'
  455. ' hey\n'
  456. 'setup_requires = \n'
  457. ' docutils>=0.3\n'
  458. ' spack ==1.1, ==1.3\n'
  459. ' there\n'
  460. 'dependency_links = \n'
  461. ' http://some.com/here/1\n'
  462. ' http://some.com/there/2\n',
  463. )
  464. deprec = pytest.warns(SetuptoolsDeprecationWarning, match="namespace_packages")
  465. with deprec, get_dist(tmpdir) as dist:
  466. assert dist.package_dir == {'': 'src', 'b': 'c'}
  467. assert dist.packages == ['pack_a', 'pack_b.subpack']
  468. assert dist.namespace_packages == ['pack1', 'pack2']
  469. assert dist.scripts == ['bin/one.py', 'bin/two.py']
  470. assert dist.dependency_links == ([
  471. 'http://some.com/here/1',
  472. 'http://some.com/there/2',
  473. ])
  474. assert dist.install_requires == ([
  475. 'docutils>=0.3',
  476. 'pack==1.1,==1.3',
  477. 'hey',
  478. ])
  479. assert dist.setup_requires == ([
  480. 'docutils>=0.3',
  481. 'spack ==1.1, ==1.3',
  482. 'there',
  483. ])
  484. def test_package_dir_fail(self, tmpdir):
  485. fake_env(tmpdir, '[options]\npackage_dir = a b\n')
  486. with get_dist(tmpdir, parse=False) as dist:
  487. with pytest.raises(DistutilsOptionError):
  488. dist.parse_config_files()
  489. def test_package_data(self, tmpdir):
  490. fake_env(
  491. tmpdir,
  492. '[options.package_data]\n'
  493. '* = *.txt, *.rst\n'
  494. 'hello = *.msg\n'
  495. '\n'
  496. '[options.exclude_package_data]\n'
  497. '* = fake1.txt, fake2.txt\n'
  498. 'hello = *.dat\n',
  499. )
  500. with get_dist(tmpdir) as dist:
  501. assert dist.package_data == {
  502. '': ['*.txt', '*.rst'],
  503. 'hello': ['*.msg'],
  504. }
  505. assert dist.exclude_package_data == {
  506. '': ['fake1.txt', 'fake2.txt'],
  507. 'hello': ['*.dat'],
  508. }
  509. def test_packages(self, tmpdir):
  510. fake_env(tmpdir, '[options]\npackages = find:\n')
  511. with get_dist(tmpdir) as dist:
  512. assert dist.packages == ['fake_package']
  513. def test_find_directive(self, tmpdir):
  514. dir_package, config = fake_env(tmpdir, '[options]\npackages = find:\n')
  515. make_package_dir('sub_one', dir_package)
  516. make_package_dir('sub_two', dir_package)
  517. with get_dist(tmpdir) as dist:
  518. assert set(dist.packages) == set([
  519. 'fake_package',
  520. 'fake_package.sub_two',
  521. 'fake_package.sub_one',
  522. ])
  523. config.write(
  524. '[options]\n'
  525. 'packages = find:\n'
  526. '\n'
  527. '[options.packages.find]\n'
  528. 'where = .\n'
  529. 'include =\n'
  530. ' fake_package.sub_one\n'
  531. ' two\n'
  532. )
  533. with get_dist(tmpdir) as dist:
  534. assert dist.packages == ['fake_package.sub_one']
  535. config.write(
  536. '[options]\n'
  537. 'packages = find:\n'
  538. '\n'
  539. '[options.packages.find]\n'
  540. 'exclude =\n'
  541. ' fake_package.sub_one\n'
  542. )
  543. with get_dist(tmpdir) as dist:
  544. assert set(dist.packages) == set(['fake_package', 'fake_package.sub_two'])
  545. def test_find_namespace_directive(self, tmpdir):
  546. dir_package, config = fake_env(
  547. tmpdir, '[options]\npackages = find_namespace:\n'
  548. )
  549. make_package_dir('sub_one', dir_package)
  550. make_package_dir('sub_two', dir_package, ns=True)
  551. with get_dist(tmpdir) as dist:
  552. assert set(dist.packages) == {
  553. 'fake_package',
  554. 'fake_package.sub_two',
  555. 'fake_package.sub_one',
  556. }
  557. config.write(
  558. '[options]\n'
  559. 'packages = find_namespace:\n'
  560. '\n'
  561. '[options.packages.find]\n'
  562. 'where = .\n'
  563. 'include =\n'
  564. ' fake_package.sub_one\n'
  565. ' two\n'
  566. )
  567. with get_dist(tmpdir) as dist:
  568. assert dist.packages == ['fake_package.sub_one']
  569. config.write(
  570. '[options]\n'
  571. 'packages = find_namespace:\n'
  572. '\n'
  573. '[options.packages.find]\n'
  574. 'exclude =\n'
  575. ' fake_package.sub_one\n'
  576. )
  577. with get_dist(tmpdir) as dist:
  578. assert set(dist.packages) == {'fake_package', 'fake_package.sub_two'}
  579. def test_extras_require(self, tmpdir):
  580. fake_env(
  581. tmpdir,
  582. '[options.extras_require]\n'
  583. 'pdf = ReportLab>=1.2; RXP\n'
  584. 'rest = \n'
  585. ' docutils>=0.3\n'
  586. ' pack ==1.1, ==1.3\n',
  587. )
  588. with get_dist(tmpdir) as dist:
  589. assert dist.extras_require == {
  590. 'pdf': ['ReportLab>=1.2', 'RXP'],
  591. 'rest': ['docutils>=0.3', 'pack==1.1,==1.3'],
  592. }
  593. assert set(dist.metadata.provides_extras) == {'pdf', 'rest'}
  594. @pytest.mark.parametrize(
  595. "config",
  596. [
  597. "[options.extras_require]\nfoo = bar;python_version<'3'",
  598. "[options.extras_require]\nfoo = bar;os_name=='linux'",
  599. "[options.extras_require]\nfoo = bar;python_version<'3'\n",
  600. "[options.extras_require]\nfoo = bar;os_name=='linux'\n",
  601. "[options]\ninstall_requires = bar;python_version<'3'",
  602. "[options]\ninstall_requires = bar;os_name=='linux'",
  603. "[options]\ninstall_requires = bar;python_version<'3'\n",
  604. "[options]\ninstall_requires = bar;os_name=='linux'\n",
  605. ],
  606. )
  607. @pytest.mark.xfail(IS_PYPY, reason="Exceptions missing on PyPy")
  608. # TODO: investigate PyPy problem
  609. def test_raises_accidental_env_marker_misconfig(self, config, tmpdir):
  610. fake_env(tmpdir, config)
  611. match = (
  612. r"One of the parsed requirements in `(install_requires|extras_require.+)` "
  613. "looks like a valid environment marker.*"
  614. )
  615. with pytest.raises(InvalidRequirement, match=match):
  616. with get_dist(tmpdir) as _:
  617. pass
  618. @pytest.mark.parametrize(
  619. "config",
  620. [
  621. "[options.extras_require]\nfoo = bar;python_version<3",
  622. "[options.extras_require]\nfoo = bar;python_version<3\n",
  623. "[options]\ninstall_requires = bar;python_version<3",
  624. "[options]\ninstall_requires = bar;python_version<3\n",
  625. ],
  626. )
  627. @pytest.mark.xfail(IS_PYPY, reason="Warnings missing on PyPy (minor issue)")
  628. # TODO: investigate PyPy problem
  629. def test_warn_accidental_env_marker_misconfig(self, config, tmpdir):
  630. fake_env(tmpdir, config)
  631. match = (
  632. r"One of the parsed requirements in `(install_requires|extras_require.+)` "
  633. "looks like a valid environment marker.*"
  634. )
  635. with pytest.warns(SetuptoolsDeprecationWarning, match=match):
  636. with get_dist(tmpdir) as _:
  637. pass
  638. @pytest.mark.parametrize(
  639. "config",
  640. [
  641. "[options.extras_require]\nfoo =\n bar;python_version<'3'",
  642. "[options.extras_require]\nfoo = bar;baz\nboo = xxx;yyy",
  643. "[options.extras_require]\nfoo =\n bar;python_version<'3'\n",
  644. "[options.extras_require]\nfoo = bar;baz\nboo = xxx;yyy\n",
  645. "[options.extras_require]\nfoo =\n bar\n python_version<3\n",
  646. "[options]\ninstall_requires =\n bar;python_version<'3'",
  647. "[options]\ninstall_requires = bar;baz\nboo = xxx;yyy",
  648. "[options]\ninstall_requires =\n bar;python_version<'3'\n",
  649. "[options]\ninstall_requires = bar;baz\nboo = xxx;yyy\n",
  650. "[options]\ninstall_requires =\n bar\n python_version<3\n",
  651. ],
  652. )
  653. @pytest.mark.filterwarnings("error::setuptools.SetuptoolsDeprecationWarning")
  654. def test_nowarn_accidental_env_marker_misconfig(self, config, tmpdir, recwarn):
  655. fake_env(tmpdir, config)
  656. num_warnings = len(recwarn)
  657. with get_dist(tmpdir) as _:
  658. pass
  659. # The examples are valid, no warnings shown
  660. assert len(recwarn) == num_warnings
  661. def test_dash_preserved_extras_require(self, tmpdir):
  662. fake_env(tmpdir, '[options.extras_require]\nfoo-a = foo\nfoo_b = test\n')
  663. with get_dist(tmpdir) as dist:
  664. assert dist.extras_require == {'foo-a': ['foo'], 'foo_b': ['test']}
  665. def test_entry_points(self, tmpdir):
  666. _, config = fake_env(
  667. tmpdir,
  668. '[options.entry_points]\n'
  669. 'group1 = point1 = pack.module:func, '
  670. '.point2 = pack.module2:func_rest [rest]\n'
  671. 'group2 = point3 = pack.module:func2\n',
  672. )
  673. with get_dist(tmpdir) as dist:
  674. assert dist.entry_points == {
  675. 'group1': [
  676. 'point1 = pack.module:func',
  677. '.point2 = pack.module2:func_rest [rest]',
  678. ],
  679. 'group2': ['point3 = pack.module:func2'],
  680. }
  681. expected = (
  682. '[blogtool.parsers]\n'
  683. '.rst = some.nested.module:SomeClass.some_classmethod[reST]\n'
  684. )
  685. tmpdir.join('entry_points').write(expected)
  686. # From file.
  687. config.write('[options]\nentry_points = file: entry_points\n')
  688. with get_dist(tmpdir) as dist:
  689. assert dist.entry_points == expected
  690. def test_case_sensitive_entry_points(self, tmpdir):
  691. fake_env(
  692. tmpdir,
  693. '[options.entry_points]\n'
  694. 'GROUP1 = point1 = pack.module:func, '
  695. '.point2 = pack.module2:func_rest [rest]\n'
  696. 'group2 = point3 = pack.module:func2\n',
  697. )
  698. with get_dist(tmpdir) as dist:
  699. assert dist.entry_points == {
  700. 'GROUP1': [
  701. 'point1 = pack.module:func',
  702. '.point2 = pack.module2:func_rest [rest]',
  703. ],
  704. 'group2': ['point3 = pack.module:func2'],
  705. }
  706. def test_data_files(self, tmpdir):
  707. fake_env(
  708. tmpdir,
  709. '[options.data_files]\n'
  710. 'cfg =\n'
  711. ' a/b.conf\n'
  712. ' c/d.conf\n'
  713. 'data = e/f.dat, g/h.dat\n',
  714. )
  715. with get_dist(tmpdir) as dist:
  716. expected = [
  717. ('cfg', ['a/b.conf', 'c/d.conf']),
  718. ('data', ['e/f.dat', 'g/h.dat']),
  719. ]
  720. assert sorted(dist.data_files) == sorted(expected)
  721. def test_data_files_globby(self, tmpdir):
  722. fake_env(
  723. tmpdir,
  724. '[options.data_files]\n'
  725. 'cfg =\n'
  726. ' a/b.conf\n'
  727. ' c/d.conf\n'
  728. 'data = *.dat\n'
  729. 'icons = \n'
  730. ' *.ico\n'
  731. 'audio = \n'
  732. ' *.wav\n'
  733. ' sounds.db\n',
  734. )
  735. # Create dummy files for glob()'s sake:
  736. tmpdir.join('a.dat').write('')
  737. tmpdir.join('b.dat').write('')
  738. tmpdir.join('c.dat').write('')
  739. tmpdir.join('a.ico').write('')
  740. tmpdir.join('b.ico').write('')
  741. tmpdir.join('c.ico').write('')
  742. tmpdir.join('beep.wav').write('')
  743. tmpdir.join('boop.wav').write('')
  744. tmpdir.join('sounds.db').write('')
  745. with get_dist(tmpdir) as dist:
  746. expected = [
  747. ('cfg', ['a/b.conf', 'c/d.conf']),
  748. ('data', ['a.dat', 'b.dat', 'c.dat']),
  749. ('icons', ['a.ico', 'b.ico', 'c.ico']),
  750. ('audio', ['beep.wav', 'boop.wav', 'sounds.db']),
  751. ]
  752. assert sorted(dist.data_files) == sorted(expected)
  753. def test_python_requires_simple(self, tmpdir):
  754. fake_env(
  755. tmpdir,
  756. DALS(
  757. """
  758. [options]
  759. python_requires=>=2.7
  760. """
  761. ),
  762. )
  763. with get_dist(tmpdir) as dist:
  764. dist.parse_config_files()
  765. def test_python_requires_compound(self, tmpdir):
  766. fake_env(
  767. tmpdir,
  768. DALS(
  769. """
  770. [options]
  771. python_requires=>=2.7,!=3.0.*
  772. """
  773. ),
  774. )
  775. with get_dist(tmpdir) as dist:
  776. dist.parse_config_files()
  777. def test_python_requires_invalid(self, tmpdir):
  778. fake_env(
  779. tmpdir,
  780. DALS(
  781. """
  782. [options]
  783. python_requires=invalid
  784. """
  785. ),
  786. )
  787. with pytest.raises(Exception):
  788. with get_dist(tmpdir) as dist:
  789. dist.parse_config_files()
  790. def test_cmdclass(self, tmpdir):
  791. module_path = Path(tmpdir, "src/custom_build.py") # auto discovery for src
  792. module_path.parent.mkdir(parents=True, exist_ok=True)
  793. module_path.write_text(
  794. "from distutils.core import Command\nclass CustomCmd(Command): pass\n",
  795. encoding="utf-8",
  796. )
  797. setup_cfg = """
  798. [options]
  799. cmdclass =
  800. customcmd = custom_build.CustomCmd
  801. """
  802. fake_env(tmpdir, inspect.cleandoc(setup_cfg))
  803. with get_dist(tmpdir) as dist:
  804. cmdclass = dist.cmdclass['customcmd']
  805. assert cmdclass.__name__ == "CustomCmd"
  806. assert cmdclass.__module__ == "custom_build"
  807. assert module_path.samefile(inspect.getfile(cmdclass))
  808. def test_requirements_file(self, tmpdir):
  809. fake_env(
  810. tmpdir,
  811. DALS(
  812. """
  813. [options]
  814. install_requires = file:requirements.txt
  815. [options.extras_require]
  816. colors = file:requirements-extra.txt
  817. """
  818. ),
  819. )
  820. tmpdir.join('requirements.txt').write('\ndocutils>=0.3\n\n')
  821. tmpdir.join('requirements-extra.txt').write('colorama')
  822. with get_dist(tmpdir) as dist:
  823. assert dist.install_requires == ['docutils>=0.3']
  824. assert dist.extras_require == {'colors': ['colorama']}
  825. saved_dist_init = _Distribution.__init__
  826. class TestExternalSetters:
  827. # During creation of the setuptools Distribution() object, we call
  828. # the init of the parent distutils Distribution object via
  829. # _Distribution.__init__ ().
  830. #
  831. # It's possible distutils calls out to various keyword
  832. # implementations (i.e. distutils.setup_keywords entry points)
  833. # that may set a range of variables.
  834. #
  835. # This wraps distutil's Distribution.__init__ and simulates
  836. # pbr or something else setting these values.
  837. def _fake_distribution_init(self, dist, attrs):
  838. saved_dist_init(dist, attrs)
  839. # see self._DISTUTILS_UNSUPPORTED_METADATA
  840. dist.metadata.long_description_content_type = 'text/something'
  841. # Test overwrite setup() args
  842. dist.metadata.project_urls = {
  843. 'Link One': 'https://example.com/one/',
  844. 'Link Two': 'https://example.com/two/',
  845. }
  846. @patch.object(_Distribution, '__init__', autospec=True)
  847. def test_external_setters(self, mock_parent_init, tmpdir):
  848. mock_parent_init.side_effect = self._fake_distribution_init
  849. dist = Distribution(attrs={'project_urls': {'will_be': 'ignored'}})
  850. assert dist.metadata.long_description_content_type == 'text/something'
  851. assert dist.metadata.project_urls == {
  852. 'Link One': 'https://example.com/one/',
  853. 'Link Two': 'https://example.com/two/',
  854. }