GeezerVison is showing github.com — Merge pull request #569 from AlexanderRichert-NOAA/test_log_fix_oct25 · JCSDA/spack@af7df61
File treeExpand file treeCollapse file tree Open diff view settings var/spack/test_repos/spack_repo/builtin_mock/packages/printing_package
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/spack/spack/install_test.py +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -300,7 +300,9 @@ def test_logger(self, verbose: bool = False, externals: bool = False): | 300 | 300 | fs.touch(self.test_log_file) # Otherwise log_parse complains | 301 | 301 | fs.set_install_permissions(self.test_log_file) | 302 | 302 |
| 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: | 304 | 306 | with self.logger.force_echo(): # type: ignore[union-attr] | 305 | 307 | tty.msg("Testing package " + colorize(r"@*g{" + self.pkg_id + r"}")) | 306 | 308 |
| |
Collapse file 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 number | Diff line number | Diff line change |
|---|
@@ -336,13 +336,14 @@ class FileWrapper: | 336 | 336 | object. | 337 | 337 | """ | 338 | 338 |
| 339 | | - def __init__(self, file_like): | | 339 | + def __init__(self, file_like, append=False): | 340 | 340 | # This records whether the file-like object returned by "unwrap" is | 341 | 341 | # purely in-memory. In that case a subprocess will need to explicitly | 342 | 342 | # transmit the contents to the parent. | 343 | 343 | self.write_in_parent = False | 344 | 344 |
| 345 | 345 | self.file_like = file_like | | 346 | + self.append = append | 346 | 347 |
| 347 | 348 | if isinstance(file_like, str): | 348 | 349 | self.open = True | @@ -358,7 +359,8 @@ def __init__(self, file_like): | 358 | 359 | def unwrap(self): | 359 | 360 | if self.open: | 360 | 361 | 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") | 362 | 364 | else: | 363 | 365 | self.file = io.StringIO() | 364 | 366 | return self.file | @@ -424,7 +426,14 @@ class nixlog: | 424 | 426 | """ | 425 | 427 |
| 426 | 428 | 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, | 428 | 437 | ): | 429 | 438 | """Create a new output log context manager. | 430 | 439 |
| @@ -437,6 +446,7 @@ def __init__( | 437 | 446 | this doesn't set up any *new* buffering | 438 | 447 | filter_fn (callable, optional): Callable[str] -> str to filter each | 439 | 448 | line of output | | 449 | + append (bool): whether to append to file ('a' mode) | 440 | 450 |
| 441 | 451 | log_output can take either a file object or a filename. If a | 442 | 452 | filename is passed, the file will be opened and closed entirely | @@ -456,6 +466,7 @@ def __init__( | 456 | 466 | self.debug = debug | 457 | 467 | self.buffer = buffer | 458 | 468 | self.filter_fn = filter_fn | | 469 | + self.append = append | 459 | 470 |
| 460 | 471 | self._active = False # used to prevent re-entry | 461 | 472 |
| @@ -496,7 +507,7 @@ def __enter__(self): | 496 | 507 | raise RuntimeError("file argument must be set by either __init__ or __call__") | 497 | 508 |
| 498 | 509 | # 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) | 500 | 511 |
| 501 | 512 | # record parent color settings before redirecting. We do this | 502 | 513 | # because color output depends on whether the *original* stdout | @@ -735,7 +746,9 @@ class winlog: | 735 | 746 | Does not support the use of 'v' toggling as nixlog does. | 736 | 747 | """ | 737 | 748 |
| 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 | + ): | 739 | 752 | self.debug = debug | 740 | 753 | self.echo = echo | 741 | 754 | self.logfile = file_like | @@ -745,6 +758,7 @@ def __init__(self, file_like=None, echo=False, debug=0, buffer=False, filter_fn= | 745 | 758 | self._ioflag = False | 746 | 759 | self.old_stdout = sys.stdout | 747 | 760 | self.old_stderr = sys.stderr | | 761 | + self.append = append | 748 | 762 |
| 749 | 763 | def __enter__(self): | 750 | 764 | if self._active: | @@ -760,7 +774,8 @@ def __enter__(self): | 760 | 774 | sys.stdout = self.logfile | 761 | 775 | sys.stderr = self.logfile | 762 | 776 | 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) | 764 | 779 | self.reader = open(self.logfile, mode="rb+") | 765 | 780 |
| 766 | 781 | # Dup stdout so we can still write to it after redirection | |
Collapse file Copy file name to clipboardExpand all lines: lib/spack/spack/test/cmd/install.py +12-1Lines changed: 12 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -141,7 +141,7 @@ def test_package_output(capsys, install_mockery, mock_fetch): | 141 | 141 | # when nested AND in pytest | 142 | 142 | spec = spack.concretize.concretize_one("printing-package") | 143 | 143 | pkg = spec.package | 144 | | - PackageInstaller([pkg], explicit=True, verbose=True).install() | | 144 | + PackageInstaller([pkg], explicit=True, verbose=True, tests=sys.platform != "win32").install() | 145 | 145 |
| 146 | 146 | with gzip.open(pkg.install_log_path, "rt") as f: | 147 | 147 | out = f.read() | @@ -151,6 +151,17 @@ def test_package_output(capsys, install_mockery, mock_fetch): | 151 | 151 | assert "BEFORE INSTALL" in out | 152 | 152 | assert "AFTER INSTALL" in out | 153 | 153 |
| | 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 | + | 154 | 165 |
| 155 | 166 | @pytest.mark.disable_clean_stage_check | 156 | 167 | def test_install_output_on_build_error( | |
Collapse file 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 number | Diff line number | Diff line change |
|---|
@@ -133,6 +133,23 @@ def test_log_output_with_filter(capfd, tmp_path: pathlib.Path): | 133 | 133 | assert capfd.readouterr()[0] == "bar blah\nblah bar\nbar bar\n" | 134 | 134 |
| 135 | 135 |
| | 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 | + | 136 | 153 | @pytest.mark.skipif(not which("echo"), reason="needs echo command") | 137 | 154 | def test_log_subproc_and_echo_output_no_capfd(capfd, tmp_path: pathlib.Path): | 138 | 155 | echo = which("echo", required=True) | |
Collapse file 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 number | Diff line number | Diff line change |
|---|
| 4 | 4 |
| 5 | 5 | import os | 6 | 6 |
| 7 | | -from spack_repo.builtin_mock.build_systems.generic import Package | | 7 | +from spack_repo.builtin_mock.build_systems.makefile import MakefilePackage | 8 | 8 |
| 9 | 9 | from spack.package import * | 10 | 10 |
| 11 | 11 |
| 12 | | -class PrintingPackage(Package): | | 12 | +class PrintingPackage(MakefilePackage): | 13 | 13 | """This package prints some output from its install method. | 14 | 14 |
| 15 | 15 | We use this to test whether that output is properly logged. | @@ -28,6 +28,14 @@ def install(self, spec, prefix): | 28 | 28 |
| 29 | 29 | print("AFTER INSTALL") | 30 | 30 |
| | 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 | + | 31 | 39 | def test_print(self): | 32 | 40 | """Test print example.""" | 33 | 41 |
| |
|