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."""
23
from ..sixish import (
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))
40
class TestConfigPath(tests.TestCase):
43
super(TestConfigPath, self).setUp()
44
self.overrideEnv('HOME', '/home/bogus')
45
self.overrideEnv('XDG_CACHE_HOME', '')
46
if sys.platform == 'win32':
49
r'C:\Documents and Settings\bogus\Application Data')
51
'C:/Documents and Settings/bogus/Application Data/breezy'
53
self.brz_home = '/home/bogus/.config/breezy'
55
def test_config_dir(self):
56
self.assertEqual(bedding.config_dir(), self.brz_home)
58
def test_config_dir_is_unicode(self):
59
self.assertIsInstance(bedding.config_dir(), text_type)
61
def test_config_path(self):
62
self.assertEqual(bedding.config_path(),
63
self.brz_home + '/breezy.conf')
65
def test_locations_config_path(self):
66
self.assertEqual(bedding.locations_config_path(),
67
self.brz_home + '/locations.conf')
69
def test_authentication_config_path(self):
70
self.assertEqual(bedding.authentication_config_path(),
71
self.brz_home + '/authentication.conf')
74
class TestConfigPathFallback(tests.TestCaseInTempDir):
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)
83
def test_config_dir(self):
84
self.assertEqual(bedding.config_dir(), self.bzr_home)
86
def test_config_dir_is_unicode(self):
87
self.assertIsInstance(bedding.config_dir(), text_type)
89
def test_config_path(self):
90
self.assertEqual(bedding.config_path(),
91
self.bzr_home + '/bazaar.conf')
93
def test_locations_config_path(self):
94
self.assertEqual(bedding.locations_config_path(),
95
self.bzr_home + '/locations.conf')
97
def test_authentication_config_path(self):
98
self.assertEqual(bedding.authentication_config_path(),
99
self.bzr_home + '/authentication.conf')
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
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)
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')
119
self.assertEqual(bedding.config_dir(), newdir)
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')
127
self.assertEqual(bedding.config_dir(), newdir)
130
class TestDefaultMailDomain(tests.TestCaseInTempDir):
131
"""Test retrieving default domain from mailname file"""
133
def test_default_mail_domain_simple(self):
134
with open('simple', 'w') as f:
135
f.write("domainname.com\n")
136
r = bedding._get_default_mail_domain('simple')
137
self.assertEqual('domainname.com', r)
139
def test_default_mail_domain_no_eol(self):
140
with open('no_eol', 'w') as f:
141
f.write("domainname.com")
142
r = bedding._get_default_mail_domain('no_eol')
143
self.assertEqual('domainname.com', r)
145
def test_default_mail_domain_multiple_lines(self):
146
with open('multiple_lines', 'w') as f:
147
f.write("domainname.com\nsome other text\n")
148
r = bedding._get_default_mail_domain('multiple_lines')
149
self.assertEqual('domainname.com', r)
152
class TestAutoUserId(tests.TestCase):
153
"""Test inferring an automatic user name."""
155
def test_auto_user_id(self):
156
"""Automatic inference of user name.
158
This is a bit hard to test in an isolated way, because it depends on
159
system functions that go direct to /etc or perhaps somewhere else.
160
But it's reasonable to say that on Unix, with an /etc/mailname, we ought
161
to be able to choose a user name with no configuration.
163
if sys.platform == 'win32':
164
raise tests.TestSkipped(
165
"User name inference not implemented on win32")
166
realname, address = bedding._auto_user_id()
167
if os.path.exists('/etc/mailname'):
168
self.assertIsNot(None, realname)
169
self.assertIsNot(None, address)
171
self.assertEqual((None, None), (realname, address))
174
class TestXDGCacheDir(tests.TestCaseInTempDir):
175
# must be in temp dir because tests for the existence of the breezy
176
# subdirectory of $XDG_CACHE_HOME
179
super(TestXDGCacheDir, self).setUp()
180
if sys.platform in ('darwin', 'win32'):
181
raise tests.TestNotApplicable(
182
'XDG cache dir not used on this platform')
183
self.overrideEnv('HOME', self.test_home_dir)
184
# BZR_HOME overrides everything we want to test so unset it.
185
self.overrideEnv('BZR_HOME', None)
187
def test_xdg_cache_dir_exists(self):
188
"""When ~/.cache/breezy exists, use it as the cache dir."""
189
cachedir = osutils.pathjoin(self.test_home_dir, '.cache')
190
newdir = osutils.pathjoin(cachedir, 'breezy')
191
self.assertEqual(bedding.cache_dir(), newdir)
193
def test_xdg_cache_home_unix(self):
194
"""When XDG_CACHE_HOME is set, use it."""
195
if sys.platform in ('nt', 'win32'):
196
raise tests.TestNotApplicable(
197
'XDG cache dir not used on this platform')
198
xdgcachedir = osutils.pathjoin(self.test_home_dir, 'xdgcache')
199
self.overrideEnv('XDG_CACHE_HOME', xdgcachedir)
200
newdir = osutils.pathjoin(xdgcachedir, 'breezy')
201
self.assertEqual(bedding.cache_dir(), newdir)