Skip to content

Commit

Permalink
add test for capturing intrinsic errors
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmicexplorer committed Oct 13, 2020
1 parent ab10341 commit e6c262c
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion src/python/pants/engine/internals/engine_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import unittest
from dataclasses import dataclass, field
from textwrap import dedent
from typing import List, Optional
from typing import List, Optional, Tuple

from pants.engine.engine_aware import EngineAwareReturnType
from pants.engine.fs import (
Expand All @@ -16,6 +16,7 @@
Digest,
DigestContents,
FileContent,
MergeDigests,
Snapshot,
)
from pants.engine.internals.engine_testutil import (
Expand Down Expand Up @@ -167,6 +168,30 @@ def rule_C(e: Epsilon) -> Alpha:
return Alpha()


@dataclass(frozen=True)
class FileInput:
filename: str


@dataclass(frozen=True)
class MergedOutput:
digest: Digest


@rule
async def catch_merge_digests_error(file_input: FileInput) -> MergedOutput:
# Create two separate digests writing different contents to the same file path.
input_1 = CreateDigest((FileContent(path=file_input.filename, content=b"yes"),))
input_2 = CreateDigest((FileContent(path=file_input.filename, content=b"no"),))
digests = await MultiGet(Get(Digest, CreateDigest, input_1),
Get(Digest, CreateDigest, input_2))
try:
merged = await Get(Digest, MergeDigests(digests))
except Exception as e:
raise Exception(f'error merging digests for input {file_input}: {e}')
return MergedOutput(merged)


class EngineTest(unittest.TestCase, SchedulerTestBase):

assert_equal_with_printing = assert_equal_with_printing
Expand Down Expand Up @@ -250,6 +275,13 @@ def test_missing_query_rule(self) -> None:
"for MyInt)."
) in str(cm.exception)

def test_catch_intrinsic_error(self) -> None:
rules = [catch_merge_digests_error, QueryRule(MergedOutput, (FileInput,))]
scheduler = self.mk_scheduler(rules=rules, include_trace_on_error=False)
with self.assertRaises(Exception) as cm:
scheduler.product_request(MergedOutput, subjects=[FileInput('some-file.txt')])
assert "error merging digests for input FileInput(filename=\'some-file.txt\')" in str(cm.exception)


@dataclass
class WorkunitTracker:
Expand Down

0 comments on commit e6c262c

Please sign in to comment.