Skip to content

Commit

Permalink
Merge pull request #40 from tekktrik/dev/improved-feedback
Browse files Browse the repository at this point in the history
Add improved feedback messages
  • Loading branch information
tekktrik authored Feb 23, 2024
2 parents 24a4179 + 1a47992 commit 02fe5b5
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 24 deletions.
45 changes: 39 additions & 6 deletions circfirm/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import shutil
import sys
import time
from typing import Optional
from typing import Any, Callable, Dict, Iterable, Optional

import click

Expand All @@ -28,6 +28,22 @@ def cli() -> None:
circfirm.startup.ensure_app_setup()


def announce_and_await(
msg: str,
func: Callable,
args: Iterable = (),
kwargs: Optional[Dict[str, Any]] = None,
) -> Any:
"""Announce an action to be performed, do it, then announce its completion."""
if kwargs is None:
kwargs = {}
fmt_msg = f"{msg}..."
click.echo(fmt_msg, nl=False)
resp = func(*args, **kwargs)
click.echo(" done")
return resp


@cli.command()
@click.argument("version")
@click.option("-l", "--language", default="en_US", help="CircuitPython language/locale")
Expand Down Expand Up @@ -61,13 +77,25 @@ def install(version: str, language: str, board: Optional[str]) -> None:
sys.exit(2)

if not circfirm.backend.is_downloaded(board, version, language):
click.echo("Downloading UF2...")
circfirm.backend.download_uf2(board, version, language)
try:
announce_and_await(
"Downloading UF2",
circfirm.backend.download_uf2,
args=(board, version, language),
)
except ConnectionError as err:
click.echo(" failed") # Mark as failed
click.echo(f"Error: {err.args[0]}")
sys.exit(4)
else:
click.echo("Using cached firmware file")

uf2file = circfirm.backend.get_uf2_filepath(board, version, language)
uf2filename = os.path.basename(uf2file)
shutil.copyfile(uf2file, os.path.join(bootloader, uf2filename))
click.echo("UF2 file copied to device!")
uf2_path = os.path.join(bootloader, uf2filename)
announce_and_await(
f"Copying UF2 to {board}", shutil.copyfile, args=(uf2file, uf2_path)
)
click.echo("Device should reboot momentarily.")


Expand Down Expand Up @@ -138,6 +166,11 @@ def cache_list(board: Optional[str]) -> None:
def cache_save(board: str, version: str, language: str) -> None:
"""Install a version of CircuitPython to cache."""
try:
circfirm.backend.download_uf2(board, version, language)
announce_and_await(
f"Caching firmware version {version} for {board}",
circfirm.backend.download_uf2,
args=(board, version, language),
)
except ConnectionError as err:
click.echo(" failed") # Mark as failed
raise click.exceptions.ClickException(err.args[0])
45 changes: 27 additions & 18 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,26 +45,34 @@ def wait_and_add() -> None:
assert os.path.exists(expected_uf2_filepath)
os.remove(expected_uf2_filepath)

# Test using cached version of firmware
result = runner.invoke(cli, ["install", version, "--board", "feather_m4_express"])
assert result.exit_code == 0
assert "Using cached firmware file" in result.output
os.remove(expected_uf2_filepath)

ERR_NOT_FOUND = 1
ERR_FOUND_CIRCUITPY = 2
ERR_IN_BOOTLOADER = 3
try:
# Test not finding the mounted drive
tests.helpers.delete_mount_node(circfirm.UF2INFO_FILE)
result = runner.invoke(
cli, ["install", version, "--board", "feather_m4_express"]
)
assert result.exit_code == ERR_NOT_FOUND

# Test finding the mounted drive as CIRCUITPY
tests.helpers.copy_boot_out()
result = runner.invoke(
cli, ["install", version, "--board", "feather_m4_express"]
)
assert result.exit_code == ERR_FOUND_CIRCUITPY
tests.helpers.delete_mount_node(circfirm.BOOTOUT_FILE)
finally:
tests.helpers.copy_uf2_info()
ERR_UF2_DOWNLOAD = 4

# Test not finding the mounted drive
tests.helpers.delete_mount_node(circfirm.UF2INFO_FILE)
result = runner.invoke(cli, ["install", version, "--board", "feather_m4_express"])
assert result.exit_code == ERR_NOT_FOUND

# Test finding the mounted drive as CIRCUITPY
tests.helpers.copy_boot_out()
result = runner.invoke(cli, ["install", version, "--board", "feather_m4_express"])
assert result.exit_code == ERR_FOUND_CIRCUITPY

# Test using bad board version
tests.helpers.delete_mount_node(circfirm.BOOTOUT_FILE)
tests.helpers.copy_uf2_info()
result = runner.invoke(
cli, ["install", "doesnotexist", "--board", "feather_m4_express"]
)
assert result.exit_code == ERR_UF2_DOWNLOAD

# Test using install when in bootloader mode
result = runner.invoke(cli, ["install", version])
Expand Down Expand Up @@ -133,12 +141,13 @@ def test_cache_save() -> None:
assert expected_path.exists()
shutil.rmtree(expected_path.parent.resolve())

# Save a specifici firmware (unsuccessful)
# Save a specific firmware (unsuccessful)
result = runner.invoke(
cli, ["cache", "save", board, version, "--language", "nolanguage"]
)
assert result.exit_code == 1
assert result.output == (
"Caching firmware version 7.3.0 for feather_m4_express... failed\n"
"Error: Could not download spectified UF2 file:\n"
"https://downloads.circuitpython.org/bin/feather_m4_express/"
"nolanguage/"
Expand Down

0 comments on commit 02fe5b5

Please sign in to comment.