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