1"""Tests for distutils.pypirc.pypirc."""
2import sys
3import os
4import unittest
5import tempfile
6import shutil
7
8from distutils.core import PyPIRCCommand
9from distutils.core import Distribution
10from distutils.log import set_threshold
11from distutils.log import WARN
12
13from distutils.tests import support
14from test.test_support import run_unittest
15
16PYPIRC = """\
17[distutils]
18
19index-servers =
20    server1
21    server2
22
23[server1]
24username:me
25password:secret
26
27[server2]
28username:meagain
29password: secret
30realm:acme
31repository:http://another.pypi/
32"""
33
34PYPIRC_OLD = """\
35[server-login]
36username:tarek
37password:secret
38"""
39
40WANTED = """\
41[distutils]
42index-servers =
43    pypi
44
45[pypi]
46username:tarek
47password:xxx
48"""
49
50
51class 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', 'https://upload.pypi.org/legacy/'),
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', 'https://upload.pypi.org/legacy/'),
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.assertFalse(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
119def test_suite():
120    return unittest.makeSuite(PyPIRCCommandTestCase)
121
122if __name__ == "__main__":
123    run_unittest(test_suite())
124