From 99a998a161741793acff3daa9afebaf884737554 Mon Sep 17 00:00:00 2001 From: Appana Durga Kedareswara rao Date: Sat, 19 Oct 2024 16:01:04 +0530 Subject: [PATCH] scripts: west_commands: tests: add pytest for xsdb runner Add pytest case for xsdb runner. Signed-off-by: Appana Durga Kedareswara rao --- scripts/west_commands/tests/test_imports.py | 1 + scripts/west_commands/tests/test_xsdb.py | 85 +++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 scripts/west_commands/tests/test_xsdb.py diff --git a/scripts/west_commands/tests/test_imports.py b/scripts/west_commands/tests/test_imports.py index d6afd20dffd..95d2c374a59 100644 --- a/scripts/west_commands/tests/test_imports.py +++ b/scripts/west_commands/tests/test_imports.py @@ -50,6 +50,7 @@ def test_runner_imports(): 'teensy', 'trace32', 'uf2', + 'xsdb', 'xtensa', # zephyr-keep-sorted-stop )) diff --git a/scripts/west_commands/tests/test_xsdb.py b/scripts/west_commands/tests/test_xsdb.py new file mode 100644 index 00000000000..d64055b5291 --- /dev/null +++ b/scripts/west_commands/tests/test_xsdb.py @@ -0,0 +1,85 @@ +# Copyright (c) 2024 Advanced Micro Devices, Inc. +# +# SPDX-License-Identifier: Apache-2.0 + +import argparse +from unittest.mock import patch, call +import pytest +from runners.xsdb import XSDBBinaryRunner +from conftest import RC_KERNEL_ELF + +TEST_CASES = [ + { + "config": None, + "bitstream": None, + "fsbl": None, + "expected_cmd": ["xsdb", "default_cfg_path"], + }, + { + "config": "custom_cfg_path", + "bitstream": None, + "fsbl": None, + "expected_cmd": ["xsdb", "custom_cfg_path"], + }, + { + "config": None, + "bitstream": "bitstream_path", + "fsbl": None, + "expected_cmd": ["xsdb", "default_cfg_path", RC_KERNEL_ELF, "bitstream_path"], + }, + { + "config": None, + "bitstream": None, + "fsbl": "fsbl_path", + "expected_cmd": ["xsdb", "default_cfg_path", RC_KERNEL_ELF, "fsbl_path"], + }, + { + "config": None, + "bitstream": "bitstream_path", + "fsbl": "fsbl_path", + "expected_cmd": ["xsdb", "default_cfg_path", RC_KERNEL_ELF, "bitstream_path", "fsbl_path"], + }, +] + +@pytest.mark.parametrize("tc", TEST_CASES) +@patch("runners.xsdb.os.path.exists", return_value=True) +@patch("runners.xsdb.XSDBBinaryRunner.check_call") +def test_xsdbbinaryrunner_init(check_call, path_exists, tc, runner_config): + '''Test actions using a runner created by constructor.''' + # Mock the default config path + with patch("runners.xsdb.os.path.join", return_value="default_cfg_path"): + runner = XSDBBinaryRunner( + cfg=runner_config, + config=tc["config"], + bitstream=tc["bitstream"], + fsbl=tc["fsbl"], + ) + + runner.do_run("flash") + + assert check_call.call_args_list == [call(tc["expected_cmd"])] + +@pytest.mark.parametrize("tc", TEST_CASES) +@patch("runners.xsdb.os.path.exists", return_value=True) +@patch("runners.xsdb.XSDBBinaryRunner.check_call") +def test_xsdbbinaryrunner_create(check_call, path_exists, tc, runner_config): + '''Test actions using a runner created from action line parameters.''' + args = [] + if tc["config"]: + args.extend(["--config", tc["config"]]) + if tc["bitstream"]: + args.extend(["--bitstream", tc["bitstream"]]) + if tc["fsbl"]: + args.extend(["--fsbl", tc["fsbl"]]) + + parser = argparse.ArgumentParser(allow_abbrev=False) + XSDBBinaryRunner.add_parser(parser) + arg_namespace = parser.parse_args(args) + + # Mock the default config path + with patch("runners.xsdb.os.path.join", return_value="default_cfg_path"): + runner = XSDBBinaryRunner.create(runner_config, arg_namespace) + + runner.do_run("flash") + + assert check_call.call_args_list == [call(tc["expected_cmd"])]