mirror of
https://github.com/rr-/pq-cli.git
synced 2026-11-12 22:40:36 -07:00
pqcli: reject --load-save 0 and negative numbers
`if args.load_save:` treated 0 as unset, so the menu opened instead, and a negative number indexed from the end of the roster, so -1 played the last character. Both now list the characters and exit like any other number out of range.
This commit is contained in:
2 files changed
+6
-15
No files matched your search
+4
-5
@@ -112,7 +112,7 @@ def list_players(roster: Roster, file: TextIO | None = None) -> None:
|
|||||||
def bootstrap_first_run(
|
def bootstrap_first_run(
|
||||||
roster: Roster, args: argparse.Namespace, data: GameData
|
roster: Roster, args: argparse.Namespace, data: GameData
|
||||||
) -> None:
|
) -> None:
|
||||||
if roster.players or args.list_saves or args.load_save:
|
if roster.players or args.list_saves or args.load_save is not None:
|
||||||
return
|
return
|
||||||
|
|
||||||
print("No saved characters found. Starting first-run character creation.")
|
print("No saved characters found. Starting first-run character creation.")
|
||||||
@@ -193,13 +193,12 @@ def main() -> None:
|
|||||||
exit(0)
|
exit(0)
|
||||||
|
|
||||||
player: Player | None = None
|
player: Player | None = None
|
||||||
if args.load_save:
|
if args.load_save is not None:
|
||||||
try:
|
if not 1 <= args.load_save <= len(roster.players):
|
||||||
player = list(roster.players)[args.load_save - 1]
|
|
||||||
except IndexError:
|
|
||||||
print("Invalid player. Available players:", file=sys.stderr)
|
print("Invalid player. Available players:", file=sys.stderr)
|
||||||
list_players(roster, file=sys.stderr)
|
list_players(roster, file=sys.stderr)
|
||||||
exit(1)
|
exit(1)
|
||||||
|
player = roster.players[args.load_save - 1]
|
||||||
|
|
||||||
bootstrap_first_run(roster, args, data)
|
bootstrap_first_run(roster, args, data)
|
||||||
|
|
||||||
|
|||||||
+2
-10
@@ -90,19 +90,12 @@ def test_list_saves_when_there_are_none(
|
|||||||
assert not config.exists()
|
assert not config.exists()
|
||||||
|
|
||||||
|
|
||||||
BAD_LOAD_SAVE = "--load-save 0 and negative numbers are not rejected"
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"num",
|
"num",
|
||||||
[
|
[
|
||||||
"3",
|
"3",
|
||||||
pytest.param(
|
"0",
|
||||||
"0", marks=pytest.mark.xfail(strict=True, reason=BAD_LOAD_SAVE)
|
"-1",
|
||||||
),
|
|
||||||
pytest.param(
|
|
||||||
"-1", marks=pytest.mark.xfail(strict=True, reason=BAD_LOAD_SAVE)
|
|
||||||
),
|
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
def test_load_save_rejects_unknown_character(
|
def test_load_save_rejects_unknown_character(
|
||||||
@@ -113,7 +106,6 @@ def test_load_save_rejects_unknown_character(
|
|||||||
num: str,
|
num: str,
|
||||||
) -> None:
|
) -> None:
|
||||||
write_roster(config, hero("Ann"), hero("Bob"))
|
write_roster(config, hero("Ann"), hero("Bob"))
|
||||||
# 0 falls through to the menu and -1 plays Bob; neither should
|
|
||||||
feed(monkeypatch, ["7"])
|
feed(monkeypatch, ["7"])
|
||||||
monkeypatch.setattr(BasicUserInterface, "play", lambda self, p: None)
|
monkeypatch.setattr(BasicUserInterface, "play", lambda self, p: None)
|
||||||
assert run("--basic", "--load-save", num) == 1
|
assert run("--basic", "--load-save", num) == 1
|
||||||
|
|||||||
Reference in new issue
Block a user