Merge remote-tracking branch 'aosp/upstream' into master
* aosp/upstream:
Support trailing slash in pathtools.Match patterns
Bug: 111389216
Test: m checkbuild
Change-Id: I3ec96d6da9d6c00c1428a8c064db9b1b60a53edd
diff --git a/pathtools/glob.go b/pathtools/glob.go
index f7b32d7..57c91fb 100644
--- a/pathtools/glob.go
+++ b/pathtools/glob.go
@@ -222,6 +222,18 @@
return false, GlobLastRecursiveErr
}
+ patternDir := pattern[len(pattern)-1] == '/'
+ nameDir := name[len(name)-1] == '/'
+
+ if patternDir != nameDir {
+ return false, nil
+ }
+
+ if nameDir {
+ name = name[:len(name)-1]
+ pattern = pattern[:len(pattern)-1]
+ }
+
for {
var patternFile, nameFile string
pattern, patternFile = saneSplit(pattern)
diff --git a/pathtools/glob_test.go b/pathtools/glob_test.go
index 1d17e10..dda1404 100644
--- a/pathtools/glob_test.go
+++ b/pathtools/glob_test.go
@@ -516,3 +516,51 @@
t.Errorf("expected: %#v", testCase.deps)
}
}
+
+func TestMatch(t *testing.T) {
+ testCases := []struct {
+ pattern, name string
+ match bool
+ }{
+ {"a/*", "b/", false},
+ {"a/*", "b/a", false},
+ {"a/*", "b/b/", false},
+ {"a/*", "b/b/c", false},
+ {"a/**/*", "b/", false},
+ {"a/**/*", "b/a", false},
+ {"a/**/*", "b/b/", false},
+ {"a/**/*", "b/b/c", false},
+
+ {"a/*", "a/", false},
+ {"a/*", "a/a", true},
+ {"a/*", "a/b/", false},
+ {"a/*", "a/b/c", false},
+
+ {"a/*/", "a/", false},
+ {"a/*/", "a/a", false},
+ {"a/*/", "a/b/", true},
+ {"a/*/", "a/b/c", false},
+
+ {"a/**/*", "a/", false},
+ {"a/**/*", "a/a", true},
+ {"a/**/*", "a/b/", false},
+ {"a/**/*", "a/b/c", true},
+
+ {"a/**/*/", "a/", false},
+ {"a/**/*/", "a/a", false},
+ {"a/**/*/", "a/b/", true},
+ {"a/**/*/", "a/b/c", false},
+ }
+
+ for _, test := range testCases {
+ t.Run(test.pattern+","+test.name, func(t *testing.T) {
+ match, err := Match(test.pattern, test.name)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if match != test.match {
+ t.Errorf("want: %v, got %v", test.match, match)
+ }
+ })
+ }
+}