-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathtest_paths.py
More file actions
219 lines (166 loc) · 7.13 KB
/
test_paths.py
File metadata and controls
219 lines (166 loc) · 7.13 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
from __future__ import division, absolute_import, print_function
import confuse
import ntpath
import os
import platform
import posixpath
import shutil
import tempfile
import unittest
DEFAULT = [platform.system, os.environ, os.path]
SYSTEMS = {
'Linux': [{'HOME': '/home/test', 'XDG_CONFIG_HOME': '~/xdgconfig'},
posixpath],
'Darwin': [{'HOME': '/Users/test'}, posixpath],
'Windows': [{'APPDATA': '~\\winconfig', 'HOME': 'C:\\Users\\test'}, ntpath]
}
def _touch(path):
open(path, 'a').close()
class FakeSystem(unittest.TestCase):
SYS_NAME = None
TMP_HOME = False
def setUp(self):
if self.SYS_NAME in SYSTEMS:
self.os_path = os.path
os.environ = {}
environ, os.path = SYSTEMS[self.SYS_NAME]
os.environ.update(environ) # copy
platform.system = lambda: self.SYS_NAME
if self.TMP_HOME:
self.home = tempfile.mkdtemp()
os.environ['HOME'] = self.home
def tearDown(self):
platform.system, os.environ, os.path = DEFAULT
if hasattr(self, 'home'):
shutil.rmtree(self.home)
class LinuxTestCases(FakeSystem):
SYS_NAME = 'Linux'
def test_both_xdg_and_fallback_dirs(self):
self.assertEqual(confuse.config_dirs(),
['/home/test/.config', '/home/test/xdgconfig',
'/etc/xdg', '/etc'])
def test_fallback_only(self):
del os.environ['XDG_CONFIG_HOME']
self.assertEqual(confuse.config_dirs(), ['/home/test/.config',
'/etc/xdg', '/etc'])
def test_xdg_matching_fallback_not_duplicated(self):
os.environ['XDG_CONFIG_HOME'] = '~/.config'
self.assertEqual(confuse.config_dirs(), ['/home/test/.config',
'/etc/xdg', '/etc'])
def test_xdg_config_dirs(self):
os.environ['XDG_CONFIG_DIRS'] = '/usr/local/etc/xdg:/etc/xdg'
self.assertEqual(confuse.config_dirs(), ['/home/test/.config',
'/home/test/xdgconfig',
'/usr/local/etc/xdg',
'/etc/xdg', '/etc'])
class OSXTestCases(FakeSystem):
SYS_NAME = 'Darwin'
def test_mac_dirs(self):
self.assertEqual(confuse.config_dirs(),
['/Users/test/.config',
'/Users/test/Library/Application Support',
'/etc/xdg', '/etc'])
def test_xdg_config_dirs(self):
os.environ['XDG_CONFIG_DIRS'] = '/usr/local/etc/xdg:/etc/xdg'
self.assertEqual(confuse.config_dirs(),
['/Users/test/.config',
'/Users/test/Library/Application Support',
'/usr/local/etc/xdg',
'/etc/xdg', '/etc'])
class WindowsTestCases(FakeSystem):
SYS_NAME = 'Windows'
def test_dir_from_environ(self):
self.assertEqual(confuse.config_dirs(),
['C:\\Users\\test\\AppData\\Roaming',
'C:\\Users\\test\\winconfig'])
def test_fallback_dir(self):
del os.environ['APPDATA']
self.assertEqual(confuse.config_dirs(),
['C:\\Users\\test\\AppData\\Roaming'])
class ConfigFilenamesTest(unittest.TestCase):
def setUp(self):
self._old = os.path.isfile, confuse.load_yaml
os.path.isfile, confuse.load_yaml = lambda x: True, lambda x: {}
def tearDown(self):
confuse.load_yaml, os.path.isfile = self._old
def test_no_sources_when_files_missing(self):
config = confuse.Configuration('myapp', read=False)
filenames = [s.filename for s in config.sources]
self.assertEqual(filenames, [])
def test_search_package(self):
config = confuse.Configuration('myapp', __name__, read=False)
config.read(user=False, defaults=True)
for source in config.sources:
if source.default:
default_source = source
break
else:
self.fail("no default source")
self.assertEqual(
default_source.filename,
os.path.join(os.path.dirname(__file__), 'config_default.yaml')
)
self.assertTrue(source.default)
class EnvVarTest(FakeSystem):
TMP_HOME = True
def setUp(self):
super(EnvVarTest, self).setUp()
os.environ['MYAPPDIR'] = self.home # use the tmp home as a config dir
@property
def config(self):
return confuse.Configuration('myapp', read=False)
def test_env_var_name(self):
self.assertEqual(self.config._env_var, 'MYAPPDIR')
def test_env_var_dir_has_first_priority(self):
self.assertEqual(self.config.config_dir(), self.home)
def test_env_var_missing(self):
del os.environ['MYAPPDIR']
self.assertNotEqual(self.config.config_dir(), self.home)
class PrimaryConfigDirTest(FakeSystem):
SYS_NAME = 'Linux' # conversion from posix to nt is easy
TMP_HOME = True
if platform.system() == 'Windows':
# wrap these functions as they need to work on the host system which is
# only needed on Windows as we are using `posixpath`
def join(self, *args):
return self.os_path.normpath(self.os_path.join(*args))
def makedirs(self, path, *args):
os.path, os_path = self.os_path, os.path
self._makedirs(path, *args)
os.path = os_path
def setUp(self):
super(PrimaryConfigDirTest, self).setUp()
if hasattr(self, 'join'):
os.path.join = self.join
os.makedirs, self._makedirs = self.makedirs, os.makedirs
@property
def config(self):
return confuse.Configuration('test', read=False)
def tearDown(self):
super(PrimaryConfigDirTest, self).tearDown()
if hasattr(self, '_makedirs'):
os.makedirs = self._makedirs
def test_create_dir_if_none_exists(self):
path = os.path.join(self.home, '.config', 'test')
assert not os.path.exists(path)
self.assertEqual(self.config.config_dir(), path)
self.assertTrue(os.path.isdir(path))
def test_return_existing_dir(self):
path = os.path.join(self.home, 'xdgconfig', 'test')
os.makedirs(path)
_touch(os.path.join(path, confuse.CONFIG_FILENAME))
self.assertEqual(self.config.config_dir(), path)
def test_do_not_create_dir_if_lower_priority_exists(self):
path1 = os.path.join(self.home, 'xdgconfig', 'test')
path2 = os.path.join(self.home, '.config', 'test')
os.makedirs(path2)
_touch(os.path.join(path2, confuse.CONFIG_FILENAME))
assert not os.path.exists(path1)
assert os.path.exists(path2)
self.assertEqual(self.config.config_dir(), path2)
self.assertFalse(os.path.isdir(path1))
self.assertTrue(os.path.isdir(path2))
def test_override_config_dir(self):
path = os.path.join(self.home, 'asdfasdfasdfd', 'test')
config = confuse.Configuration('test', source=path, read=False)
self.assertEqual(config.config_dir(), path)