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
class TestConfigPath(tests.TestCase):
36
super(TestConfigPath, self).setUp()
37
self.overrideEnv('HOME', '/home/bogus')
38
self.overrideEnv('XDG_CACHE_HOME', '')
39
if sys.platform == 'win32':
42
r'C:\Documents and Settings\bogus\Application Data')
44
'C:/Documents and Settings/bogus/Application Data/breezy'
46
self.brz_home = '/home/bogus/.config/breezy'
48
def test_config_dir(self):
49
self.assertEqual(bedding.config_dir(), self.brz_home)
51
def test_config_dir_is_unicode(self):
52
self.assertIsInstance(bedding.config_dir(), text_type)
54
def test_config_path(self):
55
self.assertEqual(bedding.config_path(),
56
self.brz_home + '/breezy.conf')
58
def test_locations_config_path(self):
59
self.assertEqual(bedding.locations_config_path(),
60
self.brz_home + '/locations.conf')
62
def test_authentication_config_path(self):
63
self.assertEqual(bedding.authentication_config_path(),
64
self.brz_home + '/authentication.conf')
67
class TestConfigPathFallback(tests.TestCaseInTempDir):
70
super(TestConfigPathFallback, self).setUp()
71
self.overrideEnv('HOME', self.test_dir)
72
self.overrideEnv('XDG_CACHE_HOME', '')
73
self.bzr_home = os.path.join(self.test_dir, '.bazaar')
74
os.mkdir(self.bzr_home)
76
def test_config_dir(self):
77
self.assertEqual(bedding.config_dir(), self.bzr_home)
79
def test_config_dir_is_unicode(self):
80
self.assertIsInstance(bedding.config_dir(), text_type)
82
def test_config_path(self):
83
self.assertEqual(bedding.config_path(),
84
self.bzr_home + '/bazaar.conf')
86
def test_locations_config_path(self):
87
self.assertEqual(bedding.locations_config_path(),
88
self.bzr_home + '/locations.conf')
90
def test_authentication_config_path(self):
91
self.assertEqual(bedding.authentication_config_path(),
92
self.bzr_home + '/authentication.conf')
95
class TestXDGConfigDir(tests.TestCaseInTempDir):
96
# must be in temp dir because config tests for the existence of the bazaar
97
# subdirectory of $XDG_CONFIG_HOME
100
if sys.platform == 'win32':
101
raise tests.TestNotApplicable(
102
'XDG config dir not used on this platform')
103
super(TestXDGConfigDir, self).setUp()
104
self.overrideEnv('HOME', self.test_home_dir)
105
# BRZ_HOME overrides everything we want to test so unset it.
106
self.overrideEnv('BRZ_HOME', None)
108
def test_xdg_config_dir_exists(self):
109
"""When ~/.config/bazaar exists, use it as the config dir."""
110
newdir = osutils.pathjoin(self.test_home_dir, '.config', 'bazaar')
112
self.assertEqual(bedding.config_dir(), newdir)
114
def test_xdg_config_home(self):
115
"""When XDG_CONFIG_HOME is set, use it."""
116
xdgconfigdir = osutils.pathjoin(self.test_home_dir, 'xdgconfig')
117
self.overrideEnv('XDG_CONFIG_HOME', xdgconfigdir)
118
newdir = osutils.pathjoin(xdgconfigdir, 'bazaar')
120
self.assertEqual(bedding.config_dir(), newdir)
123
class TestDefaultMailDomain(tests.TestCaseInTempDir):
124
"""Test retrieving default domain from mailname file"""
126
def test_default_mail_domain_simple(self):
127
with open('simple', 'w') as f:
128
f.write("domainname.com\n")
129
r = bedding._get_default_mail_domain('simple')
130
self.assertEqual('domainname.com', r)
132
def test_default_mail_domain_no_eol(self):
133
with open('no_eol', 'w') as f:
134
f.write("domainname.com")
135
r = bedding._get_default_mail_domain('no_eol')
136
self.assertEqual('domainname.com', r)
138
def test_default_mail_domain_multiple_lines(self):
139
with open('multiple_lines', 'w') as f:
140
f.write("domainname.com\nsome other text\n")
141
r = bedding._get_default_mail_domain('multiple_lines')
142
self.assertEqual('domainname.com', r)
145
class TestAutoUserId(tests.TestCase):
146
"""Test inferring an automatic user name."""
148
def test_auto_user_id(self):
149
"""Automatic inference of user name.
151
This is a bit hard to test in an isolated way, because it depends on
152
system functions that go direct to /etc or perhaps somewhere else.
153
But it's reasonable to say that on Unix, with an /etc/mailname, we ought
154
to be able to choose a user name with no configuration.
156
if sys.platform == 'win32':
157
raise tests.TestSkipped(
158
"User name inference not implemented on win32")
159
realname, address = bedding._auto_user_id()
160
if os.path.exists('/etc/mailname'):
161
self.assertIsNot(None, realname)
162
self.assertIsNot(None, address)
164
self.assertEqual((None, None), (realname, address))
167
class TestXDGCacheDir(tests.TestCaseInTempDir):
168
# must be in temp dir because tests for the existence of the breezy
169
# subdirectory of $XDG_CACHE_HOME
172
super(TestXDGCacheDir, self).setUp()
173
if sys.platform in ('darwin', 'win32'):
174
raise tests.TestNotApplicable(
175
'XDG cache dir not used on this platform')
176
self.overrideEnv('HOME', self.test_home_dir)
177
# BZR_HOME overrides everything we want to test so unset it.
178
self.overrideEnv('BZR_HOME', None)
180
def test_xdg_cache_dir_exists(self):
181
"""When ~/.cache/breezy exists, use it as the cache dir."""
182
cachedir = osutils.pathjoin(self.test_home_dir, '.cache')
183
newdir = osutils.pathjoin(cachedir, 'breezy')
184
self.assertEqual(bedding.cache_dir(), newdir)
186
def test_xdg_cache_home_unix(self):
187
"""When XDG_CACHE_HOME is set, use it."""
188
if sys.platform in ('nt', 'win32'):
189
raise tests.TestNotApplicable(
190
'XDG cache dir not used on this platform')
191
xdgcachedir = osutils.pathjoin(self.test_home_dir, 'xdgcache')
192
self.overrideEnv('XDG_CACHE_HOME', xdgcachedir)
193
newdir = osutils.pathjoin(xdgcachedir, 'breezy')
194
self.assertEqual(bedding.cache_dir(), newdir)