v0.6.1
Example document / build (push) Has been cancelled
Python application / build (push) Has been cancelled

Fix gui_schemes
This commit is contained in:
Igor20264
2026-10-20 21:53:34 +03:00
parent 8223e0ac14
commit 8fc1f9e096
6 changed files with 253 additions and 86 deletions
+82 -7
View File
@@ -39,6 +39,7 @@ from md2gost.diagram_schemes import (
ensure_user_schemes,
load_schemes_from_path,
migrate_user_schemes,
save_schemes,
prepare_with_schemes,
scheme_id_from_lang,
user_schemes_path,
@@ -125,7 +126,7 @@ def test_ensure_user_schemes_once(tmp_path):
assert path == user_schemes_path(tmp_path)
def test_migrate_removes_orphaned_builtin_and_adds_missing(tmp_path, monkeypatch):
def test_migrate_keeps_custom_schemes_and_adds_missing(tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
path = user_schemes_path(tmp_path)
path.write_text(
@@ -147,10 +148,11 @@ def test_migrate_removes_orphaned_builtin_and_adds_missing(tmp_path, monkeypatch
)
result = migrate_user_schemes(tmp_path)
assert result.written
assert "bpmn" in result.removed
assert result.removed == []
assert "c4" in result.added
schemes = load_schemes_from_path(path)
assert "bpmn" not in schemes
assert schemes["bpmn"].title == "BPMN 2.0"
assert schemes["bpmn"].author == "md2gost"
assert "mine" in schemes
assert schemes["mine"].title == "x"
bundled = load_schemes_from_path(bundled_schemes_path())
@@ -186,7 +188,7 @@ def test_migrate_preserves_user_override_of_builtin(tmp_path, monkeypatch):
assert "usecase" in schemes # other bundled ids still added
def test_migrate_updates_app_owned_builtin(tmp_path, monkeypatch):
def test_migrate_keeps_edited_builtin(tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
path = user_schemes_path(tmp_path)
path.write_text(
@@ -208,10 +210,83 @@ def test_migrate_updates_app_owned_builtin(tmp_path, monkeypatch):
encoding="utf-8",
)
result = migrate_user_schemes(tmp_path)
assert "c4" in result.updated
assert "c4" not in result.updated
assert "c4" not in result.added
schemes = load_schemes_from_path(path)
bundled = load_schemes_from_path(bundled_schemes_path())
assert schemes["c4"].to_dict() == bundled["c4"].to_dict()
assert schemes["c4"].title == "Old C4"
assert schemes["c4"].docs == "stale"
assert schemes["c4"].author == "md2gost"
assert "usecase" in schemes
def test_migrate_does_not_overwrite_broken_json(tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
path = user_schemes_path(tmp_path)
original = "{\n \"c4\": {\"title\": \"x\",}\n}\n"
path.write_text(original, encoding="utf-8")
result = migrate_user_schemes(tmp_path)
assert not result.written
assert not result.changed
assert path.read_text(encoding="utf-8") == original
def test_migrate_reads_utf8_bom(tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
path = user_schemes_path(tmp_path)
body = json.dumps(
{
"c4": {
"title": "My C4",
"author": "md2gost",
"docs": "mine",
"prefix": "",
"postfix": "",
}
},
ensure_ascii=False,
)
path.write_bytes(b"\xef\xbb\xbf" + body.encode("utf-8"))
result = migrate_user_schemes(tmp_path)
assert "usecase" in result.added
schemes = load_schemes_from_path(path)
assert schemes["c4"].title == "My C4"
assert schemes["c4"].docs == "mine"
text = path.read_text(encoding="utf-8-sig")
assert "mine" in text
def test_save_schemes_keeps_unknown_entries(tmp_path):
path = user_schemes_path(tmp_path)
path.write_text(
json.dumps(
{
"my-scheme": {"title": "hyphen", "note": "keep"},
"mine": {"title": "x", "author": "", "extra": "field", "prefix": "A"},
}
)
+ "\n",
encoding="utf-8",
)
loaded = load_schemes_from_path(path)
assert "mine" in loaded
assert "my-scheme" not in loaded
loaded["mine"] = DiagramScheme(
id="mine",
title="y",
author="",
prefix="A",
)
save_schemes(loaded, path, removed_ids=set())
raw = json.loads(path.read_text(encoding="utf-8"))
assert raw["my-scheme"]["title"] == "hyphen"
assert raw["my-scheme"]["note"] == "keep"
assert raw["mine"]["title"] == "y"
assert raw["mine"]["extra"] == "field"
del loaded["mine"]
save_schemes(loaded, path, removed_ids={"mine"})
raw = json.loads(path.read_text(encoding="utf-8"))
assert "mine" not in raw
assert raw["my-scheme"]["note"] == "keep"
def test_migrate_idempotent(tmp_path, monkeypatch):