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