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