| 401 | |
| 402 | |
| 403 | def _run_with_coverage(session, *test_cmd, env=None, on_rerun=False): |
| 404 | _install_coverage_requirement(session) |
| 405 | if on_rerun is False: |
| 406 | session.run("coverage", "erase") |
| 407 | |
| 408 | if env is None: |
| 409 | env = {} |
| 410 | |
| 411 | sitecustomize_dir = session.run( |
| 412 | "salt-factories", "--coverage", silent=True, log=True, stderr=None |
| 413 | ) |
| 414 | if sitecustomize_dir is not None: |
| 415 | sitecustomize_dir = pathlib.Path(sitecustomize_dir.strip()).resolve() |
| 416 | if not sitecustomize_dir.exists(): |
| 417 | session.error( |
| 418 | f"The path to 'sitecustomize.py', '{str(sitecustomize_dir)}', does not exist." |
| 419 | ) |
| 420 | |
| 421 | if sitecustomize_dir: |
| 422 | try: |
| 423 | relative_sitecustomize_dir = sitecustomize_dir.relative_to(REPO_ROOT) |
| 424 | except ValueError: |
| 425 | relative_sitecustomize_dir = sitecustomize_dir |
| 426 | log_msg = f"Discovered salt-factories coverage 'sitecustomize.py' path: {relative_sitecustomize_dir}" |
| 427 | try: |
| 428 | session.debug(log_msg) |
| 429 | except AttributeError: |
| 430 | # Older nox |
| 431 | session.log(log_msg) |
| 432 | python_path_env_var = os.environ.get("PYTHONPATH") or None |
| 433 | if python_path_env_var is None: |
| 434 | python_path_env_var = str(sitecustomize_dir) |
| 435 | else: |
| 436 | python_path_entries = python_path_env_var.split(os.pathsep) |
| 437 | if str(sitecustomize_dir) in python_path_entries: |
| 438 | python_path_entries.remove(str(sitecustomize_dir)) |
| 439 | python_path_entries.insert(0, str(sitecustomize_dir)) |
| 440 | python_path_env_var = os.pathsep.join(python_path_entries) |
| 441 | |
| 442 | env.update( |
| 443 | { |
| 444 | # The updated python path so that sitecustomize is importable |
| 445 | "PYTHONPATH": python_path_env_var, |
| 446 | # Instruct sub processes to also run under coverage |
| 447 | "COVERAGE_PROCESS_START": str(REPO_ROOT / ".coveragerc"), |
| 448 | # The full path to the .coverage data file. Makes sure we always write |
| 449 | # them to the same directory |
| 450 | "COVERAGE_FILE": COVERAGE_FILE, |
| 451 | } |
| 452 | ) |
| 453 | |
| 454 | session.run(*test_cmd, env=env) |
| 455 | |
| 456 | |
| 457 | def _report_coverage( |