/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
7490.38.1 by Jelmer Vernooij
Create $XDG_HOME_DIR if it does not exist.
126
    def test_ensure_config_dir_exists(self):
127
        xdgconfigdir = osutils.pathjoin(self.test_home_dir, 'xdgconfig')
128
        self.overrideEnv('XDG_CONFIG_HOME', xdgconfigdir)
129
        bedding.ensure_config_dir_exists()
130
        newdir = osutils.pathjoin(xdgconfigdir, 'breezy')
131
        self.assertTrue(os.path.isdir(newdir))
132
7336.2.1 by Martin
Split non-ini config methods to bedding
133
134
class TestDefaultMailDomain(tests.TestCaseInTempDir):
135
    """Test retrieving default domain from mailname file"""
136
137
    def test_default_mail_domain_simple(self):
138
        with open('simple', 'w') as f:
139
            f.write("domainname.com\n")
140
        r = bedding._get_default_mail_domain('simple')
141
        self.assertEqual('domainname.com', r)
142
143
    def test_default_mail_domain_no_eol(self):
144
        with open('no_eol', 'w') as f:
145
            f.write("domainname.com")
146
        r = bedding._get_default_mail_domain('no_eol')
147
        self.assertEqual('domainname.com', r)
148
149
    def test_default_mail_domain_multiple_lines(self):
150
        with open('multiple_lines', 'w') as f:
151
            f.write("domainname.com\nsome other text\n")
152
        r = bedding._get_default_mail_domain('multiple_lines')
153
        self.assertEqual('domainname.com', r)
154
155
156
class TestAutoUserId(tests.TestCase):
157
    """Test inferring an automatic user name."""
158
159
    def test_auto_user_id(self):
160
        """Automatic inference of user name.
161
162
        This is a bit hard to test in an isolated way, because it depends on
163
        system functions that go direct to /etc or perhaps somewhere else.
164
        But it's reasonable to say that on Unix, with an /etc/mailname, we ought
165
        to be able to choose a user name with no configuration.
166
        """
167
        if sys.platform == 'win32':
168
            raise tests.TestSkipped(
169
                "User name inference not implemented on win32")
170
        realname, address = bedding._auto_user_id()
171
        if os.path.exists('/etc/mailname'):
172
            self.assertIsNot(None, realname)
173
            self.assertIsNot(None, address)
174
        else:
175
            self.assertEqual((None, None), (realname, address))
176
177
178
class TestXDGCacheDir(tests.TestCaseInTempDir):
179
    # must be in temp dir because tests for the existence of the breezy
180
    # subdirectory of $XDG_CACHE_HOME
181
182
    def setUp(self):
183
        super(TestXDGCacheDir, self).setUp()
184
        if sys.platform in ('darwin', 'win32'):
185
            raise tests.TestNotApplicable(
186
                'XDG cache dir not used on this platform')
187
        self.overrideEnv('HOME', self.test_home_dir)
188
        # BZR_HOME overrides everything we want to test so unset it.
189
        self.overrideEnv('BZR_HOME', None)
190
191
    def test_xdg_cache_dir_exists(self):
192
        """When ~/.cache/breezy exists, use it as the cache dir."""
193
        cachedir = osutils.pathjoin(self.test_home_dir, '.cache')
194
        newdir = osutils.pathjoin(cachedir, 'breezy')
195
        self.assertEqual(bedding.cache_dir(), newdir)
196
197
    def test_xdg_cache_home_unix(self):
198
        """When XDG_CACHE_HOME is set, use it."""
199
        if sys.platform in ('nt', 'win32'):
200
            raise tests.TestNotApplicable(
201
                'XDG cache dir not used on this platform')
202
        xdgcachedir = osutils.pathjoin(self.test_home_dir, 'xdgcache')
203
        self.overrideEnv('XDG_CACHE_HOME', xdgcachedir)
204
        newdir = osutils.pathjoin(xdgcachedir, 'breezy')
205
        self.assertEqual(bedding.cache_dir(), newdir)