Fix OS compatibility issues for editors with spaces, slashes, and quotes (#1153)

* Fix inverted POSIX check, refactor os_compat, and add tests for it
* Fix missing parentheses and remove skip_win on test that is passing in Windows now
* Fix expected quotes in quoted args
* Make  output clearer on failing test
* Bringing skip_win back to test whose failure is a bit more complicated than expected
This commit is contained in:
Micah Jerome Ellison 2021-01-16 15:19:11 -08:00 committed by GitHub
parent b6b6e7750e
commit 9e6cd8820f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 121 additions and 20 deletions

View file

@ -7,7 +7,7 @@ import colorama
from .os_compat import on_windows
if on_windows:
if on_windows():
colorama.init()
WARNING_COLOR = colorama.Fore.YELLOW

View file

@ -1,6 +1,5 @@
import logging
import os
import shlex
import subprocess
import sys
import tempfile
@ -10,6 +9,7 @@ from pathlib import Path
from .color import ERROR_COLOR
from .color import RESET_COLOR
from .os_compat import on_windows
from .os_compat import split_args
def get_text_from_editor(config, template=""):
@ -25,7 +25,7 @@ def get_text_from_editor(config, template=""):
f.write(template)
try:
subprocess.call(shlex.split(config["editor"], posix=on_windows) + [tmpfile])
subprocess.call(split_args(config["editor"]) + [tmpfile])
except Exception as e:
error_msg = f"""
{ERROR_COLOR}{str(e)}{RESET_COLOR}
@ -47,7 +47,7 @@ def get_text_from_editor(config, template=""):
def get_text_from_stdin():
_how_to_quit = "Ctrl+z and then Enter" if on_windows else "Ctrl+d"
_how_to_quit = "Ctrl+z and then Enter" if on_windows() else "Ctrl+d"
print(
f"[Writing Entry; on a blank line, press {_how_to_quit} to finish writing]\n",
file=sys.stderr,

View file

@ -1,6 +1,18 @@
# Copyright (C) 2012-2021 jrnl contributors
# License: https://www.gnu.org/licenses/gpl-3.0.html
import shlex
from sys import platform
on_windows = "win32" in platform
def on_windows():
return "win32" in platform
def on_posix():
return not on_windows()
def split_args(args):
"""Split arguments and add escape characters as appropriate for the OS"""
return shlex.split(args, posix=on_posix())