Skip to content

Commit

Permalink
feat: Add ContainsMatchers and ValidPattern. (#7)
Browse files Browse the repository at this point in the history
* feat: Add ContainsMatchers and ValidPattern.

* fix: Fix failing tests.
  • Loading branch information
erikgeiser committed Nov 8, 2020
1 parent 8c8b91c commit 95758cf
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 22 deletions.
62 changes: 47 additions & 15 deletions prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,50 @@ import (
"github.com/gobwas/glob/syntax/lexer"
)

// ValidPattern determines whether a pattern is valid. It returns the parser
// error if the pattern is invalid and nil otherwise.
func ValidPattern(pattern string) error {
_, err := ast.Parse(lexer.NewLexer(pattern))
return err // nolint:wrapcheck
}

// ContainsMatchers determines whether the pattern contains any type of glob
// matcher. It will also return false if the pattern is an invalid expression.
func ContainsMatchers(pattern string) bool {
rootNode, err := ast.Parse(lexer.NewLexer(pattern))
if err != nil {
return false
}

containsMatchers, _ := containsMatchers(rootNode)
return containsMatchers
}

func containsMatchers(node *ast.Node) (result bool, staticText string) {
// nolint:exhaustive
switch node.Kind {
case ast.KindPattern:
text := ""

for _, child := range node.Children {
cm, staticText := containsMatchers(child)
if cm {
return true, ""
}

text += staticText
}

return false, text
case ast.KindText:
return false, node.Value.(ast.Text).Text
case ast.KindNothing:
return false, ""
default:
return true, ""
}
}

// staticPrefix returns the file path inside the pattern up
// to the first path element that contains a wildcard.
func staticPrefix(pattern string) (string, error) {
Expand All @@ -29,24 +73,12 @@ func staticPrefix(pattern string) (string, error) {
return "", fmt.Errorf("parse glob pattern: %w", err)
}

nChildren := len(rootNode.Children)

if nChildren > 1 {
// this pattern is not static
break
}

candidate := rootNode
if len(rootNode.Children) == 1 {
candidate = rootNode.Children[0]
}

v, ok := candidate.Value.(ast.Text)
if !ok {
cm, staticText := containsMatchers(rootNode)
if cm {
break
}

prefix = filepath.Join(prefix, v.Text)
prefix = filepath.Join(prefix, staticText)
}

if prefix == "" {
Expand Down
56 changes: 49 additions & 7 deletions prefix_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package fileglob

import (
"fmt"
"testing"

"github.com/gobwas/glob/syntax/ast"
"github.com/gobwas/glob/syntax/lexer"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestStaticPrefix(t *testing.T) {
Expand All @@ -20,12 +26,48 @@ func TestStaticPrefix(t *testing.T) {

for _, testCase := range testCases {
prefix, err := staticPrefix(testCase.pattern)
if err != nil {
t.Errorf("staticPrefix: %v", err)
}
if prefix != testCase.prefix {
t.Errorf("prefixPrefix returned %q instead of %q for pattern %q",
prefix, testCase.prefix, testCase.pattern)
}
require.NoError(t, err)
assert.Equal(t, testCase.prefix, prefix)
}
}

func TestContainsMatchers(t *testing.T) {
var testCases = []struct {
pattern string
containsMatchers bool
}{
{"/a/*/b", true},
{"\\{a\\}/\\*/", false},
{"a/b/c", false},
{"", false},
{"\\*/\\?", false},
{"*/\\?", true},
{"\\{*\\}", true},
{"{a,b}/c", true},
{"\\{\\\\[a-z]", true},
}

for _, testCase := range testCases {
_, err := ast.Parse(lexer.NewLexer(testCase.pattern))
require.NoError(t, err)

assert.Equal(t, testCase.containsMatchers, ContainsMatchers(testCase.pattern),
fmt.Sprintf("pattern: %s", testCase.pattern))
}
}

func TestValidPattern(t *testing.T) {
var testCases = []struct {
pattern string
valid bool
}{
{"/a/*/b", true},
{"{a[", false},
{"[*]", true},
}

for _, testCase := range testCases {
assert.Equal(t, testCase.valid, ValidPattern(testCase.pattern) == nil,
fmt.Sprintf("pattern: %s", testCase.pattern))
}
}

0 comments on commit 95758cf

Please sign in to comment.