1
# Copyright (C) 2005 by Canonical Ltd
2
# Authors: Robert Collins <robert.collins@canonical.com>
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
"""Tests for finding and reading the bzr config file[s]."""
19
# import system imports here
20
from ConfigParser import ConfigParser
21
from cStringIO import StringIO
25
#import bzrlib specific imports here
26
import bzrlib.config as config
27
from bzrlib.selftest import TestCase, TestCaseInTempDir
30
sample_config_text = ("[DEFAULT]\n"
31
"email=Robert Collins <robertc@example.com>\n"
33
"gpg_signing_command=gnome-gpg\n")
35
class InstrumentedConfigParser(object):
36
"""A config parser look-enough-alike to record calls made to it."""
41
def read(self, filenames):
42
self._calls.append(('read', filenames))
45
class TestConfigPath(TestCase):
48
super(TestConfigPath, self).setUp()
49
self.oldenv = os.environ.get('HOME', None)
50
os.environ['HOME'] = '/home/bogus'
53
os.environ['HOME'] = self.oldenv
55
def test_config_dir(self):
56
self.assertEqual(config.config_dir(), '/home/bogus/.bazaar')
58
def test_config_filename(self):
59
self.assertEqual(config.config_filename(),
60
'/home/bogus/.bazaar/bazaar.conf')
63
class TestGetConfig(TestCase):
65
def test_from_fp(self):
66
config_file = StringIO(sample_config_text)
67
self.failUnless(isinstance(config._get_config_parser(file=config_file),
70
def test_calls_read_filenames(self):
71
# note the monkey patching. if config access was via a class instance,
72
# we would not have to - if this changes in future, be sure to stop
73
# monkey patching RBC 20051011
74
oldparserclass = config.ConfigParser
75
config.ConfigParser = InstrumentedConfigParser
77
parser = config._get_config_parser()
79
config.ConfigParser = oldparserclass
80
self.failUnless(isinstance(parser, InstrumentedConfigParser))
81
self.assertEqual(parser._calls, [('read', [config.config_filename()])])
84
class TestConfigItems(TestCase):
87
super(TestConfigItems, self).setUp()
88
self.bzr_email = os.environ.get('BZREMAIL')
89
if self.bzr_email is not None:
90
del os.environ['BZREMAIL']
91
self.email = os.environ.get('EMAIL')
92
if self.email is not None:
93
del os.environ['EMAIL']
94
self.oldenv = os.environ.get('HOME', None)
95
os.environ['HOME'] = os.getcwd()
98
os.environ['HOME'] = self.oldenv
99
if self.bzr_email is not None:
100
os.environ['BZREMAIL'] = self.bzr_email
101
if self.email is not None:
102
os.environ['EMAIL'] = self.email
103
super(TestConfigItems, self).tearDown()
105
def test_user_id(self):
106
config_file = StringIO(sample_config_text)
107
parser = config._get_config_parser(file=config_file)
108
self.assertEqual("Robert Collins <robertc@example.com>",
109
config._get_user_id(parser = parser))
111
def test_absent_user_id(self):
112
config_file = StringIO("")
113
parser = config._get_config_parser(file=config_file)
114
self.assertEqual(None,
115
config._get_user_id(parser = parser))
117
def test_configured_edit(self):
118
config_file = StringIO(sample_config_text)
119
parser = config._get_config_parser(file=config_file)
120
self.assertEqual("vim", config.get_editor(parser = parser))