Skip to content

Commit

Permalink
Add RuleRunner.write_digest() (#13044)
Browse files Browse the repository at this point in the history
Also use it to improve our test for `go_package` support.

[ci skip-rust]
[ci skip-build-wheels]
  • Loading branch information
Eric-Arellano authored Sep 30, 2021
1 parent 8ba0dfa commit 15bc445
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

from __future__ import annotations

import os.path
import subprocess
from textwrap import dedent

import pytest
Expand All @@ -22,9 +24,9 @@
link,
sdk,
)
from pants.build_graph.address import Address
from pants.core.goals.package import BuiltPackage
from pants.core.util_rules import source_files
from pants.engine.addresses import Address
from pants.engine.rules import QueryRule
from pants.engine.target import Target
from pants.testutil.rule_runner import RuleRunner
Expand Down Expand Up @@ -56,7 +58,9 @@ def rule_runner() -> RuleRunner:

def build_package(rule_runner: RuleRunner, binary_target: Target) -> BuiltPackage:
field_set = GoBinaryFieldSet.create(binary_target)
return rule_runner.request(BuiltPackage, [field_set])
result = rule_runner.request(BuiltPackage, [field_set])
rule_runner.write_digest(result.digest)
return result


def test_package_simple(rule_runner: RuleRunner) -> None:
Expand Down Expand Up @@ -90,6 +94,10 @@ def test_package_simple(rule_runner: RuleRunner) -> None:
assert len(built_package.artifacts) == 1
assert built_package.artifacts[0].relpath == "bin"

result = subprocess.run([os.path.join(rule_runner.build_root, "bin")], stdout=subprocess.PIPE)
assert result.returncode == 0
assert result.stdout == b"Hello world!\n"


def test_package_with_dependency(rule_runner: RuleRunner) -> None:
rule_runner.write_files(
Expand Down Expand Up @@ -136,3 +144,7 @@ def test_package_with_dependency(rule_runner: RuleRunner) -> None:
built_package = build_package(rule_runner, binary_tgt)
assert len(built_package.artifacts) == 1
assert built_package.artifacts[0].relpath == "bin"

result = subprocess.run([os.path.join(rule_runner.build_root, "bin")], stdout=subprocess.PIPE)
assert result.returncode == 0
assert result.stdout == b">> Hello world! <<\n"
12 changes: 11 additions & 1 deletion src/python/pants/testutil/rule_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
from pants.engine.addresses import Address
from pants.engine.console import Console
from pants.engine.environment import CompleteEnvironment
from pants.engine.fs import PathGlobs, PathGlobsAndRoot, Snapshot, Workspace
from pants.engine.fs import Digest, PathGlobs, PathGlobsAndRoot, Snapshot, Workspace
from pants.engine.goal import Goal
from pants.engine.internals import native_engine
from pants.engine.internals.native_engine import PyExecutor
from pants.engine.internals.scheduler import SchedulerSession
from pants.engine.internals.selectors import Get, Params
Expand Down Expand Up @@ -391,6 +392,15 @@ def get_target(self, address: Address) -> Target:
"""
return self.request(WrappedTarget, [address]).target

def write_digest(self, digest: Digest, *, path_prefix: str | None = None) -> None:
"""Write a digest to disk, relative to the test's build root.
Access the written files by using `os.path.join(rule_runner.build_root, <relpath>)`.
"""
native_engine.write_digest(
self.scheduler.py_scheduler, self.scheduler.py_session, digest, path_prefix or ""
)


# -----------------------------------------------------------------------------------------------
# `run_rule_with_mocks()`
Expand Down

0 comments on commit 15bc445

Please sign in to comment.