Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
33544edf07 | ||
|
|
34d28c03fa |
No files matched your search
+14
-5
@@ -960,7 +960,15 @@ class OnyxCLI:
|
||||
first item. Each item is taken whole (no comma splitting)."""
|
||||
vault_name, vault_path = self._get_vault_and_path(vault)
|
||||
note_path = self._find_note(vault_path, note_name)
|
||||
existing, _, _ = self._parse_frontmatter(note_path.read_text(encoding='utf-8-sig'))
|
||||
match = _FRONTMATTER.match(note_path.read_text(encoding='utf-8-sig'))
|
||||
try:
|
||||
existing = (yaml.load(match.group(1), Loader=_YAML_LOADER) or {}) if match else {}
|
||||
except yaml.YAMLError as error:
|
||||
# An unreadable frontmatter would look like "key missing" and the splice would
|
||||
# replace the existing (unparsed) items: refuse instead of losing them.
|
||||
raise click.UsageError(f"Frontmatter of {note_name} is not valid YAML; fix it first:\n{error}")
|
||||
if not isinstance(existing, dict):
|
||||
raise click.UsageError(f"Frontmatter of {note_name} is not a key: value mapping; fix it first")
|
||||
|
||||
value = existing.get(key)
|
||||
current = value if isinstance(value, list) else ([] if value in (None, '') else [value])
|
||||
@@ -1214,7 +1222,7 @@ def _periodic_output_options(command):
|
||||
help="Don't open Obsidian; a newly created note keeps raw Templater syntax "
|
||||
"until Obsidian processes it")(command)
|
||||
|
||||
@click.version_option(version='0.4.0', prog_name='onyx')
|
||||
@click.version_option(version='0.4.2', prog_name='onyx')
|
||||
@click.group(name='onyx')
|
||||
def cli():
|
||||
"""Onyx - Interact with Obsidian vaults from the command line
|
||||
@@ -1559,9 +1567,10 @@ def frontmatter_command(note_name, edit, view, clear, delete, add, remove, key,
|
||||
cli.frontmatter_list_items(note_name, key[0], value, remove=remove, vault=vault)
|
||||
|
||||
if __name__ == '__main__':
|
||||
# Scripts read onyx's output (e.g. `--print-path`): emit UTF-8 with LF on every OS.
|
||||
# Without this, Windows pipes get the ANSI code page (é -> 0xE9; PyInstaller ignores
|
||||
# PYTHONUTF8) and CRLF, whose trailing \r survives bash's $(...).
|
||||
# Scripts talk to onyx through pipes (`--content -`, `--print-path`): UTF-8 with LF on
|
||||
# every OS. Without this, Windows pipes use the ANSI code page (é -> 0xE9 out, UTF-8 in
|
||||
# read as "é"; PyInstaller ignores PYTHONUTF8) and CRLF, whose \r survives bash's $(...).
|
||||
sys.stdin.reconfigure(encoding='utf-8')
|
||||
for stream in (sys.stdout, sys.stderr):
|
||||
stream.reconfigure(encoding='utf-8', newline='\n')
|
||||
cli()
|
||||
@@ -1,5 +1,7 @@
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import unittest
|
||||
import yaml
|
||||
import shutil
|
||||
@@ -10,6 +12,8 @@ import click
|
||||
from click.testing import CliRunner
|
||||
from datetime import datetime
|
||||
|
||||
SRC = Path(__file__).resolve().parent.parent / 'src' / 'onyx.py'
|
||||
|
||||
class TestOnyxCLI(unittest.TestCase):
|
||||
def setUp(self):
|
||||
# Create temporary test directory and files
|
||||
@@ -245,6 +249,16 @@ Test content"""
|
||||
self.assertEqual([c.args[0] for c in echo.call_args_list],
|
||||
["No change to 'participants' in m", "No change to 'absent' in m"])
|
||||
|
||||
def test_frontmatter_add_refuses_invalid_yaml(self):
|
||||
"""Unparseable frontmatter must not look like 'key missing' and lose existing items"""
|
||||
note = self.test_dir / 'person.md'
|
||||
content = '---\nchildren: "[[Julia]]", "[[Jacob]]"\ntype: person\n---\nCorps\n'
|
||||
note.write_text(content, encoding='utf-8')
|
||||
for remove in (False, True):
|
||||
with self.assertRaises(click.UsageError):
|
||||
self.cli.frontmatter_list_items('person', 'children', ('[[Nouveau]]',), remove=remove)
|
||||
self.assertEqual(note.read_text(encoding='utf-8'), content)
|
||||
|
||||
def test_frontmatter_add_refuses_unquoted_links(self):
|
||||
note = self.test_dir / 'bad.md'
|
||||
content = '---\nrelated:\n - [[Haro]]\n---\n'
|
||||
@@ -594,5 +608,20 @@ Test content"""
|
||||
self.assertIn(str(self.test_dir.resolve()), printed)
|
||||
self.assertNotIn('path_prefix', printed)
|
||||
|
||||
def test_pipes_are_utf8_without_utf8_mode(self):
|
||||
"""stdin/stdout are UTF-8 even when Python isn't in UTF-8 mode (frozen exe on Windows)"""
|
||||
note = self.test_dir / 'd.md'
|
||||
note.write_text('## Journal\n', encoding='utf-8')
|
||||
env = {k: v for k, v in os.environ.items() if k not in ('PYTHONUTF8', 'PYTHONIOENCODING')}
|
||||
home = str(self.temp_config_dir.resolve())
|
||||
env.update(PYTHONUTF8='0', USERPROFILE=home, HOME=home)
|
||||
result = subprocess.run([sys.executable, str(SRC), 'section', 'd', '--heading', 'Journal',
|
||||
'--content', '-'], input='Réunion « test »'.encode('utf-8'),
|
||||
capture_output=True, env=env)
|
||||
self.assertEqual(result.returncode, 0, result.stderr)
|
||||
self.assertEqual(note.read_text(encoding='utf-8'), '## Journal\nRéunion « test »\n')
|
||||
printed = subprocess.run([sys.executable, str(SRC), 'print-note', 'd'], capture_output=True, env=env)
|
||||
self.assertEqual(printed.stdout, '## Journal\nRéunion « test »\n\n'.encode('utf-8'))
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user