13 "github.com/stretchr/testify/assert"
14 "github.com/stretchr/testify/require"
17 // CopyFile with invalid src
18 func TestCopyFileWithInvalidSrc(t *testing.T) {
19 tempFolder, err := ioutil.TempDir("", "docker-fileutils-test")
20 defer os.RemoveAll(tempFolder)
24 bytes, err := CopyFile("/invalid/file/path", path.Join(tempFolder, "dest"))
26 t.Fatal("Should have fail to copy an invalid src file")
29 t.Fatal("Should have written 0 bytes")
34 // CopyFile with invalid dest
35 func TestCopyFileWithInvalidDest(t *testing.T) {
36 tempFolder, err := ioutil.TempDir("", "docker-fileutils-test")
37 defer os.RemoveAll(tempFolder)
41 src := path.Join(tempFolder, "file")
42 err = ioutil.WriteFile(src, []byte("content"), 0740)
46 bytes, err := CopyFile(src, path.Join(tempFolder, "/invalid/dest/path"))
48 t.Fatal("Should have fail to copy an invalid src file")
51 t.Fatal("Should have written 0 bytes")
56 // CopyFile with same src and dest
57 func TestCopyFileWithSameSrcAndDest(t *testing.T) {
58 tempFolder, err := ioutil.TempDir("", "docker-fileutils-test")
59 defer os.RemoveAll(tempFolder)
63 file := path.Join(tempFolder, "file")
64 err = ioutil.WriteFile(file, []byte("content"), 0740)
68 bytes, err := CopyFile(file, file)
73 t.Fatal("Should have written 0 bytes as it is the same file.")
77 // CopyFile with same src and dest but path is different and not clean
78 func TestCopyFileWithSameSrcAndDestWithPathNameDifferent(t *testing.T) {
79 tempFolder, err := ioutil.TempDir("", "docker-fileutils-test")
80 defer os.RemoveAll(tempFolder)
84 testFolder := path.Join(tempFolder, "test")
85 err = os.MkdirAll(testFolder, 0740)
89 file := path.Join(testFolder, "file")
90 sameFile := testFolder + "/../test/file"
91 err = ioutil.WriteFile(file, []byte("content"), 0740)
95 bytes, err := CopyFile(file, sameFile)
100 t.Fatal("Should have written 0 bytes as it is the same file.")
104 func TestCopyFile(t *testing.T) {
105 tempFolder, err := ioutil.TempDir("", "docker-fileutils-test")
106 defer os.RemoveAll(tempFolder)
110 src := path.Join(tempFolder, "src")
111 dest := path.Join(tempFolder, "dest")
112 ioutil.WriteFile(src, []byte("content"), 0777)
113 ioutil.WriteFile(dest, []byte("destContent"), 0777)
114 bytes, err := CopyFile(src, dest)
119 t.Fatalf("Should have written %d bytes but wrote %d", 7, bytes)
121 actual, err := ioutil.ReadFile(dest)
125 if string(actual) != "content" {
126 t.Fatalf("Dest content was '%s', expected '%s'", string(actual), "content")
130 // Reading a symlink to a directory must return the directory
131 func TestReadSymlinkedDirectoryExistingDirectory(t *testing.T) {
132 // TODO Windows: Port this test
133 if runtime.GOOS == "windows" {
134 t.Skip("Needs porting to Windows")
137 if err = os.Mkdir("/tmp/testReadSymlinkToExistingDirectory", 0777); err != nil {
138 t.Errorf("failed to create directory: %s", err)
141 if err = os.Symlink("/tmp/testReadSymlinkToExistingDirectory", "/tmp/dirLinkTest"); err != nil {
142 t.Errorf("failed to create symlink: %s", err)
146 if path, err = ReadSymlinkedDirectory("/tmp/dirLinkTest"); err != nil {
147 t.Fatalf("failed to read symlink to directory: %s", err)
150 if path != "/tmp/testReadSymlinkToExistingDirectory" {
151 t.Fatalf("symlink returned unexpected directory: %s", path)
154 if err = os.Remove("/tmp/testReadSymlinkToExistingDirectory"); err != nil {
155 t.Errorf("failed to remove temporary directory: %s", err)
158 if err = os.Remove("/tmp/dirLinkTest"); err != nil {
159 t.Errorf("failed to remove symlink: %s", err)
163 // Reading a non-existing symlink must fail
164 func TestReadSymlinkedDirectoryNonExistingSymlink(t *testing.T) {
167 if path, err = ReadSymlinkedDirectory("/tmp/test/foo/Non/ExistingPath"); err == nil {
168 t.Fatalf("error expected for non-existing symlink")
172 t.Fatalf("expected empty path, but '%s' was returned", path)
176 // Reading a symlink to a file must fail
177 func TestReadSymlinkedDirectoryToFile(t *testing.T) {
178 // TODO Windows: Port this test
179 if runtime.GOOS == "windows" {
180 t.Skip("Needs porting to Windows")
185 if file, err = os.Create("/tmp/testReadSymlinkToFile"); err != nil {
186 t.Fatalf("failed to create file: %s", err)
191 if err = os.Symlink("/tmp/testReadSymlinkToFile", "/tmp/fileLinkTest"); err != nil {
192 t.Errorf("failed to create symlink: %s", err)
196 if path, err = ReadSymlinkedDirectory("/tmp/fileLinkTest"); err == nil {
197 t.Fatalf("ReadSymlinkedDirectory on a symlink to a file should've failed")
201 t.Fatalf("path should've been empty: %s", path)
204 if err = os.Remove("/tmp/testReadSymlinkToFile"); err != nil {
205 t.Errorf("failed to remove file: %s", err)
208 if err = os.Remove("/tmp/fileLinkTest"); err != nil {
209 t.Errorf("failed to remove symlink: %s", err)
213 func TestWildcardMatches(t *testing.T) {
214 match, _ := Matches("fileutils.go", []string{"*"})
216 t.Errorf("failed to get a wildcard match, got %v", match)
220 // A simple pattern match should return true.
221 func TestPatternMatches(t *testing.T) {
222 match, _ := Matches("fileutils.go", []string{"*.go"})
224 t.Errorf("failed to get a match, got %v", match)
228 // An exclusion followed by an inclusion should return true.
229 func TestExclusionPatternMatchesPatternBefore(t *testing.T) {
230 match, _ := Matches("fileutils.go", []string{"!fileutils.go", "*.go"})
232 t.Errorf("failed to get true match on exclusion pattern, got %v", match)
236 // A folder pattern followed by an exception should return false.
237 func TestPatternMatchesFolderExclusions(t *testing.T) {
238 match, _ := Matches("docs/README.md", []string{"docs", "!docs/README.md"})
240 t.Errorf("failed to get a false match on exclusion pattern, got %v", match)
244 // A folder pattern followed by an exception should return false.
245 func TestPatternMatchesFolderWithSlashExclusions(t *testing.T) {
246 match, _ := Matches("docs/README.md", []string{"docs/", "!docs/README.md"})
248 t.Errorf("failed to get a false match on exclusion pattern, got %v", match)
252 // A folder pattern followed by an exception should return false.
253 func TestPatternMatchesFolderWildcardExclusions(t *testing.T) {
254 match, _ := Matches("docs/README.md", []string{"docs/*", "!docs/README.md"})
256 t.Errorf("failed to get a false match on exclusion pattern, got %v", match)
260 // A pattern followed by an exclusion should return false.
261 func TestExclusionPatternMatchesPatternAfter(t *testing.T) {
262 match, _ := Matches("fileutils.go", []string{"*.go", "!fileutils.go"})
264 t.Errorf("failed to get false match on exclusion pattern, got %v", match)
268 // A filename evaluating to . should return false.
269 func TestExclusionPatternMatchesWholeDirectory(t *testing.T) {
270 match, _ := Matches(".", []string{"*.go"})
272 t.Errorf("failed to get false match on ., got %v", match)
276 // A single ! pattern should return an error.
277 func TestSingleExclamationError(t *testing.T) {
278 _, err := Matches("fileutils.go", []string{"!"})
280 t.Errorf("failed to get an error for a single exclamation point, got %v", err)
284 // Matches with no patterns
285 func TestMatchesWithNoPatterns(t *testing.T) {
286 matches, err := Matches("/any/path/there", []string{})
291 t.Fatalf("Should not have match anything")
295 // Matches with malformed patterns
296 func TestMatchesWithMalformedPatterns(t *testing.T) {
297 matches, err := Matches("/any/path/there", []string{"["})
299 t.Fatal("Should have failed because of a malformed syntax in the pattern")
302 t.Fatalf("Should not have match anything")
306 type matchesTestCase struct {
312 func TestMatches(t *testing.T) {
313 tests := []matchesTestCase{
314 {"**", "file", true},
315 {"**", "file/", true},
316 {"**/", "file", true}, // weird one
317 {"**/", "file/", true},
320 {"**", "dir/file", true},
321 {"**/", "dir/file", true},
322 {"**", "dir/file/", true},
323 {"**/", "dir/file/", true},
324 {"**/**", "dir/file", true},
325 {"**/**", "dir/file/", true},
326 {"dir/**", "dir/file", true},
327 {"dir/**", "dir/file/", true},
328 {"dir/**", "dir/dir2/file", true},
329 {"dir/**", "dir/dir2/file/", true},
330 {"**/dir2/*", "dir/dir2/file", true},
331 {"**/dir2/*", "dir/dir2/file/", true},
332 {"**/dir2/**", "dir/dir2/dir3/file", true},
333 {"**/dir2/**", "dir/dir2/dir3/file/", true},
334 {"**file", "file", true},
335 {"**file", "dir/file", true},
336 {"**/file", "dir/file", true},
337 {"**file", "dir/dir/file", true},
338 {"**/file", "dir/dir/file", true},
339 {"**/file*", "dir/dir/file", true},
340 {"**/file*", "dir/dir/file.txt", true},
341 {"**/file*txt", "dir/dir/file.txt", true},
342 {"**/file*.txt", "dir/dir/file.txt", true},
343 {"**/file*.txt*", "dir/dir/file.txt", true},
344 {"**/**/*.txt", "dir/dir/file.txt", true},
345 {"**/**/*.txt2", "dir/dir/file.txt", false},
346 {"**/*.txt", "file.txt", true},
347 {"**/**/*.txt", "file.txt", true},
348 {"a**/*.txt", "a/file.txt", true},
349 {"a**/*.txt", "a/dir/file.txt", true},
350 {"a**/*.txt", "a/dir/dir/file.txt", true},
351 {"a/*.txt", "a/dir/file.txt", false},
352 {"a/*.txt", "a/file.txt", true},
353 {"a/*.txt**", "a/file.txt", true},
354 {"a[b-d]e", "ae", false},
355 {"a[b-d]e", "ace", true},
356 {"a[b-d]e", "aae", false},
357 {"a[^b-d]e", "aze", true},
358 {".*", ".foo", true},
359 {".*", "foo", false},
360 {"abc.def", "abcdef", false},
361 {"abc.def", "abc.def", true},
362 {"abc.def", "abcZdef", false},
363 {"abc?def", "abcZdef", true},
364 {"abc?def", "abcdef", false},
365 {"a\\\\", "a\\", true},
366 {"**/foo/bar", "foo/bar", true},
367 {"**/foo/bar", "dir/foo/bar", true},
368 {"**/foo/bar", "dir/dir2/foo/bar", true},
369 {"abc/**", "abc", false},
370 {"abc/**", "abc/def", true},
371 {"abc/**", "abc/def/ghi", true},
372 {"**/.foo", ".foo", true},
373 {"**/.foo", "bar.foo", false},
376 if runtime.GOOS != "windows" {
377 tests = append(tests, []matchesTestCase{
378 {"a\\*b", "a*b", true},
380 {"a\\", "a\\", false},
384 for _, test := range tests {
385 desc := fmt.Sprintf("pattern=%q text=%q", test.pattern, test.text)
386 pm, err := NewPatternMatcher([]string{test.pattern})
387 require.NoError(t, err, desc)
388 res, _ := pm.Matches(test.text)
389 assert.Equal(t, test.pass, res, desc)
393 func TestCleanPatterns(t *testing.T) {
394 patterns := []string{"docs", "config"}
395 pm, err := NewPatternMatcher(patterns)
397 t.Fatalf("invalid pattern %v", patterns)
399 cleaned := pm.Patterns()
400 if len(cleaned) != 2 {
401 t.Errorf("expected 2 element slice, got %v", len(cleaned))
405 func TestCleanPatternsStripEmptyPatterns(t *testing.T) {
406 patterns := []string{"docs", "config", ""}
407 pm, err := NewPatternMatcher(patterns)
409 t.Fatalf("invalid pattern %v", patterns)
411 cleaned := pm.Patterns()
412 if len(cleaned) != 2 {
413 t.Errorf("expected 2 element slice, got %v", len(cleaned))
417 func TestCleanPatternsExceptionFlag(t *testing.T) {
418 patterns := []string{"docs", "!docs/README.md"}
419 pm, err := NewPatternMatcher(patterns)
421 t.Fatalf("invalid pattern %v", patterns)
423 if !pm.Exclusions() {
424 t.Errorf("expected exceptions to be true, got %v", pm.Exclusions())
428 func TestCleanPatternsLeadingSpaceTrimmed(t *testing.T) {
429 patterns := []string{"docs", " !docs/README.md"}
430 pm, err := NewPatternMatcher(patterns)
432 t.Fatalf("invalid pattern %v", patterns)
434 if !pm.Exclusions() {
435 t.Errorf("expected exceptions to be true, got %v", pm.Exclusions())
439 func TestCleanPatternsTrailingSpaceTrimmed(t *testing.T) {
440 patterns := []string{"docs", "!docs/README.md "}
441 pm, err := NewPatternMatcher(patterns)
443 t.Fatalf("invalid pattern %v", patterns)
445 if !pm.Exclusions() {
446 t.Errorf("expected exceptions to be true, got %v", pm.Exclusions())
450 func TestCleanPatternsErrorSingleException(t *testing.T) {
451 patterns := []string{"!"}
452 _, err := NewPatternMatcher(patterns)
454 t.Errorf("expected error on single exclamation point, got %v", err)
458 func TestCreateIfNotExistsDir(t *testing.T) {
459 tempFolder, err := ioutil.TempDir("", "docker-fileutils-test")
463 defer os.RemoveAll(tempFolder)
465 folderToCreate := filepath.Join(tempFolder, "tocreate")
467 if err := CreateIfNotExists(folderToCreate, true); err != nil {
470 fileinfo, err := os.Stat(folderToCreate)
472 t.Fatalf("Should have create a folder, got %v", err)
475 if !fileinfo.IsDir() {
476 t.Fatalf("Should have been a dir, seems it's not")
480 func TestCreateIfNotExistsFile(t *testing.T) {
481 tempFolder, err := ioutil.TempDir("", "docker-fileutils-test")
485 defer os.RemoveAll(tempFolder)
487 fileToCreate := filepath.Join(tempFolder, "file/to/create")
489 if err := CreateIfNotExists(fileToCreate, false); err != nil {
492 fileinfo, err := os.Stat(fileToCreate)
494 t.Fatalf("Should have create a file, got %v", err)
497 if fileinfo.IsDir() {
498 t.Fatalf("Should have been a file, seems it's not")
502 // These matchTests are stolen from go's filepath Match tests.
503 type matchTest struct {
509 var matchTests = []matchTest{
510 {"abc", "abc", true, nil},
511 {"*", "abc", true, nil},
512 {"*c", "abc", true, nil},
513 {"a*", "a", true, nil},
514 {"a*", "abc", true, nil},
515 {"a*", "ab/c", true, nil},
516 {"a*/b", "abc/b", true, nil},
517 {"a*/b", "a/c/b", false, nil},
518 {"a*b*c*d*e*/f", "axbxcxdxe/f", true, nil},
519 {"a*b*c*d*e*/f", "axbxcxdxexxx/f", true, nil},
520 {"a*b*c*d*e*/f", "axbxcxdxe/xxx/f", false, nil},
521 {"a*b*c*d*e*/f", "axbxcxdxexxx/fff", false, nil},
522 {"a*b?c*x", "abxbbxdbxebxczzx", true, nil},
523 {"a*b?c*x", "abxbbxdbxebxczzy", false, nil},
524 {"ab[c]", "abc", true, nil},
525 {"ab[b-d]", "abc", true, nil},
526 {"ab[e-g]", "abc", false, nil},
527 {"ab[^c]", "abc", false, nil},
528 {"ab[^b-d]", "abc", false, nil},
529 {"ab[^e-g]", "abc", true, nil},
530 {"a\\*b", "a*b", true, nil},
531 {"a\\*b", "ab", false, nil},
532 {"a?b", "a☺b", true, nil},
533 {"a[^a]b", "a☺b", true, nil},
534 {"a???b", "a☺b", false, nil},
535 {"a[^a][^a][^a]b", "a☺b", false, nil},
536 {"[a-ζ]*", "α", true, nil},
537 {"*[a-ζ]", "A", false, nil},
538 {"a?b", "a/b", false, nil},
539 {"a*b", "a/b", false, nil},
540 {"[\\]a]", "]", true, nil},
541 {"[\\-]", "-", true, nil},
542 {"[x\\-]", "x", true, nil},
543 {"[x\\-]", "-", true, nil},
544 {"[x\\-]", "z", false, nil},
545 {"[\\-x]", "x", true, nil},
546 {"[\\-x]", "-", true, nil},
547 {"[\\-x]", "a", false, nil},
548 {"[]a]", "]", false, filepath.ErrBadPattern},
549 {"[-]", "-", false, filepath.ErrBadPattern},
550 {"[x-]", "x", false, filepath.ErrBadPattern},
551 {"[x-]", "-", false, filepath.ErrBadPattern},
552 {"[x-]", "z", false, filepath.ErrBadPattern},
553 {"[-x]", "x", false, filepath.ErrBadPattern},
554 {"[-x]", "-", false, filepath.ErrBadPattern},
555 {"[-x]", "a", false, filepath.ErrBadPattern},
556 {"\\", "a", false, filepath.ErrBadPattern},
557 {"[a-b-c]", "a", false, filepath.ErrBadPattern},
558 {"[", "a", false, filepath.ErrBadPattern},
559 {"[^", "a", false, filepath.ErrBadPattern},
560 {"[^bc", "a", false, filepath.ErrBadPattern},
561 {"a[", "a", false, filepath.ErrBadPattern}, // was nil but IMO its wrong
562 {"a[", "ab", false, filepath.ErrBadPattern},
563 {"*x", "xxx", true, nil},
566 func errp(e error) string {
573 // TestMatch test's our version of filepath.Match, called regexpMatch.
574 func TestMatch(t *testing.T) {
575 for _, tt := range matchTests {
576 pattern := tt.pattern
578 if runtime.GOOS == "windows" {
579 if strings.Contains(pattern, "\\") {
580 // no escape allowed on windows.
583 pattern = filepath.Clean(pattern)
584 s = filepath.Clean(s)
586 ok, err := Matches(s, []string{pattern})
587 if ok != tt.match || err != tt.err {
588 t.Fatalf("Match(%#q, %#q) = %v, %q want %v, %q", pattern, s, ok, errp(err), tt.match, errp(tt.err))