test_config.py 3.1 KB

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