| 1 |
"""Tests for distutils.pypirc.pypirc.""" |
| 2 |
import os |
| 3 |
import unittest |
| 4 |
|
| 5 |
from distutils.core import PyPIRCCommand |
| 6 |
from distutils.core import Distribution |
| 7 |
from distutils.log import set_threshold |
| 8 |
from distutils.log import WARN |
| 9 |
|
| 10 |
from distutils.tests import support |
| 11 |
|
| 12 |
PYPIRC = """\ |
| 13 |
[distutils] |
| 14 |
|
| 15 |
index-servers = |
| 16 |
server1 |
| 17 |
server2 |
| 18 |
server3 |
| 19 |
|
| 20 |
[server1] |
| 21 |
username:me |
| 22 |
password:secret |
| 23 |
|
| 24 |
[server2] |
| 25 |
username:meagain |
| 26 |
password: secret |
| 27 |
realm:acme |
| 28 |
repository:http://another.pypi/ |
| 29 |
|
| 30 |
[server3] |
| 31 |
username:cbiggles |
| 32 |
password:yh^%#rest-of-my-password |
| 33 |
""" |
| 34 |
|
| 35 |
PYPIRC_OLD = """\ |
| 36 |
[server-login] |
| 37 |
username:tarek |
| 38 |
password:secret |
| 39 |
""" |
| 40 |
|
| 41 |
WANTED = """\ |
| 42 |
[distutils] |
| 43 |
index-servers = |
| 44 |
pypi |
| 45 |
|
| 46 |
[pypi] |
| 47 |
username:tarek |
| 48 |
password:xxx |
| 49 |
""" |
| 50 |
|
| 51 |
|
| 52 |
class BasePyPIRCCommandTestCase(support.TempdirManager, |
| 53 |
support.LoggingSilencer, |
| 54 |
support.EnvironGuard, |
| 55 |
unittest.TestCase): |
| 56 |
|
| 57 |
def setUp(self): |
| 58 |
"""Patches the environment.""" |
| 59 |
super(BasePyPIRCCommandTestCase, self).setUp() |
| 60 |
self.tmp_dir = self.mkdtemp() |
| 61 |
os.environ['HOME'] = self.tmp_dir |
| 62 |
os.environ['USERPROFILE'] = 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 |
|
| 82 |
class PyPIRCCommandTestCase(BasePyPIRCCommandTestCase): |
| 83 |
|
| 84 |
def test_server_registration(self): |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 136 |
if __name__ == "__main__": |
| 137 |
unittest.main() |