mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-10 08:38:32 +02:00
Implement handling of temp and working dirs
Pytest-bdd uses fixtures to handle most things. This makes a fixture that will create a temp directory for the tests to run in. Co-authored-by: Micah Jerome Ellison <micah.jerome.ellison@gmail.com>
This commit is contained in:
parent
1390493a29
commit
530190df7a
1 changed files with 24 additions and 42 deletions
|
@ -4,8 +4,9 @@
|
|||
import shutil
|
||||
import os
|
||||
import re
|
||||
import tempfile
|
||||
|
||||
import pytest
|
||||
from pytest import fixture
|
||||
from pytest_bdd import given
|
||||
from pytest_bdd import then
|
||||
from pytest_bdd import when
|
||||
|
@ -16,52 +17,35 @@ from jrnl import __version__
|
|||
from jrnl.os_compat import split_args
|
||||
from jrnl.cli import cli
|
||||
|
||||
WORKING_DIR = ''
|
||||
TEMP_DIR = ''
|
||||
|
||||
# ----- FIXTURES ----- #
|
||||
@pytest.fixture
|
||||
@fixture
|
||||
def cli_run():
|
||||
return dict(status=0, stdout=None, stderr=None)
|
||||
return {"status": 0, "stdout": None, "stderr": None}
|
||||
|
||||
@fixture
|
||||
def temp_dir():
|
||||
return tempfile.TemporaryDirectory()
|
||||
|
||||
def get_working_dirs(request):
|
||||
CWD = request.config.rootpath
|
||||
return os.path.join(CWD, 'tests', '.current_test'), CWD
|
||||
|
||||
|
||||
# ----- HOOKS ----- #
|
||||
def pytest_bdd_before_scenario(request, feature, scenario):
|
||||
"""Before each scenario, backup all config and journal test data."""
|
||||
global TEMP_DIR
|
||||
global WORKING_DIR
|
||||
TEMP_DIR, WORKING_DIR = get_working_dirs(request)
|
||||
|
||||
if not os.path.exists(TEMP_DIR):
|
||||
os.mkdir(TEMP_DIR)
|
||||
|
||||
|
||||
def pytest_bdd_after_scenario(request, feature, scenario):
|
||||
"""After each scenario, restore all test data and remove working_dirs."""
|
||||
TEMP_DIR, WORKING_DIR = get_working_dirs(request)
|
||||
|
||||
if os.path.exists(TEMP_DIR):
|
||||
shutil.rmtree(TEMP_DIR)
|
||||
@fixture
|
||||
def working_dir(request):
|
||||
return os.path.join(request.config.rootpath, "tests")
|
||||
|
||||
|
||||
# ----- STEPS ----- #
|
||||
@given(parse('we use the config "{config_file}"'), target_fixture="config_path")
|
||||
def set_config(config_file):
|
||||
def set_config(config_file, temp_dir, working_dir):
|
||||
# Copy the config file over
|
||||
config_source = os.path.join(WORKING_DIR, 'features', 'data', 'configs', config_file)
|
||||
config_dest = os.path.join(TEMP_DIR, config_file)
|
||||
config_source = os.path.join(
|
||||
working_dir, "features", "data", "configs", config_file
|
||||
)
|
||||
config_dest = os.path.join(temp_dir.name, config_file)
|
||||
shutil.copy2(config_source, config_dest)
|
||||
|
||||
# @todo make this only copy some journals over
|
||||
# Copy all of the journals over
|
||||
journal_source = os.path.join(WORKING_DIR, 'features', 'data', 'journals')
|
||||
journal_dest = os.path.join(TEMP_DIR, 'features', 'journals')
|
||||
shutil.copytree(journal_source, journal_dest, dirs_exist_ok=True)
|
||||
journal_source = os.path.join(working_dir, "features", "data", "journals")
|
||||
journal_dest = os.path.join(temp_dir.name, "features", "journals")
|
||||
shutil.copytree(journal_source, journal_dest)
|
||||
|
||||
# @todo get rid of this by using default config values
|
||||
# merge in version number
|
||||
|
@ -91,21 +75,19 @@ def run(command, config_path, cli_run, capsys):
|
|||
status = e.code
|
||||
# fmt: on
|
||||
|
||||
cli_run['status'] = status
|
||||
cli_run["status"] = status
|
||||
captured = capsys.readouterr()
|
||||
cli_run['stdout'] = captured.out
|
||||
cli_run['stderr'] = captured.err
|
||||
cli_run["stdout"] = captured.out
|
||||
cli_run["stderr"] = captured.err
|
||||
|
||||
|
||||
@then("we should get no error")
|
||||
def no_error(cli_run):
|
||||
assert cli_run['status'] == 0, cli_run['status']
|
||||
assert cli_run["status"] == 0, cli_run["status"]
|
||||
|
||||
|
||||
@then(parse('the output should match "{regex}"'))
|
||||
def matches_std_output(regex, cli_run):
|
||||
out = cli_run['stdout']
|
||||
out = cli_run["stdout"]
|
||||
matches = re.findall(regex, out)
|
||||
assert (
|
||||
matches
|
||||
), f"\nRegex didn't match:\n{regex}\n{str(out)}\n{str(matches)}"
|
||||
assert matches, f"\nRegex didn't match:\n{regex}\n{str(out)}\n{str(matches)}"
|
||||
|
|
Loading…
Add table
Reference in a new issue