• 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

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

5
import re
×
6
from collections.abc import Callable
×
7

8
# Adapted from Go toolchain:
9
# https://github.com/golang/go/blob/6a70292d1cb3464e5b2c2c03341e5148730a1889/src/cmd/internal/pkgpattern/pkgpattern.go
10
#
11
# // Copyright 2022 The Go Authors. All rights reserved.
12
# // Use of this source code is governed by a BSD-style
13
# // license that can be found in the LICENSE file.
14

15

16
def match_pattern(pattern: str) -> Callable[[str], bool]:
×
17
    """MatchPattern(pattern)(name) reports whether name matches pattern. Pattern is a limited glob
18
    pattern in which '...' means 'any string' and there is no other special syntax. Unfortunately,
19
    there are two special cases. Quoting "go help packages":
20

21
    First, /... at the end of the pattern can match an empty string, so that net/... matches both
22
    net and packages in its subdirectories, like net/http. Second, any slash-separated pattern
23
    element containing a wildcard never participates in a match of the "vendor" element in the path
24
    of a vendored package, so that ./... does not match packages in subdirectories of ./vendor or
25
    ./mycode/vendor, but ./vendor/... and ./mycode/vendor/... do. Note, however, that a directory
26
    named vendor that itself contains code is not a vendored package: cmd/vendor would be a command
27
    named vendor, and the pattern cmd/... matches it.
28
    """
29
    return _match_pattern_internal(pattern, True)
×
30

31

32
def match_simple_pattern(pattern: str) -> Callable[[str], bool]:
×
33
    """MatchSimplePattern returns a function that can be used to check whether a given name matches
34
    a pattern, where pattern is a limited glob pattern in which '...' means 'any string', with no
35
    other special syntax.
36

37
    There is one special case for MatchPatternSimple: according to the rules in "go help packages":
38
    a /... at the end of the pattern can match an empty string, so that net/... matches both net and
39
    packages in its subdirectories, like net/http.
40
    """
41
    return _match_pattern_internal(pattern, False)
×
42

43

44
def _match_pattern_internal(pattern: str, vendor_exclude: bool) -> Callable[[str], bool]:
×
45
    # Convert pattern to regular expression.
46
    # The strategy for the trailing /... is to nest it in an explicit ? expression.
47
    # The strategy for the vendor exclusion is to change the unmatchable
48
    # vendor strings to a disallowed code point (vendorChar) and to use
49
    # "(anything but that codepoint)*" as the implementation of the ... wildcard.
50
    # This is a bit complicated, but the obvious alternative,
51
    # namely a handwritten search like in most shell glob matchers,
52
    # is too easy to make accidentally exponential.
53
    # Using package regexp guarantees linear-time matching.
54

55
    vendor_char = chr(0)  # "\x00"
×
56

57
    if vendor_exclude and vendor_char in pattern:
×
58
        return lambda _name: False
×
59

60
    r: str = re.escape(pattern)
×
61
    wild = ".*"
×
62
    if vendor_exclude:
×
63
        wild = rf"[^{vendor_char}]*"
×
64
        r = _replace_vendor(r, vendor_char)
×
65

66
        suffix = rf"/{vendor_char}/\.\.\."
×
67
        if r.endswith(suffix):
×
68
            r = r[0 : -len(suffix)] + rf"(/vendor|/{vendor_char}/\.\.\.)"
×
69
        elif r == rf"{vendor_char}/\.\.\.":
×
70
            r = rf"(/vendor|/{vendor_char}/\.\.\.)"
×
71

72
    suffix = r"/\.\.\."
×
73
    if r.endswith(suffix):
×
74
        r = r[0 : -len(suffix)] + r"(/\.\.\.)?"
×
75
    r = r.replace(r"\.\.\.", wild)
×
76

77
    reg = re.compile(rf"^{r}$")
×
78

79
    def f(name: str) -> bool:
×
80
        if vendor_exclude:
×
81
            if vendor_char in name:
×
82
                return False
×
83
            name = _replace_vendor(name, vendor_char)
×
84
        return bool(reg.match(name))
×
85

86
    return f
×
87

88

89
# replaceVendor returns the result of replacing
90
# non-trailing vendor path elements in x with repl.
91
def _replace_vendor(x: str, repl: str) -> str:
×
92
    if "vendor" not in x:
×
93
        return x
×
94

95
    elems = x.split("/")
×
96
    for i, elem in enumerate(elems[0:-1]):
×
97
        if elem == "vendor":
×
98
            elems[i] = repl
×
99
    return "/".join(elems)
×
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