From 3b0497541a824357ab00266d7c34ef42095cdd5a Mon Sep 17 00:00:00 2001 From: Derek McGowan Date: Fri, 13 Jan 2017 17:08:46 -0800 Subject: [PATCH] Move match function to helpers Update match function to operate on familiar names Signed-off-by: Derek McGowan (github: dmcgowan) --- reference/helpers.go | 12 ++++++ reference/normalize_test.go | 78 +++++++++++++++++++++++++++++++++++++ reference/reference.go | 11 ------ reference/reference_test.go | 78 ------------------------------------- 4 files changed, 90 insertions(+), 89 deletions(-) diff --git a/reference/helpers.go b/reference/helpers.go index a8f46cedd..fbcd2c082 100644 --- a/reference/helpers.go +++ b/reference/helpers.go @@ -1,5 +1,7 @@ package reference +import "path" + // IsNameOnly returns true if reference only contains a repo name. func IsNameOnly(ref Named) bool { if _, ok := ref.(NamedTagged); ok { @@ -28,3 +30,13 @@ func FamiliarString(ref Reference) string { } return ref.String() } + +// FamiliarMatch reports whether ref matches the specified pattern. +// See https://godoc.org/path#Match for supported patterns. +func FamiliarMatch(pattern string, ref Reference) (bool, error) { + matched, err := path.Match(pattern, FamiliarString(ref)) + if namedRef, isNamed := ref.(Named); isNamed && !matched { + matched, _ = path.Match(pattern, FamiliarName(namedRef)) + } + return matched, err +} diff --git a/reference/normalize_test.go b/reference/normalize_test.go index 9697a36be..948da0b77 100644 --- a/reference/normalize_test.go +++ b/reference/normalize_test.go @@ -503,3 +503,81 @@ func TestNormalizedSplitHostname(t *testing.T) { } } } + +func TestMatchError(t *testing.T) { + named, err := ParseAnyReference("foo") + if err != nil { + t.Fatal(err) + } + _, err = FamiliarMatch("[-x]", named) + if err == nil { + t.Fatalf("expected an error, got nothing") + } +} + +func TestMatch(t *testing.T) { + matchCases := []struct { + reference string + pattern string + expected bool + }{ + { + reference: "foo", + pattern: "foo/**/ba[rz]", + expected: false, + }, + { + reference: "foo/any/bat", + pattern: "foo/**/ba[rz]", + expected: false, + }, + { + reference: "foo/a/bar", + pattern: "foo/**/ba[rz]", + expected: true, + }, + { + reference: "foo/b/baz", + pattern: "foo/**/ba[rz]", + expected: true, + }, + { + reference: "foo/c/baz:tag", + pattern: "foo/**/ba[rz]", + expected: true, + }, + { + reference: "foo/c/baz:tag", + pattern: "foo/*/baz:tag", + expected: true, + }, + { + reference: "foo/c/baz:tag", + pattern: "foo/c/baz:tag", + expected: true, + }, + { + reference: "example.com/foo/c/baz:tag", + pattern: "*/foo/c/baz", + expected: true, + }, + { + reference: "example.com/foo/c/baz:tag", + pattern: "example.com/foo/c/baz", + expected: true, + }, + } + for _, c := range matchCases { + named, err := ParseAnyReference(c.reference) + if err != nil { + t.Fatal(err) + } + actual, err := FamiliarMatch(c.pattern, named) + if err != nil { + t.Fatal(err) + } + if actual != c.expected { + t.Fatalf("expected %s match %s to be %v, was %v", c.reference, c.pattern, c.expected, actual) + } + } +} diff --git a/reference/reference.go b/reference/reference.go index 888e9b6d3..57ce88a89 100644 --- a/reference/reference.go +++ b/reference/reference.go @@ -27,7 +27,6 @@ package reference import ( "errors" "fmt" - "path" "strings" "github.com/opencontainers/go-digest" @@ -317,16 +316,6 @@ func WithDigest(name Named, digest digest.Digest) (Canonical, error) { }, nil } -// Match reports whether ref matches the specified pattern. -// See https://godoc.org/path#Match for supported patterns. -func Match(pattern string, ref Reference) (bool, error) { - matched, err := path.Match(pattern, ref.String()) - if namedRef, isNamed := ref.(Named); isNamed && !matched { - matched, _ = path.Match(pattern, namedRef.Name()) - } - return matched, err -} - // TrimNamed removes any tag or digest from the named reference. func TrimNamed(ref Named) Named { domain, path := SplitHostname(ref) diff --git a/reference/reference_test.go b/reference/reference_test.go index e02702083..b1264dbdc 100644 --- a/reference/reference_test.go +++ b/reference/reference_test.go @@ -583,81 +583,3 @@ func TestWithDigest(t *testing.T) { } } } - -func TestMatchError(t *testing.T) { - named, err := Parse("foo") - if err != nil { - t.Fatal(err) - } - _, err = Match("[-x]", named) - if err == nil { - t.Fatalf("expected an error, got nothing") - } -} - -func TestMatch(t *testing.T) { - matchCases := []struct { - reference string - pattern string - expected bool - }{ - { - reference: "foo", - pattern: "foo/**/ba[rz]", - expected: false, - }, - { - reference: "foo/any/bat", - pattern: "foo/**/ba[rz]", - expected: false, - }, - { - reference: "foo/a/bar", - pattern: "foo/**/ba[rz]", - expected: true, - }, - { - reference: "foo/b/baz", - pattern: "foo/**/ba[rz]", - expected: true, - }, - { - reference: "foo/c/baz:tag", - pattern: "foo/**/ba[rz]", - expected: true, - }, - { - reference: "foo/c/baz:tag", - pattern: "foo/*/baz:tag", - expected: true, - }, - { - reference: "foo/c/baz:tag", - pattern: "foo/c/baz:tag", - expected: true, - }, - { - reference: "example.com/foo/c/baz:tag", - pattern: "*/foo/c/baz", - expected: true, - }, - { - reference: "example.com/foo/c/baz:tag", - pattern: "example.com/foo/c/baz", - expected: true, - }, - } - for _, c := range matchCases { - named, err := Parse(c.reference) - if err != nil { - t.Fatal(err) - } - actual, err := Match(c.pattern, named) - if err != nil { - t.Fatal(err) - } - if actual != c.expected { - t.Fatalf("expected %s match %s to be %v, was %v", c.reference, c.pattern, c.expected, actual) - } - } -}