-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbehave.py
More file actions
150 lines (133 loc) · 4.88 KB
/
behave.py
File metadata and controls
150 lines (133 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# -*- mode: python; coding: utf-8 -*-
#
# Copyright (C) 2022 CONTACT Software GmbH
# https://www.contact-software.com
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Module implementing the behave plugin for spin"""
import contextlib
import sys
from typing import Generator, Iterable
from csspin import config, die, info, option, rmtree, setenv, sh, task, writetext
from csspin.tree import ConfigTree
from path import Path
defaults = config(
# Exclude the flaky tests in the defaults for now.
# Will switch the default back to True as soon as
# we have an easy way to set this in the CI.
flaky=False,
coverage=False,
cov_report="python-at-coverage.xml",
cov_config="setup.cfg",
# Default to concise and readable output
opts=[
"--no-source",
"--tags=~skip",
"--format=pretty",
"--no-skipped",
],
report=config(
name="cept_test_results.json",
format="json.pretty",
),
# This is the default location of behave tests
tests=["tests/accepttests"],
requires=config(
spin=[
"csspin_python.python",
],
python=[
"behave",
"coverage",
],
),
)
def configure(cfg: ConfigTree) -> None:
"""Add some runtime-dependent options"""
if sys.platform == "win32":
cfg.behave.opts.append("--tags=~linux")
else:
cfg.behave.opts.append("--tags=~windows")
def create_coverage_pth(cfg: ConfigTree) -> Path: # pylint: disable=unused-argument
"""Creating the coverage path file and returning its path"""
coverage_pth_path: Path = cfg.python.site_packages / "coverage.pth"
info(f"Create {coverage_pth_path}")
writetext(coverage_pth_path, "import coverage; coverage.process_startup()")
return coverage_pth_path
@contextlib.contextmanager
def with_coverage(cfg: ConfigTree) -> Generator[None, None, None]:
"""Context-manager enabling to run coverage"""
coverage_pth = ""
try:
sh("coverage", "erase", check=False)
setenv(COVERAGE_PROCESS_START=cfg.behave.cov_config)
coverage_pth = create_coverage_pth(cfg)
yield
finally:
setenv(COVERAGE_PROCESS_START=None)
rmtree(coverage_pth)
sh("coverage", "combine", check=False)
sh("coverage", "report", check=False)
sh("coverage", "xml", "-o", cfg.behave.cov_report, check=False)
@task(when="cept")
def behave( # pylint: disable=too-many-arguments,too-many-positional-arguments
cfg: ConfigTree,
instance: option( # type: ignore[valid-type]
"-i", # noqa: F821
"--instance", # noqa: F821
help="Directory of the CONTACT Elements instance.", # noqa: F722
),
coverage: option( # type: ignore[valid-type]
"-c", # noqa: F821
"--coverage", # noqa: F821
is_flag=True,
help="Run the tests while collecting coverage.", # noqa: F722
),
debug: option( # type: ignore[valid-type]
"--debug", is_flag=True, help="Start debug server." # noqa: F722,F821
),
with_test_report: option( # type: ignore[valid-type]
"--with-test-report", # noqa: F722
is_flag=True,
help="Create a test execution report.", # noqa: F722
),
args: Iterable[str],
) -> None:
"""Run Gherkin tests using behave."""
# pylint: disable=missing-function-docstring
coverage_enabled = coverage or cfg.behave.coverage
coverage_context = with_coverage if coverage_enabled else contextlib.nullcontext
opts = cfg.behave.opts
if not cfg.behave.flaky:
opts.append("--tags=~flaky")
if with_test_report and cfg.behave.report.name and cfg.behave.report.format:
opts = [
f"--format={cfg.behave.report.format}",
f"-o={cfg.behave.report.name}",
] + opts
if cfg.loaded.get("csspin_ce.mkinstance"):
inst = Path(instance or cfg.mkinstance.base.instance_location).absolute()
if not (inst).is_dir():
die(f"Cannot find the CE instance '{inst}'.")
setenv(CADDOK_BASE=inst)
cmd = ["powerscript"]
if debug:
cmd.append("--debugpy")
with coverage_context(cfg):
sh(*cmd, "-m", "behave", *opts, *args, *cfg.behave.tests)
else:
cmd = ["python"]
if debug:
cmd = ["debugpy"] + cfg.debugpy.opts
with coverage_context(cfg):
sh(*cmd, "-m", "behave", *opts, *args, *cfg.behave.tests)