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

pantsbuild / pants / 20333307239

18 Dec 2025 10:07AM UTC coverage: 75.452% (-4.8%) from 80.295%
20333307239

Pull #22949

github

web-flow
Merge b07232683 into 407284c67
Pull Request #22949: Add experimental uv resolver for Python lockfiles

51 of 96 new or added lines in 5 files covered. (53.13%)

2857 existing lines in 120 files now uncovered.

66315 of 87890 relevant lines covered (75.45%)

2.78 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
1✔
5

6
import pytest
1✔
7

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

22

23
@pytest.fixture
1✔
24
def rule_runner() -> RuleRunner:
1✔
25
    return RuleRunner(
1✔
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"
1✔
40
BAD_FILE = "from foo import Foo\nprint(Bar())\n"
1✔
41
FIXED_BAD_FILE = "print(Bar())\n"
1✔
42

43

44
def run_autoflake(
1✔
45
    rule_runner: RuleRunner,
46
    targets: list[Target],
47
    *,
48
    extra_args: list[str] | None = None,
49
) -> FixResult:
50
    rule_runner.set_options(
1✔
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]
1✔
55
    input_sources = rule_runner.request(
1✔
56
        SourceFiles,
57
        [
58
            SourceFilesRequest(field_set.source for field_set in field_sets),
59
        ],
60
    )
61
    fix_result = rule_runner.request(
1✔
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
1✔
73

74

75
@pytest.mark.platform_specific_behavior
1✔
76
@pytest.mark.parametrize(
1✔
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:
1✔
81
    rule_runner.write_files({"f.py": GOOD_FILE, "BUILD": "python_sources(name='t')"})
1✔
82
    tgt = rule_runner.get_target(Address("", target_name="t", relative_file_path="f.py"))
1✔
83
    fix_result = run_autoflake(
1✔
84
        rule_runner,
85
        [tgt],
86
        extra_args=[f"--autoflake-interpreter-constraints=['=={major_minor_interpreter}.*']"],
87
    )
88
    assert fix_result.stdout == ""
1✔
89
    assert fix_result.output == rule_runner.make_snapshot({"f.py": GOOD_FILE})
1✔
90
    assert fix_result.did_change is False
1✔
91

92

93
def test_failing_source(rule_runner: RuleRunner) -> None:
1✔
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:
1✔
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:
1✔
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