8 from test
import test_support
10 # Silence Py3k warning
11 hotshot
= test_support
.import_module('hotshot', deprecated
=True)
12 from hotshot
.log
import ENTER
, EXIT
, LINE
15 def shortfilename(fn
):
16 # We use a really shortened filename since an exact match is made,
17 # and the source may be either a Python source file or a
18 # pre-compiled bytecode file.
20 return os
.path
.splitext(os
.path
.basename(fn
))[0]
25 class UnlinkingLogReader(hotshot
.log
.LogReader
):
26 """Extend the LogReader so the log file is unlinked when we're
29 def __init__(self
, logfn
):
31 hotshot
.log
.LogReader
.__init
__(self
, logfn
)
33 def next(self
, index
=None):
35 return hotshot
.log
.LogReader
.next(self
)
38 os
.unlink(self
.__logfn
)
42 class HotShotTestCase(unittest
.TestCase
):
43 def new_profiler(self
, lineevents
=0, linetimings
=1):
44 self
.logfn
= test_support
.TESTFN
45 return hotshot
.Profile(self
.logfn
, lineevents
, linetimings
)
47 def get_logreader(self
):
48 return UnlinkingLogReader(self
.logfn
)
50 def get_events_wotime(self
):
52 for event
in self
.get_logreader():
53 what
, (filename
, lineno
, funcname
), tdelta
= event
54 L
.append((what
, (shortfilename(filename
), lineno
, funcname
)))
57 def check_events(self
, expected
):
58 events
= self
.get_events_wotime()
59 if events
!= expected
:
61 "events did not match expectation; got:\n%s\nexpected:\n%s"
62 % (pprint
.pformat(events
), pprint
.pformat(expected
)))
64 def run_test(self
, callable, events
, profiler
=None):
66 profiler
= self
.new_profiler()
67 self
.assertTrue(not profiler
._prof
.closed
)
68 profiler
.runcall(callable)
69 self
.assertTrue(not profiler
._prof
.closed
)
71 self
.assertTrue(profiler
._prof
.closed
)
72 self
.check_events(events
)
74 def test_addinfo(self
):
76 p
.addinfo("test-key", "test-value")
77 profiler
= self
.new_profiler()
78 profiler
.runcall(f
, profiler
)
80 log
= self
.get_logreader()
83 self
.assertTrue(info
["test-key"] == ["test-value"])
85 def test_line_numbers(self
):
91 f_lineno
= f
.func_code
.co_firstlineno
92 g_lineno
= g
.func_code
.co_firstlineno
93 events
= [(ENTER
, ("test_hotshot", g_lineno
, "g")),
94 (LINE
, ("test_hotshot", g_lineno
+1, "g")),
95 (ENTER
, ("test_hotshot", f_lineno
, "f")),
96 (LINE
, ("test_hotshot", f_lineno
+1, "f")),
97 (LINE
, ("test_hotshot", f_lineno
+2, "f")),
98 (EXIT
, ("test_hotshot", f_lineno
, "f")),
99 (EXIT
, ("test_hotshot", g_lineno
, "g")),
101 self
.run_test(g
, events
, self
.new_profiler(lineevents
=1))
103 def test_start_stop(self
):
104 # Make sure we don't return NULL in the start() and stop()
105 # methods when there isn't an error. Bug in 2.2 noted by
107 profiler
= self
.new_profiler()
111 os
.unlink(self
.logfn
)
113 def test_bad_sys_path(self
):
117 coverage
= hotshot
._hotshot
.coverage
119 # verify we require a list for sys.path
121 self
.assertRaises(RuntimeError, coverage
, test_support
.TESTFN
)
122 # verify that we require sys.path exists
124 self
.assertRaises(RuntimeError, coverage
, test_support
.TESTFN
)
127 if os
.path
.exists(test_support
.TESTFN
):
128 os
.remove(test_support
.TESTFN
)
130 def test_logreader_eof_error(self
):
131 emptyfile
= tempfile
.NamedTemporaryFile()
133 self
.assertRaises((IOError, EOFError), _hotshot
.logreader
,
140 test_support
.run_unittest(HotShotTestCase
)
143 if __name__
== "__main__":