• 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

64.0
/src/python/pants/backend/go/util_rules/tests_analysis_test.py
1
# Copyright 2021 Pants project contributors (see CONTRIBUTORS.md).
2
# Licensed under the Apache License, Version 2.0 (see LICENSE).
3
from __future__ import annotations
1✔
4

5
from textwrap import dedent
1✔
6

7
import pytest
1✔
8

9
from pants.backend.go.util_rules import (
1✔
10
    assembly,
11
    build_pkg,
12
    first_party_pkg,
13
    go_mod,
14
    import_analysis,
15
    link,
16
    sdk,
17
    tests_analysis,
18
    third_party_pkg,
19
)
20
from pants.backend.go.util_rules.tests_analysis import GeneratedTestMain, GenerateTestMainRequest
1✔
21
from pants.engine.addresses import Address
1✔
22
from pants.engine.fs import EMPTY_DIGEST
1✔
23
from pants.engine.rules import QueryRule
1✔
24
from pants.testutil.rule_runner import RuleRunner
1✔
25
from pants.util.ordered_set import FrozenOrderedSet
1✔
26

27

28
@pytest.fixture
1✔
29
def rule_runner() -> RuleRunner:
1✔
30
    rule_runner = RuleRunner(
1✔
31
        rules=[
32
            *assembly.rules(),
33
            *build_pkg.rules(),
34
            *import_analysis.rules(),
35
            *go_mod.rules(),
36
            *first_party_pkg.rules(),
37
            *third_party_pkg.rules(),
38
            *tests_analysis.rules(),
39
            *link.rules(),
40
            *sdk.rules(),
41
            QueryRule(GeneratedTestMain, [GenerateTestMainRequest]),
42
        ],
43
    )
44
    rule_runner.set_options([], env_inherit={"PATH"})
1✔
45
    return rule_runner
1✔
46

47

48
def test_basic_test_analysis(rule_runner: RuleRunner) -> None:
1✔
49
    input_digest = rule_runner.make_snapshot(
1✔
50
        {
51
            "foo_test.go": dedent(
52
                """
53
                package foo
54

55
                func TestThisIsATest(t *testing.T) {
56
                }
57

58
                func Test(t *testing.T) {
59
                }
60
                """
61
            ),
62
            "bar_test.go": dedent(
63
                """
64
                package foo_test
65

66
                func BenchmarkThisIsABenchmark(b *testing.B) {
67
                }
68

69
                func Benchmark(b *testing.B) {
70
                }
71
                """
72
            ),
73
        },
74
    ).digest
75

76
    metadata = rule_runner.request(
1✔
77
        GeneratedTestMain,
78
        [
79
            GenerateTestMainRequest(
80
                digest=input_digest,
81
                test_paths=FrozenOrderedSet(["foo_test.go"]),
82
                xtest_paths=FrozenOrderedSet(["bar_test.go"]),
83
                import_path="foo",
84
                register_cover=False,
85
                address=Address("foo"),
86
            )
87
        ],
88
    )
89

UNCOV
90
    assert metadata.digest != EMPTY_DIGEST
×
UNCOV
91
    assert metadata.has_tests
×
UNCOV
92
    assert metadata.has_xtests
×
93

94

95
def test_collect_examples(rule_runner: RuleRunner) -> None:
1✔
96
    input_digest = rule_runner.make_snapshot(
1✔
97
        {
98
            "foo_test.go": dedent(
99
                """
100
                package foo
101
                func ExampleEmptyOutputExpected() {
102
                    // Output:
103
                }
104
                // This does not have an `Output` comment and will be skipped.
105
                func ExampleEmptyOutputAndNoOutputDirective() {
106
                }
107
                func ExampleSomeOutput() {
108
                    fmt.Println("foo")
109
                    // Output: foo
110
                }
111
                func ExampleAnotherOne() {
112
                    fmt.Println("foo\\nbar\\n")
113
                    // Output:
114
                    // foo
115
                    // bar
116
                }
117
                """
118
            ),
119
        },
120
    ).digest
121

122
    metadata = rule_runner.request(
1✔
123
        GeneratedTestMain,
124
        [
125
            GenerateTestMainRequest(
126
                digest=input_digest,
127
                test_paths=FrozenOrderedSet(["foo_test.go"]),
128
                xtest_paths=FrozenOrderedSet(),
129
                import_path="foo",
130
                register_cover=False,
131
                address=Address("foo"),
132
            )
133
        ],
134
    )
135

UNCOV
136
    assert metadata.digest != EMPTY_DIGEST
×
UNCOV
137
    assert metadata.has_tests
×
UNCOV
138
    assert not metadata.has_xtests
×
139

140

141
def test_incorrect_signatures(rule_runner: RuleRunner) -> None:
1✔
142
    test_cases = [
1✔
143
        ("TestFoo(t *testing.T, a int)", "wrong signature for TestFoo"),
144
        ("TestFoo()", "wrong signature for TestFoo"),
145
        ("TestFoo(t *testing.B)", "wrong signature for TestFoo"),
146
        ("TestFoo(t *testing.M)", "wrong signature for TestFoo"),
147
        ("TestFoo(a int)", "wrong signature for TestFoo"),
148
        ("BenchmarkFoo(t *testing.B, a int)", "wrong signature for BenchmarkFoo"),
149
        ("BenchmarkFoo()", "wrong signature for BenchmarkFoo"),
150
        ("BenchmarkFoo(t *testing.T)", "wrong signature for BenchmarkFoo"),
151
        ("BenchmarkFoo(t *testing.M)", "wrong signature for BenchmarkFoo"),
152
        ("BenchmarkFoo(a int)", "wrong signature for BenchmarkFoo"),
153
    ]
154

155
    for test_sig, err_msg in test_cases:
1✔
156
        input_digest = rule_runner.make_snapshot(
1✔
157
            {
158
                "foo_test.go": dedent(
159
                    f"""
160
                    package foo
161
                    func {test_sig} {{
162
                    }}
163
                    """
164
                ),
165
            },
166
        ).digest
167

168
        result = rule_runner.request(
1✔
169
            GeneratedTestMain,
170
            [
171
                GenerateTestMainRequest(
172
                    digest=input_digest,
173
                    test_paths=FrozenOrderedSet(["foo_test.go"]),
174
                    xtest_paths=FrozenOrderedSet(),
175
                    import_path="foo",
176
                    register_cover=False,
177
                    address=Address("foo"),
178
                )
179
            ],
180
        )
UNCOV
181
        assert result.failed_exit_code_and_stderr is not None
×
UNCOV
182
        exit_code, stderr = result.failed_exit_code_and_stderr
×
UNCOV
183
        assert exit_code == 1
×
UNCOV
184
        assert err_msg in stderr
×
185

186

187
def test_duplicate_test_mains_same_file(rule_runner: RuleRunner) -> None:
1✔
188
    input_digest = rule_runner.make_snapshot(
1✔
189
        {
190
            "foo_test.go": dedent(
191
                """
192
                package foo
193

194
                func TestMain(m *testing.M) {
195
                }
196

197
                func TestMain(m *testing.M) {
198
                }
199
                """
200
            ),
201
        },
202
    ).digest
203

204
    result = rule_runner.request(
1✔
205
        GeneratedTestMain,
206
        [
207
            GenerateTestMainRequest(
208
                digest=input_digest,
209
                test_paths=FrozenOrderedSet(["foo_test.go", "bar_test.go"]),
210
                xtest_paths=FrozenOrderedSet(),
211
                import_path="foo",
212
                register_cover=False,
213
                address=Address("foo"),
214
            )
215
        ],
216
    )
UNCOV
217
    assert result.failed_exit_code_and_stderr is not None
×
UNCOV
218
    exit_code, stderr = result.failed_exit_code_and_stderr
×
UNCOV
219
    assert exit_code == 1
×
UNCOV
220
    assert "multiple definitions of TestMain" in stderr
×
221

222

223
def test_duplicate_test_mains_different_files(rule_runner: RuleRunner) -> None:
1✔
224
    input_digest = rule_runner.make_snapshot(
1✔
225
        {
226
            "foo_test.go": dedent(
227
                """
228
                package foo
229

230
                func TestMain(m *testing.M) {
231
                }
232
                """
233
            ),
234
            "bar_test.go": dedent(
235
                """
236
                package foo
237

238
                func TestMain(m *testing.M) {
239
                }
240
                """
241
            ),
242
        },
243
    ).digest
244

245
    result = rule_runner.request(
1✔
246
        GeneratedTestMain,
247
        [
248
            GenerateTestMainRequest(
249
                digest=input_digest,
250
                test_paths=FrozenOrderedSet(["foo_test.go", "bar_test.go"]),
251
                xtest_paths=FrozenOrderedSet(),
252
                import_path="foo",
253
                register_cover=False,
254
                address=Address("foo"),
255
            )
256
        ],
257
    )
UNCOV
258
    assert result.failed_exit_code_and_stderr is not None
×
UNCOV
259
    exit_code, stderr = result.failed_exit_code_and_stderr
×
UNCOV
260
    assert exit_code == 1
×
UNCOV
261
    assert "multiple definitions of TestMain" in stderr
×
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