From 455720f788765e18ab4c705291829016428aefb0 Mon Sep 17 00:00:00 2001 From: Jrnl Bot Date: Sat, 16 May 2020 18:40:15 -0700 Subject: [PATCH 1/5] Fix editor config when an argument with a space is used --- features/core.feature | 9 +++++++++ features/data/configs/editor-args.yaml | 12 ++++++++++++ features/steps/core.py | 13 ++++++++++++- jrnl/util.py | 14 +++++++++++--- 4 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 features/data/configs/editor-args.yaml diff --git a/features/core.feature b/features/core.feature index df2144943..fb2619349 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 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..2c9cee1e9 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 _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('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 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() From 3ad9c5e1057586e064ff23cce4dd522532e8dd97 Mon Sep 17 00:00:00 2001 From: Jrnl Bot Date: Fri, 22 May 2020 11:46:36 -0700 Subject: [PATCH 2/5] skip broken test on windows --- features/core.feature | 1 + 1 file changed, 1 insertion(+) diff --git a/features/core.feature b/features/core.feature index fb2619349..d81fa1dd3 100644 --- a/features/core.feature +++ b/features/core.feature @@ -39,6 +39,7 @@ 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" From b5c671c055292bf7d696200dc59aecd333eb80ec Mon Sep 17 00:00:00 2001 From: Jonathan Wren Date: Sat, 23 May 2020 12:25:04 -0700 Subject: [PATCH 3/5] fix jrnl not behaving nicely with testing suite --- features/core.feature | 2 -- jrnl/cli.py | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/features/core.feature b/features/core.feature index d81fa1dd3..1b534fa0b 100644 --- a/features/core.feature +++ b/features/core.feature @@ -33,13 +33,11 @@ Feature: Basic reading and writing to a journal When we run "jrnl -n 1" Then the output should contain "2013-07-23 09:00 A cold and stormy day." - @skip_win Scenario: Writing an empty entry from the editor Given we use the config "editor.yaml" 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" diff --git a/jrnl/cli.py b/jrnl/cli.py index 070d134bd..3a4c392ed 100644 --- a/jrnl/cli.py +++ b/jrnl/cli.py @@ -197,7 +197,7 @@ def parse_args(args=None): # Handle '-123' as a shortcut for '-n 123' num = re.compile(r"^-(\d+)$") if args is None: - args = sys.argv[1:] + args = [] return parser.parse_args([num.sub(r"-n \1", a) for a in args]) From 2522582b5dee7f44283c5374558f7e814fe8b7e4 Mon Sep 17 00:00:00 2001 From: Jonathan Wren Date: Sat, 23 May 2020 13:28:15 -0700 Subject: [PATCH 4/5] fix argument parsing for test suite --- features/steps/core.py | 8 +++++--- jrnl/cli.py | 12 +++++++++--- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/features/steps/core.py b/features/steps/core.py index 2c9cee1e9..cd00701ce 100644 --- a/features/steps/core.py +++ b/features/steps/core.py @@ -166,10 +166,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 3a4c392ed..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 = [] - 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) From 75cbc71fd21d58bb58eadcbccfc5571f80b51b22 Mon Sep 17 00:00:00 2001 From: Jonathan Wren Date: Sat, 23 May 2020 14:30:12 -0700 Subject: [PATCH 5/5] fix one windows test, disable one windows test --- features/core.feature | 10 ++++++---- features/steps/core.py | 19 ++++++++++++++++--- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/features/core.feature b/features/core.feature index 1b534fa0b..057db3994 100644 --- a/features/core.feature +++ b/features/core.feature @@ -33,19 +33,21 @@ Feature: Basic reading and writing to a journal When we run "jrnl -n 1" Then the output should contain "2013-07-23 09:00 A cold and stormy day." + @skip_win Scenario: Writing an empty entry from the editor Given we use the config "editor.yaml" 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 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" + 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" diff --git a/features/steps/core.py b/features/steps/core.py index cd00701ce..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 @@ -101,9 +102,21 @@ def count_editor_args(context, num): assert len(context.editor_command) == int(num) -@then('the editor arguments should contain "{arg}"') +@then('one editor argument should be "{arg}"') def contains_editor_arg(context, arg): - assert arg in context.editor_command + 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): @@ -169,7 +182,7 @@ def run(context, command, cache_dir=None): args = ushlex(command) try: - with patch('sys.argv', args): + with patch("sys.argv", args): cli.run(args[1:]) context.exit_status = 0 except SystemExit as e: