]> scripts.mit.edu Git - wizard.git/blob - wizard/tests/util_test.py
Ignore IOErrors.
[wizard.git] / wizard / tests / util_test.py
1 import traceback
2
3 from wizard import tests
4 from wizard.util import *
5
6 lockfile = tests.getTestFile("util_test.lock")
7
8 class MyError(Exception):
9     def __str__(self):
10         return """
11
12 ERROR: Foo
13 """
14
15 class MyErrorWithHTML(Exception):
16     def __str__(self):
17         return """
18
19 ERROR: Bar
20
21 <html>
22     <title>No good!</title>
23 </html>
24 """
25
26 def test_dictmap():
27     assert dictmap(lambda x: x + 1, {'a': 0, 'b': 1}) == {'a': 1, 'b': 2}
28
29 def test_get_exception_name():
30     try:
31         raise NotImplementedError
32     except NotImplementedError:
33         assert get_exception_name(traceback.format_exc()) == "NotImplementedError"
34
35 def test_get_exception_name_withstr():
36     try:
37         raise MyError
38     except MyError:
39         assert get_exception_name(traceback.format_exc()) == "MyError"
40
41 def test_get_exception_name_withhtml():
42     try:
43         raise MyErrorWithHTML
44     except MyErrorWithHTML:
45         assert get_exception_name(traceback.format_exc()) == "MyErrorWithHTML"
46
47 def test_get_exception_name_withstr2():
48     try:
49         raise Exception("This is extra info we don't care about");
50     except Exception:
51         assert get_exception_name(traceback.format_exc()) == "Exception"
52
53 def test_lock():
54     soft_unlink(lockfile)
55     with LockDirectory(lockfile):
56         pass
57
58 def test_locked():
59     soft_unlink(lockfile)
60     with LockDirectory(lockfile):
61         try:
62             with LockDirectory(lockfile):
63                 assert False
64         except DirectoryLockedError:
65             pass
66
67 def test_break_orphan_lock():
68     soft_unlink(lockfile)
69     open(lockfile, "w").write("obviouslyboguspid")
70     with LockDirectory(lockfile):
71         pass
72
73 def test_break_stale_lock():
74     soft_unlink(lockfile)
75     with LockDirectory(lockfile):
76         with LockDirectory(lockfile, expiry = 0):
77             pass
78
79 def test_disk_usage():
80     assert disk_usage(tests.getTestFile("disk_usage_test"), "ignore_me") ==  7