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 os.environ['USERPROFILE'] = self.tmp_dir 64 self.rc = os.path.join(self.tmp_dir, '.pypirc') 65 self.dist = Distribution() 66 67 class command(PyPIRCCommand): 68 def __init__(self, dist): 69 PyPIRCCommand.__init__(self, dist) 70 def initialize_options(self): 71 pass 72 finalize_options = initialize_options 73 74 self._cmd = command 75 self.old_threshold = set_threshold(WARN) 76 77 def tearDown(self): 78 """Removes the patch.""" 79 set_threshold(self.old_threshold) 80 super(BasePyPIRCCommandTestCase, self).tearDown() 81 82 83class PyPIRCCommandTestCase(BasePyPIRCCommandTestCase): 84 85 def test_server_registration(self): 86 # This test makes sure PyPIRCCommand knows how to: 87 # 1. handle several sections in .pypirc 88 # 2. handle the old format 89 90 # new format 91 self.write_file(self.rc, PYPIRC) 92 cmd = self._cmd(self.dist) 93 config = cmd._read_pypirc() 94 95 config = list(sorted(config.items())) 96 waited = [('password', 'secret'), ('realm', 'pypi'), 97 ('repository', 'https://upload.pypi.org/legacy/'), 98 ('server', 'server1'), ('username', 'me')] 99 self.assertEqual(config, waited) 100 101 # old format 102 self.write_file(self.rc, PYPIRC_OLD) 103 config = cmd._read_pypirc() 104 config = list(sorted(config.items())) 105 waited = [('password', 'secret'), ('realm', 'pypi'), 106 ('repository', 'https://upload.pypi.org/legacy/'), 107 ('server', 'server-login'), ('username', 'tarek')] 108 self.assertEqual(config, waited) 109 110 def test_server_empty_registration(self): 111 cmd = self._cmd(self.dist) 112 rc = cmd._get_rc_file() 113 self.assertFalse(os.path.exists(rc)) 114 cmd._store_pypirc('tarek', 'xxx') 115 self.assertTrue(os.path.exists(rc)) 116 f = open(rc) 117 try: 118 content = f.read() 119 self.assertEqual(content, WANTED) 120 finally: 121 f.close() 122 123 def test_config_interpolation(self): 124 # using the % character in .pypirc should not raise an error (#20120) 125 self.write_file(self.rc, PYPIRC) 126 cmd = self._cmd(self.dist) 127 cmd.repository = 'server3' 128 config = cmd._read_pypirc() 129 130 config = list(sorted(config.items())) 131 waited = [('password', 'yh^%#rest-of-my-password'), ('realm', 'pypi'), 132 ('repository', 'https://upload.pypi.org/legacy/'), 133 ('server', 'server3'), ('username', 'cbiggles')] 134 self.assertEqual(config, waited) 135 136 137def test_suite(): 138 return unittest.makeSuite(PyPIRCCommandTestCase) 139 140if __name__ == "__main__": 141 run_unittest(test_suite()) 142