diff --git a/features/core.feature b/features/core.feature index df214494..fb261934 100644 --- a/features/core.feature +++ b/features/core.feature @@ -39,6 +39,15 @@ Feature: Basic reading and writing to a journal When we open the editor and enter nothing Then we should see the message "[Nothing saved to file]" + Scenario: Sending an argument with spaces to the editor should work + Given we use the config "editor-args.yaml" + When we open the editor and enter "lorem ipsum" + Then the editor should have been called with 5 arguments + And the editor arguments should contain "vim" + And the editor arguments should contain "-f" + And the editor arguments should contain "-c" + And the editor arguments should contain "setf markdown" + Scenario: Writing an empty entry from the command line Given we use the config "basic.yaml" When we run "jrnl" and enter nothing diff --git a/features/data/configs/editor-args.yaml b/features/data/configs/editor-args.yaml new file mode 100644 index 00000000..12c5bd9c --- /dev/null +++ b/features/data/configs/editor-args.yaml @@ -0,0 +1,12 @@ +default_hour: 9 +default_minute: 0 +editor: vim -f -c 'setf markdown' +encrypt: false +highlight: true +journals: + default: features/journals/simple.journal +linewrap: 80 +tagsymbols: "@" +template: false +timeformat: "%Y-%m-%d %H:%M" +indent_character: "|" diff --git a/features/steps/core.py b/features/steps/core.py index b2e9e6a1..2c9cee1e 100644 --- a/features/steps/core.py +++ b/features/steps/core.py @@ -85,6 +85,7 @@ def open_editor_and_enter(context, text=""): text = text or context.text or "" def _mock_editor_function(command): + context.editor_command = command tmpfile = command[-1] with open(tmpfile, "w+") as f: f.write(text) @@ -92,7 +93,17 @@ def open_editor_and_enter(context, text=""): return tmpfile with patch("subprocess.call", side_effect=_mock_editor_function): - run(context, "jrnl") + context.execute_steps('when we run "jrnl"') + + +@then("the editor should have been called with {num} arguments") +def count_editor_args(context, num): + assert len(context.editor_command) == int(num) + + +@then('the editor arguments should contain "{arg}"') +def contains_editor_arg(context, arg): + assert arg in context.editor_command def _mock_getpass(inputs): diff --git a/jrnl/util.py b/jrnl/util.py index 8033306b..a347aaac 100644 --- a/jrnl/util.py +++ b/jrnl/util.py @@ -9,6 +9,7 @@ from string import punctuation, whitespace import subprocess import sys import tempfile +import textwrap from typing import Callable, Optional import unicodedata @@ -172,10 +173,17 @@ def get_text_from_editor(config, template=""): try: subprocess.call( - shlex.split(config["editor"], posix="win" not in sys.platform) + [tmpfile] + shlex.split(config["editor"], posix="win32" not in sys.platform) + [tmpfile] ) - except AttributeError: - subprocess.call(config["editor"] + [tmpfile]) + except Exception as e: + error_msg = f""" + {ERROR_COLOR}{str(e)}{RESET_COLOR} + + Please check the 'editor' key in your config file for errors: + {repr(config['editor'])} + """ + print(textwrap.dedent(error_msg).strip(), file=sys.stderr) + exit(1) with open(tmpfile, "r", encoding="utf-8") as f: raw = f.read()