fileutils_test.go 22 KB

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