~ubuntu-branches/ubuntu/maverick/python3.1/maverick

« back to all changes in this revision

Viewing changes to Lib/distutils/tests/test_register.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2009-03-23 00:01:27 UTC
  • Revision ID: james.westby@ubuntu.com-20090323000127-5fstfxju4ufrhthq
Tags: upstream-3.1~a1+20090322
ImportĀ upstreamĀ versionĀ 3.1~a1+20090322

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""Tests for distutils.command.register."""
 
2
import sys
 
3
import os
 
4
import unittest
 
5
import getpass
 
6
 
 
7
from distutils.command.register import register
 
8
from distutils.core import Distribution
 
9
 
 
10
from distutils.tests import support
 
11
from distutils.tests.test_config import PYPIRC, PyPIRCCommandTestCase
 
12
 
 
13
PYPIRC_NOPASSWORD = """\
 
14
[distutils]
 
15
 
 
16
index-servers =
 
17
    server1
 
18
 
 
19
[server1]
 
20
username:me
 
21
"""
 
22
 
 
23
WANTED_PYPIRC = """\
 
24
[distutils]
 
25
index-servers =
 
26
    pypi
 
27
 
 
28
[pypi]
 
29
username:tarek
 
30
password:password
 
31
"""
 
32
 
 
33
class Inputs(object):
 
34
    """Fakes user inputs."""
 
35
    def __init__(self, *answers):
 
36
        self.answers = answers
 
37
        self.index = 0
 
38
 
 
39
    def __call__(self, prompt=''):
 
40
        try:
 
41
            return self.answers[self.index]
 
42
        finally:
 
43
            self.index += 1
 
44
 
 
45
class FakeServer(object):
 
46
    """Fakes a PyPI server"""
 
47
    def __init__(self):
 
48
        self.calls = []
 
49
 
 
50
    def __call__(self, *args):
 
51
        # we want to compare them, so let's store
 
52
        # something comparable
 
53
        els = list(args[0].items())
 
54
        els.sort()
 
55
        self.calls.append(tuple(els))
 
56
        return 200, 'OK'
 
57
 
 
58
class registerTestCase(PyPIRCCommandTestCase):
 
59
 
 
60
    def setUp(self):
 
61
        PyPIRCCommandTestCase.setUp(self)
 
62
        # patching the password prompt
 
63
        self._old_getpass = getpass.getpass
 
64
        def _getpass(prompt):
 
65
            return 'password'
 
66
        getpass.getpass = _getpass
 
67
 
 
68
    def tearDown(self):
 
69
        getpass.getpass = self._old_getpass
 
70
        PyPIRCCommandTestCase.tearDown(self)
 
71
 
 
72
    def test_create_pypirc(self):
 
73
        # this test makes sure a .pypirc file
 
74
        # is created when requested.
 
75
 
 
76
        # let's create a fake distribution
 
77
        # and a register instance
 
78
        dist = Distribution()
 
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'
 
84
        cmd = register(dist)
 
85
 
 
86
        # we shouldn't have a .pypirc file yet
 
87
        self.assert_(not os.path.exists(self.rc))
 
88
 
 
89
        # patching input and getpass.getpass
 
90
        # so register gets happy
 
91
        #
 
92
        # Here's what we are faking :
 
93
        # use your existing login (choice 1.)
 
94
        # Username : 'tarek'
 
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__
 
100
 
 
101
        cmd.post_to_server = pypi_server = FakeServer()
 
102
 
 
103
        # let's run the command
 
104
        cmd.run()
 
105
 
 
106
        # we should have a brand new .pypirc file
 
107
        self.assert_(os.path.exists(self.rc))
 
108
 
 
109
        # with the content similar to WANTED_PYPIRC
 
110
        content = open(self.rc).read()
 
111
        self.assertEquals(content, WANTED_PYPIRC)
 
112
 
 
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
 
119
 
 
120
        cmd.run()
 
121
 
 
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])
 
126
 
 
127
    def test_password_not_in_file(self):
 
128
 
 
129
        f = open(self.rc, 'w')
 
130
        f.write(PYPIRC_NOPASSWORD)
 
131
        f.close()
 
132
 
 
133
        dist = Distribution()
 
134
        cmd = register(dist)
 
135
        cmd.post_to_server = FakeServer()
 
136
 
 
137
        cmd._set_config()
 
138
        cmd.finalize_options()
 
139
        cmd.send_metadata()
 
140
 
 
141
        # dist.password should be set
 
142
        # therefore used afterwards by other commands
 
143
        self.assertEquals(dist.password, 'password')
 
144
 
 
145
def test_suite():
 
146
    return unittest.makeSuite(registerTestCase)
 
147
 
 
148
if __name__ == "__main__":
 
149
    unittest.main(defaultTest="test_suite")