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

pantsbuild / pants / 18225878979

03 Oct 2025 02:59PM UTC coverage: 80.269% (-0.006%) from 80.275%
18225878979

Pull #22717

github

web-flow
Merge b17e03226 into f7b1ba442
Pull Request #22717: wip: run tests on mac arm

77232 of 96217 relevant lines covered (80.27%)

3.63 hits per line

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

98.61
/src/python/pants/backend/tools/yamllint/rules_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
4✔
5

6
from typing import Any, overload
4✔
7

8
import pytest
4✔
9

10
from pants.backend.python.util_rules.pex import rules as pex_rules
4✔
11
from pants.backend.tools.yamllint.rules import PartitionInfo, YamllintRequest
4✔
12
from pants.backend.tools.yamllint.rules import rules as yamllint_rules
4✔
13
from pants.core.goals.lint import LintResult, Partitions
4✔
14
from pants.core.util_rules import config_files, external_tool, source_files
4✔
15
from pants.engine.fs import PathGlobs
4✔
16
from pants.engine.internals.native_engine import Snapshot
4✔
17
from pants.testutil.rule_runner import QueryRule, RuleRunner
4✔
18

19

20
@pytest.fixture
4✔
21
def rule_runner() -> RuleRunner:
4✔
22
    return RuleRunner(
4✔
23
        rules=[
24
            *yamllint_rules(),
25
            *config_files.rules(),
26
            *source_files.rules(),
27
            *external_tool.rules(),
28
            *pex_rules(),
29
            QueryRule(Partitions, [YamllintRequest.PartitionRequest]),
30
            QueryRule(LintResult, [YamllintRequest.Batch]),
31
        ],
32
    )
33

34

35
GOOD_FILE = """\
4✔
36
this: is
37
valid: YAML
38
"""
39

40
DOCUMENT_START_DISABLE_CONFIG = """\
4✔
41
extends: default
42

43
rules:
44
  document-start: disable
45
"""
46

47
RELAXED_CONFIG = """
4✔
48
extends: relaxed
49
"""
50

51
GOOD_FILE_WITH_START = """\
4✔
52
---
53
this: is
54
valid: YAML
55
"""
56

57
REPEATED_KEY = """\
4✔
58
---
59
this: key
60
is: repeated
61
this: here
62
"""
63

64
NOT_YAML = """\
4✔
65
This definitely
66
isn't valid YAML,
67
is it?
68
"""
69

70

71
@overload
72
def run_yamllint(
73
    rule_runner: RuleRunner,
74
    *,
75
    extra_args: list[str] | None = None,
76
    expected_partitions: None = None,
77
) -> LintResult: ...
78

79

80
@overload
81
def run_yamllint(
82
    rule_runner: RuleRunner,
83
    *,
84
    extra_args: list[str] | None = None,
85
    expected_partitions: tuple[tuple[str, ...], dict[str, tuple[str, ...]]],
86
) -> list[LintResult]: ...
87

88

89
def run_yamllint(
4✔
90
    rule_runner: RuleRunner,
91
    *,
92
    extra_args: list[str] | None = None,
93
    expected_partitions: tuple[tuple[str, ...], dict[str, tuple[str, ...]]] | None = None,
94
) -> LintResult | list[LintResult]:
95
    rule_runner.set_options(
4✔
96
        ["--backend-packages=pants.backend.tools.experimental.yamllint", *(extra_args or ())],
97
        env_inherit={"PATH", "PYENV_ROOT"},
98
    )
99
    snapshot = rule_runner.request(Snapshot, [PathGlobs(["**"])])
4✔
100
    partitions = rule_runner.request(
4✔
101
        Partitions[Any, PartitionInfo], [YamllintRequest.PartitionRequest(snapshot.files)]
102
    )
103

104
    if expected_partitions:
4✔
105
        expected_default_partition = expected_partitions[0]
1✔
106
        default_partitions = tuple(p for p in partitions if p.metadata.config_snapshot is None)
1✔
107
        if expected_default_partition:
1✔
108
            assert len(default_partitions) == 1
1✔
109
            assert default_partitions[0].elements == expected_default_partition
1✔
110
        else:
111
            assert len(default_partitions) == 0
×
112

113
        config_partitions = tuple(p for p in partitions if p.metadata.config_snapshot)
1✔
114
        expected_config_partitions = expected_partitions[1]
1✔
115
        assert len(config_partitions) == len(expected_config_partitions)
1✔
116
        for partition in config_partitions:
1✔
117
            assert partition.metadata.config_snapshot is not None
1✔
118
            config_file = partition.metadata.config_snapshot.files[0]
1✔
119

120
            assert config_file in expected_config_partitions
1✔
121
            assert partition.elements == expected_config_partitions[config_file]
1✔
122
    else:
123
        assert len(partitions) == 1
4✔
124

125
    results = [
4✔
126
        rule_runner.request(
127
            LintResult,
128
            [
129
                YamllintRequest.Batch(
130
                    "",
131
                    partition.elements,
132
                    partition_metadata=partition.metadata,
133
                )
134
            ],
135
        )
136
        for partition in partitions
137
    ]
138
    return results if expected_partitions else results[0]
4✔
139

140

141
def assert_success(rule_runner: RuleRunner, *, extra_args: list[str] | None = None) -> None:
4✔
142
    result = run_yamllint(rule_runner, extra_args=extra_args)
1✔
143
    assert result.exit_code == 0
1✔
144
    assert not result.stdout
1✔
145
    assert not result.stderr
1✔
146

147

148
def assert_failure_with(
4✔
149
    snippet: str,
150
    rule_runner: RuleRunner,
151
    *,
152
    extra_args: list[str] | None = None,
153
) -> None:
154
    result = run_yamllint(rule_runner, extra_args=extra_args)
4✔
155
    assert result.exit_code == 1
4✔
156
    assert snippet in result.stdout
4✔
157

158

159
def test_passing(rule_runner: RuleRunner) -> None:
4✔
160
    rule_runner.write_files({"test.yaml": GOOD_FILE_WITH_START, "not_yaml": NOT_YAML})
1✔
161
    assert_success(rule_runner)
1✔
162

163

164
@pytest.mark.platform_specific_behavior
4✔
165
def test_failure(rule_runner: RuleRunner) -> None:
4✔
166
    rule_runner.write_files({"test.yaml": REPEATED_KEY, "not_yaml": NOT_YAML})
4✔
167
    assert_failure_with('duplication of key "this"', rule_runner)
4✔
168

169

170
def test_config_autodiscovery(rule_runner: RuleRunner) -> None:
4✔
171
    rule_runner.write_files(
1✔
172
        {
173
            "test.yaml": GOOD_FILE,
174
            ".yamllint": DOCUMENT_START_DISABLE_CONFIG,
175
            "not_yaml": NOT_YAML,
176
        }
177
    )
178
    assert_success(rule_runner)
1✔
179

180

181
def test_config_autodiscovery_yml(rule_runner: RuleRunner) -> None:
4✔
182
    rule_runner.write_files(
1✔
183
        {
184
            "test.yaml": GOOD_FILE,
185
            ".yamllint.yml": DOCUMENT_START_DISABLE_CONFIG,
186
            "not_yaml": NOT_YAML,
187
        }
188
    )
189
    assert_success(rule_runner, extra_args=["--yamllint-config-file-name=.yamllint.yml"])
1✔
190

191

192
def test_multi_config(rule_runner: RuleRunner) -> None:
4✔
193
    rule_runner.write_files(
1✔
194
        {
195
            "test.yaml": GOOD_FILE_WITH_START,
196
            "subdir1/.yamllint": DOCUMENT_START_DISABLE_CONFIG,
197
            "subdir1/subdir.yaml": GOOD_FILE,
198
            "subdir1/nested/subdir.yaml": GOOD_FILE,
199
            "subdir2/.yamllint": RELAXED_CONFIG,
200
            "subdir2/subdir.yaml": GOOD_FILE,
201
            "not_yaml": NOT_YAML,
202
        }
203
    )
204
    results = run_yamllint(
1✔
205
        rule_runner,
206
        extra_args=["--yamllint-args=-s"],
207
        expected_partitions=(
208
            ("test.yaml",),
209
            {
210
                "subdir1/.yamllint": ("subdir1/nested/subdir.yaml", "subdir1/subdir.yaml"),
211
                "subdir2/.yamllint": ("subdir2/subdir.yaml",),
212
            },
213
        ),
214
    )
215
    assert len(results) == 3
1✔
216
    for result in results:
1✔
217
        assert result.exit_code == 0
1✔
218
        assert not result.stdout
1✔
219
        assert not result.stderr
1✔
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