Skip to content

Commit

Permalink
test: fix quotemeta when starting with ../ (#22)
Browse files Browse the repository at this point in the history
* test: add another unit test

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

* test: log err

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

* fix: improve msg

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

* fix: to nix path?

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

* chore: debug

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

* chore: debug

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

* fix: made quotemeta an option instead

Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>
  • Loading branch information
caarlos0 committed Mar 1, 2021
1 parent 5ff3f6a commit f26181e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
12 changes: 7 additions & 5 deletions glob.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"io/fs"
"log"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -85,10 +86,10 @@ func MatchDirectoryAsFile(opts *globOptions) {
opts.matchDirectoriesDirectly = true
}

// QuoteMeta returns a string that quotes all glob pattern meta characters
// inside the argument text; For example, QuoteMeta(`{foo*}`) returns `\{foo\*\}`.
func QuoteMeta(pattern string) string {
return glob.QuoteMeta(pattern)
// QuoteMeta quotes all glob pattern meta characters inside the argument text.
// For example, QuoteMeta for a pattern `{foo*}` sets the pattern to `\{foo\*\}`.
func QuoteMeta(opts *globOptions) {
opts.pattern = glob.QuoteMeta(opts.pattern)
}

// toNixPath converts the path to the nix style path
Expand All @@ -109,11 +110,12 @@ func Glob(pattern string, opts ...OptFunc) ([]string, error) { // nolint:funlen,
return matches, fmt.Errorf("failed to resolve pattern: %s: %w", pattern, err)
}
pattern = filepath.ToSlash(p)
log.Println("new pattern", pattern)
}

options := compileOptions(opts, pattern)

pattern = strings.TrimSuffix(strings.TrimPrefix(pattern, options.prefix), separatorString)
pattern = strings.TrimSuffix(strings.TrimPrefix(options.pattern, options.prefix), separatorString)
matcher, err := glob.Compile(pattern, separatorRune)
if err != nil {
return matches, fmt.Errorf("compile glob pattern: %w", err)
Expand Down
35 changes: 32 additions & 3 deletions glob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import (
"os"
"path/filepath"
"runtime"
"strings"
"testing"

"github.com/caarlos0/testfs"
"github.com/gobwas/glob"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -66,6 +68,33 @@ func TestGlob(t *testing.T) { // nolint:funlen
}, matches)
})

t.Run("real with rootfs on relative path to parent disable globbing", func(t *testing.T) {
t.Parallel()

wd, err := os.Getwd()
require.NoError(t, err)

dir := filepath.Base(wd)

prefix := "/"
if isWindows() {
prefix = filepath.VolumeName(wd) + "/"
}

pattern := "../" + dir + "/{file}["

abs, err := filepath.Abs(pattern)
require.NoError(t, err)
abs = toNixPath(abs)

var w bytes.Buffer
matches, err := Glob(pattern, MaybeRootFS, QuoteMeta, WriteOptions(&w))
require.Error(t, err)
require.True(t, strings.HasSuffix(err.Error(), "file does not exist"), "should have been file does not exist, got: "+err.Error())
require.Empty(t, matches)
require.Equal(t, fmt.Sprintf("&{fs:%s matchDirectoriesDirectly:false prefix:%s pattern:%s}", prefix, prefix, glob.QuoteMeta(abs)), w.String())
})

t.Run("real with rootfs on relative path to parent", func(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -259,7 +288,7 @@ func TestGlob(t *testing.T) { // nolint:funlen

t.Run("direct match wildcard", func(t *testing.T) {
t.Parallel()
matches, err := Glob(QuoteMeta("a/b/c{a"), WithFs(testFs(t, []string{
matches, err := Glob("a/b/c{a", QuoteMeta, WithFs(testFs(t, []string{
"./a/nope.txt",
"a/b/c{a",
}, nil)))
Expand Down Expand Up @@ -288,7 +317,7 @@ func TestGlob(t *testing.T) { // nolint:funlen

t.Run("direct no match escaped wildcards", func(t *testing.T) {
t.Parallel()
matches, err := Glob(QuoteMeta("a/b/c{a"), WithFs(testFs(t, []string{
matches, err := Glob("a/b/c{a", QuoteMeta, WithFs(testFs(t, []string{
"./a/nope.txt",
"./a/b/dc",
}, nil)))
Expand Down Expand Up @@ -425,7 +454,7 @@ func TestGlob(t *testing.T) { // nolint:funlen

func TestQuoteMeta(t *testing.T) {
t.Parallel()
matches, err := Glob(QuoteMeta("{a,b}/c"), WithFs(testFs(t, []string{
matches, err := Glob("{a,b}/c", QuoteMeta, WithFs(testFs(t, []string{
"a/c",
"b/c",
"{a,b}/c",
Expand Down

0 comments on commit f26181e

Please sign in to comment.