1# Copyright (c) 2025 Pete Johanson
2#
3# SPDX-License-Identifier: Apache-2.0
4
5import argparse
6import os
7from unittest.mock import call, patch
8
9from conftest import RC_KERNEL_HEX
10from runners.rfp import RfpBinaryRunner
11
12TEST_RFP_PORT = 'test-rfp-serial'
13TEST_RFP_PORT_SPEED = '115200'
14TEST_RFP_DEVICE = 'RA'
15TEST_RFP_USR_LOCAL_RFP_CLI = '/usr/local/bin/rfp-cli'
16
17EXPECTED_COMMANDS = [
18    [
19        'rfp-cli',
20        '-port',
21        TEST_RFP_PORT,
22        '-device',
23        TEST_RFP_DEVICE,
24        '-run',
25        '-noerase',
26        '-p',
27        '-file',
28        RC_KERNEL_HEX,
29    ],
30]
31
32EXPECTED_COMMANDS_WITH_SPEED = [
33    [
34        'rfp-cli',
35        '-port',
36        TEST_RFP_PORT,
37        '-s',
38        TEST_RFP_PORT_SPEED,
39        '-device',
40        TEST_RFP_DEVICE,
41        '-run',
42        '-noerase',
43        '-p',
44        '-file',
45        RC_KERNEL_HEX,
46    ],
47]
48
49
50EXPECTED_COMMANDS_WITH_ERASE = [
51    [
52        'rfp-cli',
53        '-port',
54        TEST_RFP_PORT,
55        '-device',
56        TEST_RFP_DEVICE,
57        '-run',
58        '-erase',
59        '-p',
60        '-file',
61        RC_KERNEL_HEX,
62    ],
63]
64
65EXPECTED_COMMANDS_WITH_VERIFY = [
66    [
67        'rfp-cli',
68        '-port',
69        TEST_RFP_PORT,
70        '-device',
71        TEST_RFP_DEVICE,
72        '-run',
73        '-noerase',
74        '-v',
75        '-p',
76        '-file',
77        RC_KERNEL_HEX,
78    ],
79]
80
81EXPECTED_COMMANDS_WITH_RFP_CLI = [
82    [
83        TEST_RFP_USR_LOCAL_RFP_CLI,
84        '-port',
85        TEST_RFP_PORT,
86        '-device',
87        TEST_RFP_DEVICE,
88        '-run',
89        '-noerase',
90        '-p',
91        '-file',
92        RC_KERNEL_HEX,
93    ],
94]
95
96
97def require_patch(program):
98    assert program in ['rfp', 'rfp-cli', TEST_RFP_USR_LOCAL_RFP_CLI]
99
100
101os_path_isfile = os.path.isfile
102
103
104def os_path_isfile_patch(filename):
105    if filename == RC_KERNEL_HEX:
106        return True
107    return os_path_isfile(filename)
108
109
110@patch('runners.core.ZephyrBinaryRunner.require', side_effect=require_patch)
111@patch('runners.core.ZephyrBinaryRunner.check_call')
112@patch('runners.rfp.RfpBinaryRunner.default_rfp')
113def test_rfp_init(dr, cc, req, runner_config, tmpdir):
114    """
115    Test commands using a runner created by constructor.
116
117    Input:
118        port=A
119    device=B
120
121    Output:
122        -port A
123        -device B
124    """
125    runner = RfpBinaryRunner(runner_config, port=TEST_RFP_PORT, device=TEST_RFP_DEVICE)
126    with patch('os.path.isfile', side_effect=os_path_isfile_patch):
127        runner.run('flash')
128    assert cc.call_args_list == [call(x) for x in EXPECTED_COMMANDS]
129
130
131@patch('runners.core.ZephyrBinaryRunner.require', side_effect=require_patch)
132@patch('runners.core.ZephyrBinaryRunner.check_call')
133@patch('runners.rfp.RfpBinaryRunner.default_rfp')
134def test_rfp_create(dr, cc, req, runner_config, tmpdir):
135    """
136    Test commands using a runner created from command line parameters.
137
138
139    Input:
140        ---port A
141    --device B
142
143    Output:
144        -port A
145        -device B
146    """
147    args = ['--port', str(TEST_RFP_PORT), "--device", str(TEST_RFP_DEVICE)]
148    parser = argparse.ArgumentParser(allow_abbrev=False)
149    RfpBinaryRunner.add_parser(parser)
150    arg_namespace = parser.parse_args(args)
151    runner = RfpBinaryRunner.create(runner_config, arg_namespace)
152    with patch('os.path.isfile', side_effect=os_path_isfile_patch):
153        runner.run('flash')
154    assert cc.call_args_list == [call(x) for x in EXPECTED_COMMANDS]
155
156
157@patch('runners.core.ZephyrBinaryRunner.require', side_effect=require_patch)
158@patch('runners.core.ZephyrBinaryRunner.check_call')
159@patch('runners.rfp.RfpBinaryRunner.default_rfp')
160def test_rfp_create_with_speed(dr, cc, req, runner_config, tmpdir):
161    """
162    Test commands using a runner created from command line parameters.
163
164    Input:
165        --port
166        --speed
167    --speed
168
169    Output:
170        -s SPEED
171    """
172    args = [
173        '--device',
174        str(TEST_RFP_DEVICE),
175        '--port',
176        str(TEST_RFP_PORT),
177        '--speed',
178        str(TEST_RFP_PORT_SPEED),
179    ]
180    parser = argparse.ArgumentParser(allow_abbrev=False)
181    RfpBinaryRunner.add_parser(parser)
182    arg_namespace = parser.parse_args(args)
183    runner = RfpBinaryRunner.create(runner_config, arg_namespace)
184    with patch('os.path.isfile', side_effect=os_path_isfile_patch):
185        runner.run('flash')
186    assert cc.call_args_list == [call(x) for x in EXPECTED_COMMANDS_WITH_SPEED]
187
188
189@patch('runners.core.ZephyrBinaryRunner.require', side_effect=require_patch)
190@patch('runners.core.ZephyrBinaryRunner.check_call')
191@patch('runners.rfp.RfpBinaryRunner.default_rfp')
192def test_rfp_create_with_erase(dr, cc, req, runner_config, tmpdir):
193    """
194    Test commands using a runner created from command line parameters.
195
196    Input:
197        --erase
198
199    Output:
200        -erase
201    """
202    args = ['--device', str(TEST_RFP_DEVICE), '--port', str(TEST_RFP_PORT), '--erase']
203    parser = argparse.ArgumentParser(allow_abbrev=False)
204    RfpBinaryRunner.add_parser(parser)
205    arg_namespace = parser.parse_args(args)
206    runner = RfpBinaryRunner.create(runner_config, arg_namespace)
207    with patch('os.path.isfile', side_effect=os_path_isfile_patch):
208        runner.run('flash')
209    assert cc.call_args_list == [call(x) for x in EXPECTED_COMMANDS_WITH_ERASE]
210
211
212@patch('runners.core.ZephyrBinaryRunner.require', side_effect=require_patch)
213@patch('runners.core.ZephyrBinaryRunner.check_call')
214@patch('runners.rfp.RfpBinaryRunner.default_rfp')
215def test_rfp_create_with_verify(dr, cc, req, runner_config, tmpdir):
216    """
217    Test commands using a runner created from command line parameters.
218
219    Input:
220        --verify
221
222    Output:
223        -v
224    """
225    args = ['--device', str(TEST_RFP_DEVICE), '--port', str(TEST_RFP_PORT), '--verify']
226    parser = argparse.ArgumentParser(allow_abbrev=False)
227    RfpBinaryRunner.add_parser(parser)
228    arg_namespace = parser.parse_args(args)
229    runner = RfpBinaryRunner.create(runner_config, arg_namespace)
230    with patch('os.path.isfile', side_effect=os_path_isfile_patch):
231        runner.run('flash')
232    assert cc.call_args_list == [call(x) for x in EXPECTED_COMMANDS_WITH_VERIFY]
233
234
235@patch('runners.core.ZephyrBinaryRunner.require', side_effect=require_patch)
236@patch('runners.core.ZephyrBinaryRunner.check_call')
237@patch('runners.rfp.RfpBinaryRunner.default_rfp')
238def test_rfp_create_with_rfp_cli(dr, cc, req, runner_config, tmpdir):
239    """
240    Test commands using a runner created from command line parameters.
241
242    Input:
243        --rfp-cli /usr/local/bin/rfp-cli
244
245    Output:
246        /usr/local/bin/rfp-cli
247    """
248    args = [
249        '--device',
250        str(TEST_RFP_DEVICE),
251        '--port',
252        str(TEST_RFP_PORT),
253        '--rfp-cli',
254        str(TEST_RFP_USR_LOCAL_RFP_CLI),
255    ]
256    parser = argparse.ArgumentParser(allow_abbrev=False)
257    RfpBinaryRunner.add_parser(parser)
258    arg_namespace = parser.parse_args(args)
259    runner = RfpBinaryRunner.create(runner_config, arg_namespace)
260    with patch('os.path.isfile', side_effect=os_path_isfile_patch):
261        runner.run('flash')
262    assert cc.call_args_list == [call(x) for x in EXPECTED_COMMANDS_WITH_RFP_CLI]
263