• 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

76.39
/src/python/pants/engine/internals/platform_rules_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
3✔
5

6
from textwrap import dedent
3✔
7
from unittest.mock import Mock
3✔
8

9
import pytest
3✔
10

11
from pants.build_graph.address import Address
3✔
12
from pants.core.environments.subsystems import EnvironmentsSubsystem
3✔
13
from pants.core.environments.target_types import (
3✔
14
    DockerEnvironmentTarget,
15
    DockerImageField,
16
    DockerPlatformField,
17
    EnvironmentTarget,
18
    LocalEnvironmentTarget,
19
    RemoteEnvironmentTarget,
20
    RemotePlatformField,
21
)
22
from pants.engine.env_vars import CompleteEnvironmentVars, EnvironmentVars, EnvironmentVarsRequest
3✔
23
from pants.engine.environment import EnvironmentName
3✔
24
from pants.engine.internals.platform_rules import (
3✔
25
    complete_environment_vars,
26
    current_platform,
27
    environment_path_variable,
28
)
29
from pants.engine.internals.session import SessionValues
3✔
30
from pants.engine.platform import Platform
3✔
31
from pants.engine.process import Process, ProcessResult
3✔
32
from pants.option.global_options import GlobalOptions
3✔
33
from pants.testutil.option_util import create_subsystem
3✔
34
from pants.testutil.rule_runner import QueryRule, RuleRunner, run_rule_with_mocks
3✔
35

36

37
@pytest.mark.platform_specific_behavior
3✔
38
def test_current_platform() -> None:
3✔
39
    def assert_platform(
3✔
40
        *,
41
        envs_enabled: bool = True,
42
        env_tgt: LocalEnvironmentTarget | RemoteEnvironmentTarget | DockerEnvironmentTarget | None,
43
        remote_execution: bool,
44
        expected: Platform,
45
    ) -> None:
46
        global_options = create_subsystem(GlobalOptions, remote_execution=remote_execution)
3✔
47
        name = "name"
3✔
48
        env_subsystem = create_subsystem(
3✔
49
            EnvironmentsSubsystem,
50
            names={name: "addr"} if envs_enabled else {},
51
        )
52
        result = run_rule_with_mocks(
3✔
53
            current_platform,
54
            rule_args=[EnvironmentTarget(name, env_tgt), global_options, env_subsystem],
55
        )
56
        assert result == expected
3✔
57

58
    assert_platform(env_tgt=None, remote_execution=False, expected=Platform.create_for_localhost())
3✔
59
    assert_platform(
3✔
60
        env_tgt=None, envs_enabled=False, remote_execution=True, expected=Platform.linux_x86_64
61
    )
62
    assert_platform(
3✔
63
        env_tgt=None,
64
        envs_enabled=True,
65
        remote_execution=True,
66
        expected=Platform.create_for_localhost(),
67
    )
68

69
    for re in (False, True):
3✔
70
        assert_platform(
3✔
71
            env_tgt=LocalEnvironmentTarget({}, Address("dir")),
72
            remote_execution=re,
73
            expected=Platform.create_for_localhost(),
74
        )
75

76
    for re in (False, True):
3✔
77
        assert_platform(
3✔
78
            env_tgt=DockerEnvironmentTarget(
79
                {
80
                    DockerImageField.alias: "my_img",
81
                    DockerPlatformField.alias: Platform.linux_arm64.value,
82
                },
83
                Address("dir"),
84
            ),
85
            remote_execution=re,
86
            expected=Platform.linux_arm64,
87
        )
88

89
    for re in (False, True):
3✔
90
        assert_platform(
3✔
91
            env_tgt=RemoteEnvironmentTarget(
92
                {RemotePlatformField.alias: Platform.linux_arm64.value}, Address("dir")
93
            ),
94
            remote_execution=re,
95
            expected=Platform.linux_arm64,
96
        )
97

98

99
@pytest.mark.platform_specific_behavior
3✔
100
def test_complete_env_vars() -> None:
3✔
101
    def assert_env_vars(
3✔
102
        *,
103
        envs_enabled: bool = True,
104
        env_tgt: LocalEnvironmentTarget | RemoteEnvironmentTarget | DockerEnvironmentTarget | None,
105
        remote_execution: bool,
106
        expected_env: str,
107
    ) -> None:
108
        global_options = create_subsystem(GlobalOptions, remote_execution=remote_execution)
3✔
109
        name = "name"
3✔
110
        env_subsystem = create_subsystem(
3✔
111
            EnvironmentsSubsystem,
112
            names={name: "addr"} if envs_enabled else {},
113
        )
114

115
        def mock_env_process(process: Process) -> ProcessResult:
3✔
116
            return Mock(
3✔
117
                stdout=b"DOCKER=true" if "Docker" in process.description else b"REMOTE=true"
118
            )
119

120
        result = run_rule_with_mocks(
3✔
121
            complete_environment_vars,
122
            rule_args=[
123
                SessionValues(
124
                    {CompleteEnvironmentVars: CompleteEnvironmentVars({"LOCAL": "true"})}
125
                ),
126
                EnvironmentTarget(name, env_tgt),
127
                global_options,
128
                env_subsystem,
129
            ],
130
            mock_calls={
131
                "pants.engine.process.fallible_to_exec_result_or_raise": mock_env_process,
132
            },
133
        )
134
        assert dict(result) == {expected_env: "true"}
3✔
135

136
    assert_env_vars(env_tgt=None, remote_execution=False, expected_env="LOCAL")
3✔
137
    assert_env_vars(env_tgt=None, envs_enabled=False, remote_execution=True, expected_env="REMOTE")
3✔
138
    assert_env_vars(env_tgt=None, envs_enabled=True, remote_execution=True, expected_env="LOCAL")
3✔
139

140
    for re in (False, True):
3✔
141
        assert_env_vars(
3✔
142
            env_tgt=LocalEnvironmentTarget({}, Address("dir")),
143
            remote_execution=re,
144
            expected_env="LOCAL",
145
        )
146

147
    for re in (False, True):
3✔
148
        assert_env_vars(
3✔
149
            env_tgt=DockerEnvironmentTarget({DockerImageField.alias: "my_img"}, Address("dir")),
150
            remote_execution=re,
151
            expected_env="DOCKER",
152
        )
153

154
    for re in (False, True):
3✔
155
        assert_env_vars(
3✔
156
            env_tgt=RemoteEnvironmentTarget({}, Address("dir")),
157
            remote_execution=re,
158
            expected_env="REMOTE",
159
        )
160

161

162
@pytest.mark.parametrize(
3✔
163
    ("env", "expected_entries"),
164
    [
165
        pytest.param(
166
            {"PATH": "foo/bar:baz:/qux/quux"}, ["foo/bar", "baz", "/qux/quux"], id="populated_PATH"
167
        ),
168
        pytest.param({"PATH": ""}, [], id="empty_PATH"),
169
        pytest.param({}, [], id="unset_PATH"),
170
    ],
171
)
172
def test_get_environment_paths(env: dict[str, str], expected_entries: list[str]) -> None:
3✔
173
    def mock_environment_vars_subset(_req: EnvironmentVarsRequest) -> EnvironmentVars:
×
174
        return EnvironmentVars(env)
×
175

176
    paths = run_rule_with_mocks(
×
177
        environment_path_variable,
178
        mock_calls={
179
            "pants.core.util_rules.env_vars.environment_vars_subset": mock_environment_vars_subset,
180
        },
181
    )
182
    assert list(paths) == expected_entries
×
183

184

185
def test_docker_complete_env_vars() -> None:
3✔
186
    rule_runner = RuleRunner(
×
187
        rules=[QueryRule(CompleteEnvironmentVars, [EnvironmentName])],
188
        target_types=[DockerEnvironmentTarget],
189
        inherent_environment=EnvironmentName("docker"),
190
    )
191
    localhost_platform = Platform.create_for_localhost()
×
192
    if localhost_platform == Platform.linux_arm64:
×
193
        image_sha = "65a4aad1156d8a0679537cb78519a17eb7142e05a968b26a5361153006224fdc"
×
194
        platform = Platform.linux_arm64.value
×
195
    else:
196
        image_sha = "a1801b843b1bfaf77c501e7a6d3f709401a1e0c83863037fa3aab063a7fdb9dc"
×
197
        platform = Platform.linux_x86_64.value
×
198

199
    rule_runner.write_files(
×
200
        {
201
            "BUILD": dedent(
202
                f"""\
203
                docker_environment(
204
                    name='docker',
205
                    image='centos@sha256:{image_sha}',
206
                    platform='{platform}',
207
                )
208
                """
209
            )
210
        }
211
    )
212
    rule_runner.set_options(["--environments-preview-names={'docker': '//:docker'}"])
×
213
    result = dict(rule_runner.request(CompleteEnvironmentVars, []))
×
214

215
    # HOSTNAME is not deterministic across machines, so we don't care about the value.
216
    assert "HOSTNAME" in result
×
217
    result.pop("HOSTNAME")
×
218
    assert dict(result) == {
×
219
        "PATH": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
220
        "HOME": "/root",
221
    }
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