1
|
|
|
import os |
2
|
|
|
import shutil |
3
|
|
|
|
4
|
|
|
from jrnl.os_compat import on_windows |
5
|
|
|
|
6
|
|
|
CWD = os.getcwd() |
7
|
|
|
|
8
|
|
|
# @see https://behave.readthedocs.io/en/latest/tutorial.html#debug-on-error-in-case-of-step-failures |
9
|
|
|
BEHAVE_DEBUG_ON_ERROR = False |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
def setup_debug_on_error(userdata): |
13
|
|
|
global BEHAVE_DEBUG_ON_ERROR |
14
|
|
|
BEHAVE_DEBUG_ON_ERROR = userdata.getbool("BEHAVE_DEBUG_ON_ERROR") |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
def before_all(context): |
18
|
|
|
setup_debug_on_error(context.config.userdata) |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
# def after_step(context, step): |
22
|
|
|
# if BEHAVE_DEBUG_ON_ERROR and step.status == "failed": |
23
|
|
|
# -- ENTER DEBUGGER: Zoom in on failure location. |
24
|
|
|
# NOTE: Use IPython debugger, same for pdb (basic python debugger). |
25
|
|
|
# import ipdb |
26
|
|
|
# ipdb.post_mortem(step.exc_traceback) |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
def clean_all_working_dirs(): |
30
|
|
|
if os.path.exists("test.txt"): |
31
|
|
|
os.remove("test.txt") |
32
|
|
|
for folder in ("configs", "journals", "cache"): |
33
|
|
|
working_dir = os.path.join("features", folder) |
34
|
|
|
if os.path.exists(working_dir): |
35
|
|
|
shutil.rmtree(working_dir) |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
def before_feature(context, feature): |
39
|
|
|
# add "skip" tag |
40
|
|
|
# https://stackoverflow.com/a/42721605/4276230 |
41
|
|
|
if "skip" in feature.tags: |
42
|
|
|
feature.skip() |
43
|
|
|
return |
44
|
|
|
|
45
|
|
|
if "skip_win" in feature.tags and on_windows: |
46
|
|
|
feature.skip("Skipping on Windows") |
47
|
|
|
return |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
def before_scenario(context, scenario): |
51
|
|
|
"""Before each scenario, backup all config and journal test data.""" |
52
|
|
|
# Clean up in case something went wrong |
53
|
|
|
clean_all_working_dirs() |
54
|
|
|
for folder in ("configs", "journals"): |
55
|
|
|
original = os.path.join("features", "data", folder) |
56
|
|
|
working_dir = os.path.join("features", folder) |
57
|
|
|
if not os.path.exists(working_dir): |
58
|
|
|
os.mkdir(working_dir) |
59
|
|
|
for filename in os.listdir(original): |
60
|
|
|
source = os.path.join(original, filename) |
61
|
|
|
if os.path.isdir(source): |
62
|
|
|
shutil.copytree(source, os.path.join(working_dir, filename)) |
63
|
|
|
else: |
64
|
|
|
shutil.copy2(source, working_dir) |
65
|
|
|
|
66
|
|
|
# add "skip" tag |
67
|
|
|
# https://stackoverflow.com/a/42721605/4276230 |
68
|
|
|
if "skip" in scenario.effective_tags: |
69
|
|
|
scenario.skip() |
70
|
|
|
return |
71
|
|
|
|
72
|
|
|
if "skip_win" in scenario.effective_tags and on_windows: |
73
|
|
|
scenario.skip("Skipping on Windows") |
74
|
|
|
return |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
def after_scenario(context, scenario): |
78
|
|
|
"""After each scenario, restore all test data and remove working_dirs.""" |
79
|
|
|
if os.getcwd() != CWD: |
80
|
|
|
os.chdir(CWD) |
81
|
|
|
|
82
|
|
|
# only clean up if debugging is off and the scenario passed |
83
|
|
|
if BEHAVE_DEBUG_ON_ERROR and scenario.status != "failed": |
84
|
|
|
clean_all_working_dirs() |
85
|
|
|
else: |
86
|
|
|
clean_all_working_dirs() |
87
|
|
|
|