Skip to content

Commit

Permalink
Merge pull request #90 from ymyzk/better-grouping-start
Browse files Browse the repository at this point in the history
Start log line grouping earlier when possible
  • Loading branch information
ymyzk authored Sep 12, 2021
2 parents d7a86ae + 7665569 commit 59e01d6
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 5 deletions.
50 changes: 45 additions & 5 deletions src/tox_gh_actions/plugin.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
from itertools import product
import os
import sys
import threading
from typing import Any, Dict, Iterable, List

import pluggy
from tox.action import Action
from tox.config import Config, TestenvConfig, _split_env as split_env
from tox.reporter import verbosity1, verbosity2, warning
from tox.venv import VirtualEnv


hookimpl = pluggy.HookimplMarker("tox")

# Using thread local for just in case tox uses multiple threads for execution.
# tox seems to be using multiple processes at this point.
thread_locals = threading.local()
thread_locals.is_grouping_started = {}


@hookimpl
def tox_configure(config):
Expand Down Expand Up @@ -47,15 +54,25 @@ def tox_configure(config):
verbosity1("overriding envlist with: {}".format(envlist))


@hookimpl
def tox_testenv_create(venv, action):
# type: (VirtualEnv, Action) -> None
if is_running_on_actions():
start_grouping_if_necessary(venv)


@hookimpl
def tox_testenv_install_deps(venv, action):
# type: (VirtualEnv, Action) -> None
if is_running_on_actions():
start_grouping_if_necessary(venv)


@hookimpl
def tox_runtest_pre(venv):
# type: (VirtualEnv) -> None
if is_running_on_actions():
envconfig = venv.envconfig # type: TestenvConfig
message = envconfig.envname
if envconfig.description:
message += " - " + envconfig.description
print("::group::tox: " + message)
start_grouping_if_necessary(venv)


@hookimpl
Expand All @@ -65,6 +82,29 @@ def tox_runtest_post(venv):
print("::endgroup::")


def start_grouping_if_necessary(venv):
# type: (VirtualEnv) -> None
"""Start log line grouping when necessary.
This function can be called multiple times when running a test environment
and it ensures that "::group::" is written only once.
We shouldn't call this from tox_package and tox_get_python_executable hooks
because of the timing issue.
"""
envconfig = venv.envconfig # type: TestenvConfig
envname = envconfig.envname

if thread_locals.is_grouping_started.get(envname, False):
return
thread_locals.is_grouping_started[envname] = True

message = envname
if envconfig.description:
message += " - " + envconfig.description
print("::group::tox: " + message)


def parse_config(config):
# type: (Dict[str, Dict[str, str]]) -> Dict[str, Dict[str, Any]]
"""Parse gh-actions section in tox.ini"""
Expand Down
18 changes: 18 additions & 0 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@
from tox_gh_actions import plugin


def test_start_grouping_if_necessary(capsys, mocker):
envconfig = mocker.MagicMock()
envconfig.envname = "test123"
envconfig.description = "This is a test environment."
venv = mocker.MagicMock()
venv.envconfig = envconfig

# Start grouping in the first call
plugin.start_grouping_if_necessary(venv)
out1, _ = capsys.readouterr()
assert out1 == "::group::tox: test123 - This is a test environment.\n"

# Should not start groping again in the second call
plugin.start_grouping_if_necessary(venv)
out2, _ = capsys.readouterr()
assert out2 == ""


@pytest.mark.parametrize(
"config,expected",
[
Expand Down

0 comments on commit 59e01d6

Please sign in to comment.