(closer to) being able to run behave tests outside project root directory

This commit is contained in:
MinchinWeb 2021-05-16 19:19:23 -06:00
parent 96f1d77cdc
commit c990c8c9ad
4 changed files with 63 additions and 47 deletions

View file

@ -1,4 +1,5 @@
import os
from pathlib import Path
import shutil
from jrnl.os_compat import on_windows
@ -9,6 +10,7 @@ except ImportError:
testing_exporter = None
CWD = os.getcwd()
HERE = Path(__file__).resolve().parent
# @see https://behave.readthedocs.io/en/latest/tutorial.html#debug-on-error-in-case-of-step-failures
BEHAVE_DEBUG_ON_ERROR = False
@ -32,10 +34,10 @@ def before_all(context):
def clean_all_working_dirs():
if os.path.exists("test.txt"):
os.remove("test.txt")
if os.path.exists(HERE / "test.txt"):
os.remove(HERE / "test.txt")
for folder in ("configs", "journals", "cache"):
working_dir = os.path.join("features", folder)
working_dir = HERE / folder
if os.path.exists(working_dir):
shutil.rmtree(working_dir)
@ -65,14 +67,14 @@ def before_scenario(context, scenario):
# Clean up in case something went wrong
clean_all_working_dirs()
for folder in ("configs", "journals"):
original = os.path.join("features", "data", folder)
working_dir = os.path.join("features", folder)
original = HERE / "data" / folder
working_dir = HERE / folder
if not os.path.exists(working_dir):
os.mkdir(working_dir)
for filename in os.listdir(original):
source = os.path.join(original, filename)
source = original / filename
if os.path.isdir(source):
shutil.copytree(source, os.path.join(working_dir, filename))
shutil.copytree(source, (working_dir / filename))
else:
shutil.copy2(source, working_dir)

View file

@ -21,6 +21,9 @@ from jrnl import Journal
from jrnl import __version__
from jrnl import plugins
from jrnl.args import parse_args
from jrnl.behave_testing import _mock_getpass
from jrnl.behave_testing import _mock_input
from jrnl.behave_testing import _mock_time_parse
from jrnl.cli import cli
from jrnl.config import load_config
from jrnl.os_compat import split_args
@ -278,42 +281,6 @@ def extension_editor_file(context, suffix):
assert filename_suffix == suffix
def _mock_getpass(inputs):
def prompt_return(prompt=""):
if type(inputs) == str:
return inputs
try:
return next(inputs)
except StopIteration:
raise KeyboardInterrupt
return prompt_return
def _mock_input(inputs):
def prompt_return(prompt=""):
try:
val = next(inputs)
print(prompt, val)
return val
except StopIteration:
raise KeyboardInterrupt
return prompt_return
def _mock_time_parse(context):
original_parse = jrnl.time.parse
if "now" not in context:
return original_parse
def wrapper(input, *args, **kwargs):
input = context.now if input == "now" else input
return original_parse(input, *args, **kwargs)
return wrapper
@when('we run "{command}" and enter')
@when('we run "{command}" and enter nothing')
@when('we run "{command}" and enter "{inputs}"')

View file

@ -1,11 +1,11 @@
from jrnl.jrnl import run
from unittest import mock
# from __future__ import with_statement
from jrnl.args import parse_args
from behave import then
from features.steps.core import _mock_getpass, _mock_time_parse
from jrnl.args import parse_args
from jrnl.behave_testing import _mock_getpass
from jrnl.behave_testing import _mock_time_parse
from jrnl.jrnl import run
@then("the editor {editor} should have been called")

47
jrnl/behave_testing.py Normal file
View file

@ -0,0 +1,47 @@
# Copyright (C) 2012-2021 jrnl contributors
# License: https://www.gnu.org/licenses/gpl-3.0.html
"""
Certain functions to support the *behave* test suite.
They are placed here so they are importable in multiple places, as otherwise
imports fail when running the suite outside of the project's root folder.
"""
import jrnl.time
def _mock_getpass(inputs):
def prompt_return(prompt=""):
if type(inputs) == str:
return inputs
try:
return next(inputs)
except StopIteration:
raise KeyboardInterrupt
return prompt_return
def _mock_input(inputs):
def prompt_return(prompt=""):
try:
val = next(inputs)
print(prompt, val)
return val
except StopIteration:
raise KeyboardInterrupt
return prompt_return
def _mock_time_parse(context):
original_parse = jrnl.time.parse
if "now" not in context:
return original_parse
def wrapper(input, *args, **kwargs):
input = context.now if input == "now" else input
return original_parse(input, *args, **kwargs)
return wrapper