update handling of cache directories in test suite (easier syntax)

This commit is contained in:
Jonathan Wren 2020-10-10 17:28:16 -07:00
parent 93f383cb78
commit 11c7a878c3
No known key found for this signature in database
GPG key ID: 43D5FF8722E7F68A
3 changed files with 33 additions and 19 deletions

View file

@ -399,15 +399,15 @@ Feature: Custom formats
Scenario Outline: Export to yaml
Given we use the config "<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 "<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

View file

@ -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)

View file

@ -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)