Skip to content

Commit

Permalink
Add helper decorators, spread out tests into more discrete units
Browse files Browse the repository at this point in the history
  • Loading branch information
tekktrik committed Mar 6, 2024
1 parent 8ea3f0c commit 96353b8
Show file tree
Hide file tree
Showing 9 changed files with 204 additions and 156 deletions.
6 changes: 3 additions & 3 deletions tests/cli/test_cli_about.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

from circfirm.cli import cli

RUNNER = CliRunner()


def test_about() -> None:
"""Tests the about command."""
runner = CliRunner()

result = runner.invoke(cli, ["about"])
result = RUNNER.invoke(cli, ["about"])
assert result.exit_code == 0
assert result.output == "Written by Alec Delaney, licensed under MIT License.\n"
24 changes: 11 additions & 13 deletions tests/cli/test_cli_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
import tests.helpers
from circfirm.cli import cli

RUNNER = CliRunner()


def test_cache_list() -> None:
"""Tests the cache list command."""
runner = CliRunner()

# Test empty cache
result = runner.invoke(cli, ["cache", "list"])
result = RUNNER.invoke(cli, ["cache", "list"])
assert result.exit_code == 0
assert result.output == "Versions have not been cached yet for any boards.\n"

Expand All @@ -33,7 +33,7 @@ def test_cache_list() -> None:
# Get full list expected response
with open("tests/assets/responses/full_list.txt", encoding="utf-8") as respfile:
expected_response = respfile.read()
result = runner.invoke(cli, ["cache", "list"])
result = RUNNER.invoke(cli, ["cache", "list"])
assert result.exit_code == 0
assert result.output == expected_response

Expand All @@ -42,7 +42,7 @@ def test_cache_list() -> None:
"tests/assets/responses/specific_board.txt", encoding="utf-8"
) as respfile:
expected_response = respfile.read()
result = runner.invoke(cli, ["cache", "list", "--board", "feather_m4_express"])
result = RUNNER.invoke(cli, ["cache", "list", "--board", "feather_m4_express"])
assert result.exit_code == 0
assert result.output == expected_response

Expand All @@ -52,7 +52,7 @@ def test_cache_list() -> None:
"tests/assets/responses/specific_board.txt", encoding="utf-8"
) as respfile:
expected_response = respfile.read()
result = runner.invoke(cli, ["cache", "list", "--board", fake_board])
result = RUNNER.invoke(cli, ["cache", "list", "--board", fake_board])
assert result.exit_code == 0
assert result.output == f"No versions for board '{fake_board}' are not cached.\n"

Expand All @@ -66,10 +66,9 @@ def test_cache_save() -> None:
board = "feather_m4_express"
version = "7.3.0"
langauge = "fr"
runner = CliRunner()

# Save a specific firmware (successful)
result = runner.invoke(
result = RUNNER.invoke(
cli, ["cache", "save", board, version, "--language", langauge]
)
assert result.exit_code == 0
Expand All @@ -78,7 +77,7 @@ def test_cache_save() -> None:
shutil.rmtree(expected_path.parent.resolve())

# Save a specific firmware (unsuccessful)
result = runner.invoke(
result = RUNNER.invoke(
cli, ["cache", "save", board, version, "--language", "nolanguage"]
)
assert result.exit_code == 1
Expand All @@ -96,13 +95,12 @@ def test_cache_clear() -> None:
board = "feather_m4_express"
version = "7.1.0"
langauge = "zh_Latn_pinyin"
runner = CliRunner()

# Move firmware files to app directory
tests.helpers.copy_firmwares()

# Remove a specific firmware from the cache
result = runner.invoke(
result = RUNNER.invoke(
cli,
[
"cache",
Expand All @@ -126,13 +124,13 @@ def test_cache_clear() -> None:
assert board_folder.exists()

# Remove a specific board firmware from the cache
result = runner.invoke(cli, ["cache", "clear", "--board", board])
result = RUNNER.invoke(cli, ["cache", "clear", "--board", board])
assert result.exit_code == 0
assert result.output == "Cache cleared of specified entries!\n"
assert not board_folder.exists()

# Remove entire cache
result = runner.invoke(cli, ["cache", "clear"])
result = RUNNER.invoke(cli, ["cache", "clear"])
assert result.exit_code == 0
assert result.output == "Cache cleared!\n"
assert len(list(board_folder.parent.glob("*"))) == 0
33 changes: 16 additions & 17 deletions tests/cli/test_cli_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

from circfirm.cli import cli

RUNNER = CliRunner()


def get_printed_default_settings() -> None:
"""Get the default (template) settings as printed."""
Expand All @@ -24,58 +26,55 @@ def get_printed_default_settings() -> None:

def test_config() -> None:
"""Tests the config view command."""
runner = CliRunner()
expected_output = get_printed_default_settings()

# Test viewing all settings
result = runner.invoke(cli, ["config", "view"])
result = RUNNER.invoke(cli, ["config", "view"])
assert result.exit_code == 0
assert result.output == expected_output

# Test viewing specific setting
result = runner.invoke(cli, ["config", "view", "output.supporting.silence"])
result = RUNNER.invoke(cli, ["config", "view", "output.supporting.silence"])
assert result.exit_code == 0
assert result.output == "false\n"

# Test viewing non-existent setting
result = runner.invoke(cli, ["config", "view", "doesnotexist"])
result = RUNNER.invoke(cli, ["config", "view", "doesnotexist"])
assert result.exit_code != 0

# Test writing a setting
runner.invoke(cli, ["config", "edit", "output.supporting.silence", "true"])
result = runner.invoke(cli, ["config", "view", "output.supporting.silence"])
RUNNER.invoke(cli, ["config", "edit", "output.supporting.silence", "true"])
result = RUNNER.invoke(cli, ["config", "view", "output.supporting.silence"])
assert result.exit_code == 0
assert result.output == "true\n"
runner.invoke(cli, ["config", "edit", "output.supporting.silence", "false"])
result = runner.invoke(cli, ["config", "view", "output.supporting.silence"])
RUNNER.invoke(cli, ["config", "edit", "output.supporting.silence", "false"])
result = RUNNER.invoke(cli, ["config", "view", "output.supporting.silence"])
assert result.exit_code == 0
assert result.output == "false\n"

# Test writing a non-existent setting
result = runner.invoke(cli, ["config", "edit", "doesnotexist", "123"])
result = RUNNER.invoke(cli, ["config", "edit", "doesnotexist", "123"])
assert result.exit_code != 0

# Test writing to setting tree
result = runner.invoke(cli, ["config", "edit", "output", "123"])
result = RUNNER.invoke(cli, ["config", "edit", "output", "123"])
assert result.exit_code != 0

# Test writing bad type
runner.invoke(cli, ["config", "edit", "output.supporting.silence", "123"])
RUNNER.invoke(cli, ["config", "edit", "output.supporting.silence", "123"])
assert result.exit_code != 0


def test_config_reset() -> None:
"""Tests the reseting of the config settings file."""
runner = CliRunner()

runner.invoke(cli, ["config", "edit", "output.supporting.silence", "true"])
result = runner.invoke(cli, ["config", "view", "output.supporting.silence"])
RUNNER.invoke(cli, ["config", "edit", "output.supporting.silence", "true"])
result = RUNNER.invoke(cli, ["config", "view", "output.supporting.silence"])
assert result.exit_code == 0
assert result.output == "true\n"

expected_settings = get_printed_default_settings()

result = runner.invoke(cli, ["config", "reset"])
result = runner.invoke(cli, ["config", "view"])
result = RUNNER.invoke(cli, ["config", "reset"])
result = RUNNER.invoke(cli, ["config", "view"])
assert result.exit_code == 0
assert result.output == expected_settings
21 changes: 10 additions & 11 deletions tests/cli/test_cli_current.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,24 @@
import tests.helpers
from circfirm.cli import cli

RUNNER = CliRunner()

def test_current() -> None:
"""Tests the current name command."""
runner = CliRunner()
tests.helpers.delete_mount_node(circfirm.UF2INFO_FILE)
tests.helpers.copy_boot_out()

@tests.helpers.as_circuitpy
def test_current() -> None:
"""Tests the current name and version commands."""
# Test when connected in CIRCUITPY mode
result = runner.invoke(cli, ["current", "name"])
result = RUNNER.invoke(cli, ["current", "name"])
assert result.exit_code == 0
assert result.output == "feather_m4_express\n"

result = runner.invoke(cli, ["current", "version"])
result = RUNNER.invoke(cli, ["current", "version"])
assert result.exit_code == 0
assert result.output == "8.0.0-beta.6\n"

tests.helpers.delete_mount_node(circfirm.BOOTOUT_FILE)
tests.helpers.copy_uf2_info()

# Test when connected in bootloader mode
result = runner.invoke(cli, ["current", "name"])
@tests.helpers.as_bootloader
def test_current_in_bootloader() -> None:
"""Tests the current command whenn connected in bootloader mode."""
result = RUNNER.invoke(cli, ["current", "name"])
assert result.exit_code != 0
96 changes: 53 additions & 43 deletions tests/cli/test_cli_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,57 +17,67 @@
import tests.helpers
from circfirm.cli import cli

RUNNER = CliRunner()

def test_install() -> None:
"""Tests the install command."""
version = "8.0.0-beta.6"
runner = CliRunner()

# Test successfully installing the firmware
tests.helpers.delete_mount_node(circfirm.UF2INFO_FILE)
tests.helpers.copy_boot_out()
threading.Thread(target=tests.helpers.wait_and_set_bootloader).start()
result = runner.invoke(cli, ["install", version])
assert result.exit_code == 0
expected_uf2_filename = circfirm.backend.get_uf2_filename(
"feather_m4_express", version
)
expected_uf2_filepath = tests.helpers.get_mount_node(expected_uf2_filename)
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
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"])
ERR_NOT_FOUND = 1
ERR_FOUND_CIRCUITPY = 2
ERR_IN_BOOTLOADER = 3
ERR_UF2_DOWNLOAD = 4

VERSION = "8.0.0-beta.6"


@tests.helpers.as_circuitpy
def test_install_successful() -> None:
"""Tests the successful use of the install command."""
try:
# Test successfully installing the firmware
threading.Thread(target=tests.helpers.wait_and_set_bootloader).start()
result = RUNNER.invoke(cli, ["install", VERSION])
assert result.exit_code == 0
expected_uf2_filename = circfirm.backend.get_uf2_filename(
"feather_m4_express", VERSION
)
expected_uf2_filepath = tests.helpers.get_mount_node(expected_uf2_filename)
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)

finally:
board_folder = circfirm.backend.get_board_folder("feather_m4_express")
if board_folder.exists():
shutil.rmtree(board_folder)


@tests.helpers.as_not_present
def test_install_no_mount() -> None:
"""Tests the install command when a mounted drive is not found."""
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"])

@tests.helpers.as_circuitpy
def test_install_as_circuitpy() -> None:
"""Tests the install command when a mounted CIRCUITPY drive is found."""
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(

@tests.helpers.as_bootloader
def test_install_bad_version() -> None:
"""Tests the install command using a bad board version."""
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])
result = RUNNER.invoke(cli, ["install", VERSION])
assert result.exit_code == ERR_IN_BOOTLOADER

board_folder = circfirm.backend.get_board_folder("feather_m4_express")
shutil.rmtree(board_folder)
Loading

0 comments on commit 96353b8

Please sign in to comment.