From aa1dedfdcd97d3ab340d04b2cf9815e8de625be8 Mon Sep 17 00:00:00 2001 From: Yusuke Miyazaki Date: Sun, 12 Sep 2021 16:57:39 +0900 Subject: [PATCH] Start log line grouping as soon as possible --- src/tox_gh_actions/plugin.py | 50 ++++++++++++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 5 deletions(-) diff --git a/src/tox_gh_actions/plugin.py b/src/tox_gh_actions/plugin.py index 814defa..0d429bb 100644 --- a/src/tox_gh_actions/plugin.py +++ b/src/tox_gh_actions/plugin.py @@ -1,9 +1,11 @@ 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 @@ -11,6 +13,11 @@ 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): @@ -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 @@ -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"""