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

pantsbuild / pants / 20632486505

01 Jan 2026 04:21AM UTC coverage: 43.231% (-37.1%) from 80.281%
20632486505

Pull #22962

github

web-flow
Merge 08d5c63b0 into f52ab6675
Pull Request #22962: Bump the gha-deps group across 1 directory with 6 updates

26122 of 60424 relevant lines covered (43.23%)

0.86 hits per line

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

64.29
/src/python/pants/backend/build_files/fmt/black/integration_test.py
1
# Copyright 2022 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.build_files.fmt.black.register import BlackRequest
2✔
9
from pants.backend.build_files.fmt.black.register import rules as black_build_rules
2✔
10
from pants.backend.python.lint.black.rules import rules as black_fmt_rules
2✔
11
from pants.backend.python.lint.black.subsystem import Black
2✔
12
from pants.backend.python.lint.black.subsystem import rules as black_subsystem_rules
2✔
13
from pants.backend.python.target_types import PythonSourcesGeneratorTarget
2✔
14
from pants.core.goals.fmt import FmtResult
2✔
15
from pants.core.util_rules import config_files
2✔
16
from pants.engine.fs import PathGlobs
2✔
17
from pants.engine.internals.native_engine import Snapshot
2✔
18
from pants.testutil.python_interpreter_selection import all_major_minor_python_versions
2✔
19
from pants.testutil.rule_runner import QueryRule, RuleRunner
2✔
20

21

22
@pytest.fixture
2✔
23
def rule_runner() -> RuleRunner:
2✔
24
    return RuleRunner(
2✔
25
        rules=[
26
            *black_build_rules(),
27
            *black_fmt_rules(),
28
            *black_subsystem_rules(),
29
            *config_files.rules(),
30
            QueryRule(FmtResult, (BlackRequest.Batch,)),
31
        ],
32
        target_types=[PythonSourcesGeneratorTarget],
33
    )
34

35

36
def run_black(rule_runner: RuleRunner, *, extra_args: list[str] | None = None) -> FmtResult:
2✔
37
    rule_runner.set_options(
2✔
38
        ["--backend-packages=pants.backend.build_files.fmt.black", *(extra_args or ())],
39
        # We propagate LANG and LC_ALL to satisfy click, which black depends upon. Without this we
40
        # see something like the following in CI:
41
        #
42
        # RuntimeError: Click will abort further execution because Python was configured to use
43
        # ASCII as encoding for the environment. Consult
44
        # https://click.palletsprojects.com/unicode-support/ for mitigation steps.
45
        #
46
        # This system supports the C.UTF-8 locale which is recommended. You might be able to
47
        # resolve your issue by exporting the following environment variables:
48
        #
49
        #     export LC_ALL=C.UTF-8
50
        #     export LANG=C.UTF-8
51
        #
52
        env_inherit={"PATH", "PYENV_ROOT", "HOME", "LANG", "LC_ALL"},
53
    )
54
    snapshot = rule_runner.request(Snapshot, [PathGlobs(["**/BUILD"])])
2✔
55
    fmt_result = rule_runner.request(
2✔
56
        FmtResult,
57
        [
58
            BlackRequest.Batch("", snapshot.files, partition_metadata=None, snapshot=snapshot),
59
        ],
60
    )
61
    return fmt_result
2✔
62

63

64
@pytest.mark.platform_specific_behavior
2✔
65
@pytest.mark.parametrize(
2✔
66
    "major_minor_interpreter",
67
    all_major_minor_python_versions(Black.default_interpreter_constraints),
68
)
69
def test_passing(rule_runner: RuleRunner, major_minor_interpreter: str) -> None:
2✔
70
    rule_runner.write_files({"BUILD": 'python_sources(name="t")\n'})
2✔
71
    interpreter_constraint = (
2✔
72
        ">=3.6.2,<3.7" if major_minor_interpreter == "3.6" else f"=={major_minor_interpreter}.*"
73
    )
74
    fmt_result = run_black(
2✔
75
        rule_runner,
76
        extra_args=[f"--black-interpreter-constraints=['{interpreter_constraint}']"],
77
    )
78
    assert "1 file left unchanged" in fmt_result.stderr
2✔
79
    assert fmt_result.output == rule_runner.make_snapshot({"BUILD": 'python_sources(name="t")\n'})
2✔
80
    assert fmt_result.did_change is False
2✔
81

82

83
def test_failing(rule_runner: RuleRunner) -> None:
2✔
84
    rule_runner.write_files({"BUILD": "python_sources(name='t')\n"})
×
85
    fmt_result = run_black(rule_runner)
×
86
    assert "1 file reformatted" in fmt_result.stderr
×
87
    assert fmt_result.output == rule_runner.make_snapshot({"BUILD": 'python_sources(name="t")\n'})
×
88
    assert fmt_result.did_change is True
×
89

90

91
def test_multiple_files(rule_runner: RuleRunner) -> None:
2✔
92
    rule_runner.write_files(
×
93
        {
94
            "good/BUILD": 'python_sources(name="t")\n',
95
            "bad/BUILD": "python_sources(name='t')\n",
96
        }
97
    )
98
    fmt_result = run_black(rule_runner)
×
99
    assert "1 file reformatted, 1 file left unchanged" in fmt_result.stderr
×
100
    assert fmt_result.output == rule_runner.make_snapshot(
×
101
        {"good/BUILD": 'python_sources(name="t")\n', "bad/BUILD": 'python_sources(name="t")\n'}
102
    )
103
    assert fmt_result.did_change is True
×
104

105

106
@pytest.mark.parametrize(
2✔
107
    "config_path,extra_args",
108
    (["pyproject.toml", []], ["custom_config.toml", ["--black-config=custom_config.toml"]]),
109
)
110
def test_config_file(rule_runner: RuleRunner, config_path: str, extra_args: list[str]) -> None:
2✔
111
    rule_runner.write_files(
×
112
        {
113
            "BUILD": "python_sources(name='t')\n",
114
            config_path: "[tool.black]\nskip-string-normalization = 'true'\n",
115
        }
116
    )
117
    fmt_result = run_black(rule_runner, extra_args=extra_args)
×
118
    assert "1 file left unchanged" in fmt_result.stderr
×
119
    assert fmt_result.output == rule_runner.make_snapshot({"BUILD": "python_sources(name='t')\n"})
×
120
    assert fmt_result.did_change is False
×
121

122

123
def test_passthrough_args(rule_runner: RuleRunner) -> None:
2✔
124
    rule_runner.write_files({"BUILD": "python_sources(name='t')\n"})
×
125
    fmt_result = run_black(rule_runner, extra_args=["--black-args='--skip-string-normalization'"])
×
126
    assert "1 file left unchanged" in fmt_result.stderr
×
127
    assert fmt_result.output == rule_runner.make_snapshot({"BUILD": "python_sources(name='t')\n"})
×
128
    assert fmt_result.did_change is False
×
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

© 2026 Coveralls, Inc