From 60c03d96d6ddb0cf6599d6cc6f1229702beae233 Mon Sep 17 00:00:00 2001 From: Manuel Ebert Date: Wed, 2 Jul 2014 12:11:11 +0200 Subject: [PATCH] Fixes config tests --- features/core.feature | 10 ++++---- features/data/configs/basic.json | 14 ------------ features/data/configs/basic.yaml | 10 ++++++++ features/data/configs/broken.json | 15 ------------ features/encryption.feature | 2 +- features/fix_json.feature | 14 ------------ features/regression.feature | 6 ++--- features/starring.feature | 4 ++-- features/steps/core.py | 38 +++++++++++++++++++++++-------- 9 files changed, 49 insertions(+), 64 deletions(-) delete mode 100644 features/data/configs/basic.json create mode 100644 features/data/configs/basic.yaml delete mode 100644 features/data/configs/broken.json delete mode 100644 features/fix_json.feature diff --git a/features/core.feature b/features/core.feature index 3a9b9c7c..ecb3df6e 100644 --- a/features/core.feature +++ b/features/core.feature @@ -1,7 +1,7 @@ Feature: Basic reading and writing to a journal Scenario: Loading a sample journal - Given we use the config "basic.json" + Given we use the config "basic.yaml" When we run "jrnl -n 2" Then we should get no error and the output should be @@ -14,7 +14,7 @@ Feature: Basic reading and writing to a journal """ Scenario: Writing an entry from command line - Given we use the config "basic.json" + Given we use the config "basic.yaml" When we run "jrnl 23 july 2013: A cold and stormy day. I ate crisps on the sofa." Then we should see the message "Entry added" When we run "jrnl -n 1" @@ -28,7 +28,7 @@ Feature: Basic reading and writing to a journal Then the output should be "2013-06-10 15:40 Life is good." Scenario: Emoji support - Given we use the config "basic.json" + Given we use the config "basic.yaml" When we run "jrnl 23 july 2013: 🌞 sunny day. Saw an 🐘" Then we should see the message "Entry added" When we run "jrnl -n 1" @@ -36,14 +36,14 @@ Feature: Basic reading and writing to a journal and the output should contain "🐘" Scenario: Writing an entry at the prompt - Given we use the config "basic.json" + Given we use the config "basic.yaml" When we run "jrnl" and enter "25 jul 2013: I saw Elvis. He's alive." Then we should get no error and the journal should contain "2013-07-25 09:00 I saw Elvis." and the journal should contain "He's alive." Scenario: Displaying the version number - Given we use the config "basic.json" + Given we use the config "basic.yaml" When we run "jrnl -v" Then we should get no error Then the output should contain "version" diff --git a/features/data/configs/basic.json b/features/data/configs/basic.json deleted file mode 100644 index 2dc11d73..00000000 --- a/features/data/configs/basic.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "default_hour": 9, - "timeformat": "%Y-%m-%d %H:%M", - "linewrap": 80, - "encrypt": false, - "editor": "", - "default_minute": 0, - "highlight": true, - "password": "", - "journals": { - "default": "features/journals/simple.journal" - }, - "tagsymbols": "@" -} diff --git a/features/data/configs/basic.yaml b/features/data/configs/basic.yaml new file mode 100644 index 00000000..b5e51b19 --- /dev/null +++ b/features/data/configs/basic.yaml @@ -0,0 +1,10 @@ +default_hour: 9 +timeformat: "%Y-%m-%d %H:%M" +linewrap: 80 +encrypt: false +editor: "" +default_minute: 0 +highlight: true +journals: + default: features/journals/simple.journal +tagsymbols: "@" diff --git a/features/data/configs/broken.json b/features/data/configs/broken.json deleted file mode 100644 index bcd96f24..00000000 --- a/features/data/configs/broken.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "default_hour": 9, - "timeformat": "%Y-%m-%d %H:%M", - "linewrap": 80, - "encrypt": false, - "editor": "", - "default_minute": 0, - "highlight": true - "password": "", - "journals": { - "default": "features/journals/simple.journal" - "work": "features/journals/work.journal", - }, - "tagsymbols": "@" -} diff --git a/features/encryption.feature b/features/encryption.feature index 43d07c26..472b91d2 100644 --- a/features/encryption.feature +++ b/features/encryption.feature @@ -14,7 +14,7 @@ and the journal should have 2 entries Scenario: Encrypting a journal - Given we use the config "basic.json" + Given we use the config "basic.yaml" When we run "jrnl --encrypt" and enter "swordfish" Then we should see the message "Journal encrypted" and the config for journal "default" should have "encrypt" set to "bool:True" diff --git a/features/fix_json.feature b/features/fix_json.feature deleted file mode 100644 index a653e25c..00000000 --- a/features/fix_json.feature +++ /dev/null @@ -1,14 +0,0 @@ -Feature: Fixing broken config files - - Scenario: Loading a file with journal - Given we use the config "broken.json" - When we run "jrnl -n 2" - Then we should see the message "Some errors in your jrnl config have been fixed for you." - and the output should be - """ - 2013-06-09 15:39 My first entry. - | Everything is alright - - 2013-06-10 15:40 Life is good. - | But I'm better. - """ diff --git a/features/regression.feature b/features/regression.feature index 1672afb4..13ef6fcd 100644 --- a/features/regression.feature +++ b/features/regression.feature @@ -2,7 +2,7 @@ Feature: Zapped bugs should stay dead. Scenario: Writing an entry does not print the entire journal # https://github.com/maebert/jrnl/issues/87 - Given we use the config "basic.json" + Given we use the config "basic.yaml" When we run "jrnl 23 july 2013: A cold and stormy day. I ate crisps on the sofa." Then we should see the message "Entry added" When we run "jrnl -n 1" @@ -16,7 +16,7 @@ Feature: Zapped bugs should stay dead. Scenario: Date with time should be parsed correctly # https://github.com/maebert/jrnl/issues/117 - Given we use the config "basic.json" + Given we use the config "basic.yaml" When we run "jrnl 2013-11-30 15:42: Project Started." Then we should see the message "Entry added" and the journal should contain "2013-11-30 15:42 Project Started." @@ -39,7 +39,7 @@ Feature: Zapped bugs should stay dead. """ Scenario: Title with an embedded period. - Given we use the config "basic.json" + Given we use the config "basic.yaml" When we run "jrnl 04-24-2014: Created a new website - empty.com. Hope to get a lot of traffic." Then we should see the message "Entry added" When we run "jrnl -1" diff --git a/features/starring.feature b/features/starring.feature index 0f984896..35154045 100644 --- a/features/starring.feature +++ b/features/starring.feature @@ -1,13 +1,13 @@ Feature: Starring entries Scenario: Starring an entry will mark it in the journal file - Given we use the config "basic.json" + Given we use the config "basic.yaml" When we run "jrnl 20 july 2013 *: Best day of my life!" Then we should see the message "Entry added" and the journal should contain "2013-07-20 09:00 Best day of my life! *" Scenario: Filtering by starred entries - Given we use the config "basic.json" + Given we use the config "basic.yaml" When we run "jrnl -starred" Then the output should be """ diff --git a/features/steps/core.py b/features/steps/core.py index e3d79322..3da9216d 100644 --- a/features/steps/core.py +++ b/features/steps/core.py @@ -2,9 +2,7 @@ from behave import * from jrnl import cli, install, Journal, util from dateutil import parser as date_parser import os -import sys import json -import pytz import keyring keyring.set_keyring(keyring.backends.file.PlaintextKeyring()) try: @@ -13,8 +11,9 @@ except ImportError: from cStringIO import StringIO import tzlocal + def _parse_args(command): - nargs=[] + nargs = [] concats = [] for a in command.split()[1:]: if a.startswith("'"): @@ -27,16 +26,16 @@ def _parse_args(command): nargs.append(a) return nargs + def read_journal(journal_name="default"): - with open(install.CONFIG_FILE_PATH) as config_file: - config = json.load(config_file) + config = util.load_config(install.CONFIG_FILE_PATH) with open(config['journals'][journal_name]) as journal_file: journal = journal_file.read() return journal + def open_journal(journal_name="default"): - with open(install.CONFIG_FILE_PATH) as config_file: - config = json.load(config_file) + config = util.load_config(install.CONFIG_FILE_PATH) journal_conf = config['journals'][journal_name] if type(journal_conf) is dict: # We can override the default config on a by-journal basis config.update(journal_conf) @@ -44,11 +43,13 @@ def open_journal(journal_name="default"): config['journal'] = journal_conf return Journal.Journal(**config) + @given('we use the config "{config_file}"') def set_config(context, config_file): full_path = os.path.join("features/configs", config_file) install.CONFIG_FILE_PATH = os.path.abspath(full_path) + @when('we run "{command}" and enter') @when('we run "{command}" and enter "{inputs}"') def run_with_input(context, command, inputs=None): @@ -62,6 +63,7 @@ def run_with_input(context, command, inputs=None): except SystemExit as e: context.exit_status = e.code + @when('we run "{command}"') def run(context, command): args = _parse_args(command) @@ -71,23 +73,28 @@ def run(context, command): except SystemExit as e: context.exit_status = e.code + @when('we set the keychain password of "{journal}" to "{password}"') def set_keychain(context, journal, password): keyring.set_password('jrnl', journal, password) + @then('we should get an error') def has_error(context): assert context.exit_status != 0, context.exit_status + @then('we should get no error') def no_error(context): assert context.exit_status is 0, context.exit_status + @then('the output should be parsable as json') def check_output_json(context): out = context.stdout_capture.getvalue() assert json.loads(out), out + @then('"{field}" in the json output should have {number:d} elements') @then('"{field}" in the json output should have 1 element') def check_output_field(context, field, number=1): @@ -96,6 +103,7 @@ def check_output_field(context, field, number=1): assert field in out_json, [field, out_json] assert len(out_json[field]) == number, len(out_json[field]) + @then('"{field}" in the json output should not contain "{key}"') def check_output_field_not_key(context, field, key): out = context.stdout_capture.getvalue() @@ -103,6 +111,7 @@ def check_output_field_not_key(context, field, key): assert field in out_json assert key not in out_json[field] + @then('"{field}" in the json output should contain "{key}"') def check_output_field_key(context, field, key): out = context.stdout_capture.getvalue() @@ -120,6 +129,7 @@ def check_output(context, text=None): for line_text, line_out in zip(text, out): assert line_text.strip() == line_out.strip(), [line_text.strip(), line_out.strip()] + @then('the output should contain "{text}" in the local time') def check_output_time_inline(context, text): out = context.stdout_capture.getvalue() @@ -129,6 +139,7 @@ def check_output_time_inline(context, text): local_date = date.strftime("%Y-%m-%d %H:%M") assert local_date in out, local_date + @then('the output should contain "{text}"') def check_output_inline(context, text): out = context.stdout_capture.getvalue() @@ -136,6 +147,7 @@ def check_output_inline(context, text): out = out.decode('utf-8') assert text in out + @then('the output should not contain "{text}"') def check_output_not_inline(context, text): out = context.stdout_capture.getvalue() @@ -143,22 +155,26 @@ def check_output_not_inline(context, text): out = out.decode('utf-8') assert text not in out + @then('we should see the message "{text}"') def check_message(context, text): out = context.messages.getvalue() assert text in out, [text, out] + @then('we should not see the message "{text}"') def check_not_message(context, text): out = context.messages.getvalue() assert text not in out, [text, out] + @then('the journal should contain "{text}"') @then('journal "{journal_name}" should contain "{text}"') def check_journal_content(context, text, journal_name="default"): journal = read_journal(journal_name) assert text in journal, journal + @then('journal "{journal_name}" should not exist') def journal_doesnt_exist(context, journal_name="default"): with open(install.CONFIG_FILE_PATH) as config_file: @@ -166,6 +182,7 @@ def journal_doesnt_exist(context, journal_name="default"): journal_path = config['journals'][journal_name] assert not os.path.exists(journal_path) + @then('the config should have "{key}" set to "{value}"') @then('the config for journal "{journal}" should have "{key}" set to "{value}"') def config_var(context, key, value, journal=None): @@ -175,21 +192,22 @@ def config_var(context, key, value, journal=None): "int": int, "str": str }[t](value) - with open(install.CONFIG_FILE_PATH) as config_file: - config = json.load(config_file) + config = util.load_config(install.CONFIG_FILE_PATH) if journal: config = config["journals"][journal] assert key in config assert config[key] == value + @then('the journal should have {number:d} entries') @then('the journal should have {number:d} entry') @then('journal "{journal_name}" should have {number:d} entries') @then('journal "{journal_name}" should have {number:d} entry') -def check_journal_content(context, number, journal_name="default"): +def check_journal_entries(context, number, journal_name="default"): journal = open_journal(journal_name) assert len(journal.entries) == number + @then('fail') def debug_fail(context): assert False