From 83547ac09458df042c51976585ad4f2458ab00b6 Mon Sep 17 00:00:00 2001 From: Jonathan Wren Date: Wed, 6 May 2020 15:08:02 -0700 Subject: [PATCH] clean up behave code for creating cache directories This adds the ability to run commands in a cache directory without the test writer knowing where the cache directory is located. This will let us expand later if we want to start using system temp folders, without having to rewrite any of our tests. --- features/environment.py | 3 ++- features/exporting.feature | 14 ++++++++++---- features/regression.feature | 15 +++++++++++---- features/steps/core.py | 7 ++++++- features/steps/export_steps.py | 19 ++++++++++++------- 5 files changed, 41 insertions(+), 17 deletions(-) diff --git a/features/environment.py b/features/environment.py index 4843de45..a75bba4d 100644 --- a/features/environment.py +++ b/features/environment.py @@ -2,12 +2,14 @@ import shutil import os import sys + def clean_all_working_dirs(): for folder in ("configs", "journals", "cache"): working_dir = os.path.join("features", folder) if os.path.exists(working_dir): shutil.rmtree(working_dir) + def before_feature(context, feature): # add "skip" tag # https://stackoverflow.com/a/42721605/4276230 @@ -51,4 +53,3 @@ def before_scenario(context, scenario): def after_scenario(context, scenario): """After each scenario, restore all test data and remove working_dirs.""" clean_all_working_dirs() - diff --git a/features/exporting.feature b/features/exporting.feature index 230acd94..7f4df59f 100644 --- a/features/exporting.feature +++ b/features/exporting.feature @@ -122,10 +122,16 @@ Feature: Exporting a Journal Scenario: Export to yaml Given we use the config "tags.yaml" - And we created a directory named "exported_journal" - When we run "jrnl --export yaml -o features/cache/exported_journal" - Then "exported_journal" should contain the files ["2013-04-09_i-have-an-idea.md", "2013-06-10_i-met-with-dan.md"] - And the content of exported yaml "exported_journal/2013-04-09_i-have-an-idea.md" should be + 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 + """ + [ + "2013-04-09_i-have-an-idea.md", + "2013-06-10_i-met-with-dan.md" + ] + """ + And the content of file "2013-04-09_i-have-an-idea.md" in cache directory "exported_journal" should be """ title: I have an @idea: date: 2013-04-09 15:39 diff --git a/features/regression.feature b/features/regression.feature index 04377902..5342b746 100644 --- a/features/regression.feature +++ b/features/regression.feature @@ -148,10 +148,17 @@ Feature: Zapped bugs should stay dead. # See issues #768 and #881 Scenario: Add a blank line to YAML export is there isn't one already Given we use the config "deletion.yaml" - And we created a directory named "bug768" - When we run "jrnl --export yaml -o features/cache/bug768" - Then "bug768" should contain the files ["2019-10-29_first-entry.md", "2019-10-29_second-entry.md", "2019-10-29_third-entry.md"] - And the content of exported yaml "bug768/2019-10-29_third-entry.md" should be + 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 + """ + [ + "2019-10-29_first-entry.md", + "2019-10-29_second-entry.md", + "2019-10-29_third-entry.md" + ] + """ + And the content of file "2019-10-29_third-entry.md" in cache directory "bug768" should be """ title: Third entry. date: 2019-10-29 11:13 diff --git a/features/steps/core.py b/features/steps/core.py index b69edb9e..2a32ef95 100644 --- a/features/steps/core.py +++ b/features/steps/core.py @@ -151,7 +151,12 @@ def run_with_input(context, command, inputs=""): @when('we run "{command}"') -def run(context, command): +@when('we run "{command}" with cache directory "{cache_dir}"') +def run(context, command, cache_dir=None): + if cache_dir is not None: + cache_dir = os.path.join("features", "cache", cache_dir) + command = command.format(cache_dir=cache_dir) + args = ushlex(command)[1:] try: cli.run(args or None) diff --git a/features/steps/export_steps.py b/features/steps/export_steps.py index 0acdce8f..c75cf331 100644 --- a/features/steps/export_steps.py +++ b/features/steps/export_steps.py @@ -85,7 +85,7 @@ def assert_xml_output_tags(context, expected_tags_json_list): assert actual_tags == set(expected_tags), [actual_tags, set(expected_tags)] -@given('we created a directory named "{dir_name}"') +@given('we create cache directory "{dir_name}"') def create_directory(context, dir_name): working_dir = os.path.join("features", "cache", dir_name) if os.path.exists(working_dir): @@ -93,11 +93,16 @@ def create_directory(context, dir_name): os.makedirs(working_dir) -@then('"{dir_name}" should contain the files {expected_files_json_list}') -def assert_dir_contains_files(context, dir_name, expected_files_json_list): +@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="[]"): working_dir = os.path.join("features", "cache", dir_name) actual_files = os.listdir(working_dir) - expected_files = json.loads(expected_files_json_list) + + expected_files = context.text or expected_files_json_list + expected_files = json.loads(expected_files) # sort to deal with inconsistent default file ordering on different OS's actual_files.sort() @@ -106,10 +111,10 @@ def assert_dir_contains_files(context, dir_name, expected_files_json_list): assert actual_files == expected_files, [actual_files, expected_files] -@then('the content of exported yaml "{file_path}" should be') -def assert_exported_yaml_file_content(context, file_path): +@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): expected_content = context.text.strip().splitlines() - full_file_path = os.path.join("features", "cache", file_path) + full_file_path = os.path.join("features", "cache", cache_dir, file_path) with open(full_file_path, "r") as f: actual_content = f.read().strip().splitlines()