ยปCore Development>Code coverage>Lib/distutils/tests/test_config.py

Python code coverage for Lib/distutils/tests/test_config.py

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