-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.py
executable file
·559 lines (442 loc) · 17.6 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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
#!/usr/bin/env python3
import sys
import os
import shutil
import imp
import unittest
import tempfile
import subprocess
pj = os.path.join
HOMECTL_ROOT = os.path.realpath(os.path.dirname(sys.argv[0]))
HOMECTL_BIN = pj(HOMECTL_ROOT, 'homectl.hcpkg', 'python', 'homectl.py')
TESTDATA_DIR = pj(HOMECTL_ROOT, "testdata")
# Since the hc command isn't really intended to be used as a module, we need to
# fake it. This is a glorified "import hc", if hc were in fact importable.
hc = imp.load_source('homectl', HOMECTL_BIN)
# Since git doesn't allow you to store empty directories in version control, we
# have to create them here, so they exist in our test data set. (XXX Since a
# user's setup is stored in git, probably we should just make homectl ignore
# them entirely, but that's a project for another day.)
for d in [
("package-empty.hcpkg",),
("package-full.hcpkg", "Darwin", "lib"),
("package-full.hcpkg", "Linux", "lib"),
]:
hc.mkdirp(pj(TESTDATA_DIR, *d))
def fileset(base, *paths):
return set([(f, pj(base, f)) for f in paths])
def file_contents(path):
with open(path, 'r') as f:
return f.read()
class HomectlTest(unittest.TestCase):
# Base class for other homectl tests; handles creating and tearing down a
# homectl environment in the filesystem.
# Useful packages in testdata
EMPTY = pj(TESTDATA_DIR, 'package-empty.hcpkg')
FULL = pj(TESTDATA_DIR, 'package-full.hcpkg')
def setUp(self):
self.dir = os.path.realpath(tempfile.mkdtemp(prefix='homectl-selftest-'))
self.old_pwd = os.getcwd()
self.old_env = dict(os.environ)
os.environ.clear()
os.environ.update({
'SHELL': '/bin/sh',
'PATH': '/usr/bin:/bin',
'TERM': self.old_env['TERM'],
'HOME': self.dir,
'LOGNAME': self.old_env['LOGNAME'],
'USER': self.old_env['USER'],
'LANG': self.old_env['LANG'],
})
os.chdir(self.dir)
def tearDown(self):
shutil.rmtree(self.dir)
os.environ.clear()
os.environ.update(self.old_env)
os.chdir(self.old_pwd)
class SilentSystem(hc.System):
def __init__(self):
super(SilentSystem, self).__init__(pretend=False)
self.__log = []
def log(self, msg):
self.__log.append(msg)
def with_system(fn):
def decorator(self):
s = SilentSystem()
#s = hc.ConsoleSystem()
fn(self, s)
return decorator
def with_deployment(fn):
def decorator(self):
s = SilentSystem()
#s = hc.ConsoleSystem()
d = hc.Deployment(s, os.getcwd(), os.path.join(os.getcwd(), '.homectl'))
fn(self, d)
return decorator
class UtilTest(HomectlTest):
# Small tests for utility functions.
DATA = pj(TESTDATA_DIR, 'util')
def test_mkdirp(self):
hc.mkdirp('') # should do nothing
hc.mkdirp('.') # should do nothing
hc.mkdirp('..') # should do nothing
hc.mkdirp('foo')
self.assertEqual(os.path.isdir('foo'), True)
hc.mkdirp(pj('outer', 'inner'))
self.assertEqual(os.path.isdir('outer'), True)
self.assertEqual(os.path.isdir(pj('outer', 'inner')), True)
def test_visible_dirs(self):
self.assertEqual(
set(hc.visible_dirs(self.DATA)),
fileset(self.DATA,
'visible-dir',
'visible-link-to-dir',
))
def test_visible_links(self):
self.assertEqual(
set(hc.visible_links(self.DATA)),
fileset(self.DATA,
'visible-link',
'visible-link-to-dir',
))
def test_fs_tree(self):
self.assertEqual(
set(hc.fs_tree(self.DATA)),
fileset(self.DATA,
".invisible",
".invisible-dir",
".invisible-dir/foo",
".invisible-link",
"visible",
"visible-dir",
"visible-dir/bar",
"visible-dir/foo",
"visible-link",
"visible-link-to-dir",
))
def test_fs_files_in(self):
self.assertEqual(
set(hc.fs_files_in(self.DATA)),
fileset(self.DATA,
".invisible",
".invisible-dir/foo",
".invisible-link",
"visible",
"visible-dir/bar",
"visible-dir/foo",
"visible-link",
"visible-link-to-dir",
))
def test_sh_quote(self):
for t, a in [
("foo", "foo"),
("foo\"", "foo\\\""),
("foo bar", "\"foo bar\""),
("foo\nbar", "foo\\nbar"),
("foo\rbar", "foo\\rbar"),
("foo\tbar", "foo\\tbar"),
("foo\0bar", "foo\\0bar"),
("foo\\bar", "foo\\\\bar"),
]:
self.assertEqual(hc.sh_quote(t), a)
class PackageTest(HomectlTest):
# Tests for the public Package class API.
def test_systems(self):
empty = hc.Package(self.EMPTY)
p = hc.Package(self.FULL)
self.assertEqual(set(empty.systems), set(['common']))
self.assertEqual(set(p.systems),
set(['common', 'Linux', 'Darwin', 'Windows']))
def test_hooks_in_system(self):
empty = hc.Package(self.EMPTY)
p = hc.Package(self.FULL)
self.assertEqual(set(empty.hooks_in_system('common')), set())
self.assertEqual(set(empty.hooks_in_system('Linux')), set())
self.assertEqual(set(p.hooks_in_system('common')), set(['bin', 'share']))
self.assertEqual(set(p.hooks_in_system('Linux')), set(['bin', 'lib']))
self.assertEqual(set(p.hooks_in_system('Plan9')), set())
def test_files_in_sys_hook(self):
empty = hc.Package(self.EMPTY)
p = hc.Package(self.FULL)
self.assertEqual(set(empty.files_in_sys_hook('common', 'bin')), set())
self.assertEqual(set(empty.files_in_sys_hook('Linux', 'bin')), set())
self.assertEqual(set(p.files_in_sys_hook('common', 'bin')),
fileset(pj(self.FULL, 'bin'), 'hello'))
self.assertEqual(set(p.files_in_sys_hook('Linux', 'bin')),
fileset(pj(self.FULL, 'Linux', 'bin'), 'bye'))
self.assertEqual(set(p.files_in_sys_hook('Linux', 'share')), set())
self.assertEqual(set(p.files_in_sys_hook('Plan9', 'bin')), set())
def test_file_map(self):
def filemap(*l):
return set([
((s, h, f),
pj(self.FULL, s, h, f) if s != 'common' \
else pj(self.FULL, h, f))
for s, h, f in l])
p = hc.Package(self.FULL)
self.assertEqual(set(p.file_map()), filemap(
('common', 'bin', 'hello'),
('common', 'share', 'package/README.txt'),
('Darwin', 'bin', 'bye'),
('Linux', 'bin', 'bye'),
('Windows', 'bin', 'bye'),
('Windows', 'dlls', 'not-a-dll'),
))
self.assertEqual(set(p.file_map(systems=['common', 'Linux'])), filemap(
('common', 'bin', 'hello'),
('common', 'share', 'package/README.txt'),
('Linux', 'bin', 'bye'),
))
self.assertEqual(set(p.file_map(hooks=['bin'])), filemap(
('common', 'bin', 'hello'),
('Darwin', 'bin', 'bye'),
('Linux', 'bin', 'bye'),
('Windows', 'bin', 'bye'),
))
self.assertEqual(
set(p.file_map(systems=['common', 'Linux'], hooks=['bin'])),
filemap(
('common', 'bin', 'hello'),
('Linux', 'bin', 'bye'),
))
class SystemTest(HomectlTest):
# Tests for the System class API. Sadly not everything here is very
# testable, since it's just a thin wrapper around the Python/OS interface.
# I'm not going to bother testing trivial functionality.
@with_system
def test_update_file(self, s):
s.update_file("foo", "bar")
self.assertEqual(file_contents("foo"), "bar")
s.update_file("foo", "other")
self.assertEqual(file_contents("foo"), "other")
@with_system
def test_update_link(self, s):
s.update_link(pj("foo", "bar"), "f")
self.assertEqual(os.readlink("f"), pj("foo", "bar"))
# XXX Use pj() here, if we want this to work on Windows ever...
s.update_link("/dev/null", "f")
self.assertEqual(os.readlink("f"), "/dev/null")
@with_system
def test_rm_link(self, s):
s.update_link("f", "f")
s.rm_link("f")
self.assertEqual(os.path.exists("f"), False)
class DeploymentTest(HomectlTest):
# Tests for the Deployment class API.
#
# Utility functions
#
def enabled_list(self, d):
with open(d.enabled_list.path, 'r') as f:
return set([p.rstrip() for p in f.readlines()])
def check_links(self, *lmap, **kw):
for src, lnk in lmap:
src_f = pj(self.dir, *src.split('/'))
src_dir = os.path.dirname(src_f)
if kw.get('testdata', None):
lnk_path = os.path.realpath(pj(TESTDATA_DIR, *lnk.split('/')))
else:
lnk_path = os.path.realpath(pj(*lnk.split('/')))
self.assertEqual(os.path.realpath(src_f), lnk_path)
def check_absence(self, *path):
for p in path:
self.assertFalse(os.path.exists(pj(self.dir, *p.split('/'))))
def mk_files(self, name, *files):
for f in files:
path = pj(name, *f.split('/'))
hc.mkdirp(os.path.dirname(path))
open(path, 'w').close()
#
# Test cases
#
@with_deployment
def test_add_package_updates_enabled_list(self, d):
self.assertEqual(d.packages, set())
d.packages = set([hc.Package(self.EMPTY)])
self.assertEqual(self.enabled_list(d),
set([os.path.relpath(p, self.dir) for p in [self.EMPTY]]))
self.assertEqual(d.packages, set([hc.Package(self.EMPTY)]))
d.packages = d.packages.union([hc.Package(self.FULL)])
self.assertEqual(self.enabled_list(d),
set([os.path.relpath(p, self.dir) for p in [self.EMPTY, self.FULL]]))
self.assertEqual(d.packages,
set([hc.Package(self.EMPTY), hc.Package(self.FULL)]))
@with_deployment
def test_add_package_creates_homectl_links(self, d):
self.assertEqual(d.packages, set())
d.packages = set([hc.Package(self.FULL)])
# Just spot-check a few things
self.check_links(
('.homectl/common/bin/hello', 'package-full.hcpkg/bin/hello'),
('.homectl/Linux/bin/bye', 'package-full.hcpkg/Linux/bin/bye'),
('.homectl/common/share/package/README.txt', 'package-full.hcpkg/share/package/README.txt'),
testdata=True,
)
# Platform-specific stuff should never show up in common
self.check_absence(
'.homectl/common/bin/bye',
'.homectl/common/lib'
)
@with_deployment
def test_rm_package_deletes_homectl_links(self, d):
d.packages = set([hc.Package(self.FULL)])
self.check_links(
('.homectl/common/bin/hello', 'package-full.hcpkg/bin/hello'),
('.homectl/Linux/bin/bye', 'package-full.hcpkg/Linux/bin/bye'),
('.homectl/common/share/package/README.txt', 'package-full.hcpkg/share/package/README.txt'),
testdata=True,
)
d.packages = set()
self.check_absence(
'.homectl/common/bin/hello',
'.homectl/Linux/bin/bye',
'.homectl/common/share/package/README.txt',
)
@with_deployment
def test_refresh_creates_links(self, d):
self.mk_files('small.hcpkg', 'bin/foo', 'Linux/bin/bar')
d.packages = set([hc.Package('small.hcpkg')])
self.check_links(
('.homectl/common/bin/foo', 'small.hcpkg/bin/foo'),
('.homectl/Linux/bin/bar', 'small.hcpkg/Linux/bin/bar'),
)
self.check_absence(
'.homectl/Linux/bin/foo',
'.homectl/common/bin/bar',
)
self.mk_files('small.hcpkg', 'bin/new')
d.refresh()
self.check_links(
('.homectl/common/bin/new', 'small.hcpkg/bin/new'),
)
@with_deployment
def test_refresh_deletes_links(self, d):
self.mk_files('small.hcpkg', 'bin/foo', 'Linux/bin/bar')
d.packages = set([hc.Package('small.hcpkg')])
self.check_links(
('.homectl/common/bin/foo', 'small.hcpkg/bin/foo'),
('.homectl/Linux/bin/bar', 'small.hcpkg/Linux/bin/bar'),
)
self.check_absence(
'small.hcpkg/Linux/bin/foo',
'small.hcpkg/bin/bar',
)
os.unlink(pj('small.hcpkg', 'bin', 'foo'))
d.refresh()
self.check_absence('.homectl/common/bin/foo')
@with_deployment
def test_overlay_create_links(self, d):
self.mk_files('overlay.hcpkg', 'overlay/.mycfg', 'overlay/.config/my')
d.packages = set([hc.Package('overlay.hcpkg')])
self.check_links(
('.homectl/common/overlay/.mycfg', 'overlay.hcpkg/overlay/.mycfg'),
('.homectl/common/overlay/.config/my', 'overlay.hcpkg/overlay/.config/my'),
('.mycfg', 'overlay.hcpkg/overlay/.mycfg'),
('.config/my', 'overlay.hcpkg/overlay/.config/my'),
)
@with_deployment
def test_overlay_delete_links_on_refresh(self, d):
self.mk_files('overlay.hcpkg', 'overlay/.mycfg', 'overlay/.config/my')
d.packages = set([hc.Package('overlay.hcpkg')])
self.check_links(
('.homectl/common/overlay/.mycfg', 'overlay.hcpkg/overlay/.mycfg'),
('.homectl/common/overlay/.config/my', 'overlay.hcpkg/overlay/.config/my'),
('.mycfg', 'overlay.hcpkg/overlay/.mycfg'),
('.config/my', 'overlay.hcpkg/overlay/.config/my'),
)
os.unlink(os.path.join('overlay.hcpkg', 'overlay', '.mycfg'))
d.refresh()
self.check_absence(
'.homectl/common/overlay/.mycfg',
'.mycfg',
)
@with_deployment
def test_overlay_doesnt_touch_user_files(self, d):
self.mk_files('overlay.hcpkg', 'overlay/.mycfg')
self.mk_files('.', '.mycfg')
d.packages = set([hc.Package('overlay.hcpkg')])
self.assertTrue(os.path.exists('.mycfg'))
self.assertFalse(os.path.islink('.mycfg'))
d.packages = set()
self.assertTrue(os.path.exists('.mycfg'))
@with_deployment
def test_overlay_replaces_user_files_if_forced(self, d):
self.mk_files('overlay.hcpkg', 'overlay/.mycfg')
self.mk_files('.', '.mycfg')
self.assertTrue(os.path.exists('.mycfg'))
self.assertFalse(os.path.islink('.mycfg'))
d.set_packages(set([hc.Package('overlay.hcpkg')]), force_replace=True)
self.assertTrue(os.path.exists('.mycfg'))
self.assertTrue(os.path.islink('.mycfg'))
d.packages = set()
self.assertFalse(os.path.exists('.mycfg'))
@with_deployment
def test_trigger_pwd(self, d):
os.mkdir('trigger.hcpkg')
# XXX This is a UNIX-ism
with open(pj('trigger.hcpkg', '_trigger'), 'w') as f:
f.write('#!/bin/sh\npwd > %s\n' % pj(self.dir, 'triggered'))
os.chmod(pj('trigger.hcpkg', '_trigger'), 0o755)
pkg = hc.Package('trigger.hcpkg')
d.packages = set([pkg])
with open(pj(self.dir, 'triggered')) as f:
self.assertEqual(pkg.path, f.readline().strip())
@with_deployment
def test_refresh_trigger(self, d):
os.mkdir('trigger.hcpkg')
# XXX This is a UNIX-ism
with open(pj('trigger.hcpkg', '_trigger'), 'w') as f:
f.write('#!/bin/sh\n[ "$1" = refresh ] && touch triggered\n')
os.chmod(pj('trigger.hcpkg', '_trigger'), 0o755)
pkg = hc.Package('trigger.hcpkg')
sentinel = pj(pkg.path, 'triggered')
d.packages = set([pkg])
# Trigger should have run
self.assertTrue(os.path.exists(sentinel))
# Triggers should not be linked into ~/.homectl
self.check_absence(
'.homectl/_trigger',
'.homectl/common/_trigger',
)
@with_deployment
def test_disable_trigger(self, d):
os.mkdir('trigger.hcpkg')
# XXX This is a UNIX-ism
with open(pj('trigger.hcpkg', '_trigger'), 'w') as f:
f.write('#!/bin/sh\n[ "$1" = disable ] && touch triggered\n')
os.chmod(pj('trigger.hcpkg', '_trigger'), 0o755)
pkg = hc.Package('trigger.hcpkg')
sentinel = pj(pkg.path, 'triggered')
d.packages = set([pkg])
# Shouldn't have run yet
self.assertFalse(os.path.exists(sentinel))
# Triggers should not be linked into ~/.homectl
self.check_absence(
'.homectl/_trigger',
'.homectl/common/_trigger',
)
d.packages = set()
# Trigger should have run
self.assertTrue(os.path.exists(sentinel))
@with_deployment
def test_hook_tree(self, d):
plat = os.uname()[0]
d.packages = set([hc.Package(self.FULL)])
self.assertEqual(
set(d.hook_tree('bin', 'h*')),
set([os.path.abspath(f) for f in [
pj('.homectl', 'common', 'bin', 'hello')
]]))
self.assertEqual(
set(d.hook_tree('bin', 'b*')),
set([os.path.abspath(f) for f in [
pj('.homectl', plat, 'bin', 'bye')
]]))
class InitTest(HomectlTest):
# Tests for creating new homectl deployments.
pass
class CLITest(HomectlTest):
# System-level tests for CLI commands themselves.
pass
if __name__ == '__main__': # pragma: no branch
unittest.main()