openmedialibrary_platform_d.../lib/python3.7/distutils/tests/test_config.py

141 lines
3.8 KiB
Python
Raw Normal View History

2016-02-06 09:36:57 +00:00
"""Tests for distutils.pypirc.pypirc."""
import os
import unittest
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
from test.support import run_unittest
PYPIRC = """\
[distutils]
index-servers =
server1
server2
2018-12-31 23:25:26 +00:00
server3
2016-02-06 09:36:57 +00:00
[server1]
username:me
password:secret
[server2]
username:meagain
password: secret
realm:acme
repository:http://another.pypi/
2018-12-31 23:25:26 +00:00
[server3]
username:cbiggles
password:yh^%#rest-of-my-password
2016-02-06 09:36:57 +00:00
"""
PYPIRC_OLD = """\
[server-login]
username:tarek
password:secret
"""
WANTED = """\
[distutils]
index-servers =
pypi
[pypi]
username:tarek
password:xxx
"""
2018-12-31 23:25:26 +00:00
class BasePyPIRCCommandTestCase(support.TempdirManager,
2016-02-06 09:36:57 +00:00
support.LoggingSilencer,
support.EnvironGuard,
unittest.TestCase):
def setUp(self):
"""Patches the environment."""
2018-12-31 23:25:26 +00:00
super(BasePyPIRCCommandTestCase, self).setUp()
2016-02-06 09:36:57 +00:00
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)
2018-12-31 23:25:26 +00:00
super(BasePyPIRCCommandTestCase, self).tearDown()
class PyPIRCCommandTestCase(BasePyPIRCCommandTestCase):
2016-02-06 09:36:57 +00:00
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()
config = list(sorted(config.items()))
waited = [('password', 'secret'), ('realm', 'pypi'),
2018-12-31 23:25:26 +00:00
('repository', 'https://upload.pypi.org/legacy/'),
2016-02-06 09:36:57 +00:00
('server', 'server1'), ('username', 'me')]
self.assertEqual(config, waited)
# old format
self.write_file(self.rc, PYPIRC_OLD)
config = cmd._read_pypirc()
config = list(sorted(config.items()))
waited = [('password', 'secret'), ('realm', 'pypi'),
2018-12-31 23:25:26 +00:00
('repository', 'https://upload.pypi.org/legacy/'),
2016-02-06 09:36:57 +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()
self.assertFalse(os.path.exists(rc))
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()
2018-12-31 23:25:26 +00:00
def test_config_interpolation(self):
# using the % character in .pypirc should not raise an error (#20120)
self.write_file(self.rc, PYPIRC)
cmd = self._cmd(self.dist)
cmd.repository = 'server3'
config = cmd._read_pypirc()
config = list(sorted(config.items()))
waited = [('password', 'yh^%#rest-of-my-password'), ('realm', 'pypi'),
('repository', 'https://upload.pypi.org/legacy/'),
('server', 'server3'), ('username', 'cbiggles')]
self.assertEqual(config, waited)
2016-02-06 09:36:57 +00:00
def test_suite():
return unittest.makeSuite(PyPIRCCommandTestCase)
if __name__ == "__main__":
run_unittest(test_suite())