| 1 |
# -*- coding: UTF-8 -*- |
| 2 |
# FILE: features/environment.py |
| 3 |
# pylint: disable=unused-argument |
| 4 |
""" |
| 5 |
Functionality: |
| 6 |
|
| 7 |
* active tags |
| 8 |
""" |
| 9 |
|
| 10 |
from __future__ import print_function |
| 11 |
import sys |
| 12 |
import platform |
| 13 |
import os.path |
| 14 |
import six |
| 15 |
from behave.tag_matcher import ActiveTagMatcher |
| 16 |
from behave4cmd0.setup_command_shell import setup_command_shell_processors4behave |
| 17 |
# PREPARED: |
| 18 |
# from behave.tag_matcher import setup_active_tag_values |
| 19 |
|
| 20 |
|
| 21 |
def require_tool(tool_name): |
| 22 |
"""Check if a tool (an executable program) is provided on this platform. |
| 23 |
|
| 24 |
:params tool_name: Name of the tool to check if it is available. |
| 25 |
:return: True, if tool is found. |
| 26 |
:return: False, if tool is not available (or not in search path). |
| 27 |
""" |
| 28 |
# print("CHECK-TOOL: %s" % tool_name) |
| 29 |
path = os.environ.get("PATH") |
| 30 |
if not path: |
| 31 |
return False |
| 32 |
|
| 33 |
for searchdir in path.split(os.pathsep): |
| 34 |
executable1 = os.path.normpath(os.path.join(searchdir, tool_name)) |
| 35 |
executables = [executable1] |
| 36 |
if sys.platform.startswith("win"): |
| 37 |
executables.append(executable1 + ".exe") |
| 38 |
|
| 39 |
for executable in executables: |
| 40 |
# print("TOOL-CHECK: %s" % os.path.abspath(executable)) |
| 41 |
if os.path.isfile(executable): |
| 42 |
# print("TOOL-FOUND: %s" % os.path.abspath(executable)) |
| 43 |
return True |
| 44 |
# -- OTHERWISE: Tool not found |
| 45 |
# print("TOOL-NOT-FOUND: %s" % tool_name) |
| 46 |
return False |
| 47 |
|
| 48 |
def as_bool_string(value): |
| 49 |
if bool(value): |
| 50 |
return "yes" |
| 51 |
else: |
| 52 |
return "no" |
| 53 |
|
| 54 |
def discover_ci_server(): |
| 55 |
# pylint: disable=invalid-name |
| 56 |
ci_server = "none" |
| 57 |
CI = os.environ.get("CI", "false").lower() == "true" |
| 58 |
APPVEYOR = os.environ.get("APPVEYOR", "false").lower() == "true" |
| 59 |
TRAVIS = os.environ.get("TRAVIS", "false").lower() == "true" |
| 60 |
if CI: |
| 61 |
if APPVEYOR: |
| 62 |
ci_server = "appveyor" |
| 63 |
elif TRAVIS: |
| 64 |
ci_server = "travis" |
| 65 |
else: |
| 66 |
ci_server = "unknown" |
| 67 |
return ci_server |
| 68 |
|
| 69 |
|
| 70 |
# -- MATCHES ANY TAGS: @use.with_{category}={value} |
| 71 |
# NOTE: active_tag_value_provider provides category values for active tags. |
| 72 |
active_tag_value_provider = { |
| 73 |
"python2": str(six.PY2).lower(), |
| 74 |
"python3": str(six.PY3).lower(), |
| 75 |
# -- python.implementation: cpython, pypy, jython, ironpython |
| 76 |
"python.implementation": platform.python_implementation().lower(), |
| 77 |
"pypy": str("__pypy__" in sys.modules).lower(), |
| 78 |
"os": sys.platform, |
| 79 |
"xmllint": as_bool_string(require_tool("xmllint")), |
| 80 |
"ci": discover_ci_server() |
| 81 |
} |
| 82 |
active_tag_matcher = ActiveTagMatcher(active_tag_value_provider) |
| 83 |
|
| 84 |
def before_all(context): |
| 85 |
# -- SETUP ACTIVE-TAG MATCHER (with userdata): |
| 86 |
# USE: behave -D browser=safari ... |
| 87 |
# NOT-NEEDED: setup_active_tag_values(active_tag_value_provider, |
| 88 |
# context.config.userdata) |
| 89 |
setup_command_shell_processors4behave() |
| 90 |
|
| 91 |
def before_feature(context, feature): |
| 92 |
if active_tag_matcher.should_exclude_with(feature.tags): |
| 93 |
feature.skip(reason=active_tag_matcher.exclude_reason) |
| 94 |
|
| 95 |
def before_scenario(context, scenario): |
| 96 |
if active_tag_matcher.should_exclude_with(scenario.effective_tags): |
| 97 |
scenario.skip(reason=active_tag_matcher.exclude_reason) |