Source code for behave_kit.skip.conditions
"""Reusable skip conditions, usable standalone or via `skip.decorators`."""
from __future__ import annotations
import importlib
import logging
import platform
from behave_kit._core.types import Context
logger = logging.getLogger("behave_kit.skip.conditions")
[docs]
def is_env(context: Context, env_name: str) -> bool:
"""True if the configured environment name matches ``env_name``.
Reads ``context.kit_config.env`` when present (set by `load_env_config`);
falls back to ``context.config.env`` for setups that put the env name on
Behave's own configuration object.
"""
kit_config = getattr(context, "kit_config", None)
if kit_config is not None:
return getattr(kit_config, "env", None) == env_name
config = getattr(context, "config", None)
return getattr(config, "env", None) == env_name
[docs]
def is_os(os_name: str) -> bool:
"""True if the current OS matches ``os_name`` (case-insensitive)."""
if not isinstance(os_name, str):
logger.warning("is_os() expected a string, got %s", type(os_name).__name__)
return False
return platform.system().lower() == os_name.lower()
[docs]
def is_missing(module_name: str) -> bool:
"""True if ``module_name`` cannot be imported.
Invalid inputs (non-string or empty) are treated as missing to avoid
executing arbitrary code.
"""
if not isinstance(module_name, str) or not module_name:
return True
try:
importlib.import_module(module_name)
except Exception:
return True
return False
[docs]
def is_no_browser() -> bool:
"""True if Selenium is not installed."""
return is_missing("selenium")