feat: greatly improve capturing all formats of inline metadata (#41)

feat: greatly improve capturing metadata all formats of inline metadata
This commit is contained in:
Nathaniel Landau
2023-05-05 13:09:59 -04:00
committed by GitHub
parent 9ec6919022
commit 2e61a92ad1
38 changed files with 3634 additions and 5955 deletions

View File

@@ -13,7 +13,7 @@ from pathlib import Path
import pytest
from obsidian_metadata.models.enums import MetadataType
from tests.helpers import Regex, remove_ansi
from tests.helpers import Regex, strip_ansi
def test_instantiate_application(test_application) -> None:
@@ -48,7 +48,7 @@ def test_abort(test_application, mocker, capsys) -> None:
)
app.application_main()
captured = remove_ansi(capsys.readouterr().out)
captured = strip_ansi(capsys.readouterr().out)
assert "Done!" in captured
@@ -80,7 +80,7 @@ def test_add_metadata_frontmatter(test_application, mocker, capsys) -> None:
with pytest.raises(KeyError):
app.application_main()
captured = remove_ansi(capsys.readouterr().out)
captured = strip_ansi(capsys.readouterr().out)
assert captured == Regex(r"SUCCESS +\| Added metadata to \d+ notes", re.DOTALL)
@@ -112,7 +112,7 @@ def test_add_metadata_inline(test_application, mocker, capsys) -> None:
with pytest.raises(KeyError):
app.application_main()
captured = remove_ansi(capsys.readouterr().out)
captured = strip_ansi(capsys.readouterr().out)
assert captured == Regex(r"SUCCESS +\| Added metadata to \d+ notes", re.DOTALL)
@@ -140,7 +140,7 @@ def test_add_metadata_tag(test_application, mocker, capsys) -> None:
with pytest.raises(KeyError):
app.application_main()
captured = remove_ansi(capsys.readouterr().out)
captured = strip_ansi(capsys.readouterr().out)
assert captured == Regex(r"SUCCESS +\| Added metadata to \d+ notes", re.DOTALL)
@@ -168,7 +168,7 @@ def test_delete_tag_1(test_application, mocker, capsys) -> None:
with pytest.raises(KeyError):
app.application_main()
captured = remove_ansi(capsys.readouterr().out)
captured = strip_ansi(capsys.readouterr().out)
assert captured == Regex(r"SUCCESS +\| Deleted inline tag: breakfast in \d+ notes", re.DOTALL)
@@ -196,7 +196,7 @@ def test_delete_tag_2(test_application, mocker, capsys) -> None:
with pytest.raises(KeyError):
app.application_main()
captured = remove_ansi(capsys.readouterr().out)
captured = strip_ansi(capsys.readouterr().out)
assert "WARNING | No notes were changed" in captured
@@ -219,8 +219,8 @@ def test_delete_key(test_application, mocker, capsys) -> None:
with pytest.raises(KeyError):
app.application_main()
captured = remove_ansi(capsys.readouterr().out)
assert r"WARNING | No notes found with a key matching: \d{7}" in captured
captured = strip_ansi(capsys.readouterr().out)
assert r"WARNING | No notes found with a key matching regex: \d{7}" in captured
mocker.patch(
"obsidian_metadata.models.application.Questions.ask_application_main",
@@ -237,7 +237,7 @@ def test_delete_key(test_application, mocker, capsys) -> None:
with pytest.raises(KeyError):
app.application_main()
captured = remove_ansi(capsys.readouterr().out)
captured = strip_ansi(capsys.readouterr().out)
assert captured == Regex(r"SUCCESS \| Deleted keys matching: d\\w\+ from \d+ notes", re.DOTALL)
@@ -263,7 +263,7 @@ def test_delete_value(test_application, mocker, capsys) -> None:
)
with pytest.raises(KeyError):
app.application_main()
captured = remove_ansi(capsys.readouterr().out)
captured = strip_ansi(capsys.readouterr().out)
assert r"WARNING | No notes found matching: area: \d{7}" in captured
mocker.patch(
@@ -284,8 +284,8 @@ def test_delete_value(test_application, mocker, capsys) -> None:
)
with pytest.raises(KeyError):
app.application_main()
captured = remove_ansi(capsys.readouterr().out)
assert r"SUCCESS | Deleted value ^front\w+$ from key area in 4 notes" in captured
captured = strip_ansi(capsys.readouterr().out)
assert captured == Regex(r"SUCCESS | Deleted value \^front\\w\+\$ from key area in \d+ notes")
def test_filter_notes(test_application, mocker, capsys) -> None:
@@ -307,7 +307,7 @@ def test_filter_notes(test_application, mocker, capsys) -> None:
with pytest.raises(KeyError):
app.application_main()
captured = remove_ansi(capsys.readouterr().out)
captured = strip_ansi(capsys.readouterr().out)
assert captured == Regex(r"SUCCESS +\| Loaded \d+ notes from \d+ total", re.DOTALL)
assert "02 inline/inline 2.md" in captured
assert "03 mixed/mixed 1.md" not in captured
@@ -362,7 +362,7 @@ def test_filter_clear(test_application, mocker, capsys) -> None:
)
with pytest.raises(KeyError):
app.application_main()
captured = remove_ansi(capsys.readouterr().out)
captured = strip_ansi(capsys.readouterr().out)
assert "02 inline/inline 2.md" in captured
assert "03 mixed/mixed 1.md" in captured
assert "01 frontmatter/frontmatter 4.md" in captured
@@ -384,8 +384,11 @@ def test_inspect_metadata_all(test_application, mocker, capsys) -> None:
)
with pytest.raises(KeyError):
app.application_main()
captured = remove_ansi(capsys.readouterr().out)
assert captured == Regex(r"type +│ article", re.DOTALL)
captured = strip_ansi(capsys.readouterr().out)
assert captured == Regex(r"tags +│ bar ")
assert captured == Regex(r"status +│ new ")
assert captured == Regex(r"in_text_key +│ in-text value")
assert "#breakfast" in captured
def test_rename_tag(test_application, mocker, capsys) -> None:
@@ -411,7 +414,7 @@ def test_rename_tag(test_application, mocker, capsys) -> None:
with pytest.raises(KeyError):
app.application_main()
captured = remove_ansi(capsys.readouterr().out)
captured = strip_ansi(capsys.readouterr().out)
assert "No notes were changed" in captured
mocker.patch(
@@ -433,7 +436,7 @@ def test_rename_tag(test_application, mocker, capsys) -> None:
with pytest.raises(KeyError):
app.application_main()
captured = remove_ansi(capsys.readouterr().out)
captured = strip_ansi(capsys.readouterr().out)
assert captured == Regex(r"Renamed breakfast to new_tag in \d+ notes", re.DOTALL)
@@ -460,7 +463,7 @@ def test_rename_key(test_application, mocker, capsys) -> None:
with pytest.raises(KeyError):
app.application_main()
captured = remove_ansi(capsys.readouterr().out)
captured = strip_ansi(capsys.readouterr().out)
assert "WARNING | No notes were changed" in captured
mocker.patch(
@@ -482,7 +485,7 @@ def test_rename_key(test_application, mocker, capsys) -> None:
with pytest.raises(KeyError):
app.application_main()
captured = remove_ansi(capsys.readouterr().out)
captured = strip_ansi(capsys.readouterr().out)
assert captured == Regex(r"Renamed tags to new_tags in \d+ notes", re.DOTALL)
@@ -512,7 +515,7 @@ def test_rename_value_fail(test_application, mocker, capsys) -> None:
)
with pytest.raises(KeyError):
app.application_main()
captured = remove_ansi(capsys.readouterr().out)
captured = strip_ansi(capsys.readouterr().out)
assert "WARNING | No notes were changed" in captured
mocker.patch(
@@ -537,7 +540,7 @@ def test_rename_value_fail(test_application, mocker, capsys) -> None:
)
with pytest.raises(KeyError):
app.application_main()
captured = remove_ansi(capsys.readouterr().out)
captured = strip_ansi(capsys.readouterr().out)
assert captured == Regex(
r"SUCCESS +\| Renamed 'area:frontmatter' to 'area:new_key' in \d+ notes", re.DOTALL
)
@@ -553,7 +556,7 @@ def test_review_no_changes(test_application, mocker, capsys) -> None:
)
with pytest.raises(KeyError):
app.application_main()
captured = remove_ansi(capsys.readouterr().out)
captured = strip_ansi(capsys.readouterr().out)
assert "INFO | No changes to review" in captured
@@ -579,7 +582,7 @@ def test_review_changes(test_application, mocker, capsys) -> None:
)
with pytest.raises(KeyError):
app.application_main()
captured = remove_ansi(capsys.readouterr().out)
captured = strip_ansi(capsys.readouterr().out)
assert captured == Regex(r".*Found \d+ changed notes in the vault", re.DOTALL)
assert "- tags:" in captured
assert "+ new_tags:" in captured
@@ -595,7 +598,7 @@ def test_transpose_metadata_1(test_application, mocker, capsys) -> None:
app = test_application
app._load_vault()
assert app.vault.metadata.inline_metadata["inline_key"] == ["inline_key_value"]
assert app.vault.inline_meta["inline_key"] == ["inline_key_value"]
mocker.patch(
"obsidian_metadata.models.application.Questions.ask_application_main",
side_effect=["reorganize_metadata", KeyError],
@@ -607,9 +610,9 @@ def test_transpose_metadata_1(test_application, mocker, capsys) -> None:
with pytest.raises(KeyError):
app.application_main()
assert app.vault.metadata.inline_metadata == {}
assert app.vault.metadata.frontmatter["inline_key"] == ["inline_key_value"]
captured = remove_ansi(capsys.readouterr().out)
assert app.vault.inline_meta == {}
assert app.vault.frontmatter["inline_key"] == ["inline_key_value"]
captured = strip_ansi(capsys.readouterr().out)
assert "SUCCESS | Transposed Inline Metadata to Frontmatter in 5 notes" in captured
@@ -623,7 +626,7 @@ def test_transpose_metadata_2(test_application, mocker) -> None:
app = test_application
app._load_vault()
assert app.vault.metadata.frontmatter["date_created"] == ["2022-12-21", "2022-12-22"]
assert app.vault.frontmatter["date_created"] == ["2022-12-21", "2022-12-22"]
mocker.patch(
"obsidian_metadata.models.application.Questions.ask_application_main",
side_effect=["reorganize_metadata", KeyError],
@@ -634,8 +637,8 @@ def test_transpose_metadata_2(test_application, mocker) -> None:
)
with pytest.raises(KeyError):
app.application_main()
assert app.vault.metadata.inline_metadata["date_created"] == ["2022-12-21", "2022-12-22"]
assert app.vault.metadata.frontmatter == {}
assert app.vault.inline_meta["date_created"] == ["2022-12-21", "2022-12-22"]
assert app.vault.frontmatter == {}
def test_vault_backup(test_application, mocker, capsys) -> None:
@@ -653,7 +656,7 @@ def test_vault_backup(test_application, mocker, capsys) -> None:
)
with pytest.raises(KeyError):
app.application_main()
captured = remove_ansi(capsys.readouterr().out)
captured = strip_ansi(capsys.readouterr().out)
assert captured == Regex(
r"SUCCESS +\| Vault backed up to:[-\w\d\/\s]+application\.bak", re.DOTALL
)
@@ -676,5 +679,5 @@ def test_vault_delete(test_application, mocker, capsys, tmp_path) -> None:
)
with pytest.raises(KeyError):
app.application_main()
captured = remove_ansi(capsys.readouterr().out)
captured = strip_ansi(capsys.readouterr().out)
assert captured == Regex(r"SUCCESS +\| Backup deleted", re.DOTALL)