/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
7336.2.1 by Martin
Split non-ini config methods to bedding
1
# Copyright (C) 2005-2014, 2016 Canonical Ltd
2
# Copyright (C) 2019 Breezy developers
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
18
"""Tests for deriving user configuration from system environment."""
19
20
import os
21
import sys
22
23
from ..sixish import (
24
    text_type,
25
    )
26
from .. import (
27
    bedding,
28
    osutils,
29
    tests,
30
    )
31
32
7344.1.1 by Martin
Fix tests that need whoami not to be set
33
def override_whoami(test):
34
    test.overrideEnv('EMAIL', None)
35
    test.overrideEnv('BRZ_EMAIL', None)
36
    # Also, make sure that it's not inferred from mailname.
37
    test.overrideAttr(bedding, '_auto_user_id', lambda: (None, None))
38
39
7336.2.1 by Martin
Split non-ini config methods to bedding
40
class TestConfigPath(tests.TestCase):
41
42
    def setUp(self):
43
        super(TestConfigPath, self).setUp()
44
        self.overrideEnv('HOME', '/home/bogus')
45
        self.overrideEnv('XDG_CACHE_HOME', '')
46
        if sys.platform == 'win32':
47
            self.overrideEnv(
48
                'BRZ_HOME',
49
                r'C:\Documents and Settings\bogus\Application Data')
50
            self.brz_home = \
51
                'C:/Documents and Settings/bogus/Application Data/breezy'
52
        else:
53
            self.brz_home = '/home/bogus/.config/breezy'
54
55
    def test_config_dir(self):
56
        self.assertEqual(bedding.config_dir(), self.brz_home)
57
58
    def test_config_dir_is_unicode(self):
59
        self.assertIsInstance(bedding.config_dir(), text_type)
60
61
    def test_config_path(self):
62
        self.assertEqual(bedding.config_path(),
63
                         self.brz_home + '/breezy.conf')
64
65
    def test_locations_config_path(self):
66
        self.assertEqual(bedding.locations_config_path(),
67
                         self.brz_home + '/locations.conf')
68
69
    def test_authentication_config_path(self):
70
        self.assertEqual(bedding.authentication_config_path(),
71
                         self.brz_home + '/authentication.conf')
72
73
74
class TestConfigPathFallback(tests.TestCaseInTempDir):
75
76
    def setUp(self):
77
        super(TestConfigPathFallback, self).setUp()
78
        self.overrideEnv('HOME', self.test_dir)
79
        self.overrideEnv('XDG_CACHE_HOME', '')
80
        self.bzr_home = os.path.join(self.test_dir, '.bazaar')
81
        os.mkdir(self.bzr_home)
82
83
    def test_config_dir(self):
84
        self.assertEqual(bedding.config_dir(), self.bzr_home)
85
86
    def test_config_dir_is_unicode(self):
87
        self.assertIsInstance(bedding.config_dir(), text_type)
88
89
    def test_config_path(self):
90
        self.assertEqual(bedding.config_path(),
91
                         self.bzr_home + '/bazaar.conf')
92
93
    def test_locations_config_path(self):
94
        self.assertEqual(bedding.locations_config_path(),
95
                         self.bzr_home + '/locations.conf')
96
97
    def test_authentication_config_path(self):
98
        self.assertEqual(bedding.authentication_config_path(),
99
                         self.bzr_home + '/authentication.conf')
100
101
102
class TestXDGConfigDir(tests.TestCaseInTempDir):
103
    # must be in temp dir because config tests for the existence of the bazaar
104
    # subdirectory of $XDG_CONFIG_HOME
105
106
    def setUp(self):
107
        if sys.platform == 'win32':
108
            raise tests.TestNotApplicable(
109
                'XDG config dir not used on this platform')
110
        super(TestXDGConfigDir, self).setUp()
111
        self.overrideEnv('HOME', self.test_home_dir)
112
        # BRZ_HOME overrides everything we want to test so unset it.
113
        self.overrideEnv('BRZ_HOME', None)
114
115
    def test_xdg_config_dir_exists(self):
116
        """When ~/.config/bazaar exists, use it as the config dir."""
117
        newdir = osutils.pathjoin(self.test_home_dir, '.config', 'bazaar')
118
        os.makedirs(newdir)
119
        self.assertEqual(bedding.config_dir(), newdir)
120
121
    def test_xdg_config_home(self):
122
        """When XDG_CONFIG_HOME is set, use it."""
123
        xdgconfigdir = osutils.pathjoin(self.test_home_dir, 'xdgconfig')
124
        self.overrideEnv('XDG_CONFIG_HOME', xdgconfigdir)
125
        newdir = osutils.pathjoin(xdgconfigdir, 'bazaar')
126
        os.makedirs(newdir)
127
        self.assertEqual(bedding.config_dir(), newdir)
128
129
130
class TestDefaultMailDomain(tests.TestCaseInTempDir):
131
    """Test retrieving default domain from mailname file"""
132
133
    def test_default_mail_domain_simple(self):
134
        with open('simple', 'w') as f:
135
            f.write("domainname.com\n")
136
        r = bedding._get_default_mail_domain('simple')
137
        self.assertEqual('domainname.com', r)
138
139
    def test_default_mail_domain_no_eol(self):
140
        with open('no_eol', 'w') as f:
141
            f.write("domainname.com")
142
        r = bedding._get_default_mail_domain('no_eol')
143
        self.assertEqual('domainname.com', r)
144
145
    def test_default_mail_domain_multiple_lines(self):
146
        with open('multiple_lines', 'w') as f:
147
            f.write("domainname.com\nsome other text\n")
148
        r = bedding._get_default_mail_domain('multiple_lines')
149
        self.assertEqual('domainname.com', r)
150
151
152
class TestAutoUserId(tests.TestCase):
153
    """Test inferring an automatic user name."""
154
155
    def test_auto_user_id(self):
156
        """Automatic inference of user name.
157
158
        This is a bit hard to test in an isolated way, because it depends on
159
        system functions that go direct to /etc or perhaps somewhere else.
160
        But it's reasonable to say that on Unix, with an /etc/mailname, we ought
161
        to be able to choose a user name with no configuration.
162
        """
163
        if sys.platform == 'win32':
164
            raise tests.TestSkipped(
165
                "User name inference not implemented on win32")
166
        realname, address = bedding._auto_user_id()
167
        if os.path.exists('/etc/mailname'):
168
            self.assertIsNot(None, realname)
169
            self.assertIsNot(None, address)
170
        else:
171
            self.assertEqual((None, None), (realname, address))
172
173
174
class TestXDGCacheDir(tests.TestCaseInTempDir):
175
    # must be in temp dir because tests for the existence of the breezy
176
    # subdirectory of $XDG_CACHE_HOME
177
178
    def setUp(self):
179
        super(TestXDGCacheDir, self).setUp()
180
        if sys.platform in ('darwin', 'win32'):
181
            raise tests.TestNotApplicable(
182
                'XDG cache dir not used on this platform')
183
        self.overrideEnv('HOME', self.test_home_dir)
184
        # BZR_HOME overrides everything we want to test so unset it.
185
        self.overrideEnv('BZR_HOME', None)
186
187
    def test_xdg_cache_dir_exists(self):
188
        """When ~/.cache/breezy exists, use it as the cache dir."""
189
        cachedir = osutils.pathjoin(self.test_home_dir, '.cache')
190
        newdir = osutils.pathjoin(cachedir, 'breezy')
191
        self.assertEqual(bedding.cache_dir(), newdir)
192
193
    def test_xdg_cache_home_unix(self):
194
        """When XDG_CACHE_HOME is set, use it."""
195
        if sys.platform in ('nt', 'win32'):
196
            raise tests.TestNotApplicable(
197
                'XDG cache dir not used on this platform')
198
        xdgcachedir = osutils.pathjoin(self.test_home_dir, 'xdgcache')
199
        self.overrideEnv('XDG_CACHE_HOME', xdgcachedir)
200
        newdir = osutils.pathjoin(xdgcachedir, 'breezy')
201
        self.assertEqual(bedding.cache_dir(), newdir)