Skip to content

Commit

Permalink
store bytes for stdout/stderr output from tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tdyas committed May 28, 2023
1 parent a34feab commit 559a664
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/python/pants/backend/go/goals/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,8 +574,8 @@ async def run_go_tests(
if fallible_test_binary.exit_code != 0:
return TestResult(
exit_code=fallible_test_binary.exit_code,
stdout=fallible_test_binary.stdout,
stderr=fallible_test_binary.stderr,
stdout_bytes=fallible_test_binary.stdout.encode(),
stderr_bytes=fallible_test_binary.stderr.encode(),
stdout_digest=EMPTY_FILE_DIGEST,
stderr_digest=EMPTY_FILE_DIGEST,
addresses=(field_set.address,),
Expand Down
45 changes: 30 additions & 15 deletions src/python/pants/core/goals/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from pathlib import PurePath
from typing import Any, ClassVar, Iterable, Optional, Sequence, TypeVar, cast

from pants.base.deprecated import deprecated
from pants.core.goals.multi_tool_goal_helper import SkippableSubsystem
from pants.core.goals.package import BuiltPackage, EnvironmentAwarePackageRequest, PackageFieldSet
from pants.core.subsystems.debug_adapter import DebugAdapterSubsystem
Expand Down Expand Up @@ -64,7 +65,7 @@
from pants.util.collections import partition_sequentially
from pants.util.docutil import bin_name
from pants.util.logging import LogLevel
from pants.util.memo import memoized
from pants.util.memo import memoized, memoized_property
from pants.util.meta import classproperty
from pants.util.strutil import help_text, softwrap

Expand All @@ -76,9 +77,9 @@ class TestResult(EngineAwareReturnType):
# A None exit_code indicates a backend that performs its own test discovery/selection
# (rather than delegating that to the underlying test tool), and discovered no tests.
exit_code: int | None
stdout: str
stdout_bytes: bytes
stdout_digest: FileDigest
stderr: str
stderr_bytes: bytes
stderr_digest: FileDigest
addresses: tuple[Address, ...]
output_setting: ShowOutput
Expand All @@ -105,8 +106,8 @@ def no_tests_found(address: Address, output_setting: ShowOutput) -> TestResult:
"""Used when we do test discovery ourselves, and we didn't find any."""
return TestResult(
exit_code=None,
stdout="",
stderr="",
stdout_bytes=b"",
stderr_bytes=b"",
stdout_digest=EMPTY_FILE_DIGEST,
stderr_digest=EMPTY_FILE_DIGEST,
addresses=(address,),
Expand All @@ -121,8 +122,8 @@ def no_tests_found_in_batch(
"""Used when we do test discovery ourselves, and we didn't find any."""
return TestResult(
exit_code=None,
stdout="",
stderr="",
stdout_bytes=b"",
stderr_bytes=b"",
stdout_digest=EMPTY_FILE_DIGEST,
stderr_digest=EMPTY_FILE_DIGEST,
addresses=tuple(field_set.address for field_set in batch.elements),
Expand All @@ -144,9 +145,9 @@ def from_fallible_process_result(
) -> TestResult:
return TestResult(
exit_code=process_result.exit_code,
stdout=process_result.stdout.decode(),
stdout_bytes=process_result.stdout,
stdout_digest=process_result.stdout_digest,
stderr=process_result.stderr.decode(),
stderr_bytes=process_result.stderr,
stderr_digest=process_result.stderr_digest,
addresses=(address,),
output_setting=output_setting,
Expand All @@ -170,9 +171,9 @@ def from_batched_fallible_process_result(
) -> TestResult:
return TestResult(
exit_code=process_result.exit_code,
stdout=process_result.stdout.decode(),
stdout_bytes=process_result.stdout,
stdout_digest=process_result.stdout_digest,
stderr=process_result.stderr.decode(),
stderr_bytes=process_result.stderr,
stderr_digest=process_result.stderr_digest,
addresses=tuple(field_set.address for field_set in batch.elements),
output_setting=output_setting,
Expand All @@ -184,6 +185,20 @@ def from_batched_fallible_process_result(
partition_description=batch.partition_metadata.description,
)

@memoized_property
@deprecated(
removal_version="2.19.0.dev0", hint="Use `TestResult.stdout_bytes` instead of `stdout`."
)
def stdout(self) -> str:
return self.stdout_bytes.decode(errors="replace")

@memoized_property
@deprecated(
removal_version="2.19.0.dev0", hint="Use `TestResult.stderr_bytes` instead of `stderr`."
)
def stderr(self) -> str:
return self.stderr_bytes.decode(errors="replace")

@property
def description(self) -> str:
if len(self.addresses) == 1:
Expand Down Expand Up @@ -236,10 +251,10 @@ def message(self) -> str:
):
return message
output = ""
if self.stdout:
output += f"\n{self.stdout}"
if self.stderr:
output += f"\n{self.stderr}"
if self.stdout_bytes:
output += f"\n{self.stdout_bytes.decode(errors='replace')}"
if self.stderr_bytes:
output += f"\n{self.stderr_bytes.decode(errors='replace')}"
if output:
output = f"{output.rstrip()}\n\n"
return f"{message}{output}"
Expand Down
12 changes: 6 additions & 6 deletions src/python/pants/core/goals/test_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,9 @@ def test_result(cls, field_sets: Iterable[MockTestFieldSet]) -> TestResult:
addresses = [field_set.address for field_set in field_sets]
return TestResult(
exit_code=cls.exit_code(addresses),
stdout="",
stdout_bytes=b"",
stdout_digest=EMPTY_FILE_DIGEST,
stderr="",
stderr_bytes=b"",
stderr_digest=EMPTY_FILE_DIGEST,
addresses=tuple(addresses),
coverage_data=MockCoverageData(addresses),
Expand Down Expand Up @@ -438,8 +438,8 @@ def _assert_test_summary(
assert expected == _format_test_summary(
TestResult(
exit_code=exit_code,
stdout="",
stderr="",
stdout_bytes=b"",
stderr_bytes=b"",
stdout_digest=EMPTY_FILE_DIGEST,
stderr_digest=EMPTY_FILE_DIGEST,
addresses=(Address(spec_path="", target_name="dummy_address"),),
Expand Down Expand Up @@ -599,9 +599,9 @@ def assert_streaming_output(
) -> None:
result = TestResult(
exit_code=exit_code,
stdout=stdout,
stdout_bytes=stdout.encode(),
stdout_digest=EMPTY_FILE_DIGEST,
stderr=stderr,
stderr_bytes=stderr.encode(),
stderr_digest=EMPTY_FILE_DIGEST,
output_setting=output_setting,
addresses=(Address("demo_test"),),
Expand Down

0 comments on commit 559a664

Please sign in to comment.