Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wildcard regex matching for includes #2947

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/core/build_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"sync"
Expand Down Expand Up @@ -1082,10 +1083,12 @@ func match(pattern, s string) bool {
if pattern == s {
return true
}
if strings.HasSuffix(pattern, "*") && strings.HasPrefix(s, pattern[:len(pattern)-1]) {
return true
re, err := regexp.Compile(pattern)
if err != nil {
log.Warning("Error compiling pattern %s: %v", pattern, err)
return false
}
return false
return re.MatchString(s)
}

// PrefixedLabels returns all labels of this target with the given prefix.
Expand Down
4 changes: 2 additions & 2 deletions test/include/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ please_repo_e2e_test(
# Test that we can include targets using a wildcard
please_repo_e2e_test(
name = "include_contains_wildcard_test",
plz_command = "numtargets=$(plz query alltargets //:all --include 'f*' | wc -l) && if [[ $numtargets -ne 1 ]]; then echo \"Expected 1 target. Got $numtargets\" && exit 1; fi",
plz_command = "numtargets=$(plz query alltargets //:all --include '^f.*' | wc -l) && if [[ $numtargets -ne 1 ]]; then echo \"Expected 1 target. Got $numtargets\" && exit 1; fi",
repo = "test_repo",
)

# Test that we can exclude targets using a wildcard
please_repo_e2e_test(
name = "exclude_contains_wildcard_test",
plz_command = "numtargets=$(plz query alltargets //:all --exclude 'ba*' | wc -l) && if [[ $numtargets -ne 1 ]]; then echo \"Expected 1 target. Got $numtargets\" && exit 1; fi",
plz_command = "numtargets=$(plz query alltargets //:all --exclude '.*r$' | wc -l) && if [[ $numtargets -ne 1 ]]; then echo \"Expected 1 target. Got $numtargets\" && exit 1; fi",
repo = "test_repo",
)
Loading