File size: 23,127 Bytes
71687cf | 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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 | # Copyright (C) 2012 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause
"""Collection of helper functions used in conda tests."""
from __future__ import annotations
import os
import subprocess
import sys
from contextlib import contextmanager
from functools import cache
from os.path import abspath, dirname, join
from pathlib import Path
from tempfile import gettempdir, mkdtemp
from unittest.mock import patch
from uuid import uuid4
import pytest
from ..base.constants import REPODATA_FN
from ..base.context import context
from ..common.io import captured as common_io_captured
from ..common.serialize import json
from ..core.prefix_data import PrefixData
from ..core.subdir_data import SubdirData
from ..gateways.disk.delete import rm_rf
from ..gateways.disk.read import lexists
from ..history import History
from ..models.channel import Channel
from ..models.records import PackageRecord, PrefixRecord
from ..resolve import Resolve
# The default value will only work if we have installed conda in development mode!
TEST_DATA_DIR = os.environ.get(
"CONDA_TEST_DATA_DIR", abspath(join(dirname(__file__), "..", "..", "tests", "data"))
)
CHANNEL_DIR = CHANNEL_DIR_V1 = abspath(join(TEST_DATA_DIR, "conda_format_repo"))
CHANNEL_DIR_V2 = abspath(join(TEST_DATA_DIR, "base_url_channel"))
EXPORTED_CHANNELS_DIR = mkdtemp(suffix="-test-conda-channels")
expected_error_prefix = "Using Anaconda Cloud api site https://api.anaconda.org"
def strip_expected(stderr):
if expected_error_prefix and stderr.startswith(expected_error_prefix):
stderr = stderr[len(expected_error_prefix) :].lstrip()
return stderr
def raises(exception, func, string=None):
try:
a = func()
except exception as e:
if string:
assert string in e.args[0]
print(e)
return True
raise Exception(f"did not raise, gave {a}")
@contextmanager
def captured(disallow_stderr=True):
# same as common.io.captured but raises Exception if unexpected output was written to stderr
try:
with common_io_captured() as c:
yield c
finally:
c.stderr = strip_expected(c.stderr)
if disallow_stderr and c.stderr:
raise Exception(f"Got stderr output: {c.stderr}")
def assert_equals(a, b, output=""):
output = f"{a.lower()!r} != {b.lower()!r}" + "\n\n" + output
assert a.lower() == b.lower(), output
def assert_not_in(a, b, output=""):
assert a.lower() not in b.lower(), (
f"{output} {a.lower()!r} should not be found in {b.lower()!r}"
)
def assert_in(a, b, output=""):
assert a.lower() in b.lower(), (
f"{output} {a.lower()!r} cannot be found in {b.lower()!r}"
)
def add_subdir(dist_string):
channel_str, package_str = dist_string.split("::")
channel_str = channel_str + "/" + context.subdir
return "::".join([channel_str, package_str])
def add_subdir_to_iter(iterable):
if isinstance(iterable, dict):
return {add_subdir(k): v for k, v in iterable.items()}
elif isinstance(iterable, list):
return list(map(add_subdir, iterable))
elif isinstance(iterable, set):
return set(map(add_subdir, iterable))
elif isinstance(iterable, tuple):
return tuple(map(add_subdir, iterable))
else:
raise Exception("Unable to add subdir to object of unknown type.")
@contextmanager
def tempdir():
tempdirdir = gettempdir()
dirname = str(uuid4())[:8]
prefix = join(tempdirdir, dirname)
try:
os.makedirs(prefix)
yield prefix
finally:
if lexists(prefix):
rm_rf(prefix)
def supplement_index_with_repodata(index, repodata, channel, priority):
repodata_info = repodata["info"]
arch = repodata_info.get("arch")
platform = repodata_info.get("platform")
subdir = repodata_info.get("subdir")
if not subdir:
subdir = "{}-{}".format(repodata_info["platform"], repodata_info["arch"])
auth = channel.auth
for fn, info in repodata["packages"].items():
rec = PackageRecord.from_objects(
info,
fn=fn,
arch=arch,
platform=platform,
channel=channel,
subdir=subdir,
# schannel=schannel,
priority=priority,
# url=join_url(channel_url, fn),
auth=auth,
)
index[rec] = rec
def add_feature_records_legacy(index):
all_features = set()
for rec in index.values():
if rec.track_features:
all_features.update(rec.track_features)
for feature_name in all_features:
rec = PackageRecord.feature(feature_name)
index[rec] = rec
def _export_subdir_data_to_repodata(subdir_data: SubdirData):
"""
This function is only temporary and meant to patch wrong / undesirable
testing behaviour. It should end up being replaced with the new class-based,
backend-agnostic solver tests.
"""
state = subdir_data._internal_state
subdir = subdir_data.channel.subdir
packages = {}
packages_conda = {}
for pkg in subdir_data.iter_records():
if pkg.timestamp:
# ensure timestamp is dumped as int in milliseconds
# (pkg.timestamp is a kept as a float in seconds)
pkg.__fields__["timestamp"]._in_dump = True
data = pkg.dump()
if subdir == "noarch" and getattr(pkg, "noarch", None):
data["subdir"] = "noarch"
data["platform"] = data["arch"] = None
if "features" in data:
# Features are deprecated, so they are not implemented
# in modern solvers like mamba. Mamba does implement
# track_features minimization, so we are exposing the
# features as track_features, which seems to make the
# tests pass
data["track_features"] = data["features"]
del data["features"]
if pkg.fn.endswith(".conda"):
packages_conda[pkg.fn] = data
else:
packages[pkg.fn] = data
return {
"_cache_control": state["_cache_control"],
"_etag": state["_etag"],
"_mod": state["_mod"],
"_url": state["_url"],
"_add_pip": state["_add_pip"],
"info": {
"subdir": subdir,
},
"packages": packages,
"packages.conda": packages_conda,
}
def _sync_channel_to_disk(subdir_data: SubdirData):
"""
This function is only temporary and meant to patch wrong / undesirable
testing behaviour. It should end up being replaced with the new class-based,
backend-agnostic solver tests.
"""
base = Path(EXPORTED_CHANNELS_DIR) / subdir_data.channel.name
subdir_path = base / subdir_data.channel.subdir
subdir_path.mkdir(parents=True, exist_ok=True)
with open(subdir_path / "repodata.json", "w") as f:
json.dump(
_export_subdir_data_to_repodata(subdir_data), f, indent=2, sort_keys=True
)
f.flush()
os.fsync(f.fileno())
def _alias_canonical_channel_name_cache_to_file_prefixed(name, subdir_data=None):
"""
This function is only temporary and meant to patch wrong / undesirable
testing behaviour. It should end up being replaced with the new class-based,
backend-agnostic solver tests.
"""
# export repodata state to disk for other solvers to test
if subdir_data is None:
cache_key = Channel(name).url(with_credentials=True), "repodata.json"
subdir_data = SubdirData._cache_.get(cache_key)
if subdir_data:
local_proxy_channel = Channel(f"{EXPORTED_CHANNELS_DIR}/{name}")
SubdirData._cache_[
(local_proxy_channel.url(with_credentials=True), "repodata.json")
] = subdir_data
def _patch_for_local_exports(name, subdir_data):
"""
This function is only temporary and meant to patch wrong / undesirable
testing behaviour. It should end up being replaced with the new class-based,
backend-agnostic solver tests.
"""
_alias_canonical_channel_name_cache_to_file_prefixed(name, subdir_data)
# we need to override the modification time here so the
# cache hits this subdir_data object from the local copy too
# - without this, the legacy solver will use the local dump too
# and there's no need for that extra work
# (check conda.core.subdir_data.SubdirDataType.__call__ for
# details)
_sync_channel_to_disk(subdir_data)
subdir_data._mtime = float("inf")
def _get_index_r_base(
json_filename_or_packages,
channel_name,
subdir=context.subdir,
add_pip=False,
merge_noarch=False,
):
if isinstance(json_filename_or_packages, (str, os.PathLike)):
with open(join(TEST_DATA_DIR, json_filename_or_packages)) as fi:
all_packages = json.load(fi)
elif isinstance(json_filename_or_packages, dict):
all_packages = json_filename_or_packages
else:
raise ValueError("'json_filename_or_data' must be path-like or dict")
packages = {subdir: {}, "noarch": {}}
if merge_noarch:
packages[subdir] = all_packages
else:
for key, pkg in all_packages.items():
if pkg.get("subdir") == "noarch" or pkg.get("noarch"):
packages["noarch"][key] = pkg
else:
packages[subdir][key] = pkg
subdir_datas = []
channels = []
for subchannel, subchannel_pkgs in packages.items():
repodata = {
"info": {
"subdir": subchannel,
"arch": context.arch_name,
"platform": context.platform,
},
"packages": subchannel_pkgs,
}
channel = Channel(f"https://conda.anaconda.org/{channel_name}/{subchannel}")
channels.append(channel)
sd = SubdirData(channel)
subdir_datas.append(sd)
with context._override("add_pip_as_python_dependency", add_pip):
sd._process_raw_repodata_str(json.dumps(repodata))
sd._loaded = True
SubdirData._cache_[(channel.url(with_credentials=True), REPODATA_FN)] = sd
_patch_for_local_exports(channel_name, sd)
# this is for the classic solver only, which is fine with a single collapsed index
index = {}
for sd in subdir_datas:
index.update({prec: prec for prec in sd.iter_records()})
r = Resolve(index, channels=channels)
return index, r
# this fixture appears to introduce a test-order dependency if cached
def get_index_r_1(subdir=context.subdir, add_pip=True, merge_noarch=False):
return _get_index_r_base(
"index.json",
"channel-1",
subdir=subdir,
add_pip=add_pip,
merge_noarch=merge_noarch,
)
@cache
def get_index_r_2(subdir=context.subdir, add_pip=True, merge_noarch=False):
return _get_index_r_base(
"index2.json",
"channel-2",
subdir=subdir,
add_pip=add_pip,
merge_noarch=merge_noarch,
)
@cache
def get_index_r_4(subdir=context.subdir, add_pip=True, merge_noarch=False):
return _get_index_r_base(
"index4.json",
"channel-4",
subdir=subdir,
add_pip=add_pip,
merge_noarch=merge_noarch,
)
@cache
def get_index_r_5(subdir=context.subdir, add_pip=False, merge_noarch=False):
return _get_index_r_base(
"index5.json",
"channel-5",
subdir=subdir,
add_pip=add_pip,
merge_noarch=merge_noarch,
)
@cache
def get_index_must_unfreeze(subdir=context.subdir, add_pip=True, merge_noarch=False):
repodata = {
"foobar-1.0-0.tar.bz2": {
"build": "0",
"build_number": 0,
"depends": ["libbar 2.0.*", "libfoo 1.0.*"],
"md5": "11ec1194bcc56b9a53c127142a272772",
"name": "foobar",
"timestamp": 1562861325613,
"version": "1.0",
},
"foobar-2.0-0.tar.bz2": {
"build": "0",
"build_number": 0,
"depends": ["libbar 2.0.*", "libfoo 2.0.*"],
"md5": "f8eb5a7fa1ff6dead4e360631a6cd048",
"name": "foobar",
"version": "2.0",
},
"libbar-1.0-0.tar.bz2": {
"build": "0",
"build_number": 0,
"depends": [],
"md5": "f51f4d48a541b7105b5e343704114f0f",
"name": "libbar",
"timestamp": 1562858881022,
"version": "1.0",
},
"libbar-2.0-0.tar.bz2": {
"build": "0",
"build_number": 0,
"depends": [],
"md5": "27f4e717ed263f909074f64d9cbf935d",
"name": "libbar",
"timestamp": 1562858881748,
"version": "2.0",
},
"libfoo-1.0-0.tar.bz2": {
"build": "0",
"build_number": 0,
"depends": [],
"md5": "ad7c088566ffe2389958daedf8ff312c",
"name": "libfoo",
"timestamp": 1562858763881,
"version": "1.0",
},
"libfoo-2.0-0.tar.bz2": {
"build": "0",
"build_number": 0,
"depends": [],
"md5": "daf7af7086d8f22be49ae11bdc41f332",
"name": "libfoo",
"timestamp": 1562858836924,
"version": "2.0",
},
"qux-1.0-0.tar.bz2": {
"build": "0",
"build_number": 0,
"depends": ["libbar 2.0.*", "libfoo 1.0.*"],
"md5": "18604cbe4f789fe853232eef4babd4f9",
"name": "qux",
"timestamp": 1562861393808,
"version": "1.0",
},
"qux-2.0-0.tar.bz2": {
"build": "0",
"build_number": 0,
"depends": ["libbar 1.0.*", "libfoo 2.0.*"],
"md5": "892aa4b9ec64b67045a46866ef1ea488",
"name": "qux",
"timestamp": 1562861394828,
"version": "2.0",
},
}
_get_index_r_base(
repodata,
"channel-freeze",
subdir=subdir,
add_pip=add_pip,
merge_noarch=merge_noarch,
)
# Do not memoize this get_index to allow different CUDA versions to be detected
def get_index_cuda(subdir=context.subdir, add_pip=True, merge_noarch=False):
return _get_index_r_base(
"index.json",
"channel-1",
subdir=subdir,
add_pip=add_pip,
merge_noarch=merge_noarch,
)
def record(
name="a",
version="1.0",
depends=None,
build="0",
build_number=0,
timestamp=0,
channel=None,
**kwargs,
):
return PackageRecord(
name=name,
version=version,
depends=depends or [],
build=build,
build_number=build_number,
timestamp=timestamp,
channel=channel,
**kwargs,
)
def _get_solver_base(
channel_id,
tmpdir,
specs_to_add=(),
specs_to_remove=(),
prefix_records=(),
history_specs=(),
add_pip=False,
merge_noarch=False,
):
tmpdir = tmpdir.strpath
pd = PrefixData(tmpdir)
pd._PrefixData__prefix_records = {
rec.name: PrefixRecord.from_objects(rec) for rec in prefix_records
}
spec_map = {spec.name: spec for spec in history_specs}
if channel_id == "channel-1":
get_index_r_1(context.subdir, add_pip, merge_noarch)
_alias_canonical_channel_name_cache_to_file_prefixed("channel-1")
channels = (Channel(f"{EXPORTED_CHANNELS_DIR}/channel-1"),)
elif channel_id == "channel-2":
get_index_r_2(context.subdir, add_pip, merge_noarch)
_alias_canonical_channel_name_cache_to_file_prefixed("channel-2")
channels = (Channel(f"{EXPORTED_CHANNELS_DIR}/channel-2"),)
elif channel_id == "channel-4":
get_index_r_4(context.subdir, add_pip, merge_noarch)
_alias_canonical_channel_name_cache_to_file_prefixed("channel-4")
channels = (Channel(f"{EXPORTED_CHANNELS_DIR}/channel-4"),)
elif channel_id == "channel-5":
get_index_r_5(context.subdir, add_pip, merge_noarch)
_alias_canonical_channel_name_cache_to_file_prefixed("channel-5")
channels = (Channel(f"{EXPORTED_CHANNELS_DIR}/channel-5"),)
elif channel_id == "aggregate-1":
get_index_r_2(context.subdir, add_pip, merge_noarch)
get_index_r_4(context.subdir, add_pip, merge_noarch)
_alias_canonical_channel_name_cache_to_file_prefixed("channel-2")
_alias_canonical_channel_name_cache_to_file_prefixed("channel-4")
channels = (
Channel(f"{EXPORTED_CHANNELS_DIR}/channel-2"),
Channel(f"{EXPORTED_CHANNELS_DIR}/channel-4"),
)
elif channel_id == "aggregate-2":
get_index_r_2(context.subdir, add_pip, merge_noarch)
get_index_r_4(context.subdir, add_pip, merge_noarch)
_alias_canonical_channel_name_cache_to_file_prefixed("channel-4")
_alias_canonical_channel_name_cache_to_file_prefixed("channel-2")
# This is the only difference with aggregate-1: the priority
channels = (
Channel(f"{EXPORTED_CHANNELS_DIR}/channel-4"),
Channel(f"{EXPORTED_CHANNELS_DIR}/channel-2"),
)
elif channel_id == "must-unfreeze":
get_index_must_unfreeze(context.subdir, add_pip, merge_noarch)
_alias_canonical_channel_name_cache_to_file_prefixed("channel-freeze")
channels = (Channel(f"{EXPORTED_CHANNELS_DIR}/channel-freeze"),)
elif channel_id == "cuda":
get_index_cuda(context.subdir, add_pip, merge_noarch)
_alias_canonical_channel_name_cache_to_file_prefixed("channel-1")
channels = (Channel(f"{EXPORTED_CHANNELS_DIR}/channel-1"),)
subdirs = (context.subdir,) if merge_noarch else (context.subdir, "noarch")
with context._override("add_pip_as_python_dependency", add_pip):
with patch.object(History, "get_requested_specs_map", return_value=spec_map):
# We need CONDA_ADD_PIP_AS_PYTHON_DEPENDENCY=false here again (it's also in
# get_index_r_*) to cover solver logics that need to load from disk instead of
# hitting the SubdirData cache
yield context.plugin_manager.get_solver_backend()(
tmpdir,
channels,
subdirs,
specs_to_add=specs_to_add,
specs_to_remove=specs_to_remove,
)
@contextmanager
def get_solver(
tmpdir,
specs_to_add=(),
specs_to_remove=(),
prefix_records=(),
history_specs=(),
add_pip=False,
merge_noarch=False,
):
yield from _get_solver_base(
"channel-1",
tmpdir,
specs_to_add=specs_to_add,
specs_to_remove=specs_to_remove,
prefix_records=prefix_records,
history_specs=history_specs,
add_pip=add_pip,
merge_noarch=merge_noarch,
)
@contextmanager
def get_solver_2(
tmpdir,
specs_to_add=(),
specs_to_remove=(),
prefix_records=(),
history_specs=(),
add_pip=False,
merge_noarch=False,
):
yield from _get_solver_base(
"channel-2",
tmpdir,
specs_to_add=specs_to_add,
specs_to_remove=specs_to_remove,
prefix_records=prefix_records,
history_specs=history_specs,
add_pip=add_pip,
merge_noarch=merge_noarch,
)
@contextmanager
def get_solver_4(
tmpdir,
specs_to_add=(),
specs_to_remove=(),
prefix_records=(),
history_specs=(),
add_pip=False,
merge_noarch=False,
):
yield from _get_solver_base(
"channel-4",
tmpdir,
specs_to_add=specs_to_add,
specs_to_remove=specs_to_remove,
prefix_records=prefix_records,
history_specs=history_specs,
add_pip=add_pip,
merge_noarch=merge_noarch,
)
@contextmanager
def get_solver_5(
tmpdir,
specs_to_add=(),
specs_to_remove=(),
prefix_records=(),
history_specs=(),
add_pip=False,
merge_noarch=False,
):
yield from _get_solver_base(
"channel-5",
tmpdir,
specs_to_add=specs_to_add,
specs_to_remove=specs_to_remove,
prefix_records=prefix_records,
history_specs=history_specs,
add_pip=add_pip,
merge_noarch=merge_noarch,
)
@contextmanager
def get_solver_aggregate_1(
tmpdir,
specs_to_add=(),
specs_to_remove=(),
prefix_records=(),
history_specs=(),
add_pip=False,
merge_noarch=False,
):
yield from _get_solver_base(
"aggregate-1",
tmpdir,
specs_to_add=specs_to_add,
specs_to_remove=specs_to_remove,
prefix_records=prefix_records,
history_specs=history_specs,
add_pip=add_pip,
merge_noarch=merge_noarch,
)
@contextmanager
def get_solver_aggregate_2(
tmpdir,
specs_to_add=(),
specs_to_remove=(),
prefix_records=(),
history_specs=(),
add_pip=False,
merge_noarch=False,
):
yield from _get_solver_base(
"aggregate-2",
tmpdir,
specs_to_add=specs_to_add,
specs_to_remove=specs_to_remove,
prefix_records=prefix_records,
history_specs=history_specs,
add_pip=add_pip,
merge_noarch=merge_noarch,
)
@contextmanager
def get_solver_must_unfreeze(
tmpdir,
specs_to_add=(),
specs_to_remove=(),
prefix_records=(),
history_specs=(),
add_pip=False,
merge_noarch=False,
):
yield from _get_solver_base(
"must-unfreeze",
tmpdir,
specs_to_add=specs_to_add,
specs_to_remove=specs_to_remove,
prefix_records=prefix_records,
history_specs=history_specs,
add_pip=add_pip,
merge_noarch=merge_noarch,
)
@contextmanager
def get_solver_cuda(
tmpdir,
specs_to_add=(),
specs_to_remove=(),
prefix_records=(),
history_specs=(),
add_pip=False,
merge_noarch=False,
):
yield from _get_solver_base(
"cuda",
tmpdir,
specs_to_add=specs_to_add,
specs_to_remove=specs_to_remove,
prefix_records=prefix_records,
history_specs=history_specs,
add_pip=add_pip,
merge_noarch=merge_noarch,
)
def convert_to_dist_str(solution):
return tuple(prec.dist_str(canonical_name=False) for prec in solution)
@pytest.fixture()
def solver_class():
return context.plugin_manager.get_solver_backend()
def in_subprocess():
return bool(os.getenv("_RERUN_IN_SUBPROCESS"))
def forward_to_subprocess(
request, *cli_args, **subprocess_kwargs
) -> subprocess.CompletedProcess | None:
if in_subprocess():
return
args = cli_args or (
"--no-header",
"--disable-warnings",
"--color=no",
"-vvv",
)
env = os.environ.copy()
env["_RERUN_IN_SUBPROCESS"] = "1"
env.update(subprocess_kwargs.pop("env", {}))
return subprocess.run(
[
sys.executable,
"-m",
"pytest",
*args,
f"{request.node.path}::{request.node.name}",
],
check=subprocess_kwargs.pop("check", True),
env=env,
**subprocess_kwargs,
)
|