Testing-Builder refactor: move print_message to tty/log.py

This commit is contained in:
Tamara Dahlgren 2024-11-14 17:10:44 -08:00
parent ac0ed2c4cc
commit de3fb78477
No known key found for this signature in database
GPG Key ID: 1DFB48B82B5BB5C4
2 changed files with 35 additions and 29 deletions

View File

@ -21,7 +21,7 @@
from multiprocessing.connection import Connection from multiprocessing.connection import Connection
from threading import Thread from threading import Thread
from types import ModuleType from types import ModuleType
from typing import Callable, Optional from typing import Callable, Optional, Union
import llnl.util.tty as tty import llnl.util.tty as tty
@ -1022,3 +1022,22 @@ def wrapped(*args, **kwargs):
def _input_available(f): def _input_available(f):
return f in select.select([f], [], [], 0)[0] return f in select.select([f], [], [], 0)[0]
LogType = Union[nixlog, winlog]
def print_message(logger: LogType, msg: str, verbose: bool = False):
"""Print the message to the log, optionally echoing.
Args:
logger: instance of the output logger (e.g. nixlog or winlog)
msg: message being output
verbose: ``True`` displays verbose output, ``False`` suppresses
it (``False`` is default)
"""
if verbose:
with logger.force_echo():
tty.info(msg, format="g")
else:
tty.info(msg, format="g")

View File

@ -17,7 +17,7 @@
import llnl.util.filesystem as fs import llnl.util.filesystem as fs
import llnl.util.tty as tty import llnl.util.tty as tty
import llnl.util.tty.log import llnl.util.tty.log as log
from llnl.string import plural from llnl.string import plural
from llnl.util.lang import nullcontext from llnl.util.lang import nullcontext
from llnl.util.tty.color import colorize from llnl.util.tty.color import colorize
@ -50,7 +50,6 @@
ListOrStringType = Union[str, List[str]] ListOrStringType = Union[str, List[str]]
LogType = Union[llnl.util.tty.log.nixlog, llnl.util.tty.log.winlog]
Pb = TypeVar("Pb", bound="spack.package_base.PackageBase") Pb = TypeVar("Pb", bound="spack.package_base.PackageBase")
PackageObjectOrClass = Union[Pb, Type[Pb]] PackageObjectOrClass = Union[Pb, Type[Pb]]
@ -207,22 +206,6 @@ def install_test_root(pkg: Pb):
return os.path.join(pkg.metadata_dir, "test") return os.path.join(pkg.metadata_dir, "test")
def print_message(logger: LogType, msg: str, verbose: bool = False):
"""Print the message to the log, optionally echoing.
Args:
logger: instance of the output logger (e.g. nixlog or winlog)
msg: message being output
verbose: ``True`` displays verbose output, ``False`` suppresses
it (``False`` is default)
"""
if verbose:
with logger.force_echo():
tty.info(msg, format="g")
else:
tty.info(msg, format="g")
def overall_status(current_status: "TestStatus", substatuses: List["TestStatus"]) -> "TestStatus": def overall_status(current_status: "TestStatus", substatuses: List["TestStatus"]) -> "TestStatus":
"""Determine the overall status based on the current and associated sub status values. """Determine the overall status based on the current and associated sub status values.
@ -285,10 +268,10 @@ def __init__(self, pkg: Pb):
self._logger = None self._logger = None
@property @property
def logger(self) -> Optional[LogType]: def logger(self) -> Optional[log.LogType]:
"""The current logger or, if none, sets to one.""" """The current logger or, if none, sets to one."""
if not self._logger: if not self._logger:
self._logger = llnl.util.tty.log.log_output(self.test_log_file) self._logger = log.log_output(self.test_log_file)
return self._logger return self._logger
@ -305,7 +288,7 @@ def test_logger(self, verbose: bool = False, externals: bool = False):
fs.touch(self.test_log_file) # Otherwise log_parse complains fs.touch(self.test_log_file) # Otherwise log_parse complains
fs.set_install_permissions(self.test_log_file) fs.set_install_permissions(self.test_log_file)
with llnl.util.tty.log.log_output(self.test_log_file, verbose) as self._logger: with log.log_output(self.test_log_file, verbose) as self._logger:
with self.logger.force_echo(): # type: ignore[union-attr] with self.logger.force_echo(): # type: ignore[union-attr]
tty.msg("Testing package " + colorize(r"@*g{" + self.pkg_id + r"}")) tty.msg("Testing package " + colorize(r"@*g{" + self.pkg_id + r"}"))
@ -365,7 +348,7 @@ def phase_tests(self, builder, phase_name: str, method_names: List[str]):
with self.test_logger(verbose=verbose, externals=False) as logger: with self.test_logger(verbose=verbose, externals=False) as logger:
# Report running each of the methods in the build log # Report running each of the methods in the build log
print_message(logger, f"Running {phase_name}-time tests", verbose) log.print_message(logger, f"Running {phase_name}-time tests", verbose)
builder.pkg.test_suite.current_test_spec = builder.pkg.spec builder.pkg.test_suite.current_test_spec = builder.pkg.spec
builder.pkg.test_suite.current_base_spec = builder.pkg.spec builder.pkg.test_suite.current_base_spec = builder.pkg.spec
@ -381,20 +364,20 @@ def phase_tests(self, builder, phase_name: str, method_names: List[str]):
fn = getattr(builder.pkg, name, getattr(builder, name)) fn = getattr(builder.pkg, name, getattr(builder, name))
msg = f"RUN-TESTS: {phase_name}-time tests [{name}]" msg = f"RUN-TESTS: {phase_name}-time tests [{name}]"
print_message(logger, msg, verbose) log.print_message(logger, msg, verbose)
fn() fn()
except AttributeError as e: except AttributeError as e:
msg = f"RUN-TESTS: method not implemented [{name}]" msg = f"RUN-TESTS: method not implemented [{name}]"
print_message(logger, msg, verbose) log.print_message(logger, msg, verbose)
self.add_failure(e, msg) self.add_failure(e, msg)
if fail_fast: if fail_fast:
break break
if have_tests: if have_tests:
print_message(logger, "Completed testing", verbose) log.print_message(logger, "Completed testing", verbose)
# Raise any collected failures here # Raise any collected failures here
if self.test_failures: if self.test_failures:
@ -729,12 +712,12 @@ def test_process(pkg: Pb, kwargs):
with pkg.tester.test_logger(verbose, externals) as logger: with pkg.tester.test_logger(verbose, externals) as logger:
if pkg.spec.external and not externals: if pkg.spec.external and not externals:
print_message(logger, "Skipped tests for external package", verbose) log.print_message(logger, "Skipped tests for external package", verbose)
pkg.tester.status(pkg.spec.name, TestStatus.SKIPPED) pkg.tester.status(pkg.spec.name, TestStatus.SKIPPED)
return return
if not pkg.spec.installed: if not pkg.spec.installed:
print_message(logger, "Skipped not installed package", verbose) log.print_message(logger, "Skipped not installed package", verbose)
pkg.tester.status(pkg.spec.name, TestStatus.SKIPPED) pkg.tester.status(pkg.spec.name, TestStatus.SKIPPED)
return return
@ -883,6 +866,9 @@ def content_hash(self):
self._hash = b32_hash self._hash = b32_hash
return self._hash return self._hash
def _run_test(self, pkg, dirty, externals):
pkg.do_test(dirty=dirty, externals=externals)
def __call__(self, *args, **kwargs): def __call__(self, *args, **kwargs):
self.write_reproducibility_data() self.write_reproducibility_data()
@ -912,7 +898,8 @@ def __call__(self, *args, **kwargs):
fs.mkdirp(test_dir) fs.mkdirp(test_dir)
# run the package tests # run the package tests
spec.package.do_test(dirty=dirty, externals=externals) # TLD spec.package.do_test(dirty=dirty, externals=externals)
self._run_test(spec.package, dirty=dirty, externals=externals)
# Clean up on success # Clean up on success
if remove_directory: if remove_directory: