Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #3318 - Suppress spinner in parallel runs in CI #3321

Merged
merged 3 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changelog/3318.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Suppress spinner in parallel runs in CI - by :user:`ziima`.
11 changes: 9 additions & 2 deletions src/tox/session/cmd/run/parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from tox.plugin import impl
from tox.session.env_select import CliEnv, register_env_select_flags
from tox.util.ci import is_ci
from tox.util.cpu import auto_detect_cpus

from .common import env_run_create_flags, execute
Expand All @@ -28,7 +29,7 @@ def tox_add_option(parser: ToxParser) -> None:
our = parser.add_command("run-parallel", ["p"], "run environments in parallel", run_parallel)
register_env_select_flags(our, default=CliEnv())
env_run_create_flags(our, mode="run-parallel")
parallel_flags(our, default_parallel=DEFAULT_PARALLEL)
parallel_flags(our, default_parallel=DEFAULT_PARALLEL, default_spinner=is_ci())


def parse_num_processes(str_value: str) -> int | None:
Expand All @@ -51,6 +52,8 @@ def parallel_flags(
our: ArgumentParser,
default_parallel: int | str,
no_args: bool = False, # noqa: FBT001, FBT002
*,
default_spinner: bool = False,
) -> None:
our.add_argument(
"-p",
Expand All @@ -75,7 +78,11 @@ def parallel_flags(
"--parallel-no-spinner",
action="store_true",
dest="parallel_no_spinner",
help="run tox environments in parallel, but don't show the spinner, implies --parallel",
default=default_spinner,
help=(
"run tox environments in parallel, but don't show the spinner, implies --parallel. "
"Disabled by default if CI is detected (not in legacy API)."
),
)


Expand Down
12 changes: 12 additions & 0 deletions tests/session/cmd/test_legacy.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from __future__ import annotations

import os
from typing import TYPE_CHECKING
from unittest.mock import patch

import pytest

Expand Down Expand Up @@ -121,6 +123,16 @@ def test_legacy_run_sequential(tox_project: ToxProjectCreator, mocker: MockerFix
assert run_sequential.call_count == 1


def test_legacy_run_sequential_ci(tox_project: ToxProjectCreator, mocker: MockerFixture) -> None:
"""Test legacy run sequential in CI by default."""
run_sequential = mocker.patch("tox.session.cmd.legacy.run_sequential")

with patch.dict(os.environ, {"CI": "1"}):
tox_project({"tox.ini": ""}).run("le", "-e", "py")

assert run_sequential.call_count == 1


def test_legacy_help(tox_project: ToxProjectCreator) -> None:
outcome = tox_project({"tox.ini": ""}).run("le", "-h")
outcome.assert_success()
Expand Down
14 changes: 14 additions & 0 deletions tests/session/cmd/test_parallel.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import os
import sys
from argparse import ArgumentTypeError
from signal import SIGINT
Expand Down Expand Up @@ -186,6 +187,19 @@ def test_parallel_no_spinner(tox_project: ToxProjectCreator) -> None:
)


def test_parallel_no_spinner_ci(tox_project: ToxProjectCreator) -> None:
"""Ensure spinner is disabled by default in CI."""
with mock.patch.object(parallel, "execute") as mocked, mock.patch.dict(os.environ, {"CI": "1"}):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use pytest-mock instead

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I had to rolled back. It seems pytest-mock has some issue with os.environ. Also unittest.mock is used to modify os.environ throughout tox tests.

Copy link
Member

@gaborbernat gaborbernat Aug 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I strongly urge you to reconsider. For patching the operating system environment variables, you should use the built-in pytest monkeypatch fixture. That's what I've been doing for all the other tests in this project as far as I can see and tell.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will not accept any pull request that doesn't use the already practiced tools and introduces a new way of doing the same thing.

tox_project({"tox.ini": ""}).run("p")

mocked.assert_called_once_with(
mock.ANY,
max_workers=None,
has_spinner=False,
live=False,
)


def test_parallel_no_spinner_legacy(tox_project: ToxProjectCreator) -> None:
with mock.patch.object(parallel, "execute") as mocked:
tox_project({"tox.ini": ""}).run("--parallel-no-spinner")
Expand Down
Loading