fileutils_test.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. package fileutils // import "github.com/docker/docker/pkg/fileutils"
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "path"
  7. "path/filepath"
  8. "runtime"
  9. "strings"
  10. "testing"
  11. "gotest.tools/v3/assert"
  12. is "gotest.tools/v3/assert/cmp"
  13. )
  14. // CopyFile with invalid src
  15. func TestCopyFileWithInvalidSrc(t *testing.T) {
  16. tempFolder, err := ioutil.TempDir("", "docker-fileutils-test") // #nosec G303
  17. defer os.RemoveAll(tempFolder)
  18. if err != nil {
  19. t.Fatal(err)
  20. }
  21. bytes, err := CopyFile("/invalid/file/path", path.Join(tempFolder, "dest"))
  22. if err == nil {
  23. t.Fatal("Should have fail to copy an invalid src file")
  24. }
  25. if bytes != 0 {
  26. t.Fatal("Should have written 0 bytes")
  27. }
  28. }
  29. // CopyFile with invalid dest
  30. func TestCopyFileWithInvalidDest(t *testing.T) {
  31. tempFolder, err := ioutil.TempDir("", "docker-fileutils-test")
  32. defer os.RemoveAll(tempFolder)
  33. if err != nil {
  34. t.Fatal(err)
  35. }
  36. src := path.Join(tempFolder, "file")
  37. err = ioutil.WriteFile(src, []byte("content"), 0740)
  38. if err != nil {
  39. t.Fatal(err)
  40. }
  41. bytes, err := CopyFile(src, path.Join(tempFolder, "/invalid/dest/path"))
  42. if err == nil {
  43. t.Fatal("Should have fail to copy an invalid src file")
  44. }
  45. if bytes != 0 {
  46. t.Fatal("Should have written 0 bytes")
  47. }
  48. }
  49. // CopyFile with same src and dest
  50. func TestCopyFileWithSameSrcAndDest(t *testing.T) {
  51. tempFolder, err := ioutil.TempDir("", "docker-fileutils-test")
  52. defer os.RemoveAll(tempFolder)
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. file := path.Join(tempFolder, "file")
  57. err = ioutil.WriteFile(file, []byte("content"), 0740)
  58. if err != nil {
  59. t.Fatal(err)
  60. }
  61. bytes, err := CopyFile(file, file)
  62. if err != nil {
  63. t.Fatal(err)
  64. }
  65. if bytes != 0 {
  66. t.Fatal("Should have written 0 bytes as it is the same file.")
  67. }
  68. }
  69. // CopyFile with same src and dest but path is different and not clean
  70. func TestCopyFileWithSameSrcAndDestWithPathNameDifferent(t *testing.T) {
  71. tempFolder, err := ioutil.TempDir("", "docker-fileutils-test")
  72. defer os.RemoveAll(tempFolder)
  73. if err != nil {
  74. t.Fatal(err)
  75. }
  76. testFolder := path.Join(tempFolder, "test")
  77. err = os.MkdirAll(testFolder, 0740)
  78. if err != nil {
  79. t.Fatal(err)
  80. }
  81. file := path.Join(testFolder, "file")
  82. sameFile := testFolder + "/../test/file"
  83. err = ioutil.WriteFile(file, []byte("content"), 0740)
  84. if err != nil {
  85. t.Fatal(err)
  86. }
  87. bytes, err := CopyFile(file, sameFile)
  88. if err != nil {
  89. t.Fatal(err)
  90. }
  91. if bytes != 0 {
  92. t.Fatal("Should have written 0 bytes as it is the same file.")
  93. }
  94. }
  95. func TestCopyFile(t *testing.T) {
  96. tempFolder, err := ioutil.TempDir("", "docker-fileutils-test")
  97. defer os.RemoveAll(tempFolder)
  98. if err != nil {
  99. t.Fatal(err)
  100. }
  101. src := path.Join(tempFolder, "src")
  102. dest := path.Join(tempFolder, "dest")
  103. ioutil.WriteFile(src, []byte("content"), 0777)
  104. ioutil.WriteFile(dest, []byte("destContent"), 0777)
  105. bytes, err := CopyFile(src, dest)
  106. if err != nil {
  107. t.Fatal(err)
  108. }
  109. if bytes != 7 {
  110. t.Fatalf("Should have written %d bytes but wrote %d", 7, bytes)
  111. }
  112. actual, err := ioutil.ReadFile(dest)
  113. if err != nil {
  114. t.Fatal(err)
  115. }
  116. if string(actual) != "content" {
  117. t.Fatalf("Dest content was '%s', expected '%s'", string(actual), "content")
  118. }
  119. }
  120. // Reading a symlink to a directory must return the directory
  121. func TestReadSymlinkedDirectoryExistingDirectory(t *testing.T) {
  122. // TODO Windows: Port this test
  123. if runtime.GOOS == "windows" {
  124. t.Skip("Needs porting to Windows")
  125. }
  126. var err error
  127. if err = os.Mkdir("/tmp/testReadSymlinkToExistingDirectory", 0777); err != nil {
  128. t.Errorf("failed to create directory: %s", err)
  129. }
  130. if err = os.Symlink("/tmp/testReadSymlinkToExistingDirectory", "/tmp/dirLinkTest"); err != nil {
  131. t.Errorf("failed to create symlink: %s", err)
  132. }
  133. var path string
  134. if path, err = ReadSymlinkedDirectory("/tmp/dirLinkTest"); err != nil {
  135. t.Fatalf("failed to read symlink to directory: %s", err)
  136. }
  137. if path != "/tmp/testReadSymlinkToExistingDirectory" {
  138. t.Fatalf("symlink returned unexpected directory: %s", path)
  139. }
  140. if err = os.Remove("/tmp/testReadSymlinkToExistingDirectory"); err != nil {
  141. t.Errorf("failed to remove temporary directory: %s", err)
  142. }
  143. if err = os.Remove("/tmp/dirLinkTest"); err != nil {
  144. t.Errorf("failed to remove symlink: %s", err)
  145. }
  146. }
  147. // Reading a non-existing symlink must fail
  148. func TestReadSymlinkedDirectoryNonExistingSymlink(t *testing.T) {
  149. var path string
  150. var err error
  151. if path, err = ReadSymlinkedDirectory("/tmp/test/foo/Non/ExistingPath"); err == nil {
  152. t.Fatalf("error expected for non-existing symlink")
  153. }
  154. if path != "" {
  155. t.Fatalf("expected empty path, but '%s' was returned", path)
  156. }
  157. }
  158. // Reading a symlink to a file must fail
  159. func TestReadSymlinkedDirectoryToFile(t *testing.T) {
  160. // TODO Windows: Port this test
  161. if runtime.GOOS == "windows" {
  162. t.Skip("Needs porting to Windows")
  163. }
  164. var err error
  165. var file *os.File
  166. // #nosec G303
  167. if file, err = os.Create("/tmp/testReadSymlinkToFile"); err != nil {
  168. t.Fatalf("failed to create file: %s", err)
  169. }
  170. file.Close()
  171. if err = os.Symlink("/tmp/testReadSymlinkToFile", "/tmp/fileLinkTest"); err != nil {
  172. t.Errorf("failed to create symlink: %s", err)
  173. }
  174. var path string
  175. if path, err = ReadSymlinkedDirectory("/tmp/fileLinkTest"); err == nil {
  176. t.Fatalf("ReadSymlinkedDirectory on a symlink to a file should've failed")
  177. }
  178. if path != "" {
  179. t.Fatalf("path should've been empty: %s", path)
  180. }
  181. if err = os.Remove("/tmp/testReadSymlinkToFile"); err != nil {
  182. t.Errorf("failed to remove file: %s", err)
  183. }
  184. if err = os.Remove("/tmp/fileLinkTest"); err != nil {
  185. t.Errorf("failed to remove symlink: %s", err)
  186. }
  187. }
  188. func TestWildcardMatches(t *testing.T) {
  189. match, _ := Matches("fileutils.go", []string{"*"})
  190. if !match {
  191. t.Errorf("failed to get a wildcard match, got %v", match)
  192. }
  193. }
  194. // A simple pattern match should return true.
  195. func TestPatternMatches(t *testing.T) {
  196. match, _ := Matches("fileutils.go", []string{"*.go"})
  197. if !match {
  198. t.Errorf("failed to get a match, got %v", match)
  199. }
  200. }
  201. // An exclusion followed by an inclusion should return true.
  202. func TestExclusionPatternMatchesPatternBefore(t *testing.T) {
  203. match, _ := Matches("fileutils.go", []string{"!fileutils.go", "*.go"})
  204. if !match {
  205. t.Errorf("failed to get true match on exclusion pattern, got %v", match)
  206. }
  207. }
  208. // A folder pattern followed by an exception should return false.
  209. func TestPatternMatchesFolderExclusions(t *testing.T) {
  210. match, _ := Matches("docs/README.md", []string{"docs", "!docs/README.md"})
  211. if match {
  212. t.Errorf("failed to get a false match on exclusion pattern, got %v", match)
  213. }
  214. }
  215. // A folder pattern followed by an exception should return false.
  216. func TestPatternMatchesFolderWithSlashExclusions(t *testing.T) {
  217. match, _ := Matches("docs/README.md", []string{"docs/", "!docs/README.md"})
  218. if match {
  219. t.Errorf("failed to get a false match on exclusion pattern, got %v", match)
  220. }
  221. }
  222. // A folder pattern followed by an exception should return false.
  223. func TestPatternMatchesFolderWildcardExclusions(t *testing.T) {
  224. match, _ := Matches("docs/README.md", []string{"docs/*", "!docs/README.md"})
  225. if match {
  226. t.Errorf("failed to get a false match on exclusion pattern, got %v", match)
  227. }
  228. }
  229. // A pattern followed by an exclusion should return false.
  230. func TestExclusionPatternMatchesPatternAfter(t *testing.T) {
  231. match, _ := Matches("fileutils.go", []string{"*.go", "!fileutils.go"})
  232. if match {
  233. t.Errorf("failed to get false match on exclusion pattern, got %v", match)
  234. }
  235. }
  236. // A filename evaluating to . should return false.
  237. func TestExclusionPatternMatchesWholeDirectory(t *testing.T) {
  238. match, _ := Matches(".", []string{"*.go"})
  239. if match {
  240. t.Errorf("failed to get false match on ., got %v", match)
  241. }
  242. }
  243. // A single ! pattern should return an error.
  244. func TestSingleExclamationError(t *testing.T) {
  245. _, err := Matches("fileutils.go", []string{"!"})
  246. if err == nil {
  247. t.Errorf("failed to get an error for a single exclamation point, got %v", err)
  248. }
  249. }
  250. // Matches with no patterns
  251. func TestMatchesWithNoPatterns(t *testing.T) {
  252. matches, err := Matches("/any/path/there", []string{})
  253. if err != nil {
  254. t.Fatal(err)
  255. }
  256. if matches {
  257. t.Fatalf("Should not have match anything")
  258. }
  259. }
  260. // Matches with malformed patterns
  261. func TestMatchesWithMalformedPatterns(t *testing.T) {
  262. matches, err := Matches("/any/path/there", []string{"["})
  263. if err == nil {
  264. t.Fatal("Should have failed because of a malformed syntax in the pattern")
  265. }
  266. if matches {
  267. t.Fatalf("Should not have match anything")
  268. }
  269. }
  270. type matchesTestCase struct {
  271. pattern string
  272. text string
  273. pass bool
  274. }
  275. func TestMatches(t *testing.T) {
  276. tests := []matchesTestCase{
  277. {"**", "file", true},
  278. {"**", "file/", true},
  279. {"**/", "file", true}, // weird one
  280. {"**/", "file/", true},
  281. {"**", "/", true},
  282. {"**/", "/", true},
  283. {"**", "dir/file", true},
  284. {"**/", "dir/file", true},
  285. {"**", "dir/file/", true},
  286. {"**/", "dir/file/", true},
  287. {"**/**", "dir/file", true},
  288. {"**/**", "dir/file/", true},
  289. {"dir/**", "dir/file", true},
  290. {"dir/**", "dir/file/", true},
  291. {"dir/**", "dir/dir2/file", true},
  292. {"dir/**", "dir/dir2/file/", true},
  293. {"**/dir", "dir", true},
  294. {"**/dir", "dir/file", true},
  295. {"**/dir2/*", "dir/dir2/file", true},
  296. {"**/dir2/*", "dir/dir2/file/", true},
  297. {"**/dir2/**", "dir/dir2/dir3/file", true},
  298. {"**/dir2/**", "dir/dir2/dir3/file/", true},
  299. {"**file", "file", true},
  300. {"**file", "dir/file", true},
  301. {"**/file", "dir/file", true},
  302. {"**file", "dir/dir/file", true},
  303. {"**/file", "dir/dir/file", true},
  304. {"**/file*", "dir/dir/file", true},
  305. {"**/file*", "dir/dir/file.txt", true},
  306. {"**/file*txt", "dir/dir/file.txt", true},
  307. {"**/file*.txt", "dir/dir/file.txt", true},
  308. {"**/file*.txt*", "dir/dir/file.txt", true},
  309. {"**/**/*.txt", "dir/dir/file.txt", true},
  310. {"**/**/*.txt2", "dir/dir/file.txt", false},
  311. {"**/*.txt", "file.txt", true},
  312. {"**/**/*.txt", "file.txt", true},
  313. {"a**/*.txt", "a/file.txt", true},
  314. {"a**/*.txt", "a/dir/file.txt", true},
  315. {"a**/*.txt", "a/dir/dir/file.txt", true},
  316. {"a/*.txt", "a/dir/file.txt", false},
  317. {"a/*.txt", "a/file.txt", true},
  318. {"a/*.txt**", "a/file.txt", true},
  319. {"a[b-d]e", "ae", false},
  320. {"a[b-d]e", "ace", true},
  321. {"a[b-d]e", "aae", false},
  322. {"a[^b-d]e", "aze", true},
  323. {".*", ".foo", true},
  324. {".*", "foo", false},
  325. {"abc.def", "abcdef", false},
  326. {"abc.def", "abc.def", true},
  327. {"abc.def", "abcZdef", false},
  328. {"abc?def", "abcZdef", true},
  329. {"abc?def", "abcdef", false},
  330. {"a\\\\", "a\\", true},
  331. {"**/foo/bar", "foo/bar", true},
  332. {"**/foo/bar", "dir/foo/bar", true},
  333. {"**/foo/bar", "dir/dir2/foo/bar", true},
  334. {"abc/**", "abc", false},
  335. {"abc/**", "abc/def", true},
  336. {"abc/**", "abc/def/ghi", true},
  337. {"**/.foo", ".foo", true},
  338. {"**/.foo", "bar.foo", false},
  339. }
  340. if runtime.GOOS != "windows" {
  341. tests = append(tests, []matchesTestCase{
  342. {"a\\*b", "a*b", true},
  343. }...)
  344. }
  345. t.Run("MatchesOrParentMatches", func(t *testing.T) {
  346. for _, test := range tests {
  347. desc := fmt.Sprintf("pattern=%q text=%q", test.pattern, test.text)
  348. pm, err := NewPatternMatcher([]string{test.pattern})
  349. assert.NilError(t, err, desc)
  350. res, _ := pm.MatchesOrParentMatches(test.text)
  351. assert.Check(t, is.Equal(test.pass, res), desc)
  352. }
  353. })
  354. t.Run("MatchesUsingParentResult", func(t *testing.T) {
  355. for _, test := range tests {
  356. desc := fmt.Sprintf("pattern=%q text=%q", test.pattern, test.text)
  357. pm, err := NewPatternMatcher([]string{test.pattern})
  358. assert.NilError(t, err, desc)
  359. parentPath := filepath.Dir(filepath.FromSlash(test.text))
  360. parentPathDirs := strings.Split(parentPath, string(os.PathSeparator))
  361. parentMatched := false
  362. if parentPath != "." {
  363. for i := range parentPathDirs {
  364. parentMatched, _ = pm.MatchesUsingParentResult(strings.Join(parentPathDirs[:i+1], "/"), parentMatched)
  365. }
  366. }
  367. res, _ := pm.MatchesUsingParentResult(test.text, parentMatched)
  368. assert.Check(t, is.Equal(test.pass, res), desc)
  369. }
  370. })
  371. }
  372. func TestCleanPatterns(t *testing.T) {
  373. patterns := []string{"docs", "config"}
  374. pm, err := NewPatternMatcher(patterns)
  375. if err != nil {
  376. t.Fatalf("invalid pattern %v", patterns)
  377. }
  378. cleaned := pm.Patterns()
  379. if len(cleaned) != 2 {
  380. t.Errorf("expected 2 element slice, got %v", len(cleaned))
  381. }
  382. }
  383. func TestCleanPatternsStripEmptyPatterns(t *testing.T) {
  384. patterns := []string{"docs", "config", ""}
  385. pm, err := NewPatternMatcher(patterns)
  386. if err != nil {
  387. t.Fatalf("invalid pattern %v", patterns)
  388. }
  389. cleaned := pm.Patterns()
  390. if len(cleaned) != 2 {
  391. t.Errorf("expected 2 element slice, got %v", len(cleaned))
  392. }
  393. }
  394. func TestCleanPatternsExceptionFlag(t *testing.T) {
  395. patterns := []string{"docs", "!docs/README.md"}
  396. pm, err := NewPatternMatcher(patterns)
  397. if err != nil {
  398. t.Fatalf("invalid pattern %v", patterns)
  399. }
  400. if !pm.Exclusions() {
  401. t.Errorf("expected exceptions to be true, got %v", pm.Exclusions())
  402. }
  403. }
  404. func TestCleanPatternsLeadingSpaceTrimmed(t *testing.T) {
  405. patterns := []string{"docs", " !docs/README.md"}
  406. pm, err := NewPatternMatcher(patterns)
  407. if err != nil {
  408. t.Fatalf("invalid pattern %v", patterns)
  409. }
  410. if !pm.Exclusions() {
  411. t.Errorf("expected exceptions to be true, got %v", pm.Exclusions())
  412. }
  413. }
  414. func TestCleanPatternsTrailingSpaceTrimmed(t *testing.T) {
  415. patterns := []string{"docs", "!docs/README.md "}
  416. pm, err := NewPatternMatcher(patterns)
  417. if err != nil {
  418. t.Fatalf("invalid pattern %v", patterns)
  419. }
  420. if !pm.Exclusions() {
  421. t.Errorf("expected exceptions to be true, got %v", pm.Exclusions())
  422. }
  423. }
  424. func TestCleanPatternsErrorSingleException(t *testing.T) {
  425. patterns := []string{"!"}
  426. _, err := NewPatternMatcher(patterns)
  427. if err == nil {
  428. t.Errorf("expected error on single exclamation point, got %v", err)
  429. }
  430. }
  431. func TestCreateIfNotExistsDir(t *testing.T) {
  432. tempFolder, err := ioutil.TempDir("", "docker-fileutils-test")
  433. if err != nil {
  434. t.Fatal(err)
  435. }
  436. defer os.RemoveAll(tempFolder)
  437. folderToCreate := filepath.Join(tempFolder, "tocreate")
  438. if err := CreateIfNotExists(folderToCreate, true); err != nil {
  439. t.Fatal(err)
  440. }
  441. fileinfo, err := os.Stat(folderToCreate)
  442. if err != nil {
  443. t.Fatalf("Should have create a folder, got %v", err)
  444. }
  445. if !fileinfo.IsDir() {
  446. t.Fatalf("Should have been a dir, seems it's not")
  447. }
  448. }
  449. func TestCreateIfNotExistsFile(t *testing.T) {
  450. tempFolder, err := ioutil.TempDir("", "docker-fileutils-test")
  451. if err != nil {
  452. t.Fatal(err)
  453. }
  454. defer os.RemoveAll(tempFolder)
  455. fileToCreate := filepath.Join(tempFolder, "file/to/create")
  456. if err := CreateIfNotExists(fileToCreate, false); err != nil {
  457. t.Fatal(err)
  458. }
  459. fileinfo, err := os.Stat(fileToCreate)
  460. if err != nil {
  461. t.Fatalf("Should have create a file, got %v", err)
  462. }
  463. if fileinfo.IsDir() {
  464. t.Fatalf("Should have been a file, seems it's not")
  465. }
  466. }
  467. // These matchTests are stolen from go's filepath Match tests.
  468. type matchTest struct {
  469. pattern, s string
  470. match bool
  471. err error
  472. }
  473. var matchTests = []matchTest{
  474. {"abc", "abc", true, nil},
  475. {"*", "abc", true, nil},
  476. {"*c", "abc", true, nil},
  477. {"a*", "a", true, nil},
  478. {"a*", "abc", true, nil},
  479. {"a*", "ab/c", true, nil},
  480. {"a*/b", "abc/b", true, nil},
  481. {"a*/b", "a/c/b", false, nil},
  482. {"a*b*c*d*e*/f", "axbxcxdxe/f", true, nil},
  483. {"a*b*c*d*e*/f", "axbxcxdxexxx/f", true, nil},
  484. {"a*b*c*d*e*/f", "axbxcxdxe/xxx/f", false, nil},
  485. {"a*b*c*d*e*/f", "axbxcxdxexxx/fff", false, nil},
  486. {"a*b?c*x", "abxbbxdbxebxczzx", true, nil},
  487. {"a*b?c*x", "abxbbxdbxebxczzy", false, nil},
  488. {"ab[c]", "abc", true, nil},
  489. {"ab[b-d]", "abc", true, nil},
  490. {"ab[e-g]", "abc", false, nil},
  491. {"ab[^c]", "abc", false, nil},
  492. {"ab[^b-d]", "abc", false, nil},
  493. {"ab[^e-g]", "abc", true, nil},
  494. {"a\\*b", "a*b", true, nil},
  495. {"a\\*b", "ab", false, nil},
  496. {"a?b", "a☺b", true, nil},
  497. {"a[^a]b", "a☺b", true, nil},
  498. {"a???b", "a☺b", false, nil},
  499. {"a[^a][^a][^a]b", "a☺b", false, nil},
  500. {"[a-ζ]*", "α", true, nil},
  501. {"*[a-ζ]", "A", false, nil},
  502. {"a?b", "a/b", false, nil},
  503. {"a*b", "a/b", false, nil},
  504. {"[\\]a]", "]", true, nil},
  505. {"[\\-]", "-", true, nil},
  506. {"[x\\-]", "x", true, nil},
  507. {"[x\\-]", "-", true, nil},
  508. {"[x\\-]", "z", false, nil},
  509. {"[\\-x]", "x", true, nil},
  510. {"[\\-x]", "-", true, nil},
  511. {"[\\-x]", "a", false, nil},
  512. {"[]a]", "]", false, filepath.ErrBadPattern},
  513. {"[-]", "-", false, filepath.ErrBadPattern},
  514. {"[x-]", "x", false, filepath.ErrBadPattern},
  515. {"[x-]", "-", false, filepath.ErrBadPattern},
  516. {"[x-]", "z", false, filepath.ErrBadPattern},
  517. {"[-x]", "x", false, filepath.ErrBadPattern},
  518. {"[-x]", "-", false, filepath.ErrBadPattern},
  519. {"[-x]", "a", false, filepath.ErrBadPattern},
  520. {"\\", "a", false, filepath.ErrBadPattern},
  521. {"[a-b-c]", "a", false, filepath.ErrBadPattern},
  522. {"[", "a", false, filepath.ErrBadPattern},
  523. {"[^", "a", false, filepath.ErrBadPattern},
  524. {"[^bc", "a", false, filepath.ErrBadPattern},
  525. {"a[", "a", false, filepath.ErrBadPattern}, // was nil but IMO its wrong
  526. {"a[", "ab", false, filepath.ErrBadPattern},
  527. {"*x", "xxx", true, nil},
  528. }
  529. func errp(e error) string {
  530. if e == nil {
  531. return "<nil>"
  532. }
  533. return e.Error()
  534. }
  535. // TestMatch test's our version of filepath.Match, called regexpMatch.
  536. func TestMatch(t *testing.T) {
  537. for _, tt := range matchTests {
  538. pattern := tt.pattern
  539. s := tt.s
  540. if runtime.GOOS == "windows" {
  541. if strings.Contains(pattern, "\\") {
  542. // no escape allowed on windows.
  543. continue
  544. }
  545. pattern = filepath.Clean(pattern)
  546. s = filepath.Clean(s)
  547. }
  548. ok, err := Matches(s, []string{pattern})
  549. if ok != tt.match || err != tt.err {
  550. t.Fatalf("Match(%#q, %#q) = %v, %q want %v, %q", pattern, s, ok, errp(err), tt.match, errp(tt.err))
  551. }
  552. }
  553. }