From f6bec97c58e903de87d7d9435d0f51888c3404e4 Mon Sep 17 00:00:00 2001 From: Stav Shamir Date: Mon, 27 Jan 2020 14:12:02 +0200 Subject: [PATCH] Add scenario for yaml export --- features/exporting.feature | 16 ++++++++++++++ features/steps/export_steps.py | 40 +++++++++++++++++++++++++++++++++- 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/features/exporting.feature b/features/exporting.feature index 35552a91..5705fda1 100644 --- a/features/exporting.feature +++ b/features/exporting.feature @@ -120,3 +120,19 @@ 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 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 + """ + title: I have an @idea: + date: 2013-04-09 15:39 + stared: False + tags: idea, journal + + (1) write a command line @journal software + (2) ??? + (3) PROFIT! + """ diff --git a/features/steps/export_steps.py b/features/steps/export_steps.py index a3704b74..19503707 100644 --- a/features/steps/export_steps.py +++ b/features/steps/export_steps.py @@ -1,7 +1,9 @@ import json +import os +import shutil from xml.etree import ElementTree -from behave import then +from behave import then, given @then("the output should be parsable as json") @@ -81,3 +83,39 @@ def assert_xml_output_tags(context, expected_tags_json_list): expected_tags = json.loads(expected_tags_json_list) actual_tags = set(t.attrib["name"] for t in xml_tree.find("tags")) assert actual_tags == set(expected_tags), [actual_tags, set(expected_tags)] + + +@given('we created a directory named "{dir_name}"') +def create_directory(context, dir_name): + shutil.rmtree(dir_name) + os.mkdir(dir_name) + + +@then('"{dir_name}" should contain the files {expected_files_json_list}') +def assert_dir_contains_files(context, dir_name, expected_files_json_list): + actual_files = os.listdir(dir_name) + expected_files = json.loads(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): + expected_content = context.text.strip().splitlines() + + with open(file_path, "r") as f: + actual_content = f.read().strip().splitlines() + + for actual_line, expected_line in zip(actual_content, expected_content): + if actual_line.startswith('tags: ') and expected_line.startswith('tags: '): + assert_equal_tags_ignoring_order(actual_line, expected_line) + else: + assert actual_line.strip() == expected_line.strip(), [ + actual_line.strip(), + expected_line.strip(), + ] + + +def assert_equal_tags_ignoring_order(actual_line, expected_line): + actual_tags = set(tag.strip() for tag in actual_line[len('tags: '):].split(',')) + expected_tags = set(tag.strip() for tag in expected_line[len('tags: '):].split(',')) + assert actual_tags == expected_tags, [actual_tags, expected_tags]