From 1fd1fcc00d7ad7ed6f2e08e1832f23f3fdf32c0d Mon Sep 17 00:00:00 2001 From: Aaron Lichtman Date: Fri, 15 Nov 2019 09:38:09 +0100 Subject: [PATCH] Exit jrnl if no text entered into editor Add test for aborting jrnl entry from editor Use native mocking Add comment explaining discrepancy between expected and asserted output Fix check_empty_output method Check message on stderr and patch subprocess.call Add _mock_editor_function Update features/steps/core.py Add return from mock function Add debug statements Debug Update features/steps/core.py Move sys.exit() down Fix test? Fix test? Fix test? Clean up debug statements Clean up debug statements Remove extraneous code Remove extra space Add test for empty stdin input Remove extra debug line Fix test? Fix test? Update features/core.feature Fix test? Fix no stdin input test Co-Authored-By: pspeter --- features/core.feature | 13 +++++++++++++ features/data/configs/editor.yaml | 12 ++++++++++++ features/steps/core.py | 27 ++++++++++++++++++++++++++- jrnl/cli.py | 4 ++-- 4 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 features/data/configs/editor.yaml diff --git a/features/core.feature b/features/core.feature index ab86da42..36601fde 100644 --- a/features/core.feature +++ b/features/core.feature @@ -20,6 +20,19 @@ 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." + Scenario: Writing an empty entry from the editor + Given we use the config "editor.yaml" + When we open the editor and enter "" + Then we should see the message "[Nothing saved to file]" + + Scenario: Writing an empty entry from the command line + Given we use the config "basic.yaml" + When we run "jrnl" and enter "" + Then the output should be + """ + + """ + Scenario: Filtering for dates Given we use the config "basic.yaml" When we run "jrnl -on 2013-06-10 --short" diff --git a/features/data/configs/editor.yaml b/features/data/configs/editor.yaml new file mode 100644 index 00000000..8a06f916 --- /dev/null +++ b/features/data/configs/editor.yaml @@ -0,0 +1,12 @@ +default_hour: 9 +default_minute: 0 +editor: "vim" +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 9217f77f..af4d19a3 100644 --- a/features/steps/core.py +++ b/features/steps/core.py @@ -29,6 +29,7 @@ class TestKeyring(keyring.backend.KeyringBackend): def delete_password(self, servicename, username, password): self.keys[servicename][username] = None + # set the keyring for keyring lib keyring.set_keyring(TestKeyring()) @@ -66,6 +67,24 @@ def set_config(context, config_file): cf.write("version: {}".format(__version__)) +@when('we open the editor and enter ""') +@when('we open the editor and enter "{text}"') +def open_editor_and_enter(context, text=""): + text = (text or context.text) + def _mock_editor_function(command): + tmpfile = command[-1] + with open(tmpfile, "w+") as f: + if text is not None: + f.write(text) + else: + f.write("") + + return tmpfile + + with patch('subprocess.call', side_effect=_mock_editor_function): + run(context, "jrnl") + + def _mock_getpass(inputs): def prompt_return(prompt="Password: "): print(prompt) @@ -82,12 +101,18 @@ def _mock_input(inputs): @when('we run "{command}" and enter') +@when('we run "{command}" and enter ""') @when('we run "{command}" and enter "{inputs1}"') @when('we run "{command}" and enter "{inputs1}" and "{inputs2}"') def run_with_input(context, command, inputs1="", inputs2=""): # create an iterator through all inputs. These inputs will be fed one by one # to the mocked calls for 'input()', 'util.getpass()' and 'sys.stdin.read()' - text = iter((inputs1, inputs2)) if inputs1 else iter(context.text.split("\n")) + if inputs1: + text = iter((inputs1, inputs2)) + elif context.text: + text = iter(context.text.split("\n")) + else: + text = iter(("", "")) args = ushlex(command)[1:] with patch("builtins.input", side_effect=_mock_input(text)) as mock_input: with patch("jrnl.util.getpass", side_effect=_mock_getpass(text)) as mock_getpass: diff --git a/jrnl/cli.py b/jrnl/cli.py index 3cf67030..738087e4 100644 --- a/jrnl/cli.py +++ b/jrnl/cli.py @@ -176,7 +176,7 @@ def run(manual_args=None): log.debug('Using journal "%s"', journal_name) mode_compose, mode_export, mode_import = guess_mode(args, config) - + # How to quit writing? if "win32" in sys.platform: _exit_multiline_code = "on a blank line, press Ctrl+Z and then Enter" @@ -206,7 +206,7 @@ def run(manual_args=None): if raw: args.text = [raw] else: - mode_compose = False + sys.exit() # This is where we finally open the journal! try: