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