GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Merge pull request #569 from AlexanderRichert-NOAA/test_log_fix_oct25 · JCSDA/spack@af7df61

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib/spack/spack
    • install_test.py
    • llnl/util/tty
      • log.py
    • test
      • cmd
        • install.py
      • llnl/util/tty
        • log.py
  • var/spack/test_repos/spack_repo/builtin_mock/packages/printing_package
    • package.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/spack/spack/install_test.py‎

Copy file name to clipboardExpand all lines: lib/spack/spack/install_test.py
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,9 @@ def test_logger(self, verbose: bool = False, externals: bool = False):
300300
fs.touch(self.test_log_file) # Otherwise log_parse complains
301301
fs.set_install_permissions(self.test_log_file)
302302

303-
with spack.llnl.util.tty.log.log_output(self.test_log_file, verbose) as self._logger:
303+
with spack.llnl.util.tty.log.log_output(
304+
self.test_log_file, verbose, append=True
305+
) as self._logger:
304306
with self.logger.force_echo(): # type: ignore[union-attr]
305307
tty.msg("Testing package " + colorize(r"@*g{" + self.pkg_id + r"}"))
306308

Collapse file

‎lib/spack/spack/llnl/util/tty/log.py‎

Copy file name to clipboardExpand all lines: lib/spack/spack/llnl/util/tty/log.py
+21-6Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -336,13 +336,14 @@ class FileWrapper:
336336
object.
337337
"""
338338

339-
def __init__(self, file_like):
339+
def __init__(self, file_like, append=False):
340340
# This records whether the file-like object returned by "unwrap" is
341341
# purely in-memory. In that case a subprocess will need to explicitly
342342
# transmit the contents to the parent.
343343
self.write_in_parent = False
344344

345345
self.file_like = file_like
346+
self.append = append
346347

347348
if isinstance(file_like, str):
348349
self.open = True
@@ -358,7 +359,8 @@ def __init__(self, file_like):
358359
def unwrap(self):
359360
if self.open:
360361
if self.file_like:
361-
self.file = open(self.file_like, "w", encoding="utf-8")
362+
mode = "a" if self.append else "w"
363+
self.file = open(self.file_like, mode, encoding="utf-8")
362364
else:
363365
self.file = io.StringIO()
364366
return self.file
@@ -424,7 +426,14 @@ class nixlog:
424426
"""
425427

426428
def __init__(
427-
self, file_like=None, echo=False, debug=0, buffer=False, env=None, filter_fn=None
429+
self,
430+
file_like=None,
431+
echo=False,
432+
debug=0,
433+
buffer=False,
434+
env=None,
435+
filter_fn=None,
436+
append=False,
428437
):
429438
"""Create a new output log context manager.
430439
@@ -437,6 +446,7 @@ def __init__(
437446
this doesn't set up any *new* buffering
438447
filter_fn (callable, optional): Callable[str] -> str to filter each
439448
line of output
449+
append (bool): whether to append to file ('a' mode)
440450
441451
log_output can take either a file object or a filename. If a
442452
filename is passed, the file will be opened and closed entirely
@@ -456,6 +466,7 @@ def __init__(
456466
self.debug = debug
457467
self.buffer = buffer
458468
self.filter_fn = filter_fn
469+
self.append = append
459470

460471
self._active = False # used to prevent re-entry
461472

@@ -496,7 +507,7 @@ def __enter__(self):
496507
raise RuntimeError("file argument must be set by either __init__ or __call__")
497508

498509
# set up a stream for the daemon to write to
499-
self.log_file = FileWrapper(self.file_like)
510+
self.log_file = FileWrapper(self.file_like, append=self.append)
500511

501512
# record parent color settings before redirecting. We do this
502513
# because color output depends on whether the *original* stdout
@@ -735,7 +746,9 @@ class winlog:
735746
Does not support the use of 'v' toggling as nixlog does.
736747
"""
737748

738-
def __init__(self, file_like=None, echo=False, debug=0, buffer=False, filter_fn=None):
749+
def __init__(
750+
self, file_like=None, echo=False, debug=0, buffer=False, filter_fn=None, append=False
751+
):
739752
self.debug = debug
740753
self.echo = echo
741754
self.logfile = file_like
@@ -745,6 +758,7 @@ def __init__(self, file_like=None, echo=False, debug=0, buffer=False, filter_fn=
745758
self._ioflag = False
746759
self.old_stdout = sys.stdout
747760
self.old_stderr = sys.stderr
761+
self.append = append
748762

749763
def __enter__(self):
750764
if self._active:
@@ -760,7 +774,8 @@ def __enter__(self):
760774
sys.stdout = self.logfile
761775
sys.stderr = self.logfile
762776
else:
763-
self.writer = open(self.logfile, mode="wb+")
777+
write_mode = "ab+" if self.append else "wb+"
778+
self.writer = open(self.logfile, mode=write_mode)
764779
self.reader = open(self.logfile, mode="rb+")
765780

766781
# Dup stdout so we can still write to it after redirection
Collapse file

‎lib/spack/spack/test/cmd/install.py‎

Copy file name to clipboardExpand all lines: lib/spack/spack/test/cmd/install.py
+12-1Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def test_package_output(capsys, install_mockery, mock_fetch):
141141
# when nested AND in pytest
142142
spec = spack.concretize.concretize_one("printing-package")
143143
pkg = spec.package
144-
PackageInstaller([pkg], explicit=True, verbose=True).install()
144+
PackageInstaller([pkg], explicit=True, verbose=True, tests=sys.platform != "win32").install()
145145

146146
with gzip.open(pkg.install_log_path, "rt") as f:
147147
out = f.read()
@@ -151,6 +151,17 @@ def test_package_output(capsys, install_mockery, mock_fetch):
151151
assert "BEFORE INSTALL" in out
152152
assert "AFTER INSTALL" in out
153153

154+
if not sys.platform == "win32":
155+
# Check that install-time test log contains check and installcheck output
156+
log_path = pkg.tester.archived_install_test_log
157+
assert os.path.exists(log_path), f"Missing install-time test log at {log_path}"
158+
159+
with open(log_path, "r", encoding="utf-8") as f:
160+
test_log_contents = f.read()
161+
162+
assert "PRINTING PACKAGE CHECK" in test_log_contents
163+
assert "PRINTING PACKAGE INSTALLCHECK" in test_log_contents
164+
154165

155166
@pytest.mark.disable_clean_stage_check
156167
def test_install_output_on_build_error(
Collapse file

‎lib/spack/spack/test/llnl/util/tty/log.py‎

Copy file name to clipboardExpand all lines: lib/spack/spack/test/llnl/util/tty/log.py
+17Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,23 @@ def test_log_output_with_filter(capfd, tmp_path: pathlib.Path):
133133
assert capfd.readouterr()[0] == "bar blah\nblah bar\nbar bar\n"
134134

135135

136+
def test_log_output_with_filter_and_append(capfd, tmp_path: pathlib.Path):
137+
with working_dir(str(tmp_path)):
138+
with log.log_output("foo.txt", filter_fn=_log_filter_fn):
139+
print("foo blah")
140+
print("blah foo")
141+
print("foo foo")
142+
143+
with open("foo.txt", encoding="utf-8") as f:
144+
assert f.read() == "foo blah\nblah foo\nfoo foo\n"
145+
146+
with log.log_output("foo.txt", filter_fn=_log_filter_fn, append=True):
147+
print("more foo more blah")
148+
149+
with open("foo.txt", encoding="utf-8") as f:
150+
assert f.read() == "foo blah\nblah foo\nfoo foo\nmore foo more blah\n"
151+
152+
136153
@pytest.mark.skipif(not which("echo"), reason="needs echo command")
137154
def test_log_subproc_and_echo_output_no_capfd(capfd, tmp_path: pathlib.Path):
138155
echo = which("echo", required=True)
Collapse file

‎var/spack/test_repos/spack_repo/builtin_mock/packages/printing_package/package.py‎

Copy file name to clipboardExpand all lines: var/spack/test_repos/spack_repo/builtin_mock/packages/printing_package/package.py
+10-2Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
import os
66

7-
from spack_repo.builtin_mock.build_systems.generic import Package
7+
from spack_repo.builtin_mock.build_systems.makefile import MakefilePackage
88

99
from spack.package import *
1010

1111

12-
class PrintingPackage(Package):
12+
class PrintingPackage(MakefilePackage):
1313
"""This package prints some output from its install method.
1414
1515
We use this to test whether that output is properly logged.
@@ -28,6 +28,14 @@ def install(self, spec, prefix):
2828

2929
print("AFTER INSTALL")
3030

31+
def check(self):
32+
"""Run build-time tests."""
33+
print("PRINTING PACKAGE CHECK")
34+
35+
def installcheck(self):
36+
"""Run install-time tests."""
37+
print("PRINTING PACKAGE INSTALLCHECK")
38+
3139
def test_print(self):
3240
"""Test print example."""
3341

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.