Skip to content

Commit

Permalink
Add tests for parametrize.
Browse files Browse the repository at this point in the history
# Rust tests and lints will be skipped. Delete if not intended.
[ci skip-rust]

# Building wheels and fs_util will be skipped. Delete if not intended.
[ci skip-build-wheels]
  • Loading branch information
stuhood committed Feb 9, 2022
1 parent f1bcf0d commit 9ce6c3f
Showing 1 changed file with 94 additions and 2 deletions.
96 changes: 94 additions & 2 deletions src/python/pants/engine/internals/graph_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
TransitiveExcludesNotSupportedError,
_TargetParametrizations,
)
from pants.engine.internals.parametrize import Parametrize
from pants.engine.internals.scheduler import ExecutionError
from pants.engine.rules import Get, MultiGet, rule
from pants.engine.target import (
Expand Down Expand Up @@ -107,17 +108,21 @@ class MockTarget(Target):
deprecated_alias_removal_version = "9.9.9.dev0"


class ResolveField(StringField):
alias = "resolve"


class MockGeneratedTarget(Target):
alias = "generated"
core_fields = (MockDependencies, Tags, SingleSourceField)
core_fields = (MockDependencies, Tags, SingleSourceField, ResolveField)


class MockTargetGenerator(TargetFilesGenerator):
alias = "generator"
core_fields = (Dependencies, MultipleSourcesField, OverridesField)
generated_target_cls = MockGeneratedTarget
copied_fields = (Dependencies,)
moved_fields = (Tags,)
moved_fields = (Tags, ResolveField)


@pytest.fixture
Expand Down Expand Up @@ -918,6 +923,7 @@ def generated_targets_rule_runner() -> RuleRunner:
QueryRule(_TargetParametrizations, [Address]),
],
target_types=[MockTargetGenerator, MockGeneratedTarget],
objects={"parametrize": Parametrize},
)


Expand Down Expand Up @@ -1006,6 +1012,92 @@ def test_generate_overrides_unused(generated_targets_rule_runner: RuleRunner) ->
)


def test_parametrize(generated_targets_rule_runner: RuleRunner) -> None:
assert_generated(
generated_targets_rule_runner,
Address("demo"),
"generator(tags=parametrize(t1=['t1'], t2=['t2']), sources=['f1.ext'])",
["f1.ext"],
{
MockGeneratedTarget(
{SingleSourceField.alias: "f1.ext", Tags.alias: ["t1"]},
Address("demo", relative_file_path="f1.ext", parameters={"tags": "t1"}),
residence_dir="demo",
),
MockGeneratedTarget(
{SingleSourceField.alias: "f1.ext", Tags.alias: ["t2"]},
Address("demo", relative_file_path="f1.ext", parameters={"tags": "t2"}),
residence_dir="demo",
),
},
)


def test_parametrize_multi(generated_targets_rule_runner: RuleRunner) -> None:
assert_generated(
generated_targets_rule_runner,
Address("demo"),
"generator(tags=parametrize(t1=['t1'], t2=['t2']), resolve=parametrize('a', 'b'), sources=['f1.ext'])",
["f1.ext"],
{
MockGeneratedTarget(
{SingleSourceField.alias: "f1.ext", Tags.alias: ["t1"], ResolveField.alias: "a"},
Address(
"demo", relative_file_path="f1.ext", parameters={"tags": "t1", "resolve": "a"}
),
residence_dir="demo",
),
MockGeneratedTarget(
{SingleSourceField.alias: "f1.ext", Tags.alias: ["t2"], ResolveField.alias: "a"},
Address(
"demo", relative_file_path="f1.ext", parameters={"tags": "t2", "resolve": "a"}
),
residence_dir="demo",
),
MockGeneratedTarget(
{SingleSourceField.alias: "f1.ext", Tags.alias: ["t1"], ResolveField.alias: "b"},
Address(
"demo", relative_file_path="f1.ext", parameters={"tags": "t1", "resolve": "b"}
),
residence_dir="demo",
),
MockGeneratedTarget(
{SingleSourceField.alias: "f1.ext", Tags.alias: ["t2"], ResolveField.alias: "b"},
Address(
"demo", relative_file_path="f1.ext", parameters={"tags": "t2", "resolve": "b"}
),
residence_dir="demo",
),
},
)


def test_parametrize_overrides(generated_targets_rule_runner: RuleRunner) -> None:
assert_generated(
generated_targets_rule_runner,
Address("demo"),
"generator(overrides={'f1.ext': {'resolve': parametrize('a', 'b')}}, resolve='c', sources=['*.ext'])",
["f1.ext", "f2.ext"],
{
MockGeneratedTarget(
{SingleSourceField.alias: "f1.ext", ResolveField.alias: "a"},
Address("demo", relative_file_path="f1.ext", parameters={"resolve": "a"}),
residence_dir="demo",
),
MockGeneratedTarget(
{SingleSourceField.alias: "f1.ext", ResolveField.alias: "b"},
Address("demo", relative_file_path="f1.ext", parameters={"resolve": "b"}),
residence_dir="demo",
),
MockGeneratedTarget(
{SingleSourceField.alias: "f2.ext", ResolveField.alias: "c"},
Address("demo", relative_file_path="f2.ext"),
residence_dir="demo",
),
},
)


# -----------------------------------------------------------------------------------------------
# Test FieldSets. Also see `engine/target_test.py`.
# -----------------------------------------------------------------------------------------------
Expand Down

0 comments on commit 9ce6c3f

Please sign in to comment.