From 4bbd2fe53bb0389a9eb00a430cc0faac90d4774d Mon Sep 17 00:00:00 2001 From: karimpwnz Date: Sat, 2 Jan 2021 20:54:26 +0200 Subject: [PATCH 1/4] Create tests and steps for temporary filename suffix --- .../configs/editor_markdown_extension.yaml | 18 ++++++++++++++++++ features/data/templates/extension.md | 0 features/file_storage.feature | 10 ++++++++++ features/steps/core.py | 9 +++++++++ 4 files changed, 37 insertions(+) create mode 100644 features/data/configs/editor_markdown_extension.yaml create mode 100644 features/data/templates/extension.md diff --git a/features/data/configs/editor_markdown_extension.yaml b/features/data/configs/editor_markdown_extension.yaml new file mode 100644 index 000000000..bf3b8d8e3 --- /dev/null +++ b/features/data/configs/editor_markdown_extension.yaml @@ -0,0 +1,18 @@ +default_hour: 9 +default_minute: 0 +editor: "" +encrypt: false +highlight: true +editor: "vim" +journals: + default: features/journals/editor_markdown_extension.journal +linewrap: 80 +tagsymbols: "@" +template: features/templates/extension.md +timeformat: "%Y-%m-%d %H:%M" +indent_character: "|" +colors: + date: none + title: none + body: none + tags: none diff --git a/features/data/templates/extension.md b/features/data/templates/extension.md new file mode 100644 index 000000000..e69de29bb diff --git a/features/file_storage.feature b/features/file_storage.feature index 0e2c0b3ca..336193656 100644 --- a/features/file_storage.feature +++ b/features/file_storage.feature @@ -44,3 +44,13 @@ Feature: Journals iteracting with the file system in a way that users can see And we change directory to "features" And we run "jrnl -n 1" Then the output should contain "hello world" + + Scenario: the temporary filename suffix should default to ".jrnl" + Given we use the config "editor.yaml" + When we run "jrnl --edit" + Then the temporary filename suffix should be ".jrnl" + + Scenario: the temporary filename suffix should be "-{template_filename}" + Given we use the config "editor_markdown_extension.yaml" + When we run "jrnl --edit" + Then the temporary filename suffix should be "-extension.md" diff --git a/features/steps/core.py b/features/steps/core.py index d579b6d21..1cd2ae9d5 100644 --- a/features/steps/core.py +++ b/features/steps/core.py @@ -248,6 +248,15 @@ def contains_editor_file(context, method, text=""): assert False, f"Method '{method}' not supported" +@then('the temporary filename suffix should be "{suffix}"') +def extension_editor_file(context, suffix): + filename = Path(context.editor_file["name"]).name + delimiter = "-" if "-" in filename else "." + file_suffix = delimiter + filename.split(delimiter)[-1] + print(file_suffix, suffix) + assert file_suffix == suffix + + def _mock_getpass(inputs): def prompt_return(prompt=""): if type(inputs) == str: From 7405221a8aa885ac5d86dbb0bcd640812b57f50a Mon Sep 17 00:00:00 2001 From: karimpwnz Date: Sat, 2 Jan 2021 20:55:54 +0200 Subject: [PATCH 2/4] Have temporary filename suffix be -{template_filename} or .jrnl --- jrnl/editor.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/jrnl/editor.py b/jrnl/editor.py index 3397cdaca..ea89a9ece 100644 --- a/jrnl/editor.py +++ b/jrnl/editor.py @@ -5,14 +5,18 @@ import sys import tempfile import textwrap +from pathlib import Path from .color import ERROR_COLOR from .color import RESET_COLOR from .os_compat import on_windows -def get_text_from_editor(config, template=""): - filehandle, tmpfile = tempfile.mkstemp(prefix="jrnl", text=True, suffix=".txt") +def get_text_from_editor(config, template="", suffix=".jrnl"): + if config["template"]: + template_filename = Path(config["template"]).name + suffix = "-" + template_filename + filehandle, tmpfile = tempfile.mkstemp(prefix="jrnl", text=True, suffix=suffix) os.close(filehandle) with open(tmpfile, "w", encoding="utf-8") as f: From d5bc0664833d95c3100622557774895e7fc6de8b Mon Sep 17 00:00:00 2001 From: karimpwnz Date: Sat, 2 Jan 2021 21:00:39 +0200 Subject: [PATCH 3/4] Finalize extension_editor_file --- features/steps/core.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/features/steps/core.py b/features/steps/core.py index 1cd2ae9d5..af22c0cdc 100644 --- a/features/steps/core.py +++ b/features/steps/core.py @@ -252,9 +252,8 @@ def contains_editor_file(context, method, text=""): def extension_editor_file(context, suffix): filename = Path(context.editor_file["name"]).name delimiter = "-" if "-" in filename else "." - file_suffix = delimiter + filename.split(delimiter)[-1] - print(file_suffix, suffix) - assert file_suffix == suffix + filename_suffix = delimiter + filename.split(delimiter)[-1] + assert filename_suffix == suffix def _mock_getpass(inputs): From f527a93d5d8556f26a24b16671bac188f6766aee Mon Sep 17 00:00:00 2001 From: karimpwnz Date: Sat, 2 Jan 2021 22:25:52 +0200 Subject: [PATCH 4/4] Make suffix local variable in get_text_from_editor --- jrnl/editor.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/jrnl/editor.py b/jrnl/editor.py index ea89a9ece..1a68028d7 100644 --- a/jrnl/editor.py +++ b/jrnl/editor.py @@ -12,7 +12,8 @@ from .os_compat import on_windows -def get_text_from_editor(config, template="", suffix=".jrnl"): +def get_text_from_editor(config, template=""): + suffix = ".jrnl" if config["template"]: template_filename = Path(config["template"]).name suffix = "-" + template_filename