• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""Tests for distutils.pypirc.pypirc."""
2import os
3import unittest
4
5from distutils.core import PyPIRCCommand
6from distutils.core import Distribution
7from distutils.log import set_threshold
8from distutils.log import WARN
9
10from distutils.tests import support
11from test.support import run_unittest
12
13PYPIRC = """\
14[distutils]
15
16index-servers =
17    server1
18    server2
19    server3
20
21[server1]
22username:me
23password:secret
24
25[server2]
26username:meagain
27password: secret
28realm:acme
29repository:http://another.pypi/
30
31[server3]
32username:cbiggles
33password:yh^%#rest-of-my-password
34"""
35
36PYPIRC_OLD = """\
37[server-login]
38username:tarek
39password:secret
40"""
41
42WANTED = """\
43[distutils]
44index-servers =
45    pypi
46
47[pypi]
48username:tarek
49password:xxx
50"""
51
52
53class BasePyPIRCCommandTestCase(support.TempdirManager,
54                            support.LoggingSilencer,
55                            support.EnvironGuard,
56                            unittest.TestCase):
57
58    def setUp(self):
59        """Patches the environment."""
60        super(BasePyPIRCCommandTestCase, self).setUp()
61        self.tmp_dir = self.mkdtemp()
62        os.environ['HOME'] = self.tmp_dir
63        self.rc = os.path.join(self.tmp_dir, '.pypirc')
64        self.dist = Distribution()
65
66        class command(PyPIRCCommand):
67            def __init__(self, dist):
68                PyPIRCCommand.__init__(self, dist)
69            def initialize_options(self):
70                pass
71            finalize_options = initialize_options
72
73        self._cmd = command
74        self.old_threshold = set_threshold(WARN)
75
76    def tearDown(self):
77        """Removes the patch."""
78        set_threshold(self.old_threshold)
79        super(BasePyPIRCCommandTestCase, self).tearDown()
80
81
82class PyPIRCCommandTestCase(BasePyPIRCCommandTestCase):
83
84    def test_server_registration(self):
85        # This test makes sure PyPIRCCommand knows how to:
86        # 1. handle several sections in .pypirc
87        # 2. handle the old format
88
89        # new format
90        self.write_file(self.rc, PYPIRC)
91        cmd = self._cmd(self.dist)
92        config = cmd._read_pypirc()
93
94        config = list(sorted(config.items()))
95        waited = [('password', 'secret'), ('realm', 'pypi'),
96                  ('repository', 'https://upload.pypi.org/legacy/'),
97                  ('server', 'server1'), ('username', 'me')]
98        self.assertEqual(config, waited)
99
100        # old format
101        self.write_file(self.rc, PYPIRC_OLD)
102        config = cmd._read_pypirc()
103        config = list(sorted(config.items()))
104        waited = [('password', 'secret'), ('realm', 'pypi'),
105                  ('repository', 'https://upload.pypi.org/legacy/'),
106                  ('server', 'server-login'), ('username', 'tarek')]
107        self.assertEqual(config, waited)
108
109    def test_server_empty_registration(self):
110        cmd = self._cmd(self.dist)
111        rc = cmd._get_rc_file()
112        self.assertFalse(os.path.exists(rc))
113        cmd._store_pypirc('tarek', 'xxx')
114        self.assertTrue(os.path.exists(rc))
115        f = open(rc)
116        try:
117            content = f.read()
118            self.assertEqual(content, WANTED)
119        finally:
120            f.close()
121
122    def test_config_interpolation(self):
123        # using the % character in .pypirc should not raise an error (#20120)
124        self.write_file(self.rc, PYPIRC)
125        cmd = self._cmd(self.dist)
126        cmd.repository = 'server3'
127        config = cmd._read_pypirc()
128
129        config = list(sorted(config.items()))
130        waited = [('password', 'yh^%#rest-of-my-password'), ('realm', 'pypi'),
131                  ('repository', 'https://upload.pypi.org/legacy/'),
132                  ('server', 'server3'), ('username', 'cbiggles')]
133        self.assertEqual(config, waited)
134
135
136def test_suite():
137    return unittest.makeSuite(PyPIRCCommandTestCase)
138
139if __name__ == "__main__":
140    run_unittest(test_suite())
141