| 1 |
"""pytest test parametrization for VeSync devices.""" |
| 2 |
import os |
| 3 |
import sys |
| 4 |
import pytest |
| 5 |
|
| 6 |
|
| 7 |
def pytest_addoption(parser): |
| 8 |
"""Prevent new API's from being written during pipeline tests.""" |
| 9 |
parser.addoption( |
| 10 |
"--write_api", |
| 11 |
action="store_true", |
| 12 |
default=False, |
| 13 |
help="run tests without writing API to yaml", |
| 14 |
) |
| 15 |
parser.addoption( |
| 16 |
"--overwrite", |
| 17 |
action="store_true", |
| 18 |
default=False, |
| 19 |
help="overwrite existing API in yaml - WARNING do not use unless absolutely necessary", |
| 20 |
) |
| 21 |
|
| 22 |
|
| 23 |
def pytest_generate_tests(metafunc): |
| 24 |
"""Generate tests for device methods. |
| 25 |
|
| 26 |
Excludes legacy tests that start with 'test_x'. |
| 27 |
""" |
| 28 |
if metafunc.cls is None or 'test_x' in metafunc.module.__name__: |
| 29 |
return |
| 30 |
write_api = bool(metafunc.config.getoption('--write_api')) |
| 31 |
|
| 32 |
overwrite = bool(metafunc.config.getoption('--overwrite')) |
| 33 |
|
| 34 |
metafunc.cls.overwrite = overwrite |
| 35 |
metafunc.cls.write_api = write_api |
| 36 |
|
| 37 |
|
| 38 |
if 'device' in metafunc.cls.__dict__: |
| 39 |
device = metafunc.cls.__dict__['device'] |
| 40 |
if device not in metafunc.cls.__dict__: |
| 41 |
return |
| 42 |
devices = metafunc.cls.__dict__[device] |
| 43 |
|
| 44 |
if metafunc.function.__name__ == 'test_details': |
| 45 |
return details_generator(metafunc, device, devices) |
| 46 |
|
| 47 |
if metafunc.function.__name__ == 'test_methods': |
| 48 |
return method_generator(metafunc, device, devices) |
| 49 |
|
| 50 |
|
| 51 |
def details_generator(metafunc, gen_type, devices): |
| 52 |
"""Parametrize device tests for get_details().""" |
| 53 |
id_list = [] |
| 54 |
argvalues = [] |
| 55 |
for setup_entry in devices: |
| 56 |
id_list.append(f"{gen_type}.{setup_entry}.update") |
| 57 |
argvalues.append([setup_entry, 'update']) |
| 58 |
metafunc.parametrize("setup_entry, method", argvalues, ids=id_list) |
| 59 |
|
| 60 |
|
| 61 |
def method_generator(metafunc, gen_type, devices): |
| 62 |
"""Parametrize device tests for methods.""" |
| 63 |
if 'base_methods' in metafunc.cls.__dict__: |
| 64 |
base_methods = metafunc.cls.__dict__['base_methods'] |
| 65 |
else: |
| 66 |
base_methods = [] |
| 67 |
call_dict = {dt: base_methods.copy() for dt in devices} |
| 68 |
if 'device_methods' in metafunc.cls.__dict__: |
| 69 |
dev_methods = metafunc.cls.__dict__['device_methods'] |
| 70 |
for dev in call_dict: |
| 71 |
if dev in dev_methods: |
| 72 |
call_dict[dev].extend(dev_methods[dev]) |
| 73 |
id_list = [] |
| 74 |
argvalues = [] |
| 75 |
for dev, methods in call_dict.items(): |
| 76 |
for method in methods: |
| 77 |
id_list.append(f'{gen_type}.{dev}.{method}') |
| 78 |
argvalues.append([dev, method]) |
| 79 |
metafunc.parametrize("setup_entry, method", argvalues, ids=id_list) |
| 80 |
return |
| 81 |
|
| 82 |
|
| 83 |
def _is_interactive(config: pytest.Config) -> bool: |
| 84 |
"""Best-effort check for an interactive terminal.""" |
| 85 |
tr = config.pluginmanager.getplugin("terminalreporter") |
| 86 |
if tr and getattr(tr, "isatty", False): |
| 87 |
return True |
| 88 |
for stream in (sys.__stdin__, sys.__stdout__, sys.__stderr__): |
| 89 |
try: |
| 90 |
if stream and stream.isatty(): |
| 91 |
return True |
| 92 |
except Exception: |
| 93 |
pass |
| 94 |
return False |
| 95 |
|
| 96 |
|
| 97 |
def _prompt_confirm(config: pytest.Config) -> bool: |
| 98 |
"""Suspend capture, show a prompt, and return True if user confirmed.""" |
| 99 |
flags = [] |
| 100 |
if config.getoption("--write_api"): |
| 101 |
flags.append("--write_api") |
| 102 |
if config.getoption("--overwrite"): |
| 103 |
flags.append("--overwrite") |
| 104 |
|
| 105 |
if len(flags) == 0: |
| 106 |
return True |
| 107 |
|
| 108 |
if '--overwrite' in flags and '--write_api' not in flags: |
| 109 |
|
| 110 |
pytest.exit( |
| 111 |
"\nBoth --overwrite and --write_api need to be set to overwrite API calls.", 1 |
| 112 |
) |
| 113 |
|
| 114 |
|
| 115 |
if "--write_api" in flags and "--overwrite" not in flags: |
| 116 |
msg = ( |
| 117 |
"\nAre you sure you want to write API requests to files?\n" |
| 118 |
"Continue? [y/N]: " |
| 119 |
) |
| 120 |
elif "--overwrite" in flags and "--write_api" in flags: |
| 121 |
msg = ( |
| 122 |
"\nAre you sure you want to OVERWRITE existing API requests?\n" |
| 123 |
"Continue? [y/N]: " |
| 124 |
) |
| 125 |
else: |
| 126 |
pytest.exit("\nUnrecognized combination of flags: " + ", ".join(flags), 1) |
| 127 |
|
| 128 |
if not flags: |
| 129 |
return True |
| 130 |
capman = config.pluginmanager.getplugin("capturemanager") |
| 131 |
tr = config.pluginmanager.getplugin("terminalreporter") |
| 132 |
if capman: |
| 133 |
|
| 134 |
capman.suspend_global_capture(in_=True) |
| 135 |
try: |
| 136 |
if tr and getattr(tr, "isatty", False): |
| 137 |
tr._tw.write(msg) |
| 138 |
else: |
| 139 |
print(msg, end="", flush=True) |
| 140 |
try: |
| 141 |
resp = input().strip().lower() |
| 142 |
except EOFError: |
| 143 |
resp = "" |
| 144 |
return resp in {"y", "yes"} |
| 145 |
finally: |
| 146 |
if capman: |
| 147 |
capman.resume_global_capture() |
| 148 |
|
| 149 |
|
| 150 |
def _require_confirmation(config: pytest.Config) -> None: |
| 151 |
"""Prompt once per session if any destructive flags are set.""" |
| 152 |
if not config.getoption("--write_api") and not config.getoption("--overwrite"): |
| 153 |
return |
| 154 |
|
| 155 |
|
| 156 |
if str(os.environ.get("PYTEST_CONFIRM", "")).lower() in {"1", "true", "yes", "y"}: |
| 157 |
return |
| 158 |
|
| 159 |
|
| 160 |
if not _is_interactive(config): |
| 161 |
raise pytest.UsageError( |
| 162 |
"write_api require confirmation, but the session is non-interactive. " |
| 163 |
"Re-run with environment variable set PYTEST_CONFIRM=1." |
| 164 |
) |
| 165 |
|
| 166 |
if not _prompt_confirm(config): |
| 167 |
pytest.exit("Aborted by user.", returncode=1) |
| 168 |
|
| 169 |
|
| 170 |
def pytest_sessionstart(session: pytest.Session) -> None: |
| 171 |
|
| 172 |
_require_confirmation(session.config) |