1
"""Tests for distutils.command.register."""
7
from distutils.command.register import register
8
from distutils.core import Distribution
10
from distutils.tests import support
11
from distutils.tests.test_config import PYPIRC, PyPIRCCommandTestCase
13
PYPIRC_NOPASSWORD = """\
34
"""Fakes user inputs."""
35
def __init__(self, *answers):
36
self.answers = answers
39
def __call__(self, prompt=''):
41
return self.answers[self.index]
45
class FakeServer(object):
46
"""Fakes a PyPI server"""
50
def __call__(self, *args):
51
# we want to compare them, so let's store
52
# something comparable
53
els = list(args[0].items())
55
self.calls.append(tuple(els))
58
class registerTestCase(PyPIRCCommandTestCase):
61
PyPIRCCommandTestCase.setUp(self)
62
# patching the password prompt
63
self._old_getpass = getpass.getpass
66
getpass.getpass = _getpass
69
getpass.getpass = self._old_getpass
70
PyPIRCCommandTestCase.tearDown(self)
72
def test_create_pypirc(self):
73
# this test makes sure a .pypirc file
74
# is created when requested.
76
# let's create a fake distribution
77
# and a register instance
79
dist.metadata.url = 'xxx'
80
dist.metadata.author = 'xxx'
81
dist.metadata.author_email = 'xxx'
82
dist.metadata.name = 'xxx'
83
dist.metadata.version = 'xxx'
86
# we shouldn't have a .pypirc file yet
87
self.assert_(not os.path.exists(self.rc))
89
# patching input and getpass.getpass
90
# so register gets happy
92
# Here's what we are faking :
93
# use your existing login (choice 1.)
95
# Password : 'password'
96
# Save your login (y/N)? : 'y'
97
inputs = Inputs('1', 'tarek', 'y')
98
from distutils.command import register as register_module
99
register_module.input = inputs.__call__
101
cmd.post_to_server = pypi_server = FakeServer()
103
# let's run the command
106
# we should have a brand new .pypirc file
107
self.assert_(os.path.exists(self.rc))
109
# with the content similar to WANTED_PYPIRC
110
content = open(self.rc).read()
111
self.assertEquals(content, WANTED_PYPIRC)
113
# now let's make sure the .pypirc file generated
114
# really works : we shouldn't be asked anything
115
# if we run the command again
116
def _no_way(prompt=''):
117
raise AssertionError(prompt)
118
register_module.raw_input = _no_way
122
# let's see what the server received : we should
123
# have 2 similar requests
124
self.assert_(len(pypi_server.calls), 2)
125
self.assert_(pypi_server.calls[0], pypi_server.calls[1])
127
def test_password_not_in_file(self):
129
f = open(self.rc, 'w')
130
f.write(PYPIRC_NOPASSWORD)
133
dist = Distribution()
135
cmd.post_to_server = FakeServer()
138
cmd.finalize_options()
141
# dist.password should be set
142
# therefore used afterwards by other commands
143
self.assertEquals(dist.password, 'password')
146
return unittest.makeSuite(registerTestCase)
148
if __name__ == "__main__":
149
unittest.main(defaultTest="test_suite")