behavior test for format --pretty

This commit is contained in:
Suhas 2021-02-02 20:14:32 -05:00
parent eca002ac29
commit 1f8351a9f0
3 changed files with 43 additions and 3 deletions

View file

@ -1,4 +1,9 @@
Feature: Custom formats
Scenario: Pretty Printing aka the Default
Given We use the config "pretty.yaml"
When we run "jrnl --format pretty -3"
Then we should get no error
And the output should be pretty printed
Scenario Outline: JSON format
Given we use the config "<config>.yaml"

View file

@ -15,6 +15,7 @@ from behave import when
import keyring
import toml
import yaml
from yaml.loader import FullLoader, Loader
import jrnl.time
from jrnl import Journal
@ -129,7 +130,7 @@ def set_config(context, config_file):
# Add jrnl version to file for 2.x journals
with open(context.config_path, "a") as cf:
cf.write("version: {}".format(__version__))
@given('we use the password "{password}" if prompted')
def use_password_forever(context, password):
@ -393,7 +394,8 @@ def all_input_was_used(context):
@when('we run "{command}" and pipe "{text}"')
def run(context, command, text=""):
text = text or context.text or ""
with open(context.config_path) as f:
context.jrnl_config = yaml.load(f,Loader=yaml.FullLoader)
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)
@ -423,6 +425,7 @@ def run(context, command, text=""):
patch("sys.stdin.read", side_effect=lambda: text), \
patch("jrnl.time.parse", side_effect=_mock_time_parse(context)), \
patch("jrnl.config.get_config_path", side_effect=lambda: context.config_path), \
patch("jrnl.install.load_or_install_jrnl",return_value = context.jrnl_config), \
patch("jrnl.install.get_config_path", side_effect=lambda: context.config_path) \
:
context.editor = mock_editor

View file

@ -1,15 +1,47 @@
# Copyright (C) 2012-2021 jrnl contributors
# License: https://www.gnu.org/licenses/gpl-3.0.html
from jrnl import color
import json
import os
import shutil
import random
import string
from xml.etree import ElementTree
from colorama import Fore, Style
from behave import given
from behave import then
import colorama
def style_text(to_bold: bool, text_color: colorama.Fore, text_to_print: str):
"""Generate colorized and styled text for expected output. Its purpose is the same as Entry.colorize
:param to_bold: Flag whether the text should be bolded
:type to_bold: bool
:param text_color: Valid colorama.Fore color for the text
:type text_color: colorama.Fore
:param text_to_print: Message contents
:type text_to_print: str
:return: Styled and colored output
:rtype: str
"""
if to_bold:
text_style = Style.BRIGHT
else:
text_style = Style.NORMAL
text_color = getattr(colorama.Fore,text_color.upper(), None)
return text_style + text_color + text_to_print + Style.RESET_ALL
@then("the output should be pretty printed")
def check_export_pretty(context):
out = context.stdout_capture.getvalue()
lines = out.splitlines()
# As per the configuration,
expected_colorized_title = style_text(True, context.jrnl_config['colors']['date'].upper(), '2013-06-09 15:39') + ' ' + style_text(True, context.jrnl_config['colors']['title'].upper(), 'My first entry.')
assert lines[0] == expected_colorized_title
@then("the output should be parsable as json")