/
test.py
308 lines (255 loc) · 8.61 KB
/
test.py
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# vim: fileencoding=utf-8
# Copyright (c) 2013 Mikhail Golubev
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
from contextlib import contextmanager
import tempfile
import time
import logging
import os
import pot
from pot import cd, Config, DotFile
import nose
from nose.tools import ok_, eq_, nottest
logging.basicConfig(level=logging.CRITICAL)
try:
from tempfile import TemporaryDirectory as make_temp_dir
except ImportError:
@contextmanager
def make_temp_dir(suffix='', prefix='tmp', dir=None):
temp_dir = tempfile.mkdtemp(suffix, prefix, dir)
try:
yield temp_dir
finally:
# shutil.rmtree(temp_dir)
pass
try:
import builtins
if not hasattr(builtins, 'callable'):
callable = lambda x: hasattr(x, '__call__')
except ImportError:
# python2.7 has 'callable' BIF
pass
# os.path.getmtime() follows symlinks by default. I've got a lot of creepy bugs
# because of that before I realized what happens.
# As a workaround I use lstat to get modification time without following symlinks.
# Actually in python3.3 you can get nanoseconds precision because result of lstat there
# has st_mtime_ns field. I use delays as compatibility workaround.
def mtime(path):
return os.lstat(path).st_mtime
def delayed(before=0, after=0):
def decorator(f):
def wrapper(*args, **kwargs):
time.sleep(before)
res = f(*args, **kwargs)
time.sleep(after)
return res
return wrapper
return decorator
@contextmanager
def temp_cwd(suffix='', prefix='tmp', dir=None):
with make_temp_dir(suffix, prefix, dir) as tempd:
with cd(tempd):
yield tempd
@contextmanager
def assert_modified(path):
path = os.path.abspath(path)
# can't get modification time or read broken symlink
# assume it's always modified
if pot.broken_link(path):
yield
return
old_mtime = mtime(path)
was_file = False
if os.path.isfile(path):
was_file = True
with open(path) as fd:
old_content = fd.read()
yield
if old_mtime < mtime(path):
return
# compare file content only if system mtime resolution isn't enough
if os.path.isfile(path) and was_file:
with open(path) as fd:
content = fd.read()
assert old_content != content, 'File "{}" was not modified'.format(path)
else:
raise AssertionError('File "{}" was not modified'.format(path))
@contextmanager
def assert_not_modified(path):
try:
with assert_modified(path):
yield
except AssertionError:
pass
else:
raise AssertionError('File "{}" was modified'.format(path))
def make_hierarchy(d):
for key, val in d.items():
if isinstance(val, dict):
os.mkdir(key)
with cd(key):
make_hierarchy(val)
elif callable(val):
val(key)
else:
with open(key, 'w') as fd:
fd.write(val)
@contextmanager
def updated_env(**kwargs):
old_env = os.environ.copy()
os.environ.update(kwargs)
yield
os.environ = old_env
def test_config_serialization():
config = Config([
DotFile(name='.vimrc', target='~/_vimrc', action='symlink'),
DotFile(name='.bashrc', action='include'),
DotFile(name='rc.conf', target='.config/openbox/rc.conf', action='copy')
])
expected_string = """\
dotfiles:
- name: .vimrc
target: ~/_vimrc
action: symlink
- name: .bashrc
target: ~/.bashrc
action: include
- name: rc.conf
target: .config/openbox/rc.conf
action: copy
"""
eq_(expected_string, config.to_yaml())
def test_init():
with temp_cwd(prefix='pot-test'):
make_hierarchy({
'dotfiles': {
'.gitconfig': '',
'.vimrc': '',
'.vim': {},
}
})
pot.init(path='.')
expected_config = Config(dotfiles=[DotFile('.gitconfig'), DotFile('.vimrc'), DotFile('.vim'), ])
with open('config.yaml') as fd:
builded_config = Config.from_yaml(fd)
eq_(expected_config, builded_config)
def test_install():
config = """\
dotfiles:
- name: .vimrc
target: ~/.vimrc
action: symlink
- name: .bashrc
target: ~/.bashrc
action: include
- name: .vim
target: ../somedir/vimfiles
action: copy
"""
with temp_cwd(prefix='pot-test'):
make_hierarchy({
'pot': {
'dotfiles': {
'.vim': {},
'.vimrc': '1\n',
'.bashrc': '2\n',
'.gitconfig': ''
},
'config.yaml': config
},
'home': {
'.gitconfig': 'already exists\n',
'.bashrc': ''
},
'somedir': {}
})
with assert_not_modified('home/.gitconfig'):
with updated_env(HOME=os.path.abspath('home')):
with cd('pot'):
pot.install()
# symlink pointing to correct location created
eq_(os.readlink('home/.vimrc'), os.path.abspath('pot/dotfiles/.vimrc'))
# new directory created
ok_(os.path.isdir('somedir/vimfiles'))
# configuration included
eq_(open('home/.bashrc').read(), '. {}\n'.format(os.path.abspath('pot/dotfiles/.bashrc')))
@nottest
def _test_existing(content, modified=False, force=False):
with temp_cwd(prefix='pot-test'):
make_hierarchy({
'pot': {
'dotfiles': {
'dotfile': ''
},
'config.yaml': 'dotfiles: [{name: dotfile}]'
},
'home': {
'dotfile': content
}
})
# default system mtime resolution is not sufficient
# time.sleep(2)
mgr = assert_modified if modified else assert_not_modified
with mgr('home/dotfile'):
with updated_env(HOME=os.path.abspath('home')):
with cd('pot'):
pot.install(['dotfile'], force=force)
def test_existing_file():
_test_existing('other file', modified=False, force=False)
def test_existing_dir():
_test_existing({}, modified=False, force=False)
def test_existing_symlink():
_test_existing(lambda x: os.symlink('../pot', x), modified=False, force=False)
def test_symlink_to_same_file():
_test_existing(delayed(0.1, 0.1)(lambda x: os.symlink('../pot/dotfiles/dotfile', x)), modified=True, force=False)
def test_broken_symlink():
_test_existing(lambda x: os.symlink('not-exists', x), modified=True, force=False)
def test_force_mode():
cases = [
'other file',
delayed(0.05, 0.05)(lambda x: os.mkdir(x)),
delayed(0.05, 0.05)(lambda x: os.symlink('.', x)), # valid symlink
lambda x: os.symlink('not-exists', x) # broken symlink
]
for content in cases:
yield _test_existing, content, True, True
def test_grab():
with temp_cwd(prefix='pot-test'):
make_hierarchy({
'home': {
'.pot': {
'dotfiles': {
'foo': ''
},
},
},
'foo': '',
'bar': ''
})
time.sleep(0.1)
with assert_modified('bar'):
with updated_env(POT_HOME=os.path.abspath('home/.pot')):
pot.grab('bar')
ok_(pot.same_file_symlink('bar', 'home/.pot/dotfiles/bar'))
with assert_not_modified('foo'):
with updated_env(POT_HOME=os.path.abspath('home/.pot')):
pot.grab('foo')
if __name__ == '__main__':
nose.core.runmodule()