Skip to content

Commit

Permalink
Port more tests from TestBase to RuleRunner (pantsbuild#11183)
Browse files Browse the repository at this point in the history
[ci skip-rust]
[ci skip-build-wheels]
  • Loading branch information
Eric-Arellano authored Nov 16, 2020
1 parent 5b91730 commit d3de097
Show file tree
Hide file tree
Showing 9 changed files with 936 additions and 836 deletions.
273 changes: 142 additions & 131 deletions src/python/pants/backend/project_info/dependees_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
from textwrap import dedent
from typing import List

import pytest

from pants.backend.project_info.dependees import DependeesGoal
from pants.backend.project_info.dependees import DependeesOutputFormat as OutputFormat
from pants.backend.project_info.dependees import rules as dependee_rules
from pants.engine.target import Dependencies, SpecialCasedDependencies, Target
from pants.testutil.test_base import TestBase
from pants.testutil.rule_runner import RuleRunner


class SpecialDeps(SpecialCasedDependencies):
Expand All @@ -20,133 +22,142 @@ class MockTarget(Target):
core_fields = (Dependencies, SpecialDeps)


class DependeesTest(TestBase):
@classmethod
def target_types(cls):
return [MockTarget]

@classmethod
def rules(cls):
return (*super().rules(), *dependee_rules())

def setUp(self) -> None:
super().setUp()
self.add_to_build_file("base", "tgt()")
self.add_to_build_file("intermediate", "tgt(dependencies=['base'])")
self.add_to_build_file("leaf", "tgt(dependencies=['intermediate'])")

def assert_dependees(
self,
*,
targets: List[str],
expected: List[str],
transitive: bool = False,
closed: bool = False,
output_format: OutputFormat = OutputFormat.text,
) -> None:
args = [f"--output-format={output_format.value}"]
if transitive:
args.append("--transitive")
if closed:
args.append("--closed")
result = self.run_goal_rule(DependeesGoal, args=[*args, *targets])
assert result.stdout.splitlines() == expected

def test_no_targets(self) -> None:
self.assert_dependees(targets=[], expected=[])
self.assert_dependees(targets=[], output_format=OutputFormat.json, expected=["{}"])

def test_normal(self) -> None:
self.assert_dependees(targets=["base"], expected=["intermediate"])
self.assert_dependees(
targets=["base"],
output_format=OutputFormat.json,
expected=dedent(
"""\
{
"base": [
"intermediate"
]
}"""
).splitlines(),
)

def test_no_dependees(self) -> None:
self.assert_dependees(targets=["leaf"], expected=[])
self.assert_dependees(
targets=["leaf"],
output_format=OutputFormat.json,
expected=dedent(
"""\
{
"leaf": []
}"""
).splitlines(),
)

def test_closed(self) -> None:
self.assert_dependees(targets=["leaf"], closed=True, expected=["leaf"])
self.assert_dependees(
targets=["leaf"],
closed=True,
output_format=OutputFormat.json,
expected=dedent(
"""\
{
"leaf": [
"leaf"
]
}"""
).splitlines(),
)

def test_transitive(self) -> None:
self.assert_dependees(targets=["base"], transitive=True, expected=["intermediate", "leaf"])
self.assert_dependees(
targets=["base"],
transitive=True,
output_format=OutputFormat.json,
expected=dedent(
"""\
{
"base": [
"intermediate",
"leaf"
]
}"""
).splitlines(),
)

def test_multiple_specified_targets(self) -> None:
# This tests that --output-format=text will deduplicate and that --output-format=json will
# preserve which dependee belongs to which specified target.
self.assert_dependees(
targets=["base", "intermediate"],
transitive=True,
# NB: `intermediate` is not included because it's a root and we have `--no-closed`.
expected=["leaf"],
)
self.assert_dependees(
targets=["base", "intermediate"],
transitive=True,
output_format=OutputFormat.json,
expected=dedent(
"""\
{
"base": [
"intermediate",
"leaf"
],
"intermediate": [
"leaf"
]
}"""
).splitlines(),
)

def test_special_cased_dependencies(self) -> None:
self.add_to_build_file("special", "tgt(special_deps=['intermediate'])")
self.assert_dependees(targets=["intermediate"], expected=["leaf", "special"])
self.assert_dependees(
targets=["base"], transitive=True, expected=["intermediate", "leaf", "special"]
)
@pytest.fixture
def rule_runner() -> RuleRunner:
runner = RuleRunner(rules=dependee_rules(), target_types=[MockTarget])
runner.add_to_build_file("base", "tgt()")
runner.add_to_build_file("intermediate", "tgt(dependencies=['base'])")
runner.add_to_build_file("leaf", "tgt(dependencies=['intermediate'])")
return runner


def assert_dependees(
rule_runner: RuleRunner,
*,
targets: List[str],
expected: List[str],
transitive: bool = False,
closed: bool = False,
output_format: OutputFormat = OutputFormat.text,
) -> None:
args = [f"--output-format={output_format.value}"]
if transitive:
args.append("--transitive")
if closed:
args.append("--closed")
result = rule_runner.run_goal_rule(DependeesGoal, args=[*args, *targets])
assert result.stdout.splitlines() == expected


def test_no_targets(rule_runner: RuleRunner) -> None:
assert_dependees(rule_runner, targets=[], expected=[])
assert_dependees(rule_runner, targets=[], output_format=OutputFormat.json, expected=["{}"])


def test_normal(rule_runner: RuleRunner) -> None:
assert_dependees(rule_runner, targets=["base"], expected=["intermediate"])
assert_dependees(
rule_runner,
targets=["base"],
output_format=OutputFormat.json,
expected=dedent(
"""\
{
"base": [
"intermediate"
]
}"""
).splitlines(),
)


def test_no_dependees(rule_runner: RuleRunner) -> None:
assert_dependees(rule_runner, targets=["leaf"], expected=[])
assert_dependees(
rule_runner,
targets=["leaf"],
output_format=OutputFormat.json,
expected=dedent(
"""\
{
"leaf": []
}"""
).splitlines(),
)


def test_closed(rule_runner: RuleRunner) -> None:
assert_dependees(rule_runner, targets=["leaf"], closed=True, expected=["leaf"])
assert_dependees(
rule_runner,
targets=["leaf"],
closed=True,
output_format=OutputFormat.json,
expected=dedent(
"""\
{
"leaf": [
"leaf"
]
}"""
).splitlines(),
)


def test_transitive(rule_runner: RuleRunner) -> None:
assert_dependees(
rule_runner, targets=["base"], transitive=True, expected=["intermediate", "leaf"]
)
assert_dependees(
rule_runner,
targets=["base"],
transitive=True,
output_format=OutputFormat.json,
expected=dedent(
"""\
{
"base": [
"intermediate",
"leaf"
]
}"""
).splitlines(),
)


def test_multiple_specified_targets(rule_runner: RuleRunner) -> None:
# This tests that --output-format=text will deduplicate and that --output-format=json will
# preserve which dependee belongs to which specified target.
assert_dependees(
rule_runner,
targets=["base", "intermediate"],
transitive=True,
# NB: `intermediate` is not included because it's a root and we have `--no-closed`.
expected=["leaf"],
)
assert_dependees(
rule_runner,
targets=["base", "intermediate"],
transitive=True,
output_format=OutputFormat.json,
expected=dedent(
"""\
{
"base": [
"intermediate",
"leaf"
],
"intermediate": [
"leaf"
]
}"""
).splitlines(),
)


def test_special_cased_dependencies(rule_runner: RuleRunner) -> None:
rule_runner.add_to_build_file("special", "tgt(special_deps=['intermediate'])")
assert_dependees(rule_runner, targets=["intermediate"], expected=["leaf", "special"])
assert_dependees(
rule_runner, targets=["base"], transitive=True, expected=["intermediate", "leaf", "special"]
)
Loading

0 comments on commit d3de097

Please sign in to comment.