Skip to content

Commit

Permalink
Add integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
grihabor committed Sep 14, 2024
1 parent 66da009 commit 97405ff
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/python/pants/build_graph/build_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from pants.build_graph.build_file_aliases import BuildFileAliases
from pants.core.util_rules.environments import EnvironmentsSubsystem
from pants.engine.goal import GoalSubsystem
from pants.engine.rules import Rule, RuleIndex, TaskRule
from pants.engine.rules import DeleteRule, Rule, RuleIndex, TaskRule
from pants.engine.target import Target
from pants.engine.unions import UnionRule
from pants.option.alias import CliOptions
Expand Down Expand Up @@ -216,7 +216,9 @@ def register_subsystems(
for subsystem in subsystems:
self._subsystem_to_providers[subsystem].append(plugin_or_backend)

def register_rules(self, plugin_or_backend: str, rules: Iterable[Rule | UnionRule]):
def register_rules(
self, plugin_or_backend: str, rules: Iterable[Rule | UnionRule | DeleteRule]
):
"""Registers the given rules."""
if not isinstance(rules, Iterable):
raise TypeError(f"The rules must be an iterable, given {rules!r}")
Expand Down
55 changes: 55 additions & 0 deletions src/python/pants/engine/delete_rule_integration_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Copyright 2024 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).
from dataclasses import dataclass

from pants.engine.rules import DeleteRule, collect_rules, rule
from pants.testutil.rule_runner import QueryRule, RuleRunner


@dataclass(frozen=True)
class IntRequest:
pass


@rule
async def original_rule(request: IntRequest) -> int:
return 0


@rule
def new_rule(request: IntRequest) -> int:
return 42


def test_delete() -> None:
rule_runner = RuleRunner(
target_types=[],
rules=[
*collect_rules(
{
"original_rule": original_rule,
}
),
QueryRule(int, [IntRequest]),
],
)

result = rule_runner.request(int, [IntRequest()])
assert result == 0

rule_runner = RuleRunner(
target_types=[],
rules=[
*collect_rules(
{
"original_rule": original_rule,
"new_rule": new_rule,
}
),
DeleteRule.create(original_rule),
QueryRule(int, [IntRequest]),
],
)

result = rule_runner.request(int, [IntRequest()])
assert result == 42
2 changes: 1 addition & 1 deletion src/python/pants/engine/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ class RuleIndex:
union_rules: FrozenOrderedSet[UnionRule]

@classmethod
def create(cls, rule_entries: Iterable[Rule | UnionRule]) -> RuleIndex:
def create(cls, rule_entries: Iterable[Rule | UnionRule | DeleteRule]) -> RuleIndex:
"""Creates a RuleIndex with tasks indexed by their output type."""
rules: OrderedSet[TaskRule] = OrderedSet()
delete_rules: OrderedSet[DeleteRule] = OrderedSet()
Expand Down

0 comments on commit 97405ff

Please sign in to comment.