~bzr-pqm/bzr/bzr.dev

1442.1.1 by Robert Collins
move config_dir into bzrlib.config
1
# Copyright (C) 2005 by Canonical Ltd
2
#   Authors: Robert Collins <robert.collins@canonical.com>
3
#
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.
8
#
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.
13
#
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
17
18
"""Tests for finding and reading the bzr config file[s]."""
19
# import system imports here
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
20
from ConfigParser import ConfigParser
21
from cStringIO import StringIO
1442.1.1 by Robert Collins
move config_dir into bzrlib.config
22
import os
23
import sys
24
25
#import bzrlib specific imports here
26
import bzrlib.config as config
27
from bzrlib.selftest import TestCase, TestCaseInTempDir
28
29
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
30
sample_config_text = ("[DEFAULT]\n"
31
                      "email=Robert Collins <robertc@example.com>\n"
32
                      "editor=vim\n"
33
                      "gpg_signing_command=gnome-gpg\n")
34
35
class InstrumentedConfigParser(object):
36
    """A config parser look-enough-alike to record calls made to it."""
37
38
    def __init__(self):
39
        self._calls = []
40
41
    def read(self, filenames):
42
        self._calls.append(('read', filenames))
43
44
1442.1.1 by Robert Collins
move config_dir into bzrlib.config
45
class TestConfigPath(TestCase):
46
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
47
    def setUp(self):
48
        super(TestConfigPath, self).setUp()
49
        self.oldenv = os.environ.get('HOME', None)
50
        os.environ['HOME'] = '/home/bogus'
51
52
    def tearDown(self):
53
        os.environ['HOME'] = self.oldenv
54
    
1442.1.1 by Robert Collins
move config_dir into bzrlib.config
55
    def test_config_dir(self):
1442.1.2 by Robert Collins
create a config module - there is enough config logic to make this worthwhile, and start testing config processing.
56
        self.assertEqual(config.config_dir(), '/home/bogus/.bazaar')
57
58
    def test_config_filename(self):
59
        self.assertEqual(config.config_filename(),
60
                         '/home/bogus/.bazaar/bazaar.conf')
61
62
63
class TestGetConfig(TestCase):
64
65
    def test_from_fp(self):
66
        config_file = StringIO(sample_config_text)
67
        self.failUnless(isinstance(config._get_config_parser(file=config_file),
68
                        ConfigParser))
69
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
76
        try:
77
            parser = config._get_config_parser()
78
        finally:
79
            config.ConfigParser = oldparserclass
80
        self.failUnless(isinstance(parser, InstrumentedConfigParser))
81
        self.assertEqual(parser._calls, [('read', [config.config_filename()])])
82
83
84
class TestConfigItems(TestCase):
85
86
    def setUp(self):
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()
96
97
    def tearDown(self):
98
        os.environ['HOME'] = self.oldenv
99
        if self.bzr_email is not None:
100
            os.environ['BZREMAIL'] = bzr_email
101
        if self.email is not None:
102
            os.environ['EMAIL'] = email
103
        super(TestConfigItems, self).tearDown()
104
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))
110
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))
1442.1.3 by Robert Collins
move editor into the config file too
116
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))