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

pantsbuild / pants / 22206482340

20 Feb 2026 12:44AM UTC coverage: 80.376% (+0.05%) from 80.324%
22206482340

Pull #23066

github

web-flow
Merge ddfa8ddd7 into 56952304a
Pull Request #23066: Extract JavaScript backend test resources to files

73 of 73 new or added lines in 2 files covered. (100.0%)

1093 existing lines in 48 files now uncovered.

78880 of 98139 relevant lines covered (80.38%)

3.35 hits per line

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

99.21
/src/python/pants/source/source_root_test.py
1
# Copyright 2015 Pants project contributors (see CONTRIBUTORS.md).
2
# Licensed under the Apache License, Version 2.0 (see LICENSE).
3

4
import os
1✔
5
from collections.abc import Iterable
1✔
6
from pathlib import PurePath
1✔
7

8
import pytest
1✔
9

10
from pants.engine.fs import PathGlobs, Paths
1✔
11
from pants.engine.rules import QueryRule
1✔
12
from pants.source.source_root import (
1✔
13
    OptionalSourceRoot,
14
    SourceRoot,
15
    SourceRootConfig,
16
    SourceRootRequest,
17
    SourceRootsRequest,
18
    SourceRootsResult,
19
    all_roots,
20
    get_optional_source_root,
21
)
22
from pants.source.source_root import rules as source_root_rules
1✔
23
from pants.testutil.option_util import create_subsystem
1✔
24
from pants.testutil.rule_runner import RuleRunner, run_rule_with_mocks
1✔
25
from pants.util.frozendict import FrozenDict
1✔
26

27

28
def _find_root(
1✔
29
    path: str,
30
    patterns: Iterable[str],
31
    marker_filenames: Iterable[str] | None = None,
32
    existing_marker_files: Iterable[str] | None = None,
33
) -> str | None:
34
    source_root_config = create_subsystem(
1✔
35
        SourceRootConfig,
36
        root_patterns=list(patterns),
37
        marker_filenames=list(marker_filenames or []),
38
    )
39

40
    # This inner function is passed as the callable to the mock, to allow recursion in the rule.
41
    def _mock_fs_check(pathglobs: PathGlobs) -> Paths:
1✔
42
        for glob in pathglobs.globs:
1✔
43
            if glob in (existing_marker_files or []):
1✔
44
                d, f = os.path.split(pathglobs.globs[0])
1✔
45
                return Paths(files=(f,), dirs=(d,))
1✔
46
        return Paths(files=tuple(), dirs=tuple())
1✔
47

48
    def _do_find_root(src_root_req: SourceRootRequest) -> OptionalSourceRoot:
1✔
49
        return run_rule_with_mocks(
1✔
50
            get_optional_source_root,
51
            rule_args=[src_root_req, source_root_config],
52
            mock_calls={
53
                "pants.engine.intrinsics.path_globs_to_paths": _mock_fs_check,
54
                "pants.source.source_root.get_optional_source_root": _do_find_root,
55
            },
56
        )
57

58
    source_root = _do_find_root(SourceRootRequest(PurePath(path))).source_root
1✔
59
    return None if source_root is None else source_root.path
1✔
60

61

62
def test_source_root_at_buildroot() -> None:
1✔
63
    def find_root(path):
1✔
64
        return _find_root(path, ("/",))
1✔
65

66
    assert "." == find_root("foo/bar.py")
1✔
67
    assert "." == find_root("foo/")
1✔
68
    assert "." == find_root("foo")
1✔
69
    with pytest.raises(ValueError, match="cannot contain `..` segment: ../foo/bar.py"):
1✔
70
        find_root("../foo/bar.py")
1✔
71

72

73
def test_fixed_source_roots() -> None:
1✔
74
    def find_root(path):
1✔
75
        return _find_root(path, ("/root1", "/foo/root2", "/root1/root3"))
1✔
76

77
    assert "root1" == find_root("root1/bar.py")
1✔
78
    assert "foo/root2" == find_root("foo/root2/bar/baz.py")
1✔
79
    assert "root1/root3" == find_root("root1/root3/qux.py")
1✔
80
    assert "root1/root3" == find_root("root1/root3/qux/quux.py")
1✔
81
    assert "root1/root3" == find_root("root1/root3")
1✔
82
    assert find_root("blah/blah.py") is None
1✔
83

84

85
def test_source_root_suffixes() -> None:
1✔
86
    def find_root(path):
1✔
87
        return _find_root(path, ("src/python", "/"))
1✔
88

89
    assert "src/python" == find_root("src/python/foo/bar.py")
1✔
90
    assert "src/python/foo/src/python" == find_root("src/python/foo/src/python/bar.py")
1✔
91
    assert "." == find_root("foo/bar.py")
1✔
92

93

94
def test_source_root_patterns() -> None:
1✔
95
    def find_root(path):
1✔
96
        return _find_root(path, ("src/*", "/project/*"))
1✔
97

98
    assert "src/python" == find_root("src/python/foo/bar.py")
1✔
99
    assert "src/python/foo/src/shell" == find_root("src/python/foo/src/shell/bar.sh")
1✔
100
    assert "project/python" == find_root("project/python/foo/bar.py")
1✔
101
    assert find_root("prefix/project/python/foo/bar.py") is None
1✔
102

103

104
def test_source_root_default_patterns() -> None:
1✔
105
    # Test that the default root patterns behave as expected.
106
    def find_root(path):
1✔
107
        return _find_root(path, tuple(SourceRootConfig.DEFAULT_ROOT_PATTERNS))
1✔
108

109
    assert "src/python" == find_root("src/python/foo/bar.py")
1✔
110
    assert "src" == find_root("src/baz/qux.py")
1✔
111
    assert "project1/src/python" == find_root("project1/src/python/foo/bar.py")
1✔
112
    assert "project2/src" == find_root("project2/src/baz/qux.py")
1✔
113
    assert "." == find_root("corge/grault.py")
1✔
114

115

116
def test_marker_file() -> None:
1✔
117
    def find_root(path):
1✔
118
        return _find_root(
1✔
119
            path,
120
            tuple(),
121
            ("SOURCE_ROOT",),
122
            (
123
                "project1/SOURCE_ROOT",
124
                "project2/src/SOURCE_ROOT",
125
            ),
126
        )
127

128
    assert "project1" == find_root("project1/foo/bar.py")
1✔
129
    assert "project1" == find_root("project1/foo/")
1✔
130
    assert "project1" == find_root("project1/foo")
1✔
131
    assert "project1" == find_root("project1/")
1✔
132
    assert "project1" == find_root("project1")
1✔
133
    assert "project2/src" == find_root("project2/src/baz/qux.py")
1✔
134
    assert "project2/src" == find_root("project2/src/baz/")
1✔
135
    assert "project2/src" == find_root("project2/src/baz")
1✔
136
    assert "project2/src" == find_root("project2/src/")
1✔
137
    assert "project2/src" == find_root("project2/src")
1✔
138
    assert find_root("project3/qux") is None
1✔
139
    assert find_root("project3/") is None
1✔
140
    assert find_root("project3") is None
1✔
141

142

143
def test_marker_file_nested_source_roots() -> None:
1✔
144
    def find_root(path):
1✔
145
        return _find_root(
1✔
146
            path,
147
            tuple(),
148
            ("SOURCE_ROOT",),
149
            (
150
                "SOURCE_ROOT",
151
                "project1/src/SOURCE_ROOT",
152
            ),
153
        )
154

155
    assert "project1/src" == find_root("project1/src/foo/bar.py")
1✔
156
    assert "project1/src" == find_root("project1/src/foo")
1✔
157
    assert "project1/src" == find_root("project1/src")
1✔
158
    assert "." == find_root("project1")
1✔
159
    assert "." == find_root("baz/qux.py")
1✔
160
    assert "." == find_root("baz")
1✔
161
    assert "." == find_root(".")
1✔
162
    assert "." == find_root("")
1✔
163

164

165
def test_multiple_marker_filenames() -> None:
1✔
166
    def find_root(path):
1✔
167
        return _find_root(
1✔
168
            path,
169
            tuple(),
170
            ("SOURCE_ROOT", "setup.py"),
171
            (
172
                "project1/javasrc/SOURCE_ROOT",
173
                "project2/setup.py",
174
            ),
175
        )
176

177
    assert "project1/javasrc" == find_root("project1/javasrc/foo/bar.java")
1✔
178
    assert "project2" == find_root("project2/foo/bar.py")
1✔
179

180

181
def test_marker_file_and_patterns() -> None:
1✔
182
    def find_root(path):
1✔
183
        return _find_root(path, ("src/python",), ("setup.py",), ("project1/setup.py",))
1✔
184

185
    assert "project1" == find_root("project1/foo/bar.py")
1✔
186
    assert "project2/src/python" == find_root("project2/src/python/baz/qux.py")
1✔
187

188

189
def test_all_roots() -> None:
1✔
190
    dirs = (
1✔
191
        "contrib/go/examples/src/go/src",
192
        "src/java",
193
        "src/python",
194
        "src/python/subdir/src/python",  # We allow source roots under source roots.
195
        "src/kotlin",
196
        "my/project/src/java",
197
        "src/example/java",
198
        "src/example/python",
199
        "fixed/root/jvm",
200
    )
201

202
    source_root_config = create_subsystem(
1✔
203
        SourceRootConfig,
204
        root_patterns=[
205
            "src/*",
206
            "src/example/*",
207
            "contrib/go/examples/src/go/src",
208
            # Dir does not exist, should not be listed as a root.
209
            "java",
210
            "fixed/root/jvm",
211
        ],
212
        marker_filenames=[],
213
    )
214

215
    # This function mocks out reading real directories off the file system.
216
    def provider_rule(_: PathGlobs) -> Paths:
1✔
217
        return Paths((), dirs)
1✔
218

219
    def source_root_mock_rule(req: SourceRootRequest) -> OptionalSourceRoot:
1✔
220
        for d in dirs:
1✔
221
            if str(req.path).startswith(d):
1✔
222
                return OptionalSourceRoot(SourceRoot(str(req.path)))
1✔
UNCOV
223
        return OptionalSourceRoot(None)
×
224

225
    output = run_rule_with_mocks(
1✔
226
        all_roots,
227
        rule_args=[source_root_config],
228
        mock_calls={
229
            "pants.engine.intrinsics.path_globs_to_paths": provider_rule,
230
            "pants.source.source_root.get_optional_source_root": source_root_mock_rule,
231
        },
232
    )
233

234
    assert {
1✔
235
        SourceRoot("contrib/go/examples/src/go/src"),
236
        SourceRoot("src/java"),
237
        SourceRoot("src/python"),
238
        SourceRoot("src/python/subdir/src/python"),
239
        SourceRoot("src/kotlin"),
240
        SourceRoot("src/example/java"),
241
        SourceRoot("src/example/python"),
242
        SourceRoot("my/project/src/java"),
243
        SourceRoot("fixed/root/jvm"),
244
    } == set(output)
245

246

247
def test_all_roots_with_root_at_buildroot() -> None:
1✔
248
    source_root_config = create_subsystem(
1✔
249
        SourceRootConfig,
250
        root_patterns=["/"],
251
        marker_filenames=[],
252
    )
253

254
    # This function mocks out reading real directories off the file system
255
    def provider_rule(_: PathGlobs) -> Paths:
1✔
256
        dirs = ("foo",)  # A python package at the buildroot.
1✔
257
        return Paths((), dirs)
1✔
258

259
    output = run_rule_with_mocks(
1✔
260
        all_roots,
261
        rule_args=[source_root_config],
262
        mock_calls={
263
            "pants.engine.intrinsics.path_globs_to_paths": provider_rule,
264
            "pants.source.source_root.get_optional_source_root": lambda req: OptionalSourceRoot(
265
                SourceRoot(".")
266
            ),
267
        },
268
    )
269
    assert {SourceRoot(".")} == set(output)
1✔
270

271

272
def test_source_roots_request() -> None:
1✔
273
    rule_runner = RuleRunner(
1✔
274
        rules=[
275
            *source_root_rules(),
276
            QueryRule(SourceRootsResult, (SourceRootsRequest,)),
277
        ]
278
    )
279
    rule_runner.set_options(["--source-root-patterns=['src/python','tests/python']"])
1✔
280
    req = SourceRootsRequest(
1✔
281
        files=(PurePath("src/python/foo/bar.py"), PurePath("tests/python/foo/bar_test.py")),
282
        dirs=(PurePath("src/python/foo"), PurePath("src/python/baz/qux")),
283
    )
284
    res = rule_runner.request(SourceRootsResult, [req])
1✔
285
    assert {
1✔
286
        PurePath("src/python/foo/bar.py"): SourceRoot("src/python"),
287
        PurePath("tests/python/foo/bar_test.py"): SourceRoot("tests/python"),
288
        PurePath("src/python/foo"): SourceRoot("src/python"),
289
        PurePath("src/python/baz/qux"): SourceRoot("src/python"),
290
    } == dict(res.path_to_root)
291

292

293
def test_root_to_paths() -> None:
1✔
294
    res = SourceRootsResult(
1✔
295
        FrozenDict(
296
            {
297
                PurePath("src/python/foo/bar.py"): SourceRoot("src/python"),
298
                PurePath("tests/python/foo/bar_test.py"): SourceRoot("tests/python"),
299
                PurePath("src/python/foo"): SourceRoot("src/python"),
300
                PurePath("src/python/baz/qux"): SourceRoot("src/python"),
301
            }
302
        )
303
    )
304
    assert res.root_to_paths() == FrozenDict(
1✔
305
        {
306
            SourceRoot("src/python"): (
307
                PurePath("src/python/baz/qux"),
308
                PurePath("src/python/foo"),
309
                PurePath("src/python/foo/bar.py"),
310
            ),
311
            SourceRoot("tests/python"): (PurePath("tests/python/foo/bar_test.py"),),
312
        }
313
    )
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