Add ability to put --edit partly through a cli entry to move it to the editor (#1091)

also add some functionality to test suite to see what the editor file contents were and make assertions about it
This commit is contained in:
Jonathan Wren 2020-11-21 15:20:21 -08:00 committed by GitHub
parent 336936651f
commit cfdd05358a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 5 deletions

View file

@ -230,6 +230,21 @@ def matches_editor_arg(context, regex):
), f"\nRegex didn't match exactly 1 time:\n{regex}\n{str(args)}" ), f"\nRegex didn't match exactly 1 time:\n{regex}\n{str(args)}"
@then("the editor file content should {method}")
@then("the editor file content should {method} empty")
@then('the editor file content should {method} "{text}"')
def contains_editor_file(context, method, text=""):
text = text or context.text or ""
content = context.editor_file.get("content")
format = f'\n"""\n{content}\n"""\n'
if method == "be":
assert content == text, format
elif method == "contain":
assert text in content, format
else:
assert False, f"Method '{method}' not supported"
def _mock_getpass(inputs): def _mock_getpass(inputs):
def prompt_return(prompt=""): def prompt_return(prompt=""):
if type(inputs) == str: if type(inputs) == str:
@ -270,7 +285,9 @@ def run_with_input(context, command, inputs=""):
def _mock_editor(command): def _mock_editor(command):
context.editor_command = command context.editor_command = command
tmpfile = command[-1] tmpfile = command[-1]
context.editor_file = tmpfile with open(tmpfile, "r") as editor_file:
file_content = editor_file.read()
context.editor_file = {"name": tmpfile, "content": file_content}
Path(tmpfile).touch() Path(tmpfile).touch()
if "password" in context: if "password" in context:
@ -348,6 +365,11 @@ def run(context, command, text=""):
def _mock_editor(command): def _mock_editor(command):
context.editor_command = command context.editor_command = command
tmpfile = command[-1]
with open(tmpfile, "r") as editor_file:
file_content = editor_file.read()
context.editor_file = {"name": tmpfile, "content": file_content}
Path(tmpfile).touch()
if "password" in context: if "password" in context:
password = context.password password = context.password
@ -359,10 +381,12 @@ def run(context, command, text=""):
# see: https://github.com/psf/black/issues/664 # see: https://github.com/psf/black/issues/664
with \ with \
patch("sys.argv", args), \ patch("sys.argv", args), \
patch("getpass.getpass", side_effect=_mock_getpass(password)), \ patch("getpass.getpass", side_effect=_mock_getpass(password)) as mock_getpass, \
patch("subprocess.call", side_effect=_mock_editor), \ patch("subprocess.call", side_effect=_mock_editor) as mock_editor, \
patch("sys.stdin.read", side_effect=lambda: text) \ patch("sys.stdin.read", side_effect=lambda: text) \
: :
context.editor = mock_editor
context.getpass = mock_getpass
cli(args[1:]) cli(args[1:])
context.exit_status = 0 context.exit_status = 0
# fmt: on # fmt: on

View file

@ -43,6 +43,26 @@ Feature: Writing new entries.
| dayone | | dayone |
| encrypted | | encrypted |
Scenario Outline: Writing a partial entry from command line with edit flag should go to the editor
Given we use the config "<config_file>.yaml"
And we use the password "test" if prompted
When we run "jrnl this is a partial --edit"
Then we should see the message "Entry added"
Then the editor should have been called
And the editor file content should be
"""
this is a partial
"""
When we run "jrnl -n 1"
Then the output should contain "this is a partial"
Examples: configs
| config_file |
| basic_onefile |
| basic_encrypted |
| basic_dayone |
| basic_folder |
Scenario Outline: Writing an empty entry from the editor should yield "Nothing saved to file" message Scenario Outline: Writing an empty entry from the editor should yield "Nothing saved to file" message
Given we use the config "<config_file>.yaml" Given we use the config "<config_file>.yaml"
And we use the password "test" if prompted And we use the password "test" if prompted

View file

@ -83,6 +83,10 @@ def _is_write_mode(args, config, **kwargs):
) )
) )
# Might be writing and want to move to editor part of the way through
if args.edit and args.text:
write_mode = True
# If the text is entirely tags, then we are also searching (not writing) # If the text is entirely tags, then we are also searching (not writing)
if ( if (
write_mode write_mode
@ -108,6 +112,8 @@ def write_mode(args, config, journal, **kwargs):
if args.text: if args.text:
logging.debug("Write mode: cli text detected: %s", args.text) logging.debug("Write mode: cli text detected: %s", args.text)
raw = " ".join(args.text).strip() raw = " ".join(args.text).strip()
if args.edit:
raw = _write_in_editor(config, raw)
elif not sys.stdin.isatty(): elif not sys.stdin.isatty():
logging.debug("Write mode: receiving piped text") logging.debug("Write mode: receiving piped text")
@ -157,9 +163,10 @@ def search_mode(args, journal, **kwargs):
_display_search_results(**kwargs) _display_search_results(**kwargs)
def _write_in_editor(config): def _write_in_editor(config, template=None):
if config["editor"]: if config["editor"]:
logging.debug("Write mode: opening editor") logging.debug("Write mode: opening editor")
if not template:
template = _get_editor_template(config) template = _get_editor_template(config)
raw = get_text_from_editor(config, template) raw = get_text_from_editor(config, template)