PageRenderTime 71ms CodeModel.GetById 20ms app.highlight 10ms RepoModel.GetById 20ms app.codeStats 0ms

/Lib/distutils/tests/test_config.py

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