refactor: unify document sessions close policy and resource parsing
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
from pathlib import Path
|
||||
|
||||
from mdhelper.doc_check import check_markdown_file
|
||||
from mdhelper.document_resources import analyze_document, apply_source_edits, resolve_local_reference
|
||||
from mdhelper.editor_assets import plan_image_relocation
|
||||
from mdhelper.export_check import check_export
|
||||
from mdhelper.main_window import MainWindow
|
||||
|
||||
|
||||
def test_resource_consumers_agree_on_examples_and_references(tmp_path):
|
||||
path = tmp_path / "note.md"
|
||||
source = '```md\n\n```\n\n![real][picture]\n\n[picture]: real.png\n'
|
||||
path.write_text(source, encoding="utf-8")
|
||||
(tmp_path / "real.png").write_bytes(b"image")
|
||||
assert [(r.url, r.line) for r in analyze_document(source).references] == [("real.png", 5)]
|
||||
assert check_markdown_file(path) == []
|
||||
assert check_export(source, path, "html") == []
|
||||
assert MainWindow._referenced_local_paths(None, path) == {tmp_path / "real.png"}
|
||||
|
||||
|
||||
def test_images_inside_links_and_html_attributes_have_correct_spans(tmp_path):
|
||||
source = '[](guide.md)\n\n<img alt=\'src="fake.png"\' src="real.png">\n<!-- <img src="hidden.png"> -->'
|
||||
references = analyze_document(source).references
|
||||
assert [(r.url, r.image) for r in references] == [("image.png", True), ("guide.md", False), ("real.png", True)]
|
||||
edits = [r.edit_url("new.png") for r in references if r.image]
|
||||
assert apply_source_edits(source, edits) == source.replace('](image.png', '](new.png').replace('src="real.png"','src="new.png"')
|
||||
|
||||
|
||||
def test_relocation_plan_does_not_write_before_all_resources_validate(tmp_path):
|
||||
original, target = tmp_path / "original.md", tmp_path / "new" / "copy.md"
|
||||
(tmp_path / "one.png").write_bytes(b"image")
|
||||
import pytest
|
||||
with pytest.raises(OSError):
|
||||
plan_image_relocation('\n', original, target)
|
||||
assert not target.parent.exists()
|
||||
plan = plan_image_relocation('', original, target)
|
||||
assert plan.files and not target.parent.exists()
|
||||
|
||||
|
||||
def test_file_url_and_encoded_relative_path_resolve_once(tmp_path):
|
||||
path = tmp_path / "note.md"
|
||||
asset = tmp_path / "100%20图片.png"
|
||||
assert resolve_local_reference(path, asset.as_uri()) == asset
|
||||
assert resolve_local_reference(path, "100%2520%E5%9B%BE%E7%89%87.png") == asset
|
||||
assert resolve_local_reference(path, "https://example.com/image.png") is None
|
||||
@@ -101,6 +101,22 @@ def test_close_does_not_recover_draft_that_user_already_undid(editing, monkeypat
|
||||
assert str(path) not in window.editor_drafts
|
||||
|
||||
|
||||
@pytest.mark.parametrize("operation", ["tab", "all", "project"])
|
||||
def test_all_close_routes_use_save_discard_cancel(editing, monkeypatch, operation):
|
||||
window, editor, path = editing
|
||||
editor.replace_source_undoable("pending changes")
|
||||
actions = {"tab": lambda: window._close_tab(window.tabs.indexOf(editor)),
|
||||
"all": window._close_all_tabs, "project": window.close_project_folder}
|
||||
monkeypatch.setattr(QMessageBox, "question", lambda *args: QMessageBox.StandardButton.Cancel)
|
||||
actions[operation]()
|
||||
assert window.tabs.indexOf(editor) >= 0 and path.read_text() == "# Original"
|
||||
assert window.editor_drafts[str(path)]["source"] == "pending changes"
|
||||
monkeypatch.setattr(QMessageBox, "question", lambda *args: QMessageBox.StandardButton.Save)
|
||||
actions[operation]()
|
||||
assert window.tabs.indexOf(editor) == -1 and path.read_text() == "pending changes"
|
||||
assert not window.editor_drafts
|
||||
|
||||
|
||||
def test_save_as_changes_draft_ownership_only_after_success(editing, monkeypatch, tmp_path):
|
||||
window, editor, original = editing
|
||||
target = tmp_path / "elsewhere" / "copy.md"
|
||||
|
||||
Reference in New Issue
Block a user