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