Skip to content

Commit

Permalink
fix: handle file not found (#6)
Browse files Browse the repository at this point in the history
* fix: handle file not found

Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>

* refactor: Refactor prefix helper functions.

* fix: Fix file not found error for escaped patterns.

Co-authored-by: Erik Geiser <erik.geiser@posteo.net>
  • Loading branch information
caarlos0 and erikgeiser authored Nov 9, 2020
1 parent 95758cf commit 0b47fb0
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 19 deletions.
11 changes: 8 additions & 3 deletions glob.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,17 @@ func doGlob(pattern string, options *globOptions) ([]string, error) { // nolint:

prefixInfo, err := fs.Stat(prefix)
if os.IsNotExist(err) {
// if the prefix does not exist, the whole
// glob pattern won't match anything
if !ContainsMatchers(pattern) {
// glob contains no dynamic matchers so prefix is the file name that
// the glob references directly. When the glob explicitly references
// a single non-existing file, return an error for the user to check.
return []string{}, fmt.Errorf("matching %q: %w", prefix, os.ErrNotExist)
}

return []string{}, nil
}
if err != nil {
return nil, fmt.Errorf("stat prefix: %w", err)
return nil, fmt.Errorf("stat static prefix %q: %w", prefix, err)
}

if !prefixInfo.IsDir() {
Expand Down
13 changes: 11 additions & 2 deletions glob_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package fileglob

import (
"errors"
"os"
"path/filepath"
"testing"

Expand Down Expand Up @@ -131,8 +133,15 @@ func TestGlob(t *testing.T) { // nolint:funlen
"./a/nope.txt",
"./a/b/dc",
}, nil)))
require.NoError(t, err)
require.Equal(t, []string{}, matches)
require.EqualError(t, err, "matching \"a/b/d\": file does not exist")
require.True(t, errors.Is(err, os.ErrNotExist))
require.Empty(t, matches)
})

t.Run("escaped direct no match", func(t *testing.T) {
matches, err := Glob("a/\\{b\\}", WithFs(testFs(t, nil, nil)))
require.EqualError(t, err, "matching \"a/{b}\": file does not exist")
require.Empty(t, matches)
})

t.Run("no matches", func(t *testing.T) {
Expand Down
31 changes: 17 additions & 14 deletions prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,35 @@ func ContainsMatchers(pattern string) bool {
return false
}

containsMatchers, _ := containsMatchers(rootNode)
return containsMatchers
_, isStatic := staticText(rootNode)
return !isStatic
}

func containsMatchers(node *ast.Node) (result bool, staticText string) {
// staticText returns the static string matcher represented by the AST unless
// it contains dynamic matchers (wildcards, etc.). In this case the ok return
// value is false.
func staticText(node *ast.Node) (text string, ok bool) {
// nolint:exhaustive
switch node.Kind {
case ast.KindPattern:
text := ""

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

text += staticText
text += childText
}

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

Expand All @@ -73,12 +76,12 @@ func staticPrefix(pattern string) (string, error) {
return "", fmt.Errorf("parse glob pattern: %w", err)
}

cm, staticText := containsMatchers(rootNode)
if cm {
staticPart, ok := staticText(rootNode)
if !ok {
break
}

prefix = filepath.Join(prefix, staticText)
prefix = filepath.Join(prefix, staticPart)
}

if prefix == "" {
Expand Down

0 comments on commit 0b47fb0

Please sign in to comment.