]> scripts.mit.edu Git - wizard.git/blob - wizard/tests/util_test.py
Rewrite parametrize to use new parametrizeWithVars
[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_dir_uid():
30     if os.getuid(): return # only run if on a scripts server. This is crude
31     assert get_dir_uid("/mit/ezyang/web_scripts/test-wiki") == 537864399
32
33 def test_get_dir_uid_locker():
34     if os.getuid(): return
35     assert get_dir_uid("/mit/apo/web_scripts/") == 536956980
36
37 def test_get_exception_name():
38     try:
39         raise NotImplementedError
40     except NotImplementedError:
41         assert get_exception_name(traceback.format_exc()) == "NotImplementedError"
42
43 def test_get_exception_name_withstr():
44     try:
45         raise MyError
46     except MyError:
47         assert get_exception_name(traceback.format_exc()) == "MyError"
48
49 def test_get_exception_name_withhtml():
50     try:
51         raise MyErrorWithHTML
52     except MyErrorWithHTML:
53         assert get_exception_name(traceback.format_exc()) == "MyErrorWithHTML"
54
55 def test_get_exception_name_withstr2():
56     try:
57         raise Exception("This is extra info we don't care about");
58     except Exception:
59         assert get_exception_name(traceback.format_exc()) == "Exception"
60
61 def test_lock():
62     soft_unlink(lockfile)
63     with LockDirectory(lockfile):
64         pass
65
66 def test_locked():
67     soft_unlink(lockfile)
68     with LockDirectory(lockfile):
69         try:
70             with LockDirectory(lockfile):
71                 assert False
72         except DirectoryLockedError:
73             pass
74
75 def test_break_orphan_lock():
76     soft_unlink(lockfile)
77     open(lockfile, "w").write("obviouslyboguspid")
78     with LockDirectory(lockfile):
79         pass
80
81 def test_break_stale_lock():
82     soft_unlink(lockfile)
83     with LockDirectory(lockfile):
84         with LockDirectory(lockfile, expiry = 0):
85             pass