Skip to content

Commit

Permalink
Start log line grouping as soon as possible
Browse files Browse the repository at this point in the history
  • Loading branch information
ymyzk committed Sep 12, 2021
1 parent d7a86ae commit aa1dedf
Showing 1 changed file with 45 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

0 comments on commit aa1dedf

Please sign in to comment.