1
# Copyright (C) 2005-2014, 2016 Canonical Ltd
2
# Copyright (C) 2019 Breezy developers
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.
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.
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
18
"""Tests for deriving user configuration from system environment."""
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))
37
class TestConfigPath(tests.TestCase):
40
super(TestConfigPath, self).setUp()
41
self.overrideEnv('HOME', '/home/bogus')
42
self.overrideEnv('XDG_CACHE_HOME', '')
43
if sys.platform == 'win32':
46
r'C:\Documents and Settings\bogus\Application Data')
48
'C:/Documents and Settings/bogus/Application Data/breezy'
50
self.brz_home = '/home/bogus/.config/breezy'
52
def test_config_dir(self):
53
self.assertEqual(bedding.config_dir(), self.brz_home)
55
def test_config_dir_is_unicode(self):
56
self.assertIsInstance(bedding.config_dir(), str)
58
def test_config_path(self):
59
self.assertEqual(bedding.config_path(),
60
self.brz_home + '/breezy.conf')
62
def test_locations_config_path(self):
63
self.assertEqual(bedding.locations_config_path(),
64
self.brz_home + '/locations.conf')
66
def test_authentication_config_path(self):
67
self.assertEqual(bedding.authentication_config_path(),
68
self.brz_home + '/authentication.conf')
71
class TestConfigPathFallback(tests.TestCaseInTempDir):
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)
80
def test_config_dir(self):
81
self.assertEqual(bedding.config_dir(), self.bzr_home)
83
def test_config_dir_is_unicode(self):
84
self.assertIsInstance(bedding.config_dir(), str)
86
def test_config_path(self):
87
self.assertEqual(bedding.config_path(),
88
self.bzr_home + '/bazaar.conf')
90
def test_locations_config_path(self):
91
self.assertEqual(bedding.locations_config_path(),
92
self.bzr_home + '/locations.conf')
94
def test_authentication_config_path(self):
95
self.assertEqual(bedding.authentication_config_path(),
96
self.bzr_home + '/authentication.conf')
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
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)
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')
116
self.assertEqual(bedding.config_dir(), newdir)
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')
124
self.assertEqual(bedding.config_dir(), newdir)
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))
134
class TestDefaultMailDomain(tests.TestCaseInTempDir):
135
"""Test retrieving default domain from mailname file"""
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)
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)
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)
156
class TestAutoUserId(tests.TestCase):
157
"""Test inferring an automatic user name."""
159
def test_auto_user_id(self):
160
"""Automatic inference of user name.
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.
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)
175
self.assertEqual((None, None), (realname, address))
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
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)
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)
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)