Skip to content

Commit c15dae9

Browse files
committed
Fix error handling for checking if file cached when no internet connection
1 parent c6f1bf2 commit c15dae9

File tree

2 files changed

+76
-2
lines changed

2 files changed

+76
-2
lines changed

circfirm/cli/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import click
1919
import click_spinner
20+
import requests
2021
import yaml
2122

2223
import circfirm
@@ -103,9 +104,10 @@ def download_if_needed(board: str, version: str, language: str) -> None:
103104
circfirm.backend.cache.download_uf2,
104105
args=(board, version, language),
105106
)
106-
except ConnectionError as err:
107+
except (ConnectionError, requests.exceptions.ConnectionError) as err:
107108
click.echo(" failed") # Mark as failed
108-
click.echo(f"Error: {err.args[0]}")
109+
if isinstance(err, ConnectionError):
110+
click.echo(f"Error: {err.args[0]}")
109111
sys.exit(4)
110112
else:
111113
click.echo("Using cached firmware file")

tests/cli/test_cli.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# SPDX-FileCopyrightText: 2026 Alec Delaney
2+
# SPDX-License-Identifier: MIT
3+
4+
"""Tests the core CLI functionality.
5+
6+
Author(s): Alec Delaney
7+
"""
8+
9+
import shutil
10+
from typing import NoReturn
11+
12+
import pytest
13+
14+
import circfirm.backend.cache
15+
import circfirm.cli
16+
17+
BOARD = "feather_m0_express"
18+
LANGUAGE = "cs"
19+
VERSION = "6.1.0"
20+
21+
22+
@pytest.mark.parametrize(
23+
"cached",
24+
(
25+
(True,),
26+
(False,),
27+
),
28+
)
29+
def test_download_if_needed_cached(
30+
monkeypatch: pytest.MonkeyPatch,
31+
cached: bool,
32+
) -> None:
33+
"""Tests the install command when there is a cached firmware and has internet."""
34+
monkeypatch.setattr(
35+
circfirm.backend.cache, "is_downloaded", lambda _x, _y, _z: cached
36+
)
37+
try:
38+
circfirm.cli.download_if_needed(BOARD, VERSION, LANGUAGE)
39+
finally:
40+
board_folder = circfirm.backend.cache.get_board_folder(BOARD)
41+
if board_folder.exists(): # pragma: no cover
42+
shutil.rmtree(board_folder)
43+
44+
45+
@pytest.mark.parametrize(
46+
"cached,succeeds",
47+
(
48+
(True, True),
49+
(False, False),
50+
),
51+
)
52+
def test_download_if_needed_cached_no_internet(
53+
monkeypatch: pytest.MonkeyPatch,
54+
mock_no_internet: NoReturn,
55+
cached: bool,
56+
succeeds: bool,
57+
) -> None:
58+
"""Tests the install command when there is a cached firmware but no internet."""
59+
monkeypatch.setattr(
60+
circfirm.backend.cache, "is_downloaded", lambda _x, _y, _z: cached
61+
)
62+
63+
try:
64+
if succeeds:
65+
circfirm.cli.download_if_needed(BOARD, VERSION, LANGUAGE)
66+
else:
67+
with pytest.raises(SystemExit):
68+
circfirm.cli.download_if_needed(BOARD, VERSION, LANGUAGE)
69+
finally:
70+
board_folder = circfirm.backend.cache.get_board_folder(BOARD)
71+
if board_folder.exists(): # pragma: no cover
72+
shutil.rmtree(board_folder)

0 commit comments

Comments
 (0)