test_config.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. os.environ['USERPROFILE'] = self.tmp_dir
  51. self.rc = os.path.join(self.tmp_dir, '.pypirc')
  52. self.dist = Distribution()
  53. class command(PyPIRCCommand):
  54. def __init__(self, dist):
  55. PyPIRCCommand.__init__(self, dist)
  56. def initialize_options(self):
  57. pass
  58. finalize_options = initialize_options
  59. self._cmd = command
  60. self.old_threshold = set_threshold(WARN)
  61. def tearDown(self):
  62. """Removes the patch."""
  63. set_threshold(self.old_threshold)
  64. super(BasePyPIRCCommandTestCase, self).tearDown()
  65. class PyPIRCCommandTestCase(BasePyPIRCCommandTestCase):
  66. def test_server_registration(self):
  67. # This test makes sure PyPIRCCommand knows how to:
  68. # 1. handle several sections in .pypirc
  69. # 2. handle the old format
  70. # new format
  71. self.write_file(self.rc, PYPIRC)
  72. cmd = self._cmd(self.dist)
  73. config = cmd._read_pypirc()
  74. config = list(sorted(config.items()))
  75. waited = [('password', 'secret'), ('realm', 'pypi'),
  76. ('repository', 'https://upload.pypi.org/legacy/'),
  77. ('server', 'server1'), ('username', 'me')]
  78. self.assertEqual(config, waited)
  79. # old format
  80. self.write_file(self.rc, PYPIRC_OLD)
  81. config = cmd._read_pypirc()
  82. config = list(sorted(config.items()))
  83. waited = [('password', 'secret'), ('realm', 'pypi'),
  84. ('repository', 'https://upload.pypi.org/legacy/'),
  85. ('server', 'server-login'), ('username', 'tarek')]
  86. self.assertEqual(config, waited)
  87. def test_server_empty_registration(self):
  88. cmd = self._cmd(self.dist)
  89. rc = cmd._get_rc_file()
  90. self.assertFalse(os.path.exists(rc))
  91. cmd._store_pypirc('tarek', 'xxx')
  92. self.assertTrue(os.path.exists(rc))
  93. f = open(rc)
  94. try:
  95. content = f.read()
  96. self.assertEqual(content, WANTED)
  97. finally:
  98. f.close()
  99. def test_config_interpolation(self):
  100. # using the % character in .pypirc should not raise an error (#20120)
  101. self.write_file(self.rc, PYPIRC)
  102. cmd = self._cmd(self.dist)
  103. cmd.repository = 'server3'
  104. config = cmd._read_pypirc()
  105. config = list(sorted(config.items()))
  106. waited = [('password', 'yh^%#rest-of-my-password'), ('realm', 'pypi'),
  107. ('repository', 'https://upload.pypi.org/legacy/'),
  108. ('server', 'server3'), ('username', 'cbiggles')]
  109. self.assertEqual(config, waited)
  110. def test_suite():
  111. return unittest.makeSuite(PyPIRCCommandTestCase)
  112. if __name__ == "__main__":
  113. run_unittest(test_suite())