Skip to content

Commit

Permalink
Add command for caching latest firmware version
Browse files Browse the repository at this point in the history
  • Loading branch information
tekktrik committed Mar 15, 2024
1 parent 32330c2 commit 81976c3
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
28 changes: 27 additions & 1 deletion circfirm/cli/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import circfirm
import circfirm.backend.cache
import circfirm.backend.s3
import circfirm.cli
import circfirm.startup

Expand Down Expand Up @@ -88,7 +89,7 @@ def cache_list(board_id: Optional[str]) -> None:
@click.argument("version")
@click.option("-l", "--language", default="en_US", help="CircuitPython language/locale")
def cache_save(board_id: str, version: str, language: str) -> None:
"""Install a version of CircuitPython to cache."""
"""Download a version of CircuitPython to the cache."""
try:
circfirm.cli.announce_and_await(
f"Caching firmware version {version} for {board_id}",
Expand All @@ -97,3 +98,28 @@ def cache_save(board_id: str, version: str, language: str) -> None:
)
except ConnectionError as err:
raise click.exceptions.ClickException(err.args[0])


@cli.command(name="latest")
@click.argument("board-id")
@click.option("-l", "--language", default="en_US", help="CircuitPython language/locale")
@click.option(
"-p",
"--pre-release",
is_flag=True,
default=False,
help="Whether pre-release versions should be considered",
)
def cache_latest(board_id: str, language: str, pre_release: bool) -> None:
"""Download the latest version of CircuitPython to the cache."""
try:
version = circfirm.backend.s3.get_latest_board_version(
board_id, language, pre_release
)
circfirm.cli.announce_and_await(
f"Caching firmware version {version} for {board_id}",
circfirm.backend.cache.download_uf2,
args=(board_id, version, language),
)
except ConnectionError as err:
raise click.exceptions.ClickException(err.args[0])
31 changes: 30 additions & 1 deletion tests/cli/test_cli_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def test_cache_clear() -> None:
"""Tests the cache clear command."""
board = "feather_m4_express"
version = "7.1.0"
langauge = "zh_Latn_pinyin"
language = "zh_Latn_pinyin"

# Move firmware files to app directory
tests.helpers.copy_firmwares()
Expand Down Expand Up @@ -137,3 +137,32 @@ def test_cache_clear() -> None:
assert result.exit_code == 0
assert result.output == "Cache cleared!\n"
assert len(list(board_folder.parent.glob("*"))) == 0


def test_cache_latest() -> None:
"""Test the update command when in CIRCUITPY mode."""
board = "feather_m0_express"
language = "cs"
expected_version = "6.1.0"

uf2_filepath = circfirm.backend.cache.get_uf2_filepath(
board, expected_version, language
)
assert not os.path.exists(uf2_filepath)

try:
# Save the latest version (successful)
result = RUNNER.invoke(cli, ["cache", "latest", board, "--language", language])
assert result.exit_code == 0
assert os.path.exists(uf2_filepath)

# Save the latest version (unsuccessful)
result = RUNNER.invoke(
cli, ["cache", "latest", board, "--language", "doesnotexist"]
)
assert result.exit_code == 1

finally:
board_folder = circfirm.backend.cache.get_board_folder(board)
if board_folder.exists():
shutil.rmtree(board_folder)

0 comments on commit 81976c3

Please sign in to comment.