/Lib/distutils/tests/test_register.py

http://unladen-swallow.googlecode.com/ · Python · 109 lines · 64 code · 20 blank · 25 comment · 3 complexity · af949e45fb2d811f7b3abe8b07d57606 MD5 · raw file

  1. """Tests for distutils.command.register."""
  2. import sys
  3. import os
  4. import unittest
  5. from distutils.command.register import register
  6. from distutils.core import Distribution
  7. from distutils.tests import support
  8. from distutils.tests.test_config import PYPIRC, PyPIRCCommandTestCase
  9. class RawInputs(object):
  10. """Fakes user inputs."""
  11. def __init__(self, *answers):
  12. self.answers = answers
  13. self.index = 0
  14. def __call__(self, prompt=''):
  15. try:
  16. return self.answers[self.index]
  17. finally:
  18. self.index += 1
  19. WANTED_PYPIRC = """\
  20. [distutils]
  21. index-servers =
  22. pypi
  23. [pypi]
  24. username:tarek
  25. password:xxx
  26. """
  27. class registerTestCase(PyPIRCCommandTestCase):
  28. def test_create_pypirc(self):
  29. # this test makes sure a .pypirc file
  30. # is created when requested.
  31. # let's create a fake distribution
  32. # and a register instance
  33. dist = Distribution()
  34. dist.metadata.url = 'xxx'
  35. dist.metadata.author = 'xxx'
  36. dist.metadata.author_email = 'xxx'
  37. dist.metadata.name = 'xxx'
  38. dist.metadata.version = 'xxx'
  39. cmd = register(dist)
  40. # we shouldn't have a .pypirc file yet
  41. self.assert_(not os.path.exists(self.rc))
  42. # patching raw_input and getpass.getpass
  43. # so register gets happy
  44. #
  45. # Here's what we are faking :
  46. # use your existing login (choice 1.)
  47. # Username : 'tarek'
  48. # Password : 'xxx'
  49. # Save your login (y/N)? : 'y'
  50. inputs = RawInputs('1', 'tarek', 'y')
  51. from distutils.command import register as register_module
  52. register_module.raw_input = inputs.__call__
  53. def _getpass(prompt):
  54. return 'xxx'
  55. register_module.getpass.getpass = _getpass
  56. class FakeServer(object):
  57. def __init__(self):
  58. self.calls = []
  59. def __call__(self, *args):
  60. # we want to compare them, so let's store
  61. # something comparable
  62. els = args[0].items()
  63. els.sort()
  64. self.calls.append(tuple(els))
  65. return 200, 'OK'
  66. cmd.post_to_server = pypi_server = FakeServer()
  67. # let's run the command
  68. cmd.run()
  69. # we should have a brand new .pypirc file
  70. self.assert_(os.path.exists(self.rc))
  71. # with the content similar to WANTED_PYPIRC
  72. content = open(self.rc).read()
  73. self.assertEquals(content, WANTED_PYPIRC)
  74. # now let's make sure the .pypirc file generated
  75. # really works : we shouldn't be asked anything
  76. # if we run the command again
  77. def _no_way(prompt=''):
  78. raise AssertionError(prompt)
  79. register_module.raw_input = _no_way
  80. cmd.run()
  81. # let's see what the server received : we should
  82. # have 2 similar requests
  83. self.assert_(len(pypi_server.calls), 2)
  84. self.assert_(pypi_server.calls[0], pypi_server.calls[1])
  85. def test_suite():
  86. return unittest.makeSuite(registerTestCase)
  87. if __name__ == "__main__":
  88. unittest.main(defaultTest="test_suite")