PageRenderTime 15ms CodeModel.GetById 9ms app.highlight 3ms RepoModel.GetById 2ms app.codeStats 0ms

/Lib/distutils/tests/test_register.py

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