|
| 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