blob: 525bee9416ab27b20543ce55b1c71864b496d449 [file] [log] [blame]
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +00001"""Tests for distutils.pypirc.pypirc."""
2import sys
3import os
4import unittest
Tarek Ziadéc1375d52009-02-14 14:35:51 +00005import tempfile
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +00006
7from distutils.core import PyPIRCCommand
8from distutils.core import Distribution
Benjamin Peterson92035012008-12-27 16:00:54 +00009from distutils.log import set_threshold
10from distutils.log import WARN
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000011
12from distutils.tests import support
Éric Araujo70ec44a2010-11-06 02:44:43 +000013from test.support import run_unittest
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000014
15PYPIRC = """\
16[distutils]
17
18index-servers =
19 server1
20 server2
21
22[server1]
23username:me
24password:secret
25
26[server2]
27username:meagain
28password: secret
29realm:acme
30repository:http://another.pypi/
31"""
32
33PYPIRC_OLD = """\
34[server-login]
35username:tarek
36password:secret
37"""
38
Benjamin Peterson92035012008-12-27 16:00:54 +000039WANTED = """\
40[distutils]
41index-servers =
42 pypi
43
44[pypi]
45username:tarek
46password:xxx
47"""
48
49
Tarek Ziadébaf51802009-03-31 21:37:16 +000050class PyPIRCCommandTestCase(support.TempdirManager,
51 support.LoggingSilencer,
Tarek Ziadébbf1f182009-05-10 12:20:44 +000052 support.EnvironGuard,
Tarek Ziadébaf51802009-03-31 21:37:16 +000053 unittest.TestCase):
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000054
55 def setUp(self):
56 """Patches the environment."""
Tarek Ziadé0f31e6d2009-02-25 22:31:38 +000057 super(PyPIRCCommandTestCase, self).setUp()
Tarek Ziadéc1375d52009-02-14 14:35:51 +000058 self.tmp_dir = self.mkdtemp()
Tarek Ziadé430fb632009-10-18 11:34:51 +000059 os.environ['HOME'] = self.tmp_dir
Tarek Ziadéc1375d52009-02-14 14:35:51 +000060 self.rc = os.path.join(self.tmp_dir, '.pypirc')
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000061 self.dist = Distribution()
62
63 class command(PyPIRCCommand):
64 def __init__(self, dist):
65 PyPIRCCommand.__init__(self, dist)
66 def initialize_options(self):
67 pass
68 finalize_options = initialize_options
69
70 self._cmd = command
Benjamin Peterson92035012008-12-27 16:00:54 +000071 self.old_threshold = set_threshold(WARN)
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000072
73 def tearDown(self):
74 """Removes the patch."""
Benjamin Peterson92035012008-12-27 16:00:54 +000075 set_threshold(self.old_threshold)
Tarek Ziadé0f31e6d2009-02-25 22:31:38 +000076 super(PyPIRCCommandTestCase, self).tearDown()
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000077
78 def test_server_registration(self):
79 # This test makes sure PyPIRCCommand knows how to:
80 # 1. handle several sections in .pypirc
81 # 2. handle the old format
82
83 # new format
Tarek Ziadébbf1f182009-05-10 12:20:44 +000084 self.write_file(self.rc, PYPIRC)
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000085 cmd = self._cmd(self.dist)
86 config = cmd._read_pypirc()
87
88 config = list(sorted(config.items()))
89 waited = [('password', 'secret'), ('realm', 'pypi'),
90 ('repository', 'http://pypi.python.org/pypi'),
91 ('server', 'server1'), ('username', 'me')]
Ezio Melottib3aedd42010-11-20 19:04:17 +000092 self.assertEqual(config, waited)
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000093
94 # old format
Tarek Ziadébbf1f182009-05-10 12:20:44 +000095 self.write_file(self.rc, PYPIRC_OLD)
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000096 config = cmd._read_pypirc()
97 config = list(sorted(config.items()))
98 waited = [('password', 'secret'), ('realm', 'pypi'),
99 ('repository', 'http://pypi.python.org/pypi'),
100 ('server', 'server-login'), ('username', 'tarek')]
Ezio Melottib3aedd42010-11-20 19:04:17 +0000101 self.assertEqual(config, waited)
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +0000102
Benjamin Peterson92035012008-12-27 16:00:54 +0000103 def test_server_empty_registration(self):
Benjamin Peterson92035012008-12-27 16:00:54 +0000104 cmd = self._cmd(self.dist)
105 rc = cmd._get_rc_file()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000106 self.assertTrue(not os.path.exists(rc))
Benjamin Peterson92035012008-12-27 16:00:54 +0000107 cmd._store_pypirc('tarek', 'xxx')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000108 self.assertTrue(os.path.exists(rc))
Éric Araujobee5cef2010-11-05 23:51:56 +0000109 f = open(rc)
110 try:
111 content = f.read()
Ezio Melottib3aedd42010-11-20 19:04:17 +0000112 self.assertEqual(content, WANTED)
Éric Araujobee5cef2010-11-05 23:51:56 +0000113 finally:
114 f.close()
Benjamin Peterson92035012008-12-27 16:00:54 +0000115
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +0000116def test_suite():
117 return unittest.makeSuite(PyPIRCCommandTestCase)
118
119if __name__ == "__main__":
Éric Araujo70ec44a2010-11-06 02:44:43 +0000120 run_unittest(test_suite())