From 97405ff1c511f1a8e4c98d5306b7b222e7733a85 Mon Sep 17 00:00:00 2001 From: Borodin Gregory Date: Sun, 18 Aug 2024 18:09:37 +0200 Subject: [PATCH] Add integration test --- .../pants/build_graph/build_configuration.py | 6 +- .../engine/delete_rule_integration_test.py | 55 +++++++++++++++++++ src/python/pants/engine/rules.py | 2 +- 3 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 src/python/pants/engine/delete_rule_integration_test.py diff --git a/src/python/pants/build_graph/build_configuration.py b/src/python/pants/build_graph/build_configuration.py index b53ffe16738..23c7b743a70 100644 --- a/src/python/pants/build_graph/build_configuration.py +++ b/src/python/pants/build_graph/build_configuration.py @@ -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 @@ -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}") diff --git a/src/python/pants/engine/delete_rule_integration_test.py b/src/python/pants/engine/delete_rule_integration_test.py new file mode 100644 index 00000000000..c4e5a6dd68c --- /dev/null +++ b/src/python/pants/engine/delete_rule_integration_test.py @@ -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 diff --git a/src/python/pants/engine/rules.py b/src/python/pants/engine/rules.py index 7358e1d6089..70547eb934c 100644 --- a/src/python/pants/engine/rules.py +++ b/src/python/pants/engine/rules.py @@ -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()