From 11c7a878c36de3d8abb95222ce50db85d68b873b Mon Sep 17 00:00:00 2001 From: Jonathan Wren Date: Sat, 10 Oct 2020 17:28:16 -0700 Subject: [PATCH] update handling of cache directories in test suite (easier syntax) --- features/format.feature | 16 ++++++++-------- features/steps/core.py | 7 +++---- features/steps/export_steps.py | 29 ++++++++++++++++++++++------- 3 files changed, 33 insertions(+), 19 deletions(-) diff --git a/features/format.feature b/features/format.feature index 350ae064..27a33e86 100644 --- a/features/format.feature +++ b/features/format.feature @@ -399,15 +399,15 @@ Feature: Custom formats Scenario Outline: Export to yaml Given we use the config ".yaml" And we use the password "test" if prompted - And we create cache directory "exported_journal" - When we run "jrnl --export yaml -o {cache_dir}" with cache directory "exported_journal" - Then cache directory "exported_journal" should contain the files + And we create a cache directory + When we run "jrnl --export yaml -o {cache_dir}" + Then the cache should contain the files """ 2020-08-29_entry-the-first.md 2020-08-31_a-second-entry-in-what-i-hope-to-be-a-long-series.md 2020-09-24_the-third-entry-finally-after-weeks-without-writing.md """ - And the content of file "2020-08-29_entry-the-first.md" in cache directory "exported_journal" should be + And the content of file "2020-08-29_entry-the-first.md" in the cache should be """ title: Entry the first. date: 2020-08-29 11:11 @@ -446,15 +446,15 @@ Feature: Custom formats # https://github.com/jrnl-org/jrnl/issues/881 Given we use the config ".yaml" And we use the password "test" if prompted - And we create cache directory "bug768" - When we run "jrnl --export yaml -o {cache_dir}" with cache directory "bug768" - Then cache directory "bug768" should contain the files + And we create a cache directory + When we run "jrnl --export yaml -o {cache_dir}" + Then the cache should contain the files """ 2020-08-29_entry-the-first.md 2020-08-31_a-second-entry-in-what-i-hope-to-be-a-long-series.md 2020-09-24_the-third-entry-finally-after-weeks-without-writing.md """ - And the content of file "2020-09-24_the-third-entry-finally-after-weeks-without-writing.md" in cache directory "bug768" should be + And the content of file "2020-09-24_the-third-entry-finally-after-weeks-without-writing.md" in the cache should be """ title: The third entry finally after weeks without writing. date: 2020-09-24 09:14 diff --git a/features/steps/core.py b/features/steps/core.py index e9a1e288..20c455c8 100644 --- a/features/steps/core.py +++ b/features/steps/core.py @@ -333,12 +333,11 @@ def all_input_was_used(context): @when('we run "{command}"') @when('we run "{command}" and pipe') @when('we run "{command}" and pipe "{text}"') -@when('we run "{command}" with cache directory "{cache_dir}"') -def run(context, command, text="", cache_dir=None): +def run(context, command, text=""): text = text or context.text or "" - if cache_dir is not None: - cache_dir = os.path.join("features", "cache", cache_dir) + if "cache_dir" in context and context.cache_dir is not None: + cache_dir = os.path.join("features", "cache", context.cache_dir) command = command.format(cache_dir=cache_dir) args = ushlex(command) diff --git a/features/steps/export_steps.py b/features/steps/export_steps.py index cc713127..9003c1bd 100644 --- a/features/steps/export_steps.py +++ b/features/steps/export_steps.py @@ -1,6 +1,8 @@ import json import os import shutil +import random +import string from xml.etree import ElementTree from behave import given @@ -116,18 +118,27 @@ def assert_xml_output_tags(context, expected_tags_json_list): @given('we create cache directory "{dir_name}"') -def create_directory(context, dir_name): +@given("we create a cache directory") +def create_directory(context, dir_name=None): + if not dir_name: + dir_name = "cache_" + "".join( + random.choices(string.ascii_uppercase + string.digits, k=20) + ) + working_dir = os.path.join("features", "cache", dir_name) if os.path.exists(working_dir): shutil.rmtree(working_dir) os.makedirs(working_dir) + context.cache_dir = dir_name -@then('cache directory "{dir_name}" should contain the files') -@then( - 'cache directory "{dir_name}" should contain the files {expected_files_json_list}' -) -def assert_dir_contains_files(context, dir_name, expected_files_json_list=""): +@then('cache "{dir_name}" should contain the files') +@then('cache "{dir_name}" should contain the files {expected_files_json_list}') +@then("the cache should contain the files") +def assert_dir_contains_files(context, dir_name=None, expected_files_json_list=""): + if not dir_name: + dir_name = context.cache_dir + working_dir = os.path.join("features", "cache", dir_name) actual_files = os.listdir(working_dir) @@ -142,7 +153,11 @@ def assert_dir_contains_files(context, dir_name, expected_files_json_list=""): @then('the content of file "{file_path}" in cache directory "{cache_dir}" should be') -def assert_exported_yaml_file_content(context, file_path, cache_dir): +@then('the content of file "{file_path}" in the cache should be') +def assert_exported_yaml_file_content(context, file_path, cache_dir=None): + if not cache_dir: + cache_dir = context.cache_dir + expected_content = context.text.strip().splitlines() full_file_path = os.path.join("features", "cache", cache_dir, file_path)