forked from llvm/llvm-project
- Notifications
You must be signed in to change notification settings - Fork 339
/
Copy pathlldbplaygroundrepl.py
123 lines (103 loc) · 3.95 KB
/
lldbplaygroundrepl.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
# lldbplaygroundrepl.py
#
# This source file is part of the Swift.org open source project
#
# Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
# Licensed under Apache License v2.0 with Runtime Library Exception
#
# See https://swift.org/LICENSE.txt for license information
# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
#
# ------------------------------------------------------------------------------
fromlldbsuite.test.lldbtestimport*
fromlldbsuite.test.decoratorsimport*
importlldbsuite.test.lldbutilaslldbutil
importos
importos.path
importsys
ifsys.version_info.major==2:
importcommandsassubprocess
else:
importsubprocess
classPlaygroundREPLTest(TestBase):
@decorators.skipUnlessDarwin
@decorators.swiftTest
@decorators.skipIf(
debug_info=decorators.no_match("dsym"),
bugnumber="This test only builds one way")
defbuild_all(self):
self.build()
defexecute_command(self, command):
(exit_status, output) =subprocess.getstatusoutput(command)
returnexit_status
defrepl_set_up(self):
"""
Playgrounds REPL test specific setup that must happen after class setup
"""
target, process, thread, bkpt=lldbutil.run_to_source_breakpoint(
self, 'Set breakpoint here', lldb.SBFileSpec('PlaygroundStub.swift'),
exe_name='PlaygroundStub', extra_images=['libPlaygroundsRuntime.dylib'])
self.frame=thread.frames[0]
self.assertTrue(self.frame, "Frame 0 is valid.")
# Configure lldb
self.options=lldb.SBExpressionOptions()
self.options.SetLanguage(lldb.eLanguageTypeSwift)
self.options.SetPlaygroundTransformEnabled()
self.options.SetREPLMode()
# Add cleanup to tear down
defcleanup():
self.execute_command("make cleanup")
self.addTearDownHook(cleanup)
defexecute_code(self, inputFile):
"""
Execute a block of code contained within a Swift file. Requires a
frame to have already been created.
:param inputFile: file name (with extension) of code contents
:return: result: resulting SBStream from executing playground
output: string summary of playground execution
"""
# Execute code
contents=""
withopen(inputFile, 'r') ascontents_file:
contents=contents_file.read()
result=self.frame.EvaluateExpression(contents, self.options)
output=self.frame.EvaluateExpression("get_output()")
withrecording(self, self.TraceOn()) assbuf:
print("playground result: ", file=sbuf)
print(str(result), file=sbuf)
print("playground output:", file=sbuf)
print(str(output), file=sbuf)
self.assertSuccess(output.GetError())
returnresult, output
defis_compile_or_runtime_error(self, result):
"""
Determine if any errors we care about for Playground execution occurred
:param result: SBStream from executing a playground
:return: ret: bool value of if it's an error we care about
"""
ret=result.GetError().Fail() andnot (
result.GetError().GetType() ==1and
result.GetError().GetError() ==0x1001)
returnret
defget_stream_data(self, result):
stream=lldb.SBStream()
stream.Clear()
result.GetError().GetDescription(stream)
data=stream.GetData()
returndata
defdid_crash(self, result):
error=self.get_stream_data(result)
print("Crash Error: {}".format(error))
@swiftTest
deftest_playgrounds(self):
# Build
self.build_all()
# Prepare
self.repl_set_up()
# Run user test
self.do_test()
if__name__=='__main__':
importatexit
lldb.SBDebugger.Initialize()
atexit.register(lldb.SBDebugger.Terminate)
unittest2.main()