From 4506e790ef5b317175740a8eee93148ab40d8954 Mon Sep 17 00:00:00 2001 From: Giridhar <80974392+giri256@users.noreply.github.com> Date: Mon, 27 Jul 2026 03:36:24 +0530 Subject: [PATCH] pack: return error when no devices match Signed-off-by: Giridhar <80974392+giri256@users.noreply.github.com> --- pyocd/subcommands/pack_cmd.py | 10 +++++ test/unit/test_pack_cmd.py | 81 +++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 test/unit/test_pack_cmd.py diff --git a/pyocd/subcommands/pack_cmd.py b/pyocd/subcommands/pack_cmd.py index e6f70a73d..d96d720a3 100644 --- a/pyocd/subcommands/pack_cmd.py +++ b/pyocd/subcommands/pack_cmd.py @@ -1,6 +1,7 @@ # pyOCD debugger # Copyright (c) 2021 Chris Reed # Copyright (c) 2026 Arm Limited +# Copyright (c) 2026 Giridhar # SPDX-License-Identifier: Apache-2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -189,6 +190,9 @@ def invoke(self) -> int: # Look for matching part numbers. matches = self._get_matches(cache) + if not matches: + return 1 + if matches: # Get the list of installed pack targets. installed_targets = pack_target.ManagedPacks.get_installed_targets(cache=cache) @@ -251,6 +255,9 @@ def invoke(self) -> int: # Look for matching part numbers. matches = self._get_matches(cache) + if not matches: + return 1 + if matches: devices = [cache.index[dev] for dev in matches] packs = cache.packs_for_devices(devices) @@ -339,6 +346,9 @@ def invoke(self) -> int: matches = self._get_matches(cache) + if not matches: + return 1 + if self._args.find_devices: # Get the list of installed pack targets. installed_targets = pack_target.ManagedPacks.get_installed_targets(cache=cache) diff --git a/test/unit/test_pack_cmd.py b/test/unit/test_pack_cmd.py new file mode 100644 index 000000000..2161237c4 --- /dev/null +++ b/test/unit/test_pack_cmd.py @@ -0,0 +1,81 @@ +# pyOCD debugger +# Copyright (c) 2026 Giridhar +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from argparse import Namespace +from types import SimpleNamespace + +import pytest + +from pyocd.subcommands.pack_cmd import ( + PackFindSubcommand, + PackInstallSubcommand, + PackSubcommand, + ) + + +@pytest.fixture +def cache(): + return SimpleNamespace(index={"KnownDevice": {}}) + + +def test_pack_find_returns_error_when_no_device_matches(monkeypatch, cache): + command = PackFindSubcommand(Namespace( + patterns=["MissingDevice"], + update=False, + clean=False, + no_header=False, + verbose=0, + quiet=0, + )) + monkeypatch.setattr(command, "_get_cache", lambda: cache) + + assert command.invoke() == 1 + + +def test_pack_install_returns_error_when_no_device_matches(monkeypatch, cache): + command = PackInstallSubcommand(Namespace( + patterns=["MissingDevice"], + update=False, + clean=False, + no_download=False, + verbose=0, + quiet=0, + )) + monkeypatch.setattr(command, "_get_cache", lambda: cache) + + assert command.invoke() == 1 + + +@pytest.mark.parametrize(("find_devices", "install_devices"), [ + (["MissingDevice"], None), + (None, ["MissingDevice"]), + ]) +def test_deprecated_pack_options_return_error_when_no_device_matches( + monkeypatch, cache, find_devices, install_devices): + command = PackSubcommand(Namespace( + clean=False, + update=False, + show=False, + find_devices=find_devices, + install_devices=install_devices, + no_download=False, + no_header=False, + verbose=0, + quiet=0, + )) + monkeypatch.setattr(command, "_get_cache", lambda: cache) + + assert command.invoke() == 1