2008-05-11 11:00:00 -03:00
|
|
|
"""Tests for distutils.pypirc.pypirc."""
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
from distutils.core import PyPIRCCommand
|
|
|
|
from distutils.core import Distribution
|
2008-12-27 09:28:42 -04:00
|
|
|
from distutils.log import set_threshold
|
|
|
|
from distutils.log import WARN
|
2008-05-11 11:00:00 -03:00
|
|
|
|
|
|
|
from distutils.tests import support
|
|
|
|
|
|
|
|
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
|
|
|
|
"""
|
|
|
|
|
2008-12-27 09:28:42 -04:00
|
|
|
WANTED = """\
|
|
|
|
[distutils]
|
|
|
|
index-servers =
|
|
|
|
pypi
|
|
|
|
|
|
|
|
[pypi]
|
|
|
|
username:tarek
|
|
|
|
password:xxx
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2008-05-11 11:00:00 -03:00
|
|
|
class PyPIRCCommandTestCase(support.TempdirManager, unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
"""Patches the environment."""
|
|
|
|
if os.environ.has_key('HOME'):
|
|
|
|
self._old_home = os.environ['HOME']
|
|
|
|
else:
|
|
|
|
self._old_home = None
|
|
|
|
curdir = os.path.dirname(__file__)
|
|
|
|
os.environ['HOME'] = curdir
|
|
|
|
self.rc = os.path.join(curdir, '.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
|
2008-12-27 09:28:42 -04:00
|
|
|
self.old_threshold = set_threshold(WARN)
|
2008-05-11 11:00:00 -03:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
"""Removes the patch."""
|
|
|
|
if self._old_home is None:
|
|
|
|
del os.environ['HOME']
|
|
|
|
else:
|
|
|
|
os.environ['HOME'] = self._old_home
|
|
|
|
if os.path.exists(self.rc):
|
|
|
|
os.remove(self.rc)
|
2008-12-27 09:28:42 -04:00
|
|
|
set_threshold(self.old_threshold)
|
2008-05-11 11:00:00 -03: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
|
|
|
|
f = open(self.rc, 'w')
|
|
|
|
try:
|
|
|
|
f.write(PYPIRC)
|
|
|
|
finally:
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
cmd = self._cmd(self.dist)
|
|
|
|
config = cmd._read_pypirc()
|
|
|
|
|
|
|
|
config = config.items()
|
|
|
|
config.sort()
|
|
|
|
waited = [('password', 'secret'), ('realm', 'pypi'),
|
|
|
|
('repository', 'http://pypi.python.org/pypi'),
|
|
|
|
('server', 'server1'), ('username', 'me')]
|
|
|
|
self.assertEquals(config, waited)
|
|
|
|
|
|
|
|
# old format
|
|
|
|
f = open(self.rc, 'w')
|
|
|
|
f.write(PYPIRC_OLD)
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
config = cmd._read_pypirc()
|
|
|
|
config = config.items()
|
|
|
|
config.sort()
|
|
|
|
waited = [('password', 'secret'), ('realm', 'pypi'),
|
|
|
|
('repository', 'http://pypi.python.org/pypi'),
|
|
|
|
('server', 'server-login'), ('username', 'tarek')]
|
|
|
|
self.assertEquals(config, waited)
|
|
|
|
|
2008-12-27 09:28:42 -04:00
|
|
|
def test_server_empty_registration(self):
|
|
|
|
|
|
|
|
cmd = self._cmd(self.dist)
|
|
|
|
rc = cmd._get_rc_file()
|
|
|
|
self.assert_(not os.path.exists(rc))
|
|
|
|
|
|
|
|
cmd._store_pypirc('tarek', 'xxx')
|
|
|
|
|
|
|
|
self.assert_(os.path.exists(rc))
|
|
|
|
content = open(rc).read()
|
|
|
|
|
|
|
|
self.assertEquals(content, WANTED)
|
|
|
|
|
|
|
|
|
2008-05-11 11:00:00 -03:00
|
|
|
def test_suite():
|
|
|
|
return unittest.makeSuite(PyPIRCCommandTestCase)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main(defaultTest="test_suite")
|