Skip to content

Commit 8826494

Browse files
authored
chore(bigquery): add dynamic logging banner to nox mypy session (#17492)
This PR adds a dynamic logging banner `Finished session for <package>` to nox `mypy` sessions in `bigquery` to simplify troubleshooting. Currently when a nox session runs, there may be many lines of text between a reference to which package is being tested and the result summary line. This can make it more difficult to determine which package is affected without a lot of manual scrolling (every time you need to trouble shoot an issue). This is especially true if you use `Ctrl+F` to zoom over to summaries with words like `failed`. ``` change detected in packages/bigframes/ [many lines of text...] 2547 passed, 157 skipped, 9 xfailed, 1 xpassed, 586 warnings in 136.68s (0:02:16) nox > Session unit-3.11(test_extra=True) failed in 3 minutes. ``` The PR adds a banner line right before the nox result summary: ``` change detected in packages/bigframes/ [many lines of text...] 2547 passed, 157 skipped, 9 xfailed, 1 xpassed, 586 warnings in 136.68s (0:02:16) nox > Finished session for bigframes # NEW LINE, right before the summary line nox > Session unit-3.11(test_extra=True) failed in 3 minutes. ``` > [!note] > There is no convenient way to inject a log line into the nox session and have it print **exactly** where you want. Also, because the long term intent is to add this to all nox sessions I included an easy to apply `contextmanager` to ensure the correct placement of the new logging banner. > [!note] > This PR only adds this capability to a single nox session (`mypy`) and a single handwritten package (`bigquery`). I did not want to invest a lot of time unless the team feels it is beneficial. In which case, we can add a task the next sprint to make this a global change.
1 parent 6febabf commit 8826494

1 file changed

Lines changed: 23 additions & 1 deletion

File tree

packages/google-cloud-bigquery/noxfile.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414

1515
from __future__ import absolute_import
1616

17+
import contextlib
1718
from functools import wraps
1819
import os
1920
import pathlib
21+
from typing import Generator
2022
import re
2123
import shutil
2224
import time
@@ -80,6 +82,25 @@ def wrapper(*args, **kwargs):
8082
]
8183

8284

85+
@contextlib.contextmanager
86+
def log_package_context(session: nox.Session) -> Generator[None, None, None]:
87+
"""Logs a highly visible package context banner right before a session exits.
88+
89+
Ensures metadata is printed adjacent to Nox's final status log,
90+
even if the session fails or raises an exception.
91+
"""
92+
# Dynamically extract current folder name (e.g., 'google-cloud-bigquery')
93+
package_name = CURRENT_DIRECTORY.name
94+
95+
try:
96+
# Hands control back to the session code block
97+
yield
98+
finally:
99+
# This executes AFTER test output finishes, immediately above Nox's summary line
100+
banner_text = f"Finished session for {package_name.lower()}"
101+
session.log(banner_text)
102+
103+
83104
def default(session, install_extras=True):
84105
"""Default unit test session.
85106
@@ -193,7 +214,8 @@ def mypy(session):
193214
"types-setuptools",
194215
)
195216
session.run("python", "-m", "pip", "freeze")
196-
session.run("mypy", "-p", "google", "--show-traceback")
217+
with log_package_context(session):
218+
session.run("mypy", "-p", "google", "--show-traceback")
197219

198220

199221
@nox.session(python=SYSTEM_TEST_PYTHON_VERSIONS)

0 commit comments

Comments
 (0)