/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# Copyright (C) 2005-2014, 2016 Canonical Ltd
# Copyright (C) 2019 Breezy developers
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

"""Tests for deriving user configuration from system environment."""

import os
import sys

from .. import (
    bedding,
    osutils,
    tests,
    )


def override_whoami(test):
    test.overrideEnv('EMAIL', None)
    test.overrideEnv('BRZ_EMAIL', None)
    # Also, make sure that it's not inferred from mailname.
    test.overrideAttr(bedding, '_auto_user_id', lambda: (None, None))


class TestConfigPath(tests.TestCase):

    def setUp(self):
        super(TestConfigPath, self).setUp()
        self.overrideEnv('HOME', '/home/bogus')
        self.overrideEnv('XDG_CACHE_HOME', '')
        if sys.platform == 'win32':
            self.overrideEnv(
                'BRZ_HOME',
                r'C:\Documents and Settings\bogus\Application Data')
            self.brz_home = \
                'C:/Documents and Settings/bogus/Application Data/breezy'
        else:
            self.brz_home = '/home/bogus/.config/breezy'

    def test_config_dir(self):
        self.assertEqual(bedding.config_dir(), self.brz_home)

    def test_config_dir_is_unicode(self):
        self.assertIsInstance(bedding.config_dir(), str)

    def test_config_path(self):
        self.assertEqual(bedding.config_path(),
                         self.brz_home + '/breezy.conf')

    def test_locations_config_path(self):
        self.assertEqual(bedding.locations_config_path(),
                         self.brz_home + '/locations.conf')

    def test_authentication_config_path(self):
        self.assertEqual(bedding.authentication_config_path(),
                         self.brz_home + '/authentication.conf')


class TestConfigPathFallback(tests.TestCaseInTempDir):

    def setUp(self):
        super(TestConfigPathFallback, self).setUp()
        self.overrideEnv('HOME', self.test_dir)
        self.overrideEnv('XDG_CACHE_HOME', '')
        self.bzr_home = os.path.join(self.test_dir, '.bazaar')
        os.mkdir(self.bzr_home)

    def test_config_dir(self):
        self.assertEqual(bedding.config_dir(), self.bzr_home)

    def test_config_dir_is_unicode(self):
        self.assertIsInstance(bedding.config_dir(), str)

    def test_config_path(self):
        self.assertEqual(bedding.config_path(),
                         self.bzr_home + '/bazaar.conf')

    def test_locations_config_path(self):
        self.assertEqual(bedding.locations_config_path(),
                         self.bzr_home + '/locations.conf')

    def test_authentication_config_path(self):
        self.assertEqual(bedding.authentication_config_path(),
                         self.bzr_home + '/authentication.conf')


class TestXDGConfigDir(tests.TestCaseInTempDir):
    # must be in temp dir because config tests for the existence of the bazaar
    # subdirectory of $XDG_CONFIG_HOME

    def setUp(self):
        if sys.platform == 'win32':
            raise tests.TestNotApplicable(
                'XDG config dir not used on this platform')
        super(TestXDGConfigDir, self).setUp()
        self.overrideEnv('HOME', self.test_home_dir)
        # BRZ_HOME overrides everything we want to test so unset it.
        self.overrideEnv('BRZ_HOME', None)

    def test_xdg_config_dir_exists(self):
        """When ~/.config/bazaar exists, use it as the config dir."""
        newdir = osutils.pathjoin(self.test_home_dir, '.config', 'bazaar')
        os.makedirs(newdir)
        self.assertEqual(bedding.config_dir(), newdir)

    def test_xdg_config_home(self):
        """When XDG_CONFIG_HOME is set, use it."""
        xdgconfigdir = osutils.pathjoin(self.test_home_dir, 'xdgconfig')
        self.overrideEnv('XDG_CONFIG_HOME', xdgconfigdir)
        newdir = osutils.pathjoin(xdgconfigdir, 'bazaar')
        os.makedirs(newdir)
        self.assertEqual(bedding.config_dir(), newdir)


class TestDefaultMailDomain(tests.TestCaseInTempDir):
    """Test retrieving default domain from mailname file"""

    def test_default_mail_domain_simple(self):
        with open('simple', 'w') as f:
            f.write("domainname.com\n")
        r = bedding._get_default_mail_domain('simple')
        self.assertEqual('domainname.com', r)

    def test_default_mail_domain_no_eol(self):
        with open('no_eol', 'w') as f:
            f.write("domainname.com")
        r = bedding._get_default_mail_domain('no_eol')
        self.assertEqual('domainname.com', r)

    def test_default_mail_domain_multiple_lines(self):
        with open('multiple_lines', 'w') as f:
            f.write("domainname.com\nsome other text\n")
        r = bedding._get_default_mail_domain('multiple_lines')
        self.assertEqual('domainname.com', r)


class TestAutoUserId(tests.TestCase):
    """Test inferring an automatic user name."""

    def test_auto_user_id(self):
        """Automatic inference of user name.

        This is a bit hard to test in an isolated way, because it depends on
        system functions that go direct to /etc or perhaps somewhere else.
        But it's reasonable to say that on Unix, with an /etc/mailname, we ought
        to be able to choose a user name with no configuration.
        """
        if sys.platform == 'win32':
            raise tests.TestSkipped(
                "User name inference not implemented on win32")
        realname, address = bedding._auto_user_id()
        if os.path.exists('/etc/mailname'):
            self.assertIsNot(None, realname)
            self.assertIsNot(None, address)
        else:
            self.assertEqual((None, None), (realname, address))


class TestXDGCacheDir(tests.TestCaseInTempDir):
    # must be in temp dir because tests for the existence of the breezy
    # subdirectory of $XDG_CACHE_HOME

    def setUp(self):
        super(TestXDGCacheDir, self).setUp()
        if sys.platform in ('darwin', 'win32'):
            raise tests.TestNotApplicable(
                'XDG cache dir not used on this platform')
        self.overrideEnv('HOME', self.test_home_dir)
        # BZR_HOME overrides everything we want to test so unset it.
        self.overrideEnv('BZR_HOME', None)

    def test_xdg_cache_dir_exists(self):
        """When ~/.cache/breezy exists, use it as the cache dir."""
        cachedir = osutils.pathjoin(self.test_home_dir, '.cache')
        newdir = osutils.pathjoin(cachedir, 'breezy')
        self.assertEqual(bedding.cache_dir(), newdir)

    def test_xdg_cache_home_unix(self):
        """When XDG_CACHE_HOME is set, use it."""
        if sys.platform in ('nt', 'win32'):
            raise tests.TestNotApplicable(
                'XDG cache dir not used on this platform')
        xdgcachedir = osutils.pathjoin(self.test_home_dir, 'xdgcache')
        self.overrideEnv('XDG_CACHE_HOME', xdgcachedir)
        newdir = osutils.pathjoin(xdgcachedir, 'breezy')
        self.assertEqual(bedding.cache_dir(), newdir)