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:
Jonathan Wren 2021-02-13 17:43:42 -08:00
parent 1390493a29
commit 530190df7a

View file

@ -4,8 +4,9 @@
import shutil import shutil
import os import os
import re import re
import tempfile
import pytest from pytest import fixture
from pytest_bdd import given from pytest_bdd import given
from pytest_bdd import then from pytest_bdd import then
from pytest_bdd import when from pytest_bdd import when
@ -16,52 +17,35 @@ from jrnl import __version__
from jrnl.os_compat import split_args from jrnl.os_compat import split_args
from jrnl.cli import cli from jrnl.cli import cli
WORKING_DIR = ''
TEMP_DIR = ''
# ----- FIXTURES ----- # # ----- FIXTURES ----- #
@pytest.fixture @fixture
def cli_run(): 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): @fixture
CWD = request.config.rootpath def working_dir(request):
return os.path.join(CWD, 'tests', '.current_test'), CWD return os.path.join(request.config.rootpath, "tests")
# ----- 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)
# ----- STEPS ----- # # ----- STEPS ----- #
@given(parse('we use the config "{config_file}"'), target_fixture="config_path") @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 # Copy the config file over
config_source = os.path.join(WORKING_DIR, 'features', 'data', 'configs', config_file) config_source = os.path.join(
config_dest = os.path.join(TEMP_DIR, config_file) working_dir, "features", "data", "configs", config_file
)
config_dest = os.path.join(temp_dir.name, config_file)
shutil.copy2(config_source, config_dest) shutil.copy2(config_source, config_dest)
# @todo make this only copy some journals over # @todo make this only copy some journals over
# Copy all of the journals over # Copy all of the journals over
journal_source = os.path.join(WORKING_DIR, 'features', 'data', 'journals') journal_source = os.path.join(working_dir, "features", "data", "journals")
journal_dest = os.path.join(TEMP_DIR, 'features', 'journals') journal_dest = os.path.join(temp_dir.name, "features", "journals")
shutil.copytree(journal_source, journal_dest, dirs_exist_ok=True) shutil.copytree(journal_source, journal_dest)
# @todo get rid of this by using default config values # @todo get rid of this by using default config values
# merge in version number # merge in version number
@ -91,21 +75,19 @@ def run(command, config_path, cli_run, capsys):
status = e.code status = e.code
# fmt: on # fmt: on
cli_run['status'] = status cli_run["status"] = status
captured = capsys.readouterr() captured = capsys.readouterr()
cli_run['stdout'] = captured.out cli_run["stdout"] = captured.out
cli_run['stderr'] = captured.err cli_run["stderr"] = captured.err
@then("we should get no error") @then("we should get no error")
def no_error(cli_run): 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}"')) @then(parse('the output should match "{regex}"'))
def matches_std_output(regex, cli_run): def matches_std_output(regex, cli_run):
out = cli_run['stdout'] out = cli_run["stdout"]
matches = re.findall(regex, out) matches = re.findall(regex, out)
assert ( assert matches, f"\nRegex didn't match:\n{regex}\n{str(out)}\n{str(matches)}"
matches
), f"\nRegex didn't match:\n{regex}\n{str(out)}\n{str(matches)}"