Add arrow-key menus and report loading status
wizard: when stdin and stdout are both TTYs, menus render inline and are driven by keys: up/down/j/k/Home/End move the bold '>' highlight, Enter accepts, Esc/Ctrl-C/q back out of cancellable menus while Ctrl-C on fixed menus still aborts. Rows redraw with erase+CR across the option block only and clear before returning so prompts stay aligned. Non-TTY stdio keeps the original numbered prompt verbatim. report: each configured source shows a transient "# Generating report: pulling <name> data..." line that is erased on completion, only when stdout is a TTY; piped output remains exactly the report. tests: cover the numbered fallback, row rendering, status suppression and show+clear parity; reformat stray test files to satisfy ruff.
This commit is contained in:
@@ -74,8 +74,9 @@ class BuildSpecsTest(unittest.TestCase):
|
||||
)
|
||||
specs = build_specs(config)
|
||||
# All launchpad accounts first, then github, then bts; listing order kept.
|
||||
self.assertEqual([s.username for s in specs], ["lp-a", "lp-b", "gh-a", "gh-b",
|
||||
"one@x.org", "two@x.org"])
|
||||
self.assertEqual(
|
||||
[s.username for s in specs], ["lp-a", "lp-b", "gh-a", "gh-b", "one@x.org", "two@x.org"]
|
||||
)
|
||||
self.assertEqual(
|
||||
[s.name for s in specs],
|
||||
[
|
||||
@@ -99,8 +100,9 @@ class BuildSpecsTest(unittest.TestCase):
|
||||
|
||||
def test_launchpad_spec_expands_tilde_credentials_file(self) -> None:
|
||||
config = ActivityConfig(launchpad=[LaunchpadSettings(username="anon")])
|
||||
config.launchpad.append(LaunchpadSettings(mode="credentials", username="u",
|
||||
credentials_file="~/creds.json"))
|
||||
config.launchpad.append(
|
||||
LaunchpadSettings(mode="credentials", username="u", credentials_file="~/creds.json")
|
||||
)
|
||||
specs = build_specs(config)
|
||||
with mock.patch("weekly_activity.aggregate.LaunchpadSource") as fake_source:
|
||||
for spec in specs:
|
||||
@@ -152,8 +154,9 @@ class CollectAndFormatTest(unittest.TestCase):
|
||||
|
||||
def test_heading_uses_account_label_not_bare_source_name(self) -> None:
|
||||
since, until = window()
|
||||
specs = [SourceSpec(name="github/work", username="acme-jane",
|
||||
make=lambda: StubSource("github"))]
|
||||
specs = [
|
||||
SourceSpec(name="github/work", username="acme-jane", make=lambda: StubSource("github"))
|
||||
]
|
||||
text = format_combined(collect_specs(specs, since, until), since, until)
|
||||
self.assertIn("Activity report — github/work / ~acme-jane", text)
|
||||
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
"""Unit tests for the report command's loading-status presentation layer."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import io
|
||||
import sys
|
||||
import unittest
|
||||
from datetime import datetime
|
||||
from unittest import mock
|
||||
|
||||
from weekly_activity.aggregate import SourceSpec, collect_specs
|
||||
from weekly_activity.cli import _collect_report_sources
|
||||
from weekly_activity.model import ActivityReport, Section
|
||||
|
||||
|
||||
class StubSource:
|
||||
"""Deterministic stand-in satisfying the ActivitySource interface."""
|
||||
|
||||
def __init__(self, *, fail: bool = False) -> None:
|
||||
self._fail = fail
|
||||
self.name = "stub"
|
||||
|
||||
def collect(self, username: str, since: datetime, until: datetime) -> ActivityReport:
|
||||
if self._fail:
|
||||
raise RuntimeError("some query failed")
|
||||
return ActivityReport(
|
||||
source=username,
|
||||
username=username,
|
||||
since=since,
|
||||
until=until,
|
||||
sections=[Section(title="Patches", entries=["one commit"])],
|
||||
)
|
||||
|
||||
|
||||
def spec(name: str, *, fail: bool = False) -> SourceSpec:
|
||||
source = StubSource(fail=fail)
|
||||
return SourceSpec(name=name, username="me", make=lambda: source)
|
||||
|
||||
|
||||
WINDOW = (datetime(2026, 8, 19), datetime(2026, 8, 26))
|
||||
|
||||
|
||||
class FakeTty(io.StringIO):
|
||||
"""Capture buffer that claims to be a terminal so status lines activate."""
|
||||
|
||||
def isatty(self) -> bool:
|
||||
return True
|
||||
|
||||
|
||||
class ReportStatusTest(unittest.TestCase):
|
||||
def test_status_is_silent_without_a_tty(self) -> None:
|
||||
specs = [spec("alpha"), spec("beta")]
|
||||
buffer = io.StringIO()
|
||||
with mock.patch.object(sys, "stdout", buffer):
|
||||
collected = _collect_report_sources(specs, *WINDOW)
|
||||
self.assertEqual(buffer.getvalue(), "")
|
||||
self.assertEqual(collected, collect_specs(specs, *WINDOW))
|
||||
|
||||
def test_status_names_current_source_then_clears_on_a_tty(self) -> None:
|
||||
specs = [spec("launchpad"), spec("github/personal"), spec("broken", fail=True)]
|
||||
buffer = FakeTty()
|
||||
with mock.patch.object(sys, "stdout", buffer):
|
||||
collected = _collect_report_sources(specs, *WINDOW)
|
||||
text = buffer.getvalue()
|
||||
self.assertIn("# Generating report: pulling launchpad data...", text)
|
||||
self.assertIn("# Generating report: pulling github/personal data...", text)
|
||||
self.assertNotIn("# Generating report: pulling broken data...\n", text)
|
||||
# The final erase leaves nothing visible behind the transient status.
|
||||
self.assertTrue(text.endswith("\x1b[2K\r"))
|
||||
self.assertEqual(collected, collect_specs(specs, *WINDOW))
|
||||
errors = [item.error for item in collected if item.error is not None]
|
||||
self.assertTrue(errors and all("RuntimeError" in err for err in errors))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
+27
-19
@@ -43,9 +43,7 @@ def sample_v2_config() -> ActivityConfig:
|
||||
],
|
||||
github=[
|
||||
GithubSettings(username='gh"user', token="tok\\en"),
|
||||
GithubSettings(
|
||||
username="acme-jane", token_env="WORK_GH_TOKEN", name="work"
|
||||
),
|
||||
GithubSettings(username="acme-jane", token_env="WORK_GH_TOKEN", name="work"),
|
||||
],
|
||||
gitlab=[
|
||||
GitlabSettings(
|
||||
@@ -121,13 +119,11 @@ class ConfigLoadingTest(unittest.TestCase):
|
||||
|
||||
def test_unknown_top_level_key_rejected(self) -> None:
|
||||
with self.assertRaises(ConfigError):
|
||||
load_config_from_text('version = 2\nsources = 3\n')
|
||||
load_config_from_text("version = 2\nsources = 3\n")
|
||||
|
||||
def test_unknown_account_key_rejected(self) -> None:
|
||||
with self.assertRaises(ConfigError):
|
||||
load_config_from_text(
|
||||
'[github]\naccounts = [{username = "x", tokken = "t"}]\n'
|
||||
)
|
||||
load_config_from_text('[github]\naccounts = [{username = "x", tokken = "t"}]\n')
|
||||
|
||||
def test_section_must_hold_only_accounts_table(self) -> None:
|
||||
with self.assertRaises(ConfigError):
|
||||
@@ -141,13 +137,11 @@ class ConfigLoadingTest(unittest.TestCase):
|
||||
with self.assertRaises(ConfigError):
|
||||
load_config_from_text('version = 2\n\n[github]\naccounts = "nope"\n')
|
||||
with self.assertRaises(ConfigError):
|
||||
load_config_from_text('version = 2\n\n[gitlab]\naccounts = [42]\n')
|
||||
load_config_from_text("version = 2\n\n[gitlab]\naccounts = [42]\n")
|
||||
|
||||
def test_non_string_value_rejected(self) -> None:
|
||||
with self.assertRaises(ConfigError):
|
||||
load_config_from_text(
|
||||
'version = 2\n\n[[github.accounts]]\nusername = 42\n'
|
||||
)
|
||||
load_config_from_text("version = 2\n\n[[github.accounts]]\nusername = 42\n")
|
||||
|
||||
def test_bad_launchpad_mode_rejected(self) -> None:
|
||||
with self.assertRaises(ConfigError):
|
||||
@@ -169,8 +163,8 @@ class ConfigLoadingTest(unittest.TestCase):
|
||||
def test_account_errors_carry_position(self) -> None:
|
||||
text = (
|
||||
"version = 2\n"
|
||||
"\n[[launchpad.accounts]]\nusername = \"ok\"\n"
|
||||
"\n[[launchpad.accounts]]\nusername = \"bad\"\nservice = \"oops\"\n"
|
||||
'\n[[launchpad.accounts]]\nusername = "ok"\n'
|
||||
'\n[[launchpad.accounts]]\nusername = "bad"\nservice = "oops"\n'
|
||||
)
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
path = Path(tmp) / "weekly-activity.toml"
|
||||
@@ -209,13 +203,27 @@ email = "dev@example.org"
|
||||
def test_v1_file_migrates_to_single_unnamed_accounts_in_memory(self) -> None:
|
||||
config = load_config_from_text(self.V1_TEXT)
|
||||
self.assertEqual(config.enabled_sources(), ["launchpad", "github", "gitlab", "bts"])
|
||||
self.assertEqual(config.launchpad, [LaunchpadSettings(
|
||||
username="lp-user", mode="credentials", credentials_file="~/creds.json",
|
||||
)])
|
||||
self.assertEqual(
|
||||
config.launchpad,
|
||||
[
|
||||
LaunchpadSettings(
|
||||
username="lp-user",
|
||||
mode="credentials",
|
||||
credentials_file="~/creds.json",
|
||||
)
|
||||
],
|
||||
)
|
||||
self.assertEqual(config.github, [GithubSettings(username="octo", token="t0k")])
|
||||
self.assertEqual(config.gitlab, [GitlabSettings(
|
||||
url="https://salsa.debian.org", username="gl", token_env="SALSA_TOKEN",
|
||||
)])
|
||||
self.assertEqual(
|
||||
config.gitlab,
|
||||
[
|
||||
GitlabSettings(
|
||||
url="https://salsa.debian.org",
|
||||
username="gl",
|
||||
token_env="SALSA_TOKEN",
|
||||
)
|
||||
],
|
||||
)
|
||||
self.assertEqual(config.bts, [BtsSettings(email="dev@example.org")])
|
||||
|
||||
def test_saving_migrated_config_persists_v2_shape(self) -> None:
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
"""Unit tests for the wizard menus — scripted input, no raw terminal involved."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import io
|
||||
import sys
|
||||
import unittest
|
||||
from unittest import mock
|
||||
|
||||
from weekly_activity.wizard import _menu, _option_line
|
||||
|
||||
|
||||
class NumberedMenuFallbackTest(unittest.TestCase):
|
||||
"""When stdin/stdout are not a TTY the numbered prompt stays as it was."""
|
||||
|
||||
def run_numbered(self, answers: list[str], options: list[str], *, cancellable: bool = False):
|
||||
"""Run _menu with forced non-TTY stdio and scripted interactive answers."""
|
||||
fake_stdin, fake_stdout = io.StringIO(), io.StringIO()
|
||||
with (
|
||||
mock.patch.object(sys, "stdin", fake_stdin),
|
||||
mock.patch.object(sys, "stdout", fake_stdout),
|
||||
mock.patch("builtins.input", side_effect=iter(answers)),
|
||||
):
|
||||
picked = _menu("Pick one", options, cancellable=cancellable)
|
||||
return picked, fake_stdout.getvalue()
|
||||
|
||||
def test_returns_zero_based_index_of_chosen_option(self) -> None:
|
||||
picked, output = self.run_numbered(["2"], ["Alpha", "Beta", "Gamma"])
|
||||
self.assertEqual(picked, 1)
|
||||
self.assertIn("Pick one", output)
|
||||
self.assertIn(" 2) Beta", output)
|
||||
|
||||
def test_empty_answer_backs_out_of_cancellable_menu(self) -> None:
|
||||
picked, _ = self.run_numbered([""], ["Alpha", "Beta"], cancellable=True)
|
||||
self.assertIsNone(picked)
|
||||
|
||||
def test_back_number_returns_none(self) -> None:
|
||||
picked, _ = self.run_numbered(["3"], ["Alpha", "Beta"], cancellable=True)
|
||||
self.assertIsNone(picked)
|
||||
|
||||
def test_invalid_answers_loop_until_valid_one(self) -> None:
|
||||
picked, output = self.run_numbered(["9", "nope", "3"], ["Alpha", "Beta", "Gamma"])
|
||||
self.assertEqual(picked, 2)
|
||||
self.assertEqual(output.count("Please enter a number between 1 and 3."), 2)
|
||||
|
||||
|
||||
class OptionLineRenderingTest(unittest.TestCase):
|
||||
"""Selected rows carry the '>' marker and bold codes; others stay plain."""
|
||||
|
||||
def test_selected_line_is_bold_and_marked(self) -> None:
|
||||
self.assertEqual(_option_line("Exit", True), "\x1b[1m> Exit\x1b[0m")
|
||||
|
||||
def test_unselected_line_is_indented_and_plain(self) -> None:
|
||||
self.assertEqual(_option_line("Remove a source", False), " Remove a source")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user