Debian Codegraph @

test/commander_test.py

raw symbols SCIP
1 # pyOCD debugger
2 # Copyright (c) 2020,2025 Arm Limited
3 # Copyright (c) 2021 Chris Reed
4 # SPDX-License-Identifier: Apache-2.0
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17
18 import argparse
19 import sys
20 import traceback
21 import logging
22 from types import SimpleNamespace
23 import os
24 import tempfile
25
26 from pyocd.probe.pydapaccess import DAPAccess
27 from pyocd.commands.commander import PyOCDCommander
28 from test_util import (
29 Test,
30 TestResult,
31 PYOCD_DIR,
32 )
33
34 GDB_TEST_ELF = os.path.join(PYOCD_DIR, "src/gdb_test_program/gdb_test.elf")
35
36 class CommanderTestResult(TestResult):
37 def __init__(self):
38 super(CommanderTestResult, self).__init__(None, None, None)
39 self.name = "commander"
40
41 class CommanderTest(Test):
42 def __init__(self):
43 super(CommanderTest, self).__init__("Commander Test", commander_test)
44
45 def run(self, board):
46 try:
47 result = self.test_function(board.unique_id)
48 except Exception as e:
49 result = CommanderTestResult()
50 result.passed = False
51 print("Exception %s when testing board %s" % (e, board.unique_id))
52 traceback.print_exc(file=sys.stdout)
53 result.board = board
54 result.test = self
55 return result
56
57 def commander_test(board_id):
58 test_pass_count = 0
59 test_count = 0
60 failed_commands = []
61 result = CommanderTestResult()
62
63 COMMANDS_TO_TEST = [
64 # general commands
65 "continue",
66 "status",
67 "halt",
68 "status",
69
70 # semicolon separated
71 "status ; halt ; continue",
72 "halt;continue",
73 "halt; continue",
74
75 # Python and shell
76 "$ 2+2",
77 "!echo 'hi mom'",
78 " $ target.vendor",
79
80 # commander command group - these are not tested by commands_test.py.
81 "list",
82 "exit", # Must be last command!
83 ]
84
85 # Set up commander args.
86 args = SimpleNamespace()
87 args.no_init = False
88 args.frequency = 1000000
89 args.options = {} #get_session_options()
90 args.halt = True
91 args.no_wait = True
92 args.project_dir = None
93 args.config = None
94 args.script = None
95 args.no_config = False
96 args.pack = None
97 args.cbuild_run = None
98 args.unique_id = board_id
99 args.target_override = None
100 args.elf = GDB_TEST_ELF
101 args.interactive = False
102
103 #
104 # Test basic functionality.
105 #
106 print("\n------ Testing basic functionality ------\n")
107
108 test_count += 1
109 try:
110 cmdr = PyOCDCommander(args, COMMANDS_TO_TEST)
111 cmdr.run()
112 test_pass_count += 1
113 print("TEST PASSED")
114
115 test_count += 1
116 print("Testing exit code")
117 print("Exit code:", cmdr.exit_code)
118 if cmdr.exit_code == 0:
119 test_pass_count += 1
120 print("TEST PASSED")
121 else:
122 print("TEST FAILED")
123 except Exception:
124 print("TEST FAILED")
125 traceback.print_exc()
126
127 #
128 # Test running command files.
129 #
130 print("\n------ Testing command files ------\n")
131
132 with tempfile.NamedTemporaryFile('w+') as cmdfile:
133 cmdfile.write("""# here is a comment
134 halt
135 reg
136 continue
137
138 # semicolons
139 halt ; status
140
141 # Python and system
142 $ {'a': 1, 'b': 2}
143 !echo "hello, world!"
144 $target.part_number
145 !echo first ; echo second
146 """)
147
148 # Jump back to the start of the file.
149 cmdfile.seek(0, 0)
150
151 test_count += 1
152 try:
153 cmdr = PyOCDCommander(args, [cmdfile.file])
154 cmdr.run()
155 test_pass_count += 1
156 print("TEST PASSED")
157
158 test_count += 1
159 print("Testing exit code")
160 print("Exit code:", cmdr.exit_code)
161 if cmdr.exit_code == 0:
162 test_pass_count += 1
163 print("TEST PASSED")
164 else:
165 print("TEST FAILED")
166 except Exception:
167 print("TEST FAILED")
168 traceback.print_exc()
169
170 print("\n\nTest Summary:")
171 print("Pass count %i of %i tests" % (test_pass_count, test_count))
172 if failed_commands:
173 for c in failed_commands:
174 print(" - '" + c + "'")
175 if test_pass_count == test_count:
176 print("COMMANDER TEST SCRIPT PASSED")
177 else:
178 print("COMMANDER TEST SCRIPT FAILED")
179
180 result.passed = test_count == test_pass_count
181 return result
182
183 if __name__ == "__main__":
184 parser = argparse.ArgumentParser(description='pyOCD commander test')
185 parser.add_argument('-d', '--debug', action="store_true", help='Enable debug logging')
186 parser.add_argument('-u', '--uid', help='Debug probe unique ID')
187 parser.add_argument("-da", "--daparg", dest="daparg", nargs='+', help="Send setting to DAPAccess layer.")
188 args = parser.parse_args()
189 level = logging.DEBUG if args.debug else logging.INFO
190 logging.basicConfig(level=level)
191 DAPAccess.set_args(args.daparg)
192 commander_test(args.uid)
193