From bc0819b1914e6d6bac25352dd8868f9a97bb0137 Mon Sep 17 00:00:00 2001 From: Solarboaters <354059055+Solarboaters@users.noreply.github.com> Date: Sat, 28 Nov 2026 07:08:57 +0000 Subject: [PATCH] Show probe discovery warnings in the list command --- pyocd/subcommands/list_cmd.py | 3 +- test/unit/test_list_probe_warnings.py | 56 +++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 test/unit/test_list_probe_warnings.py diff --git a/pyocd/subcommands/list_cmd.py b/pyocd/subcommands/list_cmd.py index 683053eef..7ce744b10 100644 --- a/pyocd/subcommands/list_cmd.py +++ b/pyocd/subcommands/list_cmd.py @@ -1,6 +1,7 @@ # pyOCD debugger # Copyright (c) 2021 Chris Reed # Copyright (c) 2025 Arm Limited +# Copyright (c) 2026 Solarboaters # SPDX-License-Identifier: Apache-2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -33,7 +34,7 @@ class ListSubcommand(SubcommandBase): NAMES = ['list'] HELP = "List information about probes, targets, or boards." - DEFAULT_LOG_LEVEL = logging.ERROR + DEFAULT_LOG_LEVEL = logging.WARNING ## @brief Map to convert plugin groups to user friendly names. PLUGIN_GROUP_NAMES = { diff --git a/test/unit/test_list_probe_warnings.py b/test/unit/test_list_probe_warnings.py new file mode 100644 index 000000000..ad6d28bfd --- /dev/null +++ b/test/unit/test_list_probe_warnings.py @@ -0,0 +1,56 @@ +# pyOCD debugger +# Copyright (c) 2026 Solarboaters +# 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. + +import os +from pathlib import Path +import subprocess +import sys + +import pytest + + +@pytest.mark.parametrize('quiet', [False, True]) +@pytest.mark.parametrize('denied', [False, True]) +def test_list_explains_usb_permission_failure(tmp_path, quiet, denied): + # Exercise real CLI logging and ST-Link discovery with a denied USB device. + script = r""" +import errno +import sys +from unittest.mock import Mock, patch +import usb.core +from pyocd.__main__ import PyOCDTool +from pyocd.probe.aggregator import DebugProbeAggregator +from pyocd.probe.stlink_probe import StlinkProbe + +probe = Mock(idVendor=0x0483, idProduct=0x3748) +probe.get_active_configuration.side_effect = usb.core.USBError('Access denied', errno=errno.EACCES) +def find(*, find_all, custom_match): + return [dev for dev in ([probe] if sys.argv[1] == 'denied' else []) if custom_match(dev)] +with patch('pyocd.probe.stlink.usb.usb_find', find), \ + patch('pyocd.probe.stlink.usb.platform.system', return_value='Linux'), \ + patch.object(DebugProbeAggregator, 'get_all_connected_probes', StlinkProbe.get_all_connected_probes): + sys.exit(PyOCDTool().run(['list', '--no-config', '--color=never'] + sys.argv[2:])) +""" + root = Path(__file__).resolve().parents[2] + args = [sys.executable, '-c', script, 'denied' if denied else 'none'] + if quiet: + args.append('-q') + result = subprocess.run(args, cwd=tmp_path, env=dict(os.environ, PYTHONPATH=str(root)), + capture_output=True, text=True, timeout=20) + assert result.returncode == 0, result.stderr + assert 'No available debug probes' in result.stdout + assert ('udev rule' in result.stderr) == (denied and not quiet) + assert 'udev' not in result.stdout