2018-02-05 16:05:59 -05:00
|
|
|
|
package fileutils // import "github.com/docker/docker/pkg/fileutils"
|
2015-03-29 17:17:23 -04:00
|
|
|
|
|
|
|
|
|
import (
|
2017-08-21 17:06:56 -04:00
|
|
|
|
"fmt"
|
2015-03-29 17:17:23 -04:00
|
|
|
|
"os"
|
2015-04-29 10:27:12 -04:00
|
|
|
|
"path"
|
2015-07-12 16:43:42 -04:00
|
|
|
|
"path/filepath"
|
2015-10-14 17:42:21 -04:00
|
|
|
|
"runtime"
|
|
|
|
|
"strings"
|
2015-03-29 17:17:23 -04:00
|
|
|
|
"testing"
|
2017-04-21 11:18:35 -04:00
|
|
|
|
|
2020-02-07 08:39:24 -05:00
|
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2015-03-29 17:17:23 -04:00
|
|
|
|
)
|
|
|
|
|
|
2015-04-29 10:27:12 -04:00
|
|
|
|
// CopyFile with invalid src
|
|
|
|
|
func TestCopyFileWithInvalidSrc(t *testing.T) {
|
2021-08-24 06:10:50 -04:00
|
|
|
|
tempFolder, err := os.MkdirTemp("", "docker-fileutils-test") // #nosec G303
|
2015-04-29 10:27:12 -04:00
|
|
|
|
defer os.RemoveAll(tempFolder)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
bytes, err := CopyFile("/invalid/file/path", path.Join(tempFolder, "dest"))
|
|
|
|
|
if err == nil {
|
|
|
|
|
t.Fatal("Should have fail to copy an invalid src file")
|
|
|
|
|
}
|
|
|
|
|
if bytes != 0 {
|
|
|
|
|
t.Fatal("Should have written 0 bytes")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CopyFile with invalid dest
|
|
|
|
|
func TestCopyFileWithInvalidDest(t *testing.T) {
|
2021-08-24 06:10:50 -04:00
|
|
|
|
tempFolder, err := os.MkdirTemp("", "docker-fileutils-test")
|
2015-04-29 10:27:12 -04:00
|
|
|
|
defer os.RemoveAll(tempFolder)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
src := path.Join(tempFolder, "file")
|
2021-08-24 06:10:50 -04:00
|
|
|
|
err = os.WriteFile(src, []byte("content"), 0740)
|
2015-04-29 10:27:12 -04:00
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
bytes, err := CopyFile(src, path.Join(tempFolder, "/invalid/dest/path"))
|
|
|
|
|
if err == nil {
|
|
|
|
|
t.Fatal("Should have fail to copy an invalid src file")
|
|
|
|
|
}
|
|
|
|
|
if bytes != 0 {
|
|
|
|
|
t.Fatal("Should have written 0 bytes")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CopyFile with same src and dest
|
|
|
|
|
func TestCopyFileWithSameSrcAndDest(t *testing.T) {
|
2021-08-24 06:10:50 -04:00
|
|
|
|
tempFolder, err := os.MkdirTemp("", "docker-fileutils-test")
|
2015-04-29 10:27:12 -04:00
|
|
|
|
defer os.RemoveAll(tempFolder)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
file := path.Join(tempFolder, "file")
|
2021-08-24 06:10:50 -04:00
|
|
|
|
err = os.WriteFile(file, []byte("content"), 0740)
|
2015-04-29 10:27:12 -04:00
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
bytes, err := CopyFile(file, file)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if bytes != 0 {
|
|
|
|
|
t.Fatal("Should have written 0 bytes as it is the same file.")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CopyFile with same src and dest but path is different and not clean
|
|
|
|
|
func TestCopyFileWithSameSrcAndDestWithPathNameDifferent(t *testing.T) {
|
2021-08-24 06:10:50 -04:00
|
|
|
|
tempFolder, err := os.MkdirTemp("", "docker-fileutils-test")
|
2015-04-29 10:27:12 -04:00
|
|
|
|
defer os.RemoveAll(tempFolder)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
testFolder := path.Join(tempFolder, "test")
|
|
|
|
|
err = os.MkdirAll(testFolder, 0740)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
file := path.Join(testFolder, "file")
|
|
|
|
|
sameFile := testFolder + "/../test/file"
|
2021-08-24 06:10:50 -04:00
|
|
|
|
err = os.WriteFile(file, []byte("content"), 0740)
|
2015-04-29 10:27:12 -04:00
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
bytes, err := CopyFile(file, sameFile)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if bytes != 0 {
|
|
|
|
|
t.Fatal("Should have written 0 bytes as it is the same file.")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCopyFile(t *testing.T) {
|
2021-08-24 06:10:50 -04:00
|
|
|
|
tempFolder, err := os.MkdirTemp("", "docker-fileutils-test")
|
2015-04-29 10:27:12 -04:00
|
|
|
|
defer os.RemoveAll(tempFolder)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
src := path.Join(tempFolder, "src")
|
|
|
|
|
dest := path.Join(tempFolder, "dest")
|
2021-08-24 06:10:50 -04:00
|
|
|
|
os.WriteFile(src, []byte("content"), 0777)
|
|
|
|
|
os.WriteFile(dest, []byte("destContent"), 0777)
|
2015-04-29 10:27:12 -04:00
|
|
|
|
bytes, err := CopyFile(src, dest)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if bytes != 7 {
|
|
|
|
|
t.Fatalf("Should have written %d bytes but wrote %d", 7, bytes)
|
|
|
|
|
}
|
2021-08-24 06:10:50 -04:00
|
|
|
|
actual, err := os.ReadFile(dest)
|
2015-04-29 10:27:12 -04:00
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if string(actual) != "content" {
|
|
|
|
|
t.Fatalf("Dest content was '%s', expected '%s'", string(actual), "content")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-29 17:17:23 -04:00
|
|
|
|
// Reading a symlink to a directory must return the directory
|
|
|
|
|
func TestReadSymlinkedDirectoryExistingDirectory(t *testing.T) {
|
2016-03-02 22:05:33 -05:00
|
|
|
|
// TODO Windows: Port this test
|
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
|
t.Skip("Needs porting to Windows")
|
|
|
|
|
}
|
2015-03-29 17:17:23 -04:00
|
|
|
|
var err error
|
|
|
|
|
if err = os.Mkdir("/tmp/testReadSymlinkToExistingDirectory", 0777); err != nil {
|
|
|
|
|
t.Errorf("failed to create directory: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err = os.Symlink("/tmp/testReadSymlinkToExistingDirectory", "/tmp/dirLinkTest"); err != nil {
|
|
|
|
|
t.Errorf("failed to create symlink: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var path string
|
|
|
|
|
if path, err = ReadSymlinkedDirectory("/tmp/dirLinkTest"); err != nil {
|
|
|
|
|
t.Fatalf("failed to read symlink to directory: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if path != "/tmp/testReadSymlinkToExistingDirectory" {
|
|
|
|
|
t.Fatalf("symlink returned unexpected directory: %s", path)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err = os.Remove("/tmp/testReadSymlinkToExistingDirectory"); err != nil {
|
|
|
|
|
t.Errorf("failed to remove temporary directory: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err = os.Remove("/tmp/dirLinkTest"); err != nil {
|
|
|
|
|
t.Errorf("failed to remove symlink: %s", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Reading a non-existing symlink must fail
|
|
|
|
|
func TestReadSymlinkedDirectoryNonExistingSymlink(t *testing.T) {
|
|
|
|
|
var path string
|
|
|
|
|
var err error
|
|
|
|
|
if path, err = ReadSymlinkedDirectory("/tmp/test/foo/Non/ExistingPath"); err == nil {
|
|
|
|
|
t.Fatalf("error expected for non-existing symlink")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if path != "" {
|
|
|
|
|
t.Fatalf("expected empty path, but '%s' was returned", path)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Reading a symlink to a file must fail
|
|
|
|
|
func TestReadSymlinkedDirectoryToFile(t *testing.T) {
|
2016-03-02 22:05:33 -05:00
|
|
|
|
// TODO Windows: Port this test
|
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
|
t.Skip("Needs porting to Windows")
|
|
|
|
|
}
|
2015-03-29 17:17:23 -04:00
|
|
|
|
var err error
|
|
|
|
|
var file *os.File
|
|
|
|
|
|
2019-08-28 11:07:29 -04:00
|
|
|
|
// #nosec G303
|
2015-03-29 17:17:23 -04:00
|
|
|
|
if file, err = os.Create("/tmp/testReadSymlinkToFile"); err != nil {
|
|
|
|
|
t.Fatalf("failed to create file: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
file.Close()
|
|
|
|
|
|
|
|
|
|
if err = os.Symlink("/tmp/testReadSymlinkToFile", "/tmp/fileLinkTest"); err != nil {
|
|
|
|
|
t.Errorf("failed to create symlink: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var path string
|
|
|
|
|
if path, err = ReadSymlinkedDirectory("/tmp/fileLinkTest"); err == nil {
|
|
|
|
|
t.Fatalf("ReadSymlinkedDirectory on a symlink to a file should've failed")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if path != "" {
|
|
|
|
|
t.Fatalf("path should've been empty: %s", path)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err = os.Remove("/tmp/testReadSymlinkToFile"); err != nil {
|
|
|
|
|
t.Errorf("failed to remove file: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err = os.Remove("/tmp/fileLinkTest"); err != nil {
|
|
|
|
|
t.Errorf("failed to remove symlink: %s", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-04-09 15:07:06 -04:00
|
|
|
|
|
|
|
|
|
func TestWildcardMatches(t *testing.T) {
|
|
|
|
|
match, _ := Matches("fileutils.go", []string{"*"})
|
2017-03-30 05:12:17 -04:00
|
|
|
|
if !match {
|
2015-04-09 15:07:06 -04:00
|
|
|
|
t.Errorf("failed to get a wildcard match, got %v", match)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// A simple pattern match should return true.
|
|
|
|
|
func TestPatternMatches(t *testing.T) {
|
|
|
|
|
match, _ := Matches("fileutils.go", []string{"*.go"})
|
2017-03-30 05:12:17 -04:00
|
|
|
|
if !match {
|
2015-04-09 15:07:06 -04:00
|
|
|
|
t.Errorf("failed to get a match, got %v", match)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// An exclusion followed by an inclusion should return true.
|
|
|
|
|
func TestExclusionPatternMatchesPatternBefore(t *testing.T) {
|
|
|
|
|
match, _ := Matches("fileutils.go", []string{"!fileutils.go", "*.go"})
|
2017-03-30 05:12:17 -04:00
|
|
|
|
if !match {
|
2015-04-09 15:07:06 -04:00
|
|
|
|
t.Errorf("failed to get true match on exclusion pattern, got %v", match)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// A folder pattern followed by an exception should return false.
|
|
|
|
|
func TestPatternMatchesFolderExclusions(t *testing.T) {
|
|
|
|
|
match, _ := Matches("docs/README.md", []string{"docs", "!docs/README.md"})
|
2017-03-30 05:12:17 -04:00
|
|
|
|
if match {
|
2015-04-09 15:07:06 -04:00
|
|
|
|
t.Errorf("failed to get a false match on exclusion pattern, got %v", match)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// A folder pattern followed by an exception should return false.
|
|
|
|
|
func TestPatternMatchesFolderWithSlashExclusions(t *testing.T) {
|
|
|
|
|
match, _ := Matches("docs/README.md", []string{"docs/", "!docs/README.md"})
|
2017-03-30 05:12:17 -04:00
|
|
|
|
if match {
|
2015-04-09 15:07:06 -04:00
|
|
|
|
t.Errorf("failed to get a false match on exclusion pattern, got %v", match)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// A folder pattern followed by an exception should return false.
|
|
|
|
|
func TestPatternMatchesFolderWildcardExclusions(t *testing.T) {
|
|
|
|
|
match, _ := Matches("docs/README.md", []string{"docs/*", "!docs/README.md"})
|
2017-03-30 05:12:17 -04:00
|
|
|
|
if match {
|
2015-04-09 15:07:06 -04:00
|
|
|
|
t.Errorf("failed to get a false match on exclusion pattern, got %v", match)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// A pattern followed by an exclusion should return false.
|
|
|
|
|
func TestExclusionPatternMatchesPatternAfter(t *testing.T) {
|
|
|
|
|
match, _ := Matches("fileutils.go", []string{"*.go", "!fileutils.go"})
|
2017-03-30 05:12:17 -04:00
|
|
|
|
if match {
|
2015-04-09 15:07:06 -04:00
|
|
|
|
t.Errorf("failed to get false match on exclusion pattern, got %v", match)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// A filename evaluating to . should return false.
|
|
|
|
|
func TestExclusionPatternMatchesWholeDirectory(t *testing.T) {
|
|
|
|
|
match, _ := Matches(".", []string{"*.go"})
|
2017-03-30 05:12:17 -04:00
|
|
|
|
if match {
|
2015-04-09 15:07:06 -04:00
|
|
|
|
t.Errorf("failed to get false match on ., got %v", match)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// A single ! pattern should return an error.
|
|
|
|
|
func TestSingleExclamationError(t *testing.T) {
|
|
|
|
|
_, err := Matches("fileutils.go", []string{"!"})
|
|
|
|
|
if err == nil {
|
|
|
|
|
t.Errorf("failed to get an error for a single exclamation point, got %v", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-29 10:27:12 -04:00
|
|
|
|
// Matches with no patterns
|
|
|
|
|
func TestMatchesWithNoPatterns(t *testing.T) {
|
|
|
|
|
matches, err := Matches("/any/path/there", []string{})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if matches {
|
|
|
|
|
t.Fatalf("Should not have match anything")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Matches with malformed patterns
|
|
|
|
|
func TestMatchesWithMalformedPatterns(t *testing.T) {
|
|
|
|
|
matches, err := Matches("/any/path/there", []string{"["})
|
|
|
|
|
if err == nil {
|
|
|
|
|
t.Fatal("Should have failed because of a malformed syntax in the pattern")
|
|
|
|
|
}
|
|
|
|
|
if matches {
|
|
|
|
|
t.Fatalf("Should not have match anything")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-21 11:18:35 -04:00
|
|
|
|
type matchesTestCase struct {
|
|
|
|
|
pattern string
|
|
|
|
|
text string
|
|
|
|
|
pass bool
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-19 16:19:18 -05:00
|
|
|
|
type multiPatternTestCase struct {
|
|
|
|
|
patterns []string
|
|
|
|
|
text string
|
|
|
|
|
pass bool
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-14 17:42:21 -04:00
|
|
|
|
func TestMatches(t *testing.T) {
|
2017-04-21 11:18:35 -04:00
|
|
|
|
tests := []matchesTestCase{
|
2015-10-14 17:42:21 -04:00
|
|
|
|
{"**", "file", true},
|
|
|
|
|
{"**", "file/", true},
|
|
|
|
|
{"**/", "file", true}, // weird one
|
|
|
|
|
{"**/", "file/", true},
|
|
|
|
|
{"**", "/", true},
|
|
|
|
|
{"**/", "/", true},
|
|
|
|
|
{"**", "dir/file", true},
|
2016-12-01 18:03:57 -05:00
|
|
|
|
{"**/", "dir/file", true},
|
2015-10-14 17:42:21 -04:00
|
|
|
|
{"**", "dir/file/", true},
|
|
|
|
|
{"**/", "dir/file/", true},
|
|
|
|
|
{"**/**", "dir/file", true},
|
|
|
|
|
{"**/**", "dir/file/", true},
|
|
|
|
|
{"dir/**", "dir/file", true},
|
|
|
|
|
{"dir/**", "dir/file/", true},
|
|
|
|
|
{"dir/**", "dir/dir2/file", true},
|
|
|
|
|
{"dir/**", "dir/dir2/file/", true},
|
2021-07-26 14:28:10 -04:00
|
|
|
|
{"**/dir", "dir", true},
|
|
|
|
|
{"**/dir", "dir/file", true},
|
2015-10-14 17:42:21 -04:00
|
|
|
|
{"**/dir2/*", "dir/dir2/file", true},
|
2017-03-08 17:23:25 -05:00
|
|
|
|
{"**/dir2/*", "dir/dir2/file/", true},
|
2015-10-14 17:42:21 -04:00
|
|
|
|
{"**/dir2/**", "dir/dir2/dir3/file", true},
|
|
|
|
|
{"**/dir2/**", "dir/dir2/dir3/file/", true},
|
|
|
|
|
{"**file", "file", true},
|
|
|
|
|
{"**file", "dir/file", true},
|
|
|
|
|
{"**/file", "dir/file", true},
|
|
|
|
|
{"**file", "dir/dir/file", true},
|
|
|
|
|
{"**/file", "dir/dir/file", true},
|
|
|
|
|
{"**/file*", "dir/dir/file", true},
|
|
|
|
|
{"**/file*", "dir/dir/file.txt", true},
|
|
|
|
|
{"**/file*txt", "dir/dir/file.txt", true},
|
|
|
|
|
{"**/file*.txt", "dir/dir/file.txt", true},
|
|
|
|
|
{"**/file*.txt*", "dir/dir/file.txt", true},
|
|
|
|
|
{"**/**/*.txt", "dir/dir/file.txt", true},
|
|
|
|
|
{"**/**/*.txt2", "dir/dir/file.txt", false},
|
|
|
|
|
{"**/*.txt", "file.txt", true},
|
|
|
|
|
{"**/**/*.txt", "file.txt", true},
|
|
|
|
|
{"a**/*.txt", "a/file.txt", true},
|
|
|
|
|
{"a**/*.txt", "a/dir/file.txt", true},
|
|
|
|
|
{"a**/*.txt", "a/dir/dir/file.txt", true},
|
|
|
|
|
{"a/*.txt", "a/dir/file.txt", false},
|
|
|
|
|
{"a/*.txt", "a/file.txt", true},
|
|
|
|
|
{"a/*.txt**", "a/file.txt", true},
|
|
|
|
|
{"a[b-d]e", "ae", false},
|
|
|
|
|
{"a[b-d]e", "ace", true},
|
|
|
|
|
{"a[b-d]e", "aae", false},
|
|
|
|
|
{"a[^b-d]e", "aze", true},
|
|
|
|
|
{".*", ".foo", true},
|
|
|
|
|
{".*", "foo", false},
|
|
|
|
|
{"abc.def", "abcdef", false},
|
|
|
|
|
{"abc.def", "abc.def", true},
|
|
|
|
|
{"abc.def", "abcZdef", false},
|
|
|
|
|
{"abc?def", "abcZdef", true},
|
|
|
|
|
{"abc?def", "abcdef", false},
|
|
|
|
|
{"a\\\\", "a\\", true},
|
|
|
|
|
{"**/foo/bar", "foo/bar", true},
|
|
|
|
|
{"**/foo/bar", "dir/foo/bar", true},
|
|
|
|
|
{"**/foo/bar", "dir/dir2/foo/bar", true},
|
|
|
|
|
{"abc/**", "abc", false},
|
|
|
|
|
{"abc/**", "abc/def", true},
|
|
|
|
|
{"abc/**", "abc/def/ghi", true},
|
2016-12-01 18:03:57 -05:00
|
|
|
|
{"**/.foo", ".foo", true},
|
|
|
|
|
{"**/.foo", "bar.foo", false},
|
2021-10-22 11:26:15 -04:00
|
|
|
|
{"a(b)c/def", "a(b)c/def", true},
|
|
|
|
|
{"a(b)c/def", "a(b)c/xyz", false},
|
|
|
|
|
{"a.|)$(}+{bc", "a.|)$(}+{bc", true},
|
2021-12-20 19:32:13 -05:00
|
|
|
|
{"dist/proxy.py-2.4.0rc3.dev36+g08acad9-py3-none-any.whl", "dist/proxy.py-2.4.0rc3.dev36+g08acad9-py3-none-any.whl", true},
|
|
|
|
|
{"dist/*.whl", "dist/proxy.py-2.4.0rc3.dev36+g08acad9-py3-none-any.whl", true},
|
2015-10-14 17:42:21 -04:00
|
|
|
|
}
|
2021-11-19 16:19:18 -05:00
|
|
|
|
multiPatternTests := []multiPatternTestCase{
|
|
|
|
|
{[]string{"**", "!util/docker/web"}, "util/docker/web/foo", false},
|
|
|
|
|
{[]string{"**", "!util/docker/web", "util/docker/web/foo"}, "util/docker/web/foo", true},
|
2021-12-20 19:32:13 -05:00
|
|
|
|
{[]string{"**", "!dist/proxy.py-2.4.0rc3.dev36+g08acad9-py3-none-any.whl"}, "dist/proxy.py-2.4.0rc3.dev36+g08acad9-py3-none-any.whl", false},
|
|
|
|
|
{[]string{"**", "!dist/*.whl"}, "dist/proxy.py-2.4.0rc3.dev36+g08acad9-py3-none-any.whl", false},
|
2021-11-19 16:19:18 -05:00
|
|
|
|
}
|
2015-10-14 17:42:21 -04:00
|
|
|
|
|
2017-04-21 11:18:35 -04:00
|
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
|
tests = append(tests, []matchesTestCase{
|
|
|
|
|
{"a\\*b", "a*b", true},
|
|
|
|
|
}...)
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-12 21:09:12 -04:00
|
|
|
|
t.Run("MatchesOrParentMatches", func(t *testing.T) {
|
2021-08-12 16:57:50 -04:00
|
|
|
|
for _, test := range tests {
|
|
|
|
|
desc := fmt.Sprintf("pattern=%q text=%q", test.pattern, test.text)
|
|
|
|
|
pm, err := NewPatternMatcher([]string{test.pattern})
|
|
|
|
|
assert.NilError(t, err, desc)
|
2021-08-12 21:09:12 -04:00
|
|
|
|
res, _ := pm.MatchesOrParentMatches(test.text)
|
2021-08-12 16:57:50 -04:00
|
|
|
|
assert.Check(t, is.Equal(test.pass, res), desc)
|
|
|
|
|
}
|
2021-11-19 16:19:18 -05:00
|
|
|
|
|
|
|
|
|
for _, test := range multiPatternTests {
|
|
|
|
|
desc := fmt.Sprintf("patterns=%q text=%q", test.patterns, test.text)
|
|
|
|
|
pm, err := NewPatternMatcher(test.patterns)
|
|
|
|
|
assert.NilError(t, err, desc)
|
|
|
|
|
res, _ := pm.MatchesOrParentMatches(test.text)
|
|
|
|
|
assert.Check(t, is.Equal(test.pass, res), desc)
|
|
|
|
|
}
|
2021-08-12 16:57:50 -04:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("MatchesUsingParentResult", func(t *testing.T) {
|
|
|
|
|
for _, test := range tests {
|
|
|
|
|
desc := fmt.Sprintf("pattern=%q text=%q", test.pattern, test.text)
|
|
|
|
|
pm, err := NewPatternMatcher([]string{test.pattern})
|
|
|
|
|
assert.NilError(t, err, desc)
|
|
|
|
|
|
2021-08-12 23:02:16 -04:00
|
|
|
|
parentPath := filepath.Dir(filepath.FromSlash(test.text))
|
|
|
|
|
parentPathDirs := strings.Split(parentPath, string(os.PathSeparator))
|
2021-08-12 16:57:50 -04:00
|
|
|
|
|
|
|
|
|
parentMatched := false
|
|
|
|
|
if parentPath != "." {
|
|
|
|
|
for i := range parentPathDirs {
|
|
|
|
|
parentMatched, _ = pm.MatchesUsingParentResult(strings.Join(parentPathDirs[:i+1], "/"), parentMatched)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res, _ := pm.MatchesUsingParentResult(test.text, parentMatched)
|
|
|
|
|
assert.Check(t, is.Equal(test.pass, res), desc)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
2021-11-19 16:19:18 -05:00
|
|
|
|
t.Run("MatchesUsingParentResults", func(t *testing.T) {
|
|
|
|
|
check := func(pm *PatternMatcher, text string, pass bool, desc string) {
|
|
|
|
|
parentPath := filepath.Dir(filepath.FromSlash(text))
|
|
|
|
|
parentPathDirs := strings.Split(parentPath, string(os.PathSeparator))
|
|
|
|
|
|
|
|
|
|
parentMatchInfo := MatchInfo{}
|
|
|
|
|
if parentPath != "." {
|
|
|
|
|
for i := range parentPathDirs {
|
|
|
|
|
_, parentMatchInfo, _ = pm.MatchesUsingParentResults(strings.Join(parentPathDirs[:i+1], "/"), parentMatchInfo)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res, _, _ := pm.MatchesUsingParentResults(text, parentMatchInfo)
|
|
|
|
|
assert.Check(t, is.Equal(pass, res), desc)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
|
desc := fmt.Sprintf("pattern=%q text=%q", test.pattern, test.text)
|
|
|
|
|
pm, err := NewPatternMatcher([]string{test.pattern})
|
|
|
|
|
assert.NilError(t, err, desc)
|
|
|
|
|
|
|
|
|
|
check(pm, test.text, test.pass, desc)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, test := range multiPatternTests {
|
|
|
|
|
desc := fmt.Sprintf("pattern=%q text=%q", test.patterns, test.text)
|
|
|
|
|
pm, err := NewPatternMatcher(test.patterns)
|
|
|
|
|
assert.NilError(t, err, desc)
|
|
|
|
|
|
|
|
|
|
check(pm, test.text, test.pass, desc)
|
|
|
|
|
}
|
|
|
|
|
})
|
2021-11-26 12:48:38 -05:00
|
|
|
|
|
|
|
|
|
t.Run("MatchesUsingParentResultsNoContext", func(t *testing.T) {
|
|
|
|
|
check := func(pm *PatternMatcher, text string, pass bool, desc string) {
|
|
|
|
|
res, _, _ := pm.MatchesUsingParentResults(text, MatchInfo{})
|
|
|
|
|
assert.Check(t, is.Equal(pass, res), desc)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
|
desc := fmt.Sprintf("pattern=%q text=%q", test.pattern, test.text)
|
|
|
|
|
pm, err := NewPatternMatcher([]string{test.pattern})
|
|
|
|
|
assert.NilError(t, err, desc)
|
|
|
|
|
|
|
|
|
|
check(pm, test.text, test.pass, desc)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, test := range multiPatternTests {
|
|
|
|
|
desc := fmt.Sprintf("pattern=%q text=%q", test.patterns, test.text)
|
|
|
|
|
pm, err := NewPatternMatcher(test.patterns)
|
|
|
|
|
assert.NilError(t, err, desc)
|
|
|
|
|
|
|
|
|
|
check(pm, test.text, test.pass, desc)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
2015-10-14 17:42:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
2015-04-09 15:07:06 -04:00
|
|
|
|
func TestCleanPatterns(t *testing.T) {
|
2017-03-08 17:23:25 -05:00
|
|
|
|
patterns := []string{"docs", "config"}
|
|
|
|
|
pm, err := NewPatternMatcher(patterns)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("invalid pattern %v", patterns)
|
|
|
|
|
}
|
|
|
|
|
cleaned := pm.Patterns()
|
2015-04-09 15:07:06 -04:00
|
|
|
|
if len(cleaned) != 2 {
|
|
|
|
|
t.Errorf("expected 2 element slice, got %v", len(cleaned))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCleanPatternsStripEmptyPatterns(t *testing.T) {
|
2017-03-08 17:23:25 -05:00
|
|
|
|
patterns := []string{"docs", "config", ""}
|
|
|
|
|
pm, err := NewPatternMatcher(patterns)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("invalid pattern %v", patterns)
|
|
|
|
|
}
|
|
|
|
|
cleaned := pm.Patterns()
|
2015-04-09 15:07:06 -04:00
|
|
|
|
if len(cleaned) != 2 {
|
|
|
|
|
t.Errorf("expected 2 element slice, got %v", len(cleaned))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCleanPatternsExceptionFlag(t *testing.T) {
|
2017-03-08 17:23:25 -05:00
|
|
|
|
patterns := []string{"docs", "!docs/README.md"}
|
|
|
|
|
pm, err := NewPatternMatcher(patterns)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("invalid pattern %v", patterns)
|
|
|
|
|
}
|
|
|
|
|
if !pm.Exclusions() {
|
|
|
|
|
t.Errorf("expected exceptions to be true, got %v", pm.Exclusions())
|
2015-04-09 15:07:06 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCleanPatternsLeadingSpaceTrimmed(t *testing.T) {
|
2017-03-08 17:23:25 -05:00
|
|
|
|
patterns := []string{"docs", " !docs/README.md"}
|
|
|
|
|
pm, err := NewPatternMatcher(patterns)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("invalid pattern %v", patterns)
|
|
|
|
|
}
|
|
|
|
|
if !pm.Exclusions() {
|
|
|
|
|
t.Errorf("expected exceptions to be true, got %v", pm.Exclusions())
|
2015-04-09 15:07:06 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCleanPatternsTrailingSpaceTrimmed(t *testing.T) {
|
2017-03-08 17:23:25 -05:00
|
|
|
|
patterns := []string{"docs", "!docs/README.md "}
|
|
|
|
|
pm, err := NewPatternMatcher(patterns)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("invalid pattern %v", patterns)
|
|
|
|
|
}
|
|
|
|
|
if !pm.Exclusions() {
|
|
|
|
|
t.Errorf("expected exceptions to be true, got %v", pm.Exclusions())
|
2015-04-09 15:07:06 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCleanPatternsErrorSingleException(t *testing.T) {
|
2017-03-08 17:23:25 -05:00
|
|
|
|
patterns := []string{"!"}
|
|
|
|
|
_, err := NewPatternMatcher(patterns)
|
2015-04-09 15:07:06 -04:00
|
|
|
|
if err == nil {
|
|
|
|
|
t.Errorf("expected error on single exclamation point, got %v", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-12 16:43:42 -04:00
|
|
|
|
func TestCreateIfNotExistsDir(t *testing.T) {
|
2021-08-24 06:10:50 -04:00
|
|
|
|
tempFolder, err := os.MkdirTemp("", "docker-fileutils-test")
|
2015-07-12 16:43:42 -04:00
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
defer os.RemoveAll(tempFolder)
|
|
|
|
|
|
|
|
|
|
folderToCreate := filepath.Join(tempFolder, "tocreate")
|
|
|
|
|
|
|
|
|
|
if err := CreateIfNotExists(folderToCreate, true); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
fileinfo, err := os.Stat(folderToCreate)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Should have create a folder, got %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !fileinfo.IsDir() {
|
|
|
|
|
t.Fatalf("Should have been a dir, seems it's not")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCreateIfNotExistsFile(t *testing.T) {
|
2021-08-24 06:10:50 -04:00
|
|
|
|
tempFolder, err := os.MkdirTemp("", "docker-fileutils-test")
|
2015-07-12 16:43:42 -04:00
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
defer os.RemoveAll(tempFolder)
|
|
|
|
|
|
|
|
|
|
fileToCreate := filepath.Join(tempFolder, "file/to/create")
|
|
|
|
|
|
|
|
|
|
if err := CreateIfNotExists(fileToCreate, false); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
fileinfo, err := os.Stat(fileToCreate)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Should have create a file, got %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if fileinfo.IsDir() {
|
|
|
|
|
t.Fatalf("Should have been a file, seems it's not")
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-10-14 17:42:21 -04:00
|
|
|
|
|
|
|
|
|
// These matchTests are stolen from go's filepath Match tests.
|
|
|
|
|
type matchTest struct {
|
|
|
|
|
pattern, s string
|
|
|
|
|
match bool
|
|
|
|
|
err error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var matchTests = []matchTest{
|
|
|
|
|
{"abc", "abc", true, nil},
|
|
|
|
|
{"*", "abc", true, nil},
|
|
|
|
|
{"*c", "abc", true, nil},
|
|
|
|
|
{"a*", "a", true, nil},
|
|
|
|
|
{"a*", "abc", true, nil},
|
2017-03-08 17:23:25 -05:00
|
|
|
|
{"a*", "ab/c", true, nil},
|
2015-10-14 17:42:21 -04:00
|
|
|
|
{"a*/b", "abc/b", true, nil},
|
|
|
|
|
{"a*/b", "a/c/b", false, nil},
|
|
|
|
|
{"a*b*c*d*e*/f", "axbxcxdxe/f", true, nil},
|
|
|
|
|
{"a*b*c*d*e*/f", "axbxcxdxexxx/f", true, nil},
|
|
|
|
|
{"a*b*c*d*e*/f", "axbxcxdxe/xxx/f", false, nil},
|
|
|
|
|
{"a*b*c*d*e*/f", "axbxcxdxexxx/fff", false, nil},
|
|
|
|
|
{"a*b?c*x", "abxbbxdbxebxczzx", true, nil},
|
|
|
|
|
{"a*b?c*x", "abxbbxdbxebxczzy", false, nil},
|
|
|
|
|
{"ab[c]", "abc", true, nil},
|
|
|
|
|
{"ab[b-d]", "abc", true, nil},
|
|
|
|
|
{"ab[e-g]", "abc", false, nil},
|
|
|
|
|
{"ab[^c]", "abc", false, nil},
|
|
|
|
|
{"ab[^b-d]", "abc", false, nil},
|
|
|
|
|
{"ab[^e-g]", "abc", true, nil},
|
|
|
|
|
{"a\\*b", "a*b", true, nil},
|
|
|
|
|
{"a\\*b", "ab", false, nil},
|
|
|
|
|
{"a?b", "a☺b", true, nil},
|
|
|
|
|
{"a[^a]b", "a☺b", true, nil},
|
|
|
|
|
{"a???b", "a☺b", false, nil},
|
|
|
|
|
{"a[^a][^a][^a]b", "a☺b", false, nil},
|
|
|
|
|
{"[a-ζ]*", "α", true, nil},
|
|
|
|
|
{"*[a-ζ]", "A", false, nil},
|
|
|
|
|
{"a?b", "a/b", false, nil},
|
|
|
|
|
{"a*b", "a/b", false, nil},
|
|
|
|
|
{"[\\]a]", "]", true, nil},
|
|
|
|
|
{"[\\-]", "-", true, nil},
|
|
|
|
|
{"[x\\-]", "x", true, nil},
|
|
|
|
|
{"[x\\-]", "-", true, nil},
|
|
|
|
|
{"[x\\-]", "z", false, nil},
|
|
|
|
|
{"[\\-x]", "x", true, nil},
|
|
|
|
|
{"[\\-x]", "-", true, nil},
|
|
|
|
|
{"[\\-x]", "a", false, nil},
|
|
|
|
|
{"[]a]", "]", false, filepath.ErrBadPattern},
|
|
|
|
|
{"[-]", "-", false, filepath.ErrBadPattern},
|
|
|
|
|
{"[x-]", "x", false, filepath.ErrBadPattern},
|
|
|
|
|
{"[x-]", "-", false, filepath.ErrBadPattern},
|
|
|
|
|
{"[x-]", "z", false, filepath.ErrBadPattern},
|
|
|
|
|
{"[-x]", "x", false, filepath.ErrBadPattern},
|
|
|
|
|
{"[-x]", "-", false, filepath.ErrBadPattern},
|
|
|
|
|
{"[-x]", "a", false, filepath.ErrBadPattern},
|
|
|
|
|
{"\\", "a", false, filepath.ErrBadPattern},
|
|
|
|
|
{"[a-b-c]", "a", false, filepath.ErrBadPattern},
|
|
|
|
|
{"[", "a", false, filepath.ErrBadPattern},
|
|
|
|
|
{"[^", "a", false, filepath.ErrBadPattern},
|
|
|
|
|
{"[^bc", "a", false, filepath.ErrBadPattern},
|
|
|
|
|
{"a[", "a", false, filepath.ErrBadPattern}, // was nil but IMO its wrong
|
|
|
|
|
{"a[", "ab", false, filepath.ErrBadPattern},
|
|
|
|
|
{"*x", "xxx", true, nil},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func errp(e error) string {
|
|
|
|
|
if e == nil {
|
|
|
|
|
return "<nil>"
|
|
|
|
|
}
|
|
|
|
|
return e.Error()
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-12 13:31:13 -05:00
|
|
|
|
// TestMatch tests our version of filepath.Match, called Matches.
|
2015-10-14 17:42:21 -04:00
|
|
|
|
func TestMatch(t *testing.T) {
|
|
|
|
|
for _, tt := range matchTests {
|
|
|
|
|
pattern := tt.pattern
|
|
|
|
|
s := tt.s
|
|
|
|
|
if runtime.GOOS == "windows" {
|
2017-03-30 05:12:17 -04:00
|
|
|
|
if strings.Contains(pattern, "\\") {
|
2015-10-14 17:42:21 -04:00
|
|
|
|
// no escape allowed on windows.
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
pattern = filepath.Clean(pattern)
|
|
|
|
|
s = filepath.Clean(s)
|
|
|
|
|
}
|
2017-03-08 17:23:25 -05:00
|
|
|
|
ok, err := Matches(s, []string{pattern})
|
2015-10-14 17:42:21 -04:00
|
|
|
|
if ok != tt.match || err != tt.err {
|
|
|
|
|
t.Fatalf("Match(%#q, %#q) = %v, %q want %v, %q", pattern, s, ok, errp(err), tt.match, errp(tt.err))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-12 13:31:13 -05:00
|
|
|
|
|
|
|
|
|
type compileTestCase struct {
|
|
|
|
|
pattern string
|
|
|
|
|
matchType matchType
|
|
|
|
|
compiledRegexp string
|
|
|
|
|
windowsCompiledRegexp string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var compileTests = []compileTestCase{
|
|
|
|
|
{"*", regexpMatch, `^[^/]*$`, `^[^\\]*$`},
|
|
|
|
|
{"file*", regexpMatch, `^file[^/]*$`, `^file[^\\]*$`},
|
|
|
|
|
{"*file", regexpMatch, `^[^/]*file$`, `^[^\\]*file$`},
|
|
|
|
|
{"a*/b", regexpMatch, `^a[^/]*/b$`, `^a[^\\]*\\b$`},
|
|
|
|
|
{"**", suffixMatch, "", ""},
|
|
|
|
|
{"**/**", regexpMatch, `^(.*/)?.*$`, `^(.*\\)?.*$`},
|
|
|
|
|
{"dir/**", prefixMatch, "", ""},
|
|
|
|
|
{"**/dir", suffixMatch, "", ""},
|
|
|
|
|
{"**/dir2/*", regexpMatch, `^(.*/)?dir2/[^/]*$`, `^(.*\\)?dir2\\[^\\]*$`},
|
|
|
|
|
{"**/dir2/**", regexpMatch, `^(.*/)?dir2/.*$`, `^(.*\\)?dir2\\.*$`},
|
|
|
|
|
{"**file", suffixMatch, "", ""},
|
|
|
|
|
{"**/file*txt", regexpMatch, `^(.*/)?file[^/]*txt$`, `^(.*\\)?file[^\\]*txt$`},
|
|
|
|
|
{"**/**/*.txt", regexpMatch, `^(.*/)?(.*/)?[^/]*\.txt$`, `^(.*\\)?(.*\\)?[^\\]*\.txt$`},
|
|
|
|
|
{"a[b-d]e", regexpMatch, `^a[b-d]e$`, `^a[b-d]e$`},
|
|
|
|
|
{".*", regexpMatch, `^\.[^/]*$`, `^\.[^\\]*$`},
|
|
|
|
|
{"abc.def", exactMatch, "", ""},
|
|
|
|
|
{"abc?def", regexpMatch, `^abc[^/]def$`, `^abc[^\\]def$`},
|
|
|
|
|
{"**/foo/bar", suffixMatch, "", ""},
|
|
|
|
|
{"a(b)c/def", exactMatch, "", ""},
|
|
|
|
|
{"a.|)$(}+{bc", exactMatch, "", ""},
|
|
|
|
|
{"dist/proxy.py-2.4.0rc3.dev36+g08acad9-py3-none-any.whl", exactMatch, "", ""},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TestCompile confirms that "compile" assigns the correct match type to a
|
|
|
|
|
// variety of test case patterns. If the match type is regexp, it also confirms
|
|
|
|
|
// that the compiled regexp matches the expected regexp.
|
|
|
|
|
func TestCompile(t *testing.T) {
|
|
|
|
|
t.Run("slash", testCompile("/"))
|
|
|
|
|
t.Run("backslash", testCompile(`\`))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func testCompile(sl string) func(*testing.T) {
|
|
|
|
|
return func(t *testing.T) {
|
|
|
|
|
for _, tt := range compileTests {
|
2022-01-20 11:04:50 -05:00
|
|
|
|
// Avoid NewPatternMatcher, which has platform-specific behavior
|
|
|
|
|
pm := &PatternMatcher{
|
|
|
|
|
patterns: make([]*Pattern, 1),
|
|
|
|
|
}
|
|
|
|
|
pattern := path.Clean(tt.pattern)
|
2022-01-12 13:31:13 -05:00
|
|
|
|
if sl != "/" {
|
|
|
|
|
pattern = strings.ReplaceAll(pattern, "/", sl)
|
|
|
|
|
}
|
2022-01-20 11:04:50 -05:00
|
|
|
|
newp := &Pattern{}
|
|
|
|
|
newp.cleanedPattern = pattern
|
|
|
|
|
newp.dirs = strings.Split(pattern, sl)
|
|
|
|
|
pm.patterns[0] = newp
|
|
|
|
|
|
2022-01-12 13:31:13 -05:00
|
|
|
|
if err := pm.patterns[0].compile(sl); err != nil {
|
|
|
|
|
t.Fatalf("Failed to compile pattern %q: %v", pattern, err)
|
|
|
|
|
}
|
|
|
|
|
if pm.patterns[0].matchType != tt.matchType {
|
|
|
|
|
t.Errorf("pattern %q: matchType = %v, want %v", pattern, pm.patterns[0].matchType, tt.matchType)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if tt.matchType == regexpMatch {
|
|
|
|
|
if sl == `\` {
|
|
|
|
|
if pm.patterns[0].regexp.String() != tt.windowsCompiledRegexp {
|
|
|
|
|
t.Errorf("pattern %q: regexp = %s, want %s", pattern, pm.patterns[0].regexp, tt.windowsCompiledRegexp)
|
|
|
|
|
}
|
|
|
|
|
} else if pm.patterns[0].regexp.String() != tt.compiledRegexp {
|
|
|
|
|
t.Errorf("pattern %q: regexp = %s, want %s", pattern, pm.patterns[0].regexp, tt.compiledRegexp)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|