test_config.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. """Tests for distutils.pypirc.pypirc."""
  2. import os
  3. import unittest
  4. from distutils.core import PyPIRCCommand
  5. from distutils.core import Distribution
  6. from distutils.log import set_threshold
  7. from distutils.log import WARN
  8. from distutils.tests import support
  9. from test.support import run_unittest
  10. PYPIRC = """\
  11. [distutils]
  12. index-servers =
  13. server1
  14. server2
  15. server3
  16. [server1]
  17. username:me
  18. password:secret
  19. [server2]
  20. username:meagain
  21. password: secret
  22. realm:acme
  23. repository:http://another.pypi/
  24. [server3]
  25. username:cbiggles
  26. password:yh^%#rest-of-my-password
  27. """
  28. PYPIRC_OLD = """\
  29. [server-login]
  30. username:tarek
  31. password:secret
  32. """
  33. WANTED = """\
  34. [distutils]
  35. index-servers =
  36. pypi
  37. [pypi]
  38. username:tarek
  39. password:xxx
  40. """
  41. class BasePyPIRCCommandTestCase(support.TempdirManager,
  42. support.LoggingSilencer,
  43. support.EnvironGuard,
  44. unittest.TestCase):
  45. def setUp(self):
  46. """Patches the environment."""
  47. super(BasePyPIRCCommandTestCase, self).setUp()
  48. self.tmp_dir = self.mkdtemp()
  49. os.environ['HOME'] = self.tmp_dir
  50. self.rc = os.path.join(self.tmp_dir, '.pypirc')
  51. self.dist = Distribution()
  52. class command(PyPIRCCommand):
  53. def __init__(self, dist):
  54. PyPIRCCommand.__init__(self, dist)
  55. def initialize_options(self):
  56. pass
  57. finalize_options = initialize_options
  58. self._cmd = command
  59. self.old_threshold = set_threshold(WARN)
  60. def tearDown(self):
  61. """Removes the patch."""
  62. set_threshold(self.old_threshold)
  63. super(BasePyPIRCCommandTestCase, self).tearDown()
  64. class PyPIRCCommandTestCase(BasePyPIRCCommandTestCase):
  65. def test_server_registration(self):
  66. # This test makes sure PyPIRCCommand knows how to:
  67. # 1. handle several sections in .pypirc
  68. # 2. handle the old format
  69. # new format
  70. self.write_file(self.rc, PYPIRC)
  71. cmd = self._cmd(self.dist)
  72. config = cmd._read_pypirc()
  73. config = list(sorted(config.items()))
  74. waited = [('password', 'secret'), ('realm', 'pypi'),
  75. ('repository', 'https://upload.pypi.org/legacy/'),
  76. ('server', 'server1'), ('username', 'me')]
  77. self.assertEqual(config, waited)
  78. # old format
  79. self.write_file(self.rc, PYPIRC_OLD)
  80. config = cmd._read_pypirc()
  81. config = list(sorted(config.items()))
  82. waited = [('password', 'secret'), ('realm', 'pypi'),
  83. ('repository', 'https://upload.pypi.org/legacy/'),
  84. ('server', 'server-login'), ('username', 'tarek')]
  85. self.assertEqual(config, waited)
  86. def test_server_empty_registration(self):
  87. cmd = self._cmd(self.dist)
  88. rc = cmd._get_rc_file()
  89. self.assertFalse(os.path.exists(rc))
  90. cmd._store_pypirc('tarek', 'xxx')
  91. self.assertTrue(os.path.exists(rc))
  92. f = open(rc)
  93. try:
  94. content = f.read()
  95. self.assertEqual(content, WANTED)
  96. finally:
  97. f.close()
  98. def test_config_interpolation(self):
  99. # using the % character in .pypirc should not raise an error (#20120)
  100. self.write_file(self.rc, PYPIRC)
  101. cmd = self._cmd(self.dist)
  102. cmd.repository = 'server3'
  103. config = cmd._read_pypirc()
  104. config = list(sorted(config.items()))
  105. waited = [('password', 'yh^%#rest-of-my-password'), ('realm', 'pypi'),
  106. ('repository', 'https://upload.pypi.org/legacy/'),
  107. ('server', 'server3'), ('username', 'cbiggles')]
  108. self.assertEqual(config, waited)
  109. def test_suite():
  110. return unittest.makeSuite(PyPIRCCommandTestCase)
  111. if __name__ == "__main__":
  112. run_unittest(test_suite())