pqcli: add --import-save and --export-save

--import-save FILE adds the character in a .pqw file to the roster;
it can be given more than once, and if any file fails, nothing is
imported. --export-save NUM FILE writes a character out. Export never
overwrites an existing file. The file handling lives in pqw.read_file
and pqw.write_file, so the interfaces can share it.
This commit is contained in:
Dash committed 2026-10-17 15:56:02 +02:00
1 parent 0beb550548
commit 612b223a59
4 files changed
+178 -1

No files matched your search

+57
View File
@@ -0,0 +1,57 @@
import os
import subprocess
import sys
from pathlib import Path
from test_pqw import played
from pqcli.game_data import load_game_data
from pqcli.pqw import read_file, write_file
def pqcli(
config: Path, *args: str, cwd: Path | None = None
) -> subprocess.CompletedProcess[str]:
env = {**os.environ, "XDG_CONFIG_HOME": str(config)}
return subprocess.run(
[sys.executable, "-m", "pqcli", *args],
capture_output=True,
text=True,
env=env,
cwd=cwd,
)
def test_import_then_export(tmp_path: Path) -> None:
data = load_game_data()
source = tmp_path / "in.pqw"
write_file(played(3000).player, source, data)
result = pqcli(tmp_path, "--import-save", str(source))
assert result.returncode == 0, result.stderr
assert result.stdout == "Imported Exportée\n"
assert (tmp_path / "pqcli" / "save.dat").exists()
listed = pqcli(tmp_path, "--list-saves")
assert listed.stdout == "1. Exportée\n"
target = tmp_path / "out.pqw"
result = pqcli(tmp_path, "--export-save", "1", str(target))
assert result.returncode == 0, result.stderr
assert read_file(target, data)[0].name == "Exportée"
def test_import_bad_file_changes_nothing(tmp_path: Path) -> None:
bad = tmp_path / "bad.pqw"
bad.write_text("garbage!")
result = pqcli(tmp_path, "--import-save", str(bad))
assert result.returncode == 1
assert "not a .pqw file" in result.stderr
assert not (tmp_path / "pqcli" / "save.dat").exists()
def test_export_rejects_unknown_character(tmp_path: Path) -> None:
result = pqcli(tmp_path, "--export-save", "3", str(tmp_path / "x.pqw"))
assert result.returncode == 1
assert "Invalid player" in result.stderr
assert not (tmp_path / "x.pqw").exists()
+32 -1
View File
@@ -1,6 +1,7 @@
import base64
import datetime
import json
from pathlib import Path
import pytest
from upstream import bridge
@@ -8,7 +9,16 @@ from upstream import bridge
from pqcli.config import StatType
from pqcli.game_data import load_game_data
from pqcli.mechanic import Player, Simulation, Stats
from pqcli.pqw import PqwError, decode, encode, export_game, import_game
from pqcli.pqw import (
PqwError,
decode,
default_file_name,
encode,
export_game,
import_game,
read_file,
write_file,
)
from pqcli.random import Alea
DATA = load_game_data()
@@ -131,3 +141,24 @@ def test_round_trip_keeps_playing_identically(ticks: int) -> None:
assert bridge.run(resumed, 10**8, 2000) == bridge.run(
original, 10**8, 2000
)
def test_files_round_trip(tmp_path: Path) -> None:
original = played(3000).player
path = tmp_path / default_file_name(original)
write_file(original, path, DATA)
copy, _warnings = read_file(path, DATA)
assert bridge.snapshot(copy) == bridge.snapshot(original)
def test_write_file_never_overwrites(tmp_path: Path) -> None:
path = tmp_path / "taken.pqw"
path.write_text("mine")
with pytest.raises(PqwError, match="already exists"):
write_file(played(10).player, path, DATA)
assert path.read_text() == "mine"
def test_read_file_reports_missing_file(tmp_path: Path) -> None:
with pytest.raises(PqwError, match="cannot read"):
read_file(tmp_path / "nope.pqw", DATA)