fix: v0.4.1 — read stdin as UTF-8 (section --content - got mojibake from the frozen exe)
This commit is contained in:
1 parent
d063551a1e
commit
f744774ba2
2 files changed
+24
-4
No files matched your search
+5
-4
@@ -1214,7 +1214,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.1', prog_name='onyx')
|
||||
@click.group(name='onyx')
|
||||
def cli():
|
||||
"""Onyx - Interact with Obsidian vaults from the command line
|
||||
@@ -1559,9 +1559,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
|
||||
@@ -594,5 +598,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