forked from pantsbuild/pants
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
### Problem We weren't testing `lint`, which has an edge case when one of the targets results in a lint failure. `fmt` had an integration test which can be rewritten to a less costly unit test. ### Result We test more edge cases. It should be easier to write future tests for these files and to write `run_rule()`-based tests for other rules, as the more examples we have of how to test rules, the easier it will become.
- Loading branch information
1 parent
2424580
commit 8521c1b
Showing
6 changed files
with
133 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Copyright 2019 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
from typing import Callable, List, Tuple, Type | ||
|
||
from pants.engine.fs import EMPTY_DIRECTORY_DIGEST, Digest, FilesContent | ||
from pants.engine.legacy.graph import HydratedTarget, HydratedTargets | ||
from pants.engine.legacy.structs import JvmAppAdaptor, PythonTargetAdaptor, TargetAdaptor | ||
from pants.engine.rules import UnionMembership | ||
from pants.rules.core.fmt import Fmt, FmtResult, TargetWithSources, fmt | ||
from pants.testutil.engine.util import MockConsole, MockGet, run_rule | ||
|
||
|
||
def make_target( | ||
*, name: str = "target", adaptor_type: Type[TargetAdaptor] = PythonTargetAdaptor | ||
) -> HydratedTarget: | ||
return HydratedTarget( | ||
address=f"src/{name}", | ||
adaptor=adaptor_type(sources=(), name=name), | ||
dependencies=() | ||
) | ||
|
||
|
||
def run_fmt_rule( | ||
*, | ||
targets: List[HydratedTarget], | ||
mock_formatter: Callable[[PythonTargetAdaptor], FmtResult], | ||
) -> Tuple[Fmt, MockConsole]: | ||
console = MockConsole(use_colors=False) | ||
result: Fmt = run_rule( | ||
fmt, | ||
rule_args=[ | ||
console, | ||
HydratedTargets(targets), | ||
UnionMembership(union_rules={TargetWithSources: [PythonTargetAdaptor]}) | ||
], | ||
mock_gets=[ | ||
MockGet(product_type=FmtResult, subject_type=PythonTargetAdaptor, mock=mock_formatter), | ||
MockGet(product_type=FilesContent, subject_type=Digest, mock=lambda _: FilesContent([])) | ||
], | ||
) | ||
return result, console | ||
|
||
|
||
def test_non_union_member_noops() -> None: | ||
result, console = run_fmt_rule( | ||
targets=[make_target(adaptor_type=JvmAppAdaptor)], | ||
mock_formatter=lambda adaptor: FmtResult( | ||
digest=EMPTY_DIRECTORY_DIGEST, stdout=f"Formatted target `{adaptor.name}`", stderr="" | ||
), | ||
) | ||
assert result.exit_code == 0 | ||
assert console.stdout.getvalue().strip() == "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# Copyright 2019 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
from typing import Callable, List, Tuple, Type | ||
|
||
from pants.engine.legacy.graph import HydratedTarget, HydratedTargets | ||
from pants.engine.legacy.structs import JvmAppAdaptor, PythonTargetAdaptor, TargetAdaptor | ||
from pants.engine.rules import UnionMembership | ||
from pants.rules.core.lint import Lint, LintResult, TargetWithSources, lint | ||
from pants.testutil.engine.util import MockConsole, MockGet, run_rule | ||
|
||
|
||
def make_target( | ||
*, name: str = "target", adaptor_type: Type[TargetAdaptor] = PythonTargetAdaptor | ||
) -> HydratedTarget: | ||
return HydratedTarget( | ||
address=f"src/{name}", | ||
adaptor=adaptor_type(sources=(), name=name), | ||
dependencies=() | ||
) | ||
|
||
|
||
def run_lint_rule( | ||
*, | ||
targets: List[HydratedTarget], | ||
mock_linter: Callable[[PythonTargetAdaptor], LintResult], | ||
) -> Tuple[Lint, MockConsole]: | ||
console = MockConsole(use_colors=False) | ||
result: Lint = run_rule( | ||
lint, | ||
rule_args=[ | ||
console, | ||
HydratedTargets(targets), | ||
UnionMembership(union_rules={TargetWithSources: [PythonTargetAdaptor]}) | ||
], | ||
mock_gets=[ | ||
MockGet(product_type=LintResult, subject_type=PythonTargetAdaptor, mock=mock_linter), | ||
], | ||
) | ||
return result, console | ||
|
||
|
||
def test_non_union_member_noops() -> None: | ||
result, console = run_lint_rule( | ||
targets=[make_target(adaptor_type=JvmAppAdaptor)], | ||
mock_linter=lambda target: LintResult(exit_code=1, stdout="", stderr=""), | ||
) | ||
assert result.exit_code == 0 | ||
assert console.stdout.getvalue().strip() == "" | ||
|
||
|
||
def test_failure_for_a_single_target_propagates(): | ||
def mock_linter(adaptor: PythonTargetAdaptor) -> LintResult: | ||
if adaptor.name == "bad": | ||
return LintResult(exit_code=127, stdout="failure", stderr="failure") | ||
return LintResult(exit_code=0, stdout=f"Linted the target `{adaptor.name}`", stderr="..") | ||
|
||
result, console = run_lint_rule( | ||
targets=[make_target(name="good"), make_target(name="bad")], mock_linter=mock_linter, | ||
) | ||
assert result.exit_code == 127 | ||
assert console.stdout.getvalue().splitlines() == ["Linted the target `good`", "failure"] | ||
assert console.stderr.getvalue().splitlines() == ["..", "failure"] |