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

pantsbuild / pants / 18822648545

26 Oct 2025 07:24PM UTC coverage: 79.831% (-0.4%) from 80.28%
18822648545

Pull #22809

github

web-flow
Merge 9401c4830 into 170094e99
Pull Request #22809: golang: fix Go SDK version check for coverage experiment

0 of 1 new or added line in 1 file covered. (0.0%)

439 existing lines in 25 files now uncovered.

77436 of 97000 relevant lines covered (79.83%)

3.35 hits per line

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

91.11
/src/python/pants/backend/go/goals/tailor_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.go import target_type_rules
1✔
9
from pants.backend.go.goals.tailor import (
1✔
10
    PutativeGoTargetsRequest,
11
    has_go_mod_ancestor,
12
    has_package_main,
13
)
14
from pants.backend.go.goals.tailor import rules as go_tailor_rules
1✔
15
from pants.backend.go.target_types import GoBinaryTarget, GoModTarget, GoPackageTarget
1✔
16
from pants.backend.go.util_rules import (
1✔
17
    assembly,
18
    build_pkg,
19
    build_pkg_target,
20
    first_party_pkg,
21
    go_mod,
22
    link,
23
    sdk,
24
    third_party_pkg,
25
)
26
from pants.core.goals.tailor import AllOwnedSources, PutativeTarget, PutativeTargets
1✔
27
from pants.core.goals.tailor import rules as core_tailor_rules
1✔
28
from pants.engine.rules import QueryRule
1✔
29
from pants.testutil.rule_runner import RuleRunner
1✔
30

31

32
@pytest.fixture
1✔
33
def rule_runner() -> RuleRunner:
1✔
34
    rule_runner = RuleRunner(
1✔
35
        rules=[
36
            *go_tailor_rules(),
37
            *core_tailor_rules(),
38
            *go_mod.rules(),
39
            *first_party_pkg.rules(),
40
            *third_party_pkg.rules(),
41
            *sdk.rules(),
42
            *target_type_rules.rules(),
43
            *build_pkg.rules(),
44
            *build_pkg_target.rules(),
45
            *assembly.rules(),
46
            *link.rules(),
47
            QueryRule(PutativeTargets, [PutativeGoTargetsRequest, AllOwnedSources]),
48
        ],
49
        target_types=[
50
            GoModTarget,
51
            GoBinaryTarget,
52
            GoPackageTarget,
53
        ],
54
    )
55
    rule_runner.set_options([], env_inherit={"PATH"})
1✔
56
    return rule_runner
1✔
57

58

59
def test_find_go_mod_targets(rule_runner: RuleRunner) -> None:
1✔
60
    rule_runner.write_files(
1✔
61
        {
62
            "unowned/go.mod": "module pantsbuild.org/unowned\n",
63
            "owned/go.mod": "module pantsbuild.org/owned\n",
64
            "owned/BUILD": "go_mod()",
65
        }
66
    )
67
    putative_targets = rule_runner.request(
1✔
68
        PutativeTargets,
69
        [PutativeGoTargetsRequest(("unowned", "owned")), AllOwnedSources(["owned/go.mod"])],
70
    )
UNCOV
71
    assert putative_targets == PutativeTargets(
×
72
        [
73
            PutativeTarget.for_target_type(
74
                GoModTarget, path="unowned", name=None, triggering_sources=["go.mod"]
75
            )
76
        ]
77
    )
78

79

80
def test_find_go_package_targets(rule_runner: RuleRunner) -> None:
1✔
81
    rule_runner.write_files(
1✔
82
        {
83
            "unowned/go.mod": "module pantsbuild.org/unowned\n",
84
            "unowned/f.go": "",
85
            "unowned/f1.go": "",
86
            "unowned/BUILD": "go_mod(name='mod')",
87
            "owned/go.mod": "module pantsbuild.org/owned\n",
88
            "owned/f.go": "",
89
            "owned/BUILD": "go_package()\ngo_mod(name='mod')\n",
90
            # Any `.go` files under a `testdata` or `vendor` folder should be ignored.
91
            "unowned/testdata/f.go": "",
92
            "unowned/testdata/subdir/f.go": "",
93
            "unowned/vendor/example.com/foo/bar.go": "",
94
            # Except if `vendor` is the last directory.
95
            "unowned/cmd/vendor/main.go": "",
96
            "no_go_mod/f.go": "",
97
        }
98
    )
99
    putative_targets = rule_runner.request(
1✔
100
        PutativeTargets,
101
        [
102
            PutativeGoTargetsRequest(
103
                (
104
                    "unowned",
105
                    "owned",
106
                    "unowned/testdata",
107
                    "unowned/vendor",
108
                    "unowned/cmd/vendor",
109
                    "no_go_mod",
110
                )
111
            ),
112
            AllOwnedSources(["owned/f.go", "unowned/go.mod", "owned/go.mod"]),
113
        ],
114
    )
UNCOV
115
    assert putative_targets == PutativeTargets(
×
116
        [
117
            PutativeTarget.for_target_type(
118
                GoPackageTarget,
119
                path="unowned",
120
                name=None,
121
                triggering_sources=["f.go", "f1.go"],
122
            ),
123
            PutativeTarget.for_target_type(
124
                GoPackageTarget,
125
                path="unowned/cmd/vendor",
126
                name=None,
127
                triggering_sources=["main.go"],
128
            ),
129
        ]
130
    )
131

132

133
def test_cgo_sources(rule_runner: RuleRunner) -> None:
1✔
134
    rule_runner.write_files(
1✔
135
        {
136
            "foo/BUILD": "go_mod()",
137
            "foo/go.mod": "module pantsbuild.org/example\n",
138
            "foo/main.go": "",
139
            "foo/native.c": "",
140
            "foo/another.c": "",
141
            "foo/native.h": "",
142
            "foo/assembly.s": "",
143
            "c_only/native.c": "",
144
            "c_only/assembly.s": "",
145
            "c_only/header.h": "",
146
        }
147
    )
148
    putative_targets = rule_runner.request(
1✔
149
        PutativeTargets,
150
        [
151
            PutativeGoTargetsRequest(("foo",)),
152
            AllOwnedSources(["foo/go.mod"]),
153
        ],
154
    )
UNCOV
155
    assert putative_targets == PutativeTargets(
×
156
        [
157
            PutativeTarget.for_target_type(
158
                GoPackageTarget,
159
                path="foo",
160
                name=None,
161
                kwargs={"sources": ("*.go", "*.c", "*.s", "*.h")},
162
                triggering_sources=["main.go", "another.c", "assembly.s", "native.c", "native.h"],
163
            ),
164
        ]
165
    )
166

167

168
def test_find_go_binary_targets(rule_runner: RuleRunner) -> None:
1✔
169
    rule_runner.write_files(
1✔
170
        {
171
            "missing_binary_tgt/go.mod": "module pantsbuild.org/missing_binary_tgt\n",
172
            "missing_binary_tgt/app.go": "package main",
173
            "missing_binary_tgt/BUILD": "go_package()\ngo_mod(name='mod')\n",
174
            "tgt_already_exists/go.mod": "module pantsbuild.org/tgt_already_exists\n",
175
            "tgt_already_exists/app.go": "package main",
176
            "tgt_already_exists/BUILD": "go_binary(name='bin')\ngo_package()\ngo_mod(name='mod')\n",
177
            "missing_pkg_and_binary_tgt/go.mod": "module pantsbuild.org/missing_pkg_and_binary_tgt\n",
178
            "missing_pkg_and_binary_tgt/app.go": "package main",
179
            "missing_pkg_and_binary_tgt/BUILD": "go_mod(name='mod')\n",
180
            "main_set_to_different_dir/go.mod": "module pantsbuild.org/main_set_to_different_dir\n",
181
            "main_set_to_different_dir/subdir/app.go": "package main",
182
            "main_set_to_different_dir/subdir/BUILD": "go_package()",
183
            "main_set_to_different_dir/BUILD": "go_binary(main='main_set_to_different_dir/subdir')\ngo_mod(name='mod')",
184
            "no_go_mod/app.go": "package main",
185
        }
186
    )
187
    putative_targets = rule_runner.request(
1✔
188
        PutativeTargets,
189
        [
190
            PutativeGoTargetsRequest(
191
                (
192
                    "missing_binary_tgt",
193
                    "tgt_already_exists",
194
                    "missing_pkg_and_binary_tgt",
195
                    "main_set_to_different_dir",
196
                    "no_go_mod",
197
                )
198
            ),
199
            AllOwnedSources(
200
                [
201
                    "missing_binary_tgt/go.mod",
202
                    "missing_binary_tgt/app.go",
203
                    "tgt_already_exists/go.mod",
204
                    "tgt_already_exists/app.go",
205
                    "missing_pkg_and_binary_tgt/go.mod",
206
                    "main_set_to_different_dir/go.mod",
207
                    "main_set_to_different_dir/subdir/app.go",
208
                ]
209
            ),
210
        ],
211
    )
UNCOV
212
    assert putative_targets == PutativeTargets(
×
213
        [
214
            PutativeTarget.for_target_type(
215
                GoBinaryTarget,
216
                path="missing_binary_tgt",
217
                name="bin",
218
                triggering_sources=[],
219
            ),
220
            PutativeTarget.for_target_type(
221
                GoPackageTarget,
222
                path="missing_pkg_and_binary_tgt",
223
                name="missing_pkg_and_binary_tgt",
224
                triggering_sources=["app.go"],
225
                kwargs={},
226
            ),
227
            PutativeTarget.for_target_type(
228
                GoBinaryTarget,
229
                path="missing_pkg_and_binary_tgt",
230
                name="bin",
231
                triggering_sources=[],
232
            ),
233
        ]
234
    )
235

236

237
def test_has_package_main() -> None:
1✔
238
    assert has_package_main(b"package main")
1✔
239
    assert has_package_main(b"package main // comment 1233")
1✔
240
    assert has_package_main(b"\n\npackage main\n")
1✔
241
    assert not has_package_main(b"package foo")
1✔
242
    assert not has_package_main(b'var = "package main"')
1✔
243
    assert not has_package_main(b"   package main")
1✔
244

245

246
def test_has_go_mod_ancestor() -> None:
1✔
247
    assert has_go_mod_ancestor("dir/subdir", frozenset({"dir/subdir"})) is True
1✔
248
    assert has_go_mod_ancestor("dir/subdir", frozenset({"dir/subdir/child"})) is False
1✔
249
    assert has_go_mod_ancestor("dir/subdir", frozenset({"dir/another"})) is False
1✔
250
    assert has_go_mod_ancestor("dir/subdir", frozenset({""})) is True
1✔
251
    assert has_go_mod_ancestor("dir/subdir", frozenset({"another", "dir/another", "dir"})) is True
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