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