diff --git a/features/core.feature b/features/core.feature index df2144943..057db3994 100644 --- a/features/core.feature +++ b/features/core.feature @@ -39,6 +39,16 @@ 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]" + @skip_win + 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 one editor argument should be "vim" + And one editor argument should be "-f" + And one editor argument should be "-c" + And one editor argument should match "'?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 000000000..12c5bd9cf --- /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 b2e9e6a19..99ac98596 100644 --- a/features/steps/core.py +++ b/features/steps/core.py @@ -2,6 +2,7 @@ from collections import defaultdict import os from pathlib import Path +import re import shlex import sys import time @@ -85,6 +86,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 +94,29 @@ def _mock_editor_function(command): 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('one editor argument should be "{arg}"') +def contains_editor_arg(context, arg): + args = context.editor_command + assert ( + arg in args and args.count(arg) == 1 + ), f"\narg not in args exactly 1 time:\n{arg}\n{str(args)}" + + +@then('one editor argument should match "{regex}"') +def matches_editor_arg(context, regex): + args = context.editor_command + matches = list(filter(lambda x: re.match(regex, x), args)) + assert ( + len(matches) == 1 + ), f"\nRegex didn't match exactly 1 time:\n{regex}\n{str(args)}" def _mock_getpass(inputs): @@ -155,10 +179,12 @@ def run(context, command, cache_dir=None): cache_dir = os.path.join("features", "cache", cache_dir) command = command.format(cache_dir=cache_dir) - args = ushlex(command)[1:] + args = ushlex(command) + try: - cli.run(args or None) - context.exit_status = 0 + with patch("sys.argv", args): + cli.run(args[1:]) + context.exit_status = 0 except SystemExit as e: context.exit_status = e.code diff --git a/jrnl/cli.py b/jrnl/cli.py index 070d134bd..1b25acd8f 100644 --- a/jrnl/cli.py +++ b/jrnl/cli.py @@ -194,11 +194,14 @@ def parse_args(args=None): help="Opens an interactive interface for deleting entries.", ) + if not args: + args = [] + # Handle '-123' as a shortcut for '-n 123' num = re.compile(r"^-(\d+)$") - if args is None: - args = sys.argv[1:] - return parser.parse_args([num.sub(r"-n \1", a) for a in args]) + args = [num.sub(r"-n \1", arg) for arg in args] + + return parser.parse_args(args) def guess_mode(args, config): @@ -300,6 +303,9 @@ def configure_logger(debug=False): def run(manual_args=None): + if manual_args is None: + manual_args = sys.argv[1:] + args = parse_args(manual_args) configure_logger(args.debug) diff --git a/jrnl/util.py b/jrnl/util.py index 8033306bc..a347aaac0 100644 --- a/jrnl/util.py +++ b/jrnl/util.py @@ -9,6 +9,7 @@ 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()