123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573 |
- package fileutils
- import (
- "io/ioutil"
- "os"
- "path"
- "path/filepath"
- "runtime"
- "strings"
- "testing"
- )
- // CopyFile with invalid src
- func TestCopyFileWithInvalidSrc(t *testing.T) {
- tempFolder, err := ioutil.TempDir("", "docker-fileutils-test")
- 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) {
- tempFolder, err := ioutil.TempDir("", "docker-fileutils-test")
- defer os.RemoveAll(tempFolder)
- if err != nil {
- t.Fatal(err)
- }
- src := path.Join(tempFolder, "file")
- err = ioutil.WriteFile(src, []byte("content"), 0740)
- 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) {
- tempFolder, err := ioutil.TempDir("", "docker-fileutils-test")
- defer os.RemoveAll(tempFolder)
- if err != nil {
- t.Fatal(err)
- }
- file := path.Join(tempFolder, "file")
- err = ioutil.WriteFile(file, []byte("content"), 0740)
- 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) {
- tempFolder, err := ioutil.TempDir("", "docker-fileutils-test")
- 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"
- err = ioutil.WriteFile(file, []byte("content"), 0740)
- 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) {
- tempFolder, err := ioutil.TempDir("", "docker-fileutils-test")
- defer os.RemoveAll(tempFolder)
- if err != nil {
- t.Fatal(err)
- }
- src := path.Join(tempFolder, "src")
- dest := path.Join(tempFolder, "dest")
- ioutil.WriteFile(src, []byte("content"), 0777)
- ioutil.WriteFile(dest, []byte("destContent"), 0777)
- 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)
- }
- actual, err := ioutil.ReadFile(dest)
- if err != nil {
- t.Fatal(err)
- }
- if string(actual) != "content" {
- t.Fatalf("Dest content was '%s', expected '%s'", string(actual), "content")
- }
- }
- // Reading a symlink to a directory must return the directory
- func TestReadSymlinkedDirectoryExistingDirectory(t *testing.T) {
- 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) {
- var err error
- var file *os.File
- 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)
- }
- }
- func TestWildcardMatches(t *testing.T) {
- match, _ := Matches("fileutils.go", []string{"*"})
- if match != true {
- 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"})
- if match != true {
- 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"})
- if match != true {
- 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"})
- if match != false {
- 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"})
- if match != false {
- 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"})
- if match != false {
- 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"})
- if match != false {
- 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"})
- if match != false {
- 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)
- }
- }
- // A string preceded with a ! should return true from Exclusion.
- func TestExclusion(t *testing.T) {
- exclusion := exclusion("!")
- if !exclusion {
- t.Errorf("failed to get true for a single !, got %v", exclusion)
- }
- }
- // 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")
- }
- }
- // Test lots of variants of patterns & strings
- func TestMatches(t *testing.T) {
- tests := []struct {
- pattern string
- text string
- pass bool
- }{
- {"**", "file", true},
- {"**", "file/", true},
- {"**/", "file", true}, // weird one
- {"**/", "file/", true},
- {"**", "/", true},
- {"**/", "/", true},
- {"**", "dir/file", true},
- {"**/", "dir/file", false},
- {"**", "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},
- {"**/dir2/*", "dir/dir2/file", true},
- {"**/dir2/*", "dir/dir2/file/", false},
- {"**/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\\*b", "a*b", true},
- {"a\\", "a", false},
- {"a\\", "a\\", 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},
- }
- for _, test := range tests {
- res, _ := regexpMatch(test.pattern, test.text)
- if res != test.pass {
- t.Fatalf("Failed: %v - res:%v", test, res)
- }
- }
- }
- // An empty string should return true from Empty.
- func TestEmpty(t *testing.T) {
- empty := empty("")
- if !empty {
- t.Errorf("failed to get true for an empty string, got %v", empty)
- }
- }
- func TestCleanPatterns(t *testing.T) {
- cleaned, _, _, _ := CleanPatterns([]string{"docs", "config"})
- if len(cleaned) != 2 {
- t.Errorf("expected 2 element slice, got %v", len(cleaned))
- }
- }
- func TestCleanPatternsStripEmptyPatterns(t *testing.T) {
- cleaned, _, _, _ := CleanPatterns([]string{"docs", "config", ""})
- if len(cleaned) != 2 {
- t.Errorf("expected 2 element slice, got %v", len(cleaned))
- }
- }
- func TestCleanPatternsExceptionFlag(t *testing.T) {
- _, _, exceptions, _ := CleanPatterns([]string{"docs", "!docs/README.md"})
- if !exceptions {
- t.Errorf("expected exceptions to be true, got %v", exceptions)
- }
- }
- func TestCleanPatternsLeadingSpaceTrimmed(t *testing.T) {
- _, _, exceptions, _ := CleanPatterns([]string{"docs", " !docs/README.md"})
- if !exceptions {
- t.Errorf("expected exceptions to be true, got %v", exceptions)
- }
- }
- func TestCleanPatternsTrailingSpaceTrimmed(t *testing.T) {
- _, _, exceptions, _ := CleanPatterns([]string{"docs", "!docs/README.md "})
- if !exceptions {
- t.Errorf("expected exceptions to be true, got %v", exceptions)
- }
- }
- func TestCleanPatternsErrorSingleException(t *testing.T) {
- _, _, _, err := CleanPatterns([]string{"!"})
- if err == nil {
- t.Errorf("expected error on single exclamation point, got %v", err)
- }
- }
- func TestCleanPatternsFolderSplit(t *testing.T) {
- _, dirs, _, _ := CleanPatterns([]string{"docs/config/CONFIG.md"})
- if dirs[0][0] != "docs" {
- t.Errorf("expected first element in dirs slice to be docs, got %v", dirs[0][1])
- }
- if dirs[0][1] != "config" {
- t.Errorf("expected first element in dirs slice to be config, got %v", dirs[0][1])
- }
- }
- func TestCreateIfNotExistsDir(t *testing.T) {
- tempFolder, err := ioutil.TempDir("", "docker-fileutils-test")
- 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) {
- tempFolder, err := ioutil.TempDir("", "docker-fileutils-test")
- 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")
- }
- }
- // 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},
- {"a*", "ab/c", false, nil},
- {"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()
- }
- // TestMatch test's our version of filepath.Match, called regexpMatch.
- func TestMatch(t *testing.T) {
- for _, tt := range matchTests {
- pattern := tt.pattern
- s := tt.s
- if runtime.GOOS == "windows" {
- if strings.Index(pattern, "\\") >= 0 {
- // no escape allowed on windows.
- continue
- }
- pattern = filepath.Clean(pattern)
- s = filepath.Clean(s)
- }
- ok, err := regexpMatch(pattern, s)
- 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))
- }
- }
- }
|