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

pantsbuild / pants / 18252174847

05 Oct 2025 01:36AM UTC coverage: 43.382% (-36.9%) from 80.261%
18252174847

push

github

web-flow
run tests on mac arm (#22717)

Just doing the minimal to pull forward the x86_64 pattern.

ref #20993

25776 of 59416 relevant lines covered (43.38%)

1.3 hits per line

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

53.96
/src/python/pants/core/goals/check.py
1
# Copyright 2020 Pants project contributors (see CONTRIBUTORS.md).
2
# Licensed under the Apache License, Version 2.0 (see LICENSE).
3

4
from __future__ import annotations
3✔
5

6
import logging
3✔
7
from collections import defaultdict
3✔
8
from collections.abc import Iterable
3✔
9
from dataclasses import dataclass
3✔
10
from typing import Any, ClassVar, Generic, TypeVar, cast
3✔
11

12
from pants.core.environments.rules import EnvironmentNameRequest, resolve_environment_name
3✔
13
from pants.core.goals.lint import REPORT_DIR as REPORT_DIR  # noqa: F401
3✔
14
from pants.core.goals.multi_tool_goal_helper import (
3✔
15
    OnlyOption,
16
    determine_specified_tool_ids,
17
    write_reports,
18
)
19
from pants.core.util_rules.distdir import DistDir
3✔
20
from pants.engine.collection import Collection
3✔
21
from pants.engine.console import Console
3✔
22
from pants.engine.engine_aware import EngineAwareParameter, EngineAwareReturnType
3✔
23
from pants.engine.environment import EnvironmentName
3✔
24
from pants.engine.fs import EMPTY_DIGEST, Digest, Workspace
3✔
25
from pants.engine.goal import Goal, GoalSubsystem
3✔
26
from pants.engine.internals.selectors import concurrently
3✔
27
from pants.engine.process import FallibleProcessResult
3✔
28
from pants.engine.rules import QueryRule, collect_rules, goal_rule, implicitly, rule
3✔
29
from pants.engine.target import FieldSet, FilteredTargets
3✔
30
from pants.engine.unions import UnionMembership, union
3✔
31
from pants.util.logging import LogLevel
3✔
32
from pants.util.memo import memoized_property
3✔
33
from pants.util.meta import classproperty
3✔
34
from pants.util.strutil import Simplifier
3✔
35

36
logger = logging.getLogger(__name__)
3✔
37

38
_FS = TypeVar("_FS", bound=FieldSet)
3✔
39

40

41
@dataclass(frozen=True)
3✔
42
class CheckResult:
3✔
43
    exit_code: int
3✔
44
    stdout: str
3✔
45
    stderr: str
3✔
46
    partition_description: str | None = None
3✔
47
    report: Digest = EMPTY_DIGEST
3✔
48

49
    @staticmethod
3✔
50
    def from_fallible_process_result(
3✔
51
        process_result: FallibleProcessResult,
52
        *,
53
        partition_description: str | None = None,
54
        output_simplifier: Simplifier = Simplifier(),
55
        report: Digest = EMPTY_DIGEST,
56
    ) -> CheckResult:
57
        return CheckResult(
×
58
            exit_code=process_result.exit_code,
59
            stdout=output_simplifier.simplify(process_result.stdout),
60
            stderr=output_simplifier.simplify(process_result.stderr),
61
            partition_description=partition_description,
62
            report=report,
63
        )
64

65
    def metadata(self) -> dict[str, Any]:
3✔
66
        return {"partition": self.partition_description}
×
67

68

69
@dataclass(frozen=True)
3✔
70
class CheckResults(EngineAwareReturnType):
3✔
71
    """Zero or more CheckResult objects for a single type checker.
72

73
    Typically, type checkers will return one result. If they no-oped, they will return zero results.
74
    However, some type checkers may need to partition their input and thus may need to return
75
    multiple results.
76
    """
77

78
    results: tuple[CheckResult, ...]
3✔
79
    checker_name: str
3✔
80

81
    def __init__(self, results: Iterable[CheckResult], *, checker_name: str) -> None:
3✔
82
        object.__setattr__(self, "results", tuple(results))
×
83
        object.__setattr__(self, "checker_name", checker_name)
×
84

85
    @property
3✔
86
    def skipped(self) -> bool:
3✔
87
        return bool(self.results) is False
×
88

89
    @memoized_property
3✔
90
    def exit_code(self) -> int:
3✔
91
        return next((result.exit_code for result in self.results if result.exit_code != 0), 0)
×
92

93
    def level(self) -> LogLevel | None:
3✔
94
        if self.skipped:
×
95
            return LogLevel.DEBUG
×
96
        return LogLevel.ERROR if self.exit_code != 0 else LogLevel.INFO
×
97

98
    def message(self) -> str | None:
3✔
99
        if self.skipped:
×
100
            return f"{self.checker_name} skipped."
×
101
        message = self.checker_name
×
102
        message += (
×
103
            " succeeded." if self.exit_code == 0 else f" failed (exit code {self.exit_code})."
104
        )
105

106
        def msg_for_result(result: CheckResult) -> str:
×
107
            msg = ""
×
108
            if result.stdout:
×
109
                msg += f"\n{result.stdout}"
×
110
            if result.stderr:
×
111
                msg += f"\n{result.stderr}"
×
112
            if msg:
×
113
                msg = f"{msg.rstrip()}\n\n"
×
114
            return msg
×
115

116
        if len(self.results) == 1:
×
117
            results_msg = msg_for_result(self.results[0])
×
118
        else:
119
            results_msg = "\n"
×
120
            for i, result in enumerate(self.results):
×
121
                msg = f"Partition #{i + 1}"
×
122
                msg += (
×
123
                    f" - {result.partition_description}:" if result.partition_description else ":"
124
                )
125
                msg += msg_for_result(result) or "\n\n"
×
126
                results_msg += msg
×
127
        message += results_msg
×
128
        return message
×
129

130
    def cacheable(self) -> bool:
3✔
131
        """Is marked uncacheable to ensure that it always renders."""
132
        return False
×
133

134

135
@dataclass(frozen=True)
3✔
136
@union(in_scope_types=[EnvironmentName])
3✔
137
class CheckRequest(Generic[_FS], EngineAwareParameter):
3✔
138
    """A union for targets that should be checked.
139

140
    Subclass and install a member of this type to provide a checker.
141
    """
142

143
    field_set_type: ClassVar[type[_FS]]  # type: ignore[misc]
3✔
144
    tool_name: ClassVar[str]
3✔
145

146
    @classproperty
3✔
147
    def tool_id(cls) -> str:
3✔
148
        """The "id" of the tool, used in tool selection (Eg --only=<id>)."""
149
        return cls.tool_name
×
150

151
    field_sets: Collection[_FS]
3✔
152

153
    def __init__(self, field_sets: Iterable[_FS]) -> None:
3✔
154
        object.__setattr__(self, "field_sets", Collection[_FS](field_sets))
3✔
155

156
    def debug_hint(self) -> str:
3✔
157
        return self.tool_name
×
158

159
    def metadata(self) -> dict[str, Any]:
3✔
160
        return {"addresses": [fs.address.spec for fs in self.field_sets]}
×
161

162

163
@rule(polymorphic=True)
3✔
164
async def check(
3✔
165
    req: CheckRequest,
166
    environment_name: EnvironmentName,
167
) -> CheckResults:
168
    raise NotImplementedError()
×
169

170

171
class CheckSubsystem(GoalSubsystem):
3✔
172
    name = "check"
3✔
173
    help = "Run type checking or the lightest variant of compilation available for a language."
3✔
174

175
    @classmethod
3✔
176
    def activated(cls, union_membership: UnionMembership) -> bool:
3✔
177
        return CheckRequest in union_membership
×
178

179
    only = OnlyOption("checker", "mypy", "javac")
3✔
180

181

182
class Check(Goal):
3✔
183
    subsystem_cls = CheckSubsystem
3✔
184
    environment_behavior = Goal.EnvironmentBehavior.USES_ENVIRONMENTS
3✔
185

186

187
@goal_rule
3✔
188
async def check_goal(
3✔
189
    console: Console,
190
    workspace: Workspace,
191
    targets: FilteredTargets,
192
    dist_dir: DistDir,
193
    union_membership: UnionMembership,
194
    check_subsystem: CheckSubsystem,
195
) -> Check:
196
    request_types = cast("Iterable[type[CheckRequest]]", union_membership[CheckRequest])
×
197
    specified_ids = determine_specified_tool_ids("check", check_subsystem.only, request_types)
×
198

199
    requests = tuple(
×
200
        request_type(
201
            request_type.field_set_type.create(target)
202
            for target in targets
203
            if (
204
                request_type.tool_id in specified_ids
205
                and request_type.field_set_type.is_applicable(target)
206
            )
207
        )
208
        for request_type in request_types
209
    )
210

211
    request_to_field_set = [
×
212
        (request, field_set) for request in requests for field_set in request.field_sets
213
    ]
214

215
    environment_names = await concurrently(
×
216
        resolve_environment_name(EnvironmentNameRequest.from_field_set(field_set), **implicitly())
217
        for (_, field_set) in request_to_field_set
218
    )
219

220
    request_to_env_name = {
×
221
        (request, env_name)
222
        for (request, _), env_name in zip(request_to_field_set, environment_names)
223
    }
224

225
    # Run each check request in each valid environment (potentially multiple runs per tool)
226
    all_results = await concurrently(
×
227
        check(**implicitly({request: CheckRequest, env_name: EnvironmentName}))
228
        for (request, env_name) in request_to_env_name
229
    )
230

231
    results_by_tool: dict[str, list[CheckResult]] = defaultdict(list)
×
232
    for results in all_results:
×
233
        results_by_tool[results.checker_name].extend(results.results)
×
234

235
    write_reports(
×
236
        results_by_tool,
237
        workspace,
238
        dist_dir,
239
        goal_name=CheckSubsystem.name,
240
    )
241

242
    exit_code = 0
×
243
    if all_results:
×
244
        console.print_stderr("")
×
245
    for results in sorted(all_results, key=lambda results: results.checker_name):
×
246
        if results.skipped:
×
247
            continue
×
248
        elif results.exit_code == 0:
×
249
            sigil = console.sigil_succeeded()
×
250
            status = "succeeded"
×
251
        else:
252
            sigil = console.sigil_failed()
×
253
            status = "failed"
×
254
            exit_code = results.exit_code
×
255
        console.print_stderr(f"{sigil} {results.checker_name} {status}.")
×
256

257
    return Check(exit_code)
×
258

259

260
def rules():
3✔
261
    return [
×
262
        *collect_rules(),
263
        # NB: Would be unused otherwise.
264
        QueryRule(CheckSubsystem, []),
265
    ]
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