• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pantsbuild / pants / 19250292619

11 Nov 2025 12:09AM UTC coverage: 77.865% (-2.4%) from 80.298%
19250292619

push

github

web-flow
flag non-runnable targets used with `code_quality_tool` (#22875)

2 of 5 new or added lines in 2 files covered. (40.0%)

1487 existing lines in 72 files now uncovered.

71448 of 91759 relevant lines covered (77.86%)

3.22 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

66.1
/src/python/pants/backend/python/lint/autoflake/rules_integration_test.py
1
# Copyright 2021 Pants project contributors (see CONTRIBUTORS.md).
2
# Licensed under the Apache License, Version 2.0 (see LICENSE).
3

4
from __future__ import annotations
2✔
5

6
import pytest
2✔
7

8
from pants.backend.python import target_types_rules
2✔
9
from pants.backend.python.lint.autoflake.rules import AutoflakeFieldSet, AutoflakeRequest
2✔
10
from pants.backend.python.lint.autoflake.rules import rules as autoflake_rules
2✔
11
from pants.backend.python.lint.autoflake.subsystem import Autoflake
2✔
12
from pants.backend.python.lint.autoflake.subsystem import rules as autoflake_subsystem_rules
2✔
13
from pants.backend.python.target_types import PythonSourcesGeneratorTarget
2✔
14
from pants.core.goals.fix import FixResult
2✔
15
from pants.core.util_rules import config_files, source_files
2✔
16
from pants.core.util_rules.source_files import SourceFiles, SourceFilesRequest
2✔
17
from pants.engine.addresses import Address
2✔
18
from pants.engine.target import Target
2✔
19
from pants.testutil.python_interpreter_selection import all_major_minor_python_versions
2✔
20
from pants.testutil.rule_runner import QueryRule, RuleRunner
2✔
21

22

23
@pytest.fixture
2✔
24
def rule_runner() -> RuleRunner:
2✔
25
    return RuleRunner(
2✔
26
        rules=[
27
            *autoflake_rules(),
28
            *autoflake_subsystem_rules(),
29
            *source_files.rules(),
30
            *config_files.rules(),
31
            *target_types_rules.rules(),
32
            QueryRule(FixResult, (AutoflakeRequest.Batch,)),
33
            QueryRule(SourceFiles, (SourceFilesRequest,)),
34
        ],
35
        target_types=[PythonSourcesGeneratorTarget],
36
    )
37

38

39
GOOD_FILE = "from foo import Foo\nprint(Foo())\n"
2✔
40
BAD_FILE = "from foo import Foo\nprint(Bar())\n"
2✔
41
FIXED_BAD_FILE = "print(Bar())\n"
2✔
42

43

44
def run_autoflake(
2✔
45
    rule_runner: RuleRunner,
46
    targets: list[Target],
47
    *,
48
    extra_args: list[str] | None = None,
49
) -> FixResult:
50
    rule_runner.set_options(
2✔
51
        ["--backend-packages=pants.backend.python.lint.autoflake", *(extra_args or ())],
52
        env_inherit={"PATH", "PYENV_ROOT", "HOME"},
53
    )
54
    field_sets = [AutoflakeFieldSet.create(tgt) for tgt in targets]
2✔
55
    input_sources = rule_runner.request(
2✔
56
        SourceFiles,
57
        [
58
            SourceFilesRequest(field_set.source for field_set in field_sets),
59
        ],
60
    )
61
    fix_result = rule_runner.request(
2✔
62
        FixResult,
63
        [
64
            AutoflakeRequest.Batch(
65
                "",
66
                input_sources.snapshot.files,
67
                partition_metadata=None,
68
                snapshot=input_sources.snapshot,
69
            ),
70
        ],
71
    )
72
    return fix_result
2✔
73

74

75
@pytest.mark.platform_specific_behavior
2✔
76
@pytest.mark.parametrize(
2✔
77
    "major_minor_interpreter",
78
    all_major_minor_python_versions(Autoflake.default_interpreter_constraints),
79
)
80
def test_passing_source(rule_runner: RuleRunner, major_minor_interpreter: str) -> None:
2✔
81
    rule_runner.write_files({"f.py": GOOD_FILE, "BUILD": "python_sources(name='t')"})
2✔
82
    tgt = rule_runner.get_target(Address("", target_name="t", relative_file_path="f.py"))
2✔
83
    fix_result = run_autoflake(
2✔
84
        rule_runner,
85
        [tgt],
86
        extra_args=[f"--autoflake-interpreter-constraints=['=={major_minor_interpreter}.*']"],
87
    )
88
    assert fix_result.stdout == ""
2✔
89
    assert fix_result.output == rule_runner.make_snapshot({"f.py": GOOD_FILE})
2✔
90
    assert fix_result.did_change is False
2✔
91

92

93
def test_failing_source(rule_runner: RuleRunner) -> None:
2✔
UNCOV
94
    rule_runner.write_files({"f.py": BAD_FILE, "BUILD": "python_sources(name='t')"})
×
UNCOV
95
    tgt = rule_runner.get_target(Address("", target_name="t", relative_file_path="f.py"))
×
UNCOV
96
    fix_result = run_autoflake(rule_runner, [tgt])
×
UNCOV
97
    assert fix_result.output == rule_runner.make_snapshot({"f.py": FIXED_BAD_FILE})
×
UNCOV
98
    assert fix_result.did_change is True
×
99

100

101
def test_multiple_targets(rule_runner: RuleRunner) -> None:
2✔
UNCOV
102
    rule_runner.write_files(
×
103
        {"good.py": GOOD_FILE, "bad.py": BAD_FILE, "BUILD": "python_sources(name='t')"}
104
    )
UNCOV
105
    tgts = [
×
106
        rule_runner.get_target(Address("", target_name="t", relative_file_path="good.py")),
107
        rule_runner.get_target(Address("", target_name="t", relative_file_path="bad.py")),
108
    ]
UNCOV
109
    fix_result = run_autoflake(rule_runner, tgts)
×
UNCOV
110
    assert fix_result.output == rule_runner.make_snapshot(
×
111
        {"good.py": GOOD_FILE, "bad.py": FIXED_BAD_FILE}
112
    )
UNCOV
113
    assert fix_result.did_change is True
×
114

115

116
def test_stub_files(rule_runner: RuleRunner) -> None:
2✔
UNCOV
117
    rule_runner.write_files(
×
118
        {
119
            "good.pyi": GOOD_FILE,
120
            "good.py": GOOD_FILE,
121
            "bad.pyi": BAD_FILE,
122
            "bad.py": BAD_FILE,
123
            "BUILD": "python_sources(name='t')",
124
        }
125
    )
126

UNCOV
127
    good_tgts = [
×
128
        rule_runner.get_target(Address("", target_name="t", relative_file_path="good.pyi")),
129
        rule_runner.get_target(Address("", target_name="t", relative_file_path="good.py")),
130
    ]
UNCOV
131
    fix_result = run_autoflake(rule_runner, good_tgts)
×
UNCOV
132
    assert fix_result.stdout == ""
×
UNCOV
133
    assert fix_result.output == rule_runner.make_snapshot(
×
134
        {"good.py": GOOD_FILE, "good.pyi": GOOD_FILE}
135
    )
UNCOV
136
    assert not fix_result.did_change
×
137

UNCOV
138
    bad_tgts = [
×
139
        rule_runner.get_target(Address("", target_name="t", relative_file_path="bad.pyi")),
140
        rule_runner.get_target(Address("", target_name="t", relative_file_path="bad.py")),
141
    ]
UNCOV
142
    fix_result = run_autoflake(rule_runner, bad_tgts)
×
UNCOV
143
    assert fix_result.output == rule_runner.make_snapshot(
×
144
        {"bad.py": FIXED_BAD_FILE, "bad.pyi": FIXED_BAD_FILE}
145
    )
UNCOV
146
    assert fix_result.did_change
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc