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