/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/test_bedding.py

  • Committer: Jelmer Vernooij
  • Date: 2020-09-02 16:35:18 UTC
  • mto: (7490.40.109 work)
  • mto: This revision was merged to the branch mainline in revision 7526.
  • Revision ID: jelmer@jelmer.uk-20200902163518-sy9f4unbboljphgu
Handle duplicate directories entries for git.

Show diffs side-by-side

added added

removed removed

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