openmedialibrary_platform/Darwin/lib/python3.4/distutils/tests/test_config.py

121 lines
3.1 KiB
Python
Raw Normal View History

2013-10-11 17:28:32 +00:00
"""Tests for distutils.pypirc.pypirc."""
import sys
import os
import unittest
import tempfile
from distutils.core import PyPIRCCommand
from distutils.core import Distribution
from distutils.log import set_threshold
from distutils.log import WARN
from distutils.tests import support
2014-09-30 16:15:32 +00:00
from test.support import run_unittest
2013-10-11 17:28:32 +00:00
PYPIRC = """\
[distutils]
index-servers =
server1
server2
[server1]
username:me
password:secret
[server2]
username:meagain
password: secret
realm:acme
repository:http://another.pypi/
"""
PYPIRC_OLD = """\
[server-login]
username:tarek
password:secret
"""
WANTED = """\
[distutils]
index-servers =
pypi
[pypi]
username:tarek
password:xxx
"""
class PyPIRCCommandTestCase(support.TempdirManager,
support.LoggingSilencer,
support.EnvironGuard,
unittest.TestCase):
def setUp(self):
"""Patches the environment."""
super(PyPIRCCommandTestCase, self).setUp()
self.tmp_dir = self.mkdtemp()
os.environ['HOME'] = self.tmp_dir
self.rc = os.path.join(self.tmp_dir, '.pypirc')
self.dist = Distribution()
class command(PyPIRCCommand):
def __init__(self, dist):
PyPIRCCommand.__init__(self, dist)
def initialize_options(self):
pass
finalize_options = initialize_options
self._cmd = command
self.old_threshold = set_threshold(WARN)
def tearDown(self):
"""Removes the patch."""
set_threshold(self.old_threshold)
super(PyPIRCCommandTestCase, self).tearDown()
def test_server_registration(self):
# This test makes sure PyPIRCCommand knows how to:
# 1. handle several sections in .pypirc
# 2. handle the old format
# new format
self.write_file(self.rc, PYPIRC)
cmd = self._cmd(self.dist)
config = cmd._read_pypirc()
2014-09-30 16:15:32 +00:00
config = list(sorted(config.items()))
2013-10-11 17:28:32 +00:00
waited = [('password', 'secret'), ('realm', 'pypi'),
2014-09-30 16:15:32 +00:00
('repository', 'https://pypi.python.org/pypi'),
2013-10-11 17:28:32 +00:00
('server', 'server1'), ('username', 'me')]
self.assertEqual(config, waited)
# old format
self.write_file(self.rc, PYPIRC_OLD)
config = cmd._read_pypirc()
2014-09-30 16:15:32 +00:00
config = list(sorted(config.items()))
2013-10-11 17:28:32 +00:00
waited = [('password', 'secret'), ('realm', 'pypi'),
2014-09-30 16:15:32 +00:00
('repository', 'https://pypi.python.org/pypi'),
2013-10-11 17:28:32 +00:00
('server', 'server-login'), ('username', 'tarek')]
self.assertEqual(config, waited)
def test_server_empty_registration(self):
cmd = self._cmd(self.dist)
rc = cmd._get_rc_file()
2014-09-30 16:15:32 +00:00
self.assertFalse(os.path.exists(rc))
2013-10-11 17:28:32 +00:00
cmd._store_pypirc('tarek', 'xxx')
self.assertTrue(os.path.exists(rc))
f = open(rc)
try:
content = f.read()
self.assertEqual(content, WANTED)
finally:
f.close()
def test_suite():
return unittest.makeSuite(PyPIRCCommandTestCase)
if __name__ == "__main__":
run_unittest(test_suite())