From 973f112c8f26f753f940fbe3c8d8fc2c2f462e2d Mon Sep 17 00:00:00 2001 From: isobel Date: Tue, 7 Nov 2023 15:25:18 +0000 Subject: [PATCH 1/2] Add regex wildcard matching for includes --- src/core/build_target.go | 9 ++++++--- test/include/BUILD | 4 ++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/core/build_target.go b/src/core/build_target.go index 1e4a5d5484..c5908edd79 100644 --- a/src/core/build_target.go +++ b/src/core/build_target.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "path/filepath" + "regexp" "sort" "strings" "sync" @@ -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. diff --git a/test/include/BUILD b/test/include/BUILD index fbc8d8e4c6..97ba5055b1 100644 --- a/test/include/BUILD +++ b/test/include/BUILD @@ -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", ) From 99114d79e9715ed241971d839dbb1fecf7aa7b75 Mon Sep 17 00:00:00 2001 From: isobel Date: Tue, 7 Nov 2023 15:39:22 +0000 Subject: [PATCH 2/2] Fix test --- test/include/BUILD | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/include/BUILD b/test/include/BUILD index 97ba5055b1..0fa33b97c9 100644 --- a/test/include/BUILD +++ b/test/include/BUILD @@ -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 '.*r' | 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", )