---
id: TASK-27.4
title: Add unit tests for sysbuild image selection and run/flash/runners edge cases
status: Done
assignee: []
created_date: '2026-03-03 09:15'
updated_date: '2026-03-03 09:36'
labels:
  - tests
  - coverage
dependencies: []
references:
  - src/core/cmd.rs
parent_task_id: TASK-27
---

## Description

<!-- SECTION:DESCRIPTION:BEGIN -->
Cover uncovered cmd.rs branches for sysbuild image discovery/selection and run/flash/runners edge cases based on the 2026-03-03 llvm-cov report.
<!-- SECTION:DESCRIPTION:END -->

## Acceptance Criteria
<!-- AC:BEGIN -->
- [x] #1 resolve_sysbuild_image returns error when images list is empty; returns error for invalid index (0 or >len); returns selected image by index and by name; defaults to first non-mcuboot when available, otherwise mcuboot.
- [x] #2 find_images skips non-directories and ignores non-image directories listed in non_image_dirs; includes valid image directories.
- [x] #3 cmd_run/cmd_flash cover sysbuild image resolution paths (successful image selection logs + build_dir update) and error path when image resolution fails (inspect_err log).
- [x] #4 cmd_runners covers empty runners list path (prints "None.") and optional flash-runner display.
- [x] #5 All new tests pass with `cargo llvm-cov --lib --bins`.
<!-- AC:END -->

## Implementation Plan

<!-- SECTION:PLAN:BEGIN -->
1) Add sysbuild image tests for empty list, invalid index, mcuboot-only default, and cmd_run/cmd_flash sysbuild success/error paths.
2) Add helper to format runners output and tests covering empty list + flash runner and non-empty list formatting.
3) Run `cargo llvm-cov --lib --bins` to verify coverage.
<!-- SECTION:PLAN:END -->

## Implementation Notes

<!-- SECTION:NOTES:BEGIN -->
Added sysbuild image tests for empty list error, zero index, mcuboot-only default, cmd_run/cmd_flash sysbuild success and error paths. Added format_runners_output helper and tests covering empty list/flash runner and list formatting. Adjusted discover_zephyr_restores_existing_env_base to restore original env to avoid cross-test pollution.

Ran `cargo llvm-cov --lib --bins --text --show-missing-lines --output-path /tmp/llvm-cov.txt` (123 unit tests + main test passed).
<!-- SECTION:NOTES:END -->
