fileutils_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. package fileutils
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "path"
  6. "path/filepath"
  7. "testing"
  8. )
  9. // CopyFile with invalid src
  10. func TestCopyFileWithInvalidSrc(t *testing.T) {
  11. tempFolder, err := ioutil.TempDir("", "docker-fileutils-test")
  12. defer os.RemoveAll(tempFolder)
  13. if err != nil {
  14. t.Fatal(err)
  15. }
  16. bytes, err := CopyFile("/invalid/file/path", path.Join(tempFolder, "dest"))
  17. if err == nil {
  18. t.Fatal("Should have fail to copy an invalid src file")
  19. }
  20. if bytes != 0 {
  21. t.Fatal("Should have written 0 bytes")
  22. }
  23. }
  24. // CopyFile with invalid dest
  25. func TestCopyFileWithInvalidDest(t *testing.T) {
  26. tempFolder, err := ioutil.TempDir("", "docker-fileutils-test")
  27. defer os.RemoveAll(tempFolder)
  28. if err != nil {
  29. t.Fatal(err)
  30. }
  31. src := path.Join(tempFolder, "file")
  32. err = ioutil.WriteFile(src, []byte("content"), 0740)
  33. if err != nil {
  34. t.Fatal(err)
  35. }
  36. bytes, err := CopyFile(src, path.Join(tempFolder, "/invalid/dest/path"))
  37. if err == nil {
  38. t.Fatal("Should have fail to copy an invalid src file")
  39. }
  40. if bytes != 0 {
  41. t.Fatal("Should have written 0 bytes")
  42. }
  43. }
  44. // CopyFile with same src and dest
  45. func TestCopyFileWithSameSrcAndDest(t *testing.T) {
  46. tempFolder, err := ioutil.TempDir("", "docker-fileutils-test")
  47. defer os.RemoveAll(tempFolder)
  48. if err != nil {
  49. t.Fatal(err)
  50. }
  51. file := path.Join(tempFolder, "file")
  52. err = ioutil.WriteFile(file, []byte("content"), 0740)
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. bytes, err := CopyFile(file, file)
  57. if err != nil {
  58. t.Fatal(err)
  59. }
  60. if bytes != 0 {
  61. t.Fatal("Should have written 0 bytes as it is the same file.")
  62. }
  63. }
  64. // CopyFile with same src and dest but path is different and not clean
  65. func TestCopyFileWithSameSrcAndDestWithPathNameDifferent(t *testing.T) {
  66. tempFolder, err := ioutil.TempDir("", "docker-fileutils-test")
  67. defer os.RemoveAll(tempFolder)
  68. if err != nil {
  69. t.Fatal(err)
  70. }
  71. testFolder := path.Join(tempFolder, "test")
  72. err = os.MkdirAll(testFolder, 0740)
  73. if err != nil {
  74. t.Fatal(err)
  75. }
  76. file := path.Join(testFolder, "file")
  77. sameFile := testFolder + "/../test/file"
  78. err = ioutil.WriteFile(file, []byte("content"), 0740)
  79. if err != nil {
  80. t.Fatal(err)
  81. }
  82. bytes, err := CopyFile(file, sameFile)
  83. if err != nil {
  84. t.Fatal(err)
  85. }
  86. if bytes != 0 {
  87. t.Fatal("Should have written 0 bytes as it is the same file.")
  88. }
  89. }
  90. func TestCopyFile(t *testing.T) {
  91. tempFolder, err := ioutil.TempDir("", "docker-fileutils-test")
  92. defer os.RemoveAll(tempFolder)
  93. if err != nil {
  94. t.Fatal(err)
  95. }
  96. src := path.Join(tempFolder, "src")
  97. dest := path.Join(tempFolder, "dest")
  98. ioutil.WriteFile(src, []byte("content"), 0777)
  99. ioutil.WriteFile(dest, []byte("destContent"), 0777)
  100. bytes, err := CopyFile(src, dest)
  101. if err != nil {
  102. t.Fatal(err)
  103. }
  104. if bytes != 7 {
  105. t.Fatalf("Should have written %d bytes but wrote %d", 7, bytes)
  106. }
  107. actual, err := ioutil.ReadFile(dest)
  108. if err != nil {
  109. t.Fatal(err)
  110. }
  111. if string(actual) != "content" {
  112. t.Fatalf("Dest content was '%s', expected '%s'", string(actual), "content")
  113. }
  114. }
  115. // Reading a symlink to a directory must return the directory
  116. func TestReadSymlinkedDirectoryExistingDirectory(t *testing.T) {
  117. var err error
  118. if err = os.Mkdir("/tmp/testReadSymlinkToExistingDirectory", 0777); err != nil {
  119. t.Errorf("failed to create directory: %s", err)
  120. }
  121. if err = os.Symlink("/tmp/testReadSymlinkToExistingDirectory", "/tmp/dirLinkTest"); err != nil {
  122. t.Errorf("failed to create symlink: %s", err)
  123. }
  124. var path string
  125. if path, err = ReadSymlinkedDirectory("/tmp/dirLinkTest"); err != nil {
  126. t.Fatalf("failed to read symlink to directory: %s", err)
  127. }
  128. if path != "/tmp/testReadSymlinkToExistingDirectory" {
  129. t.Fatalf("symlink returned unexpected directory: %s", path)
  130. }
  131. if err = os.Remove("/tmp/testReadSymlinkToExistingDirectory"); err != nil {
  132. t.Errorf("failed to remove temporary directory: %s", err)
  133. }
  134. if err = os.Remove("/tmp/dirLinkTest"); err != nil {
  135. t.Errorf("failed to remove symlink: %s", err)
  136. }
  137. }
  138. // Reading a non-existing symlink must fail
  139. func TestReadSymlinkedDirectoryNonExistingSymlink(t *testing.T) {
  140. var path string
  141. var err error
  142. if path, err = ReadSymlinkedDirectory("/tmp/test/foo/Non/ExistingPath"); err == nil {
  143. t.Fatalf("error expected for non-existing symlink")
  144. }
  145. if path != "" {
  146. t.Fatalf("expected empty path, but '%s' was returned", path)
  147. }
  148. }
  149. // Reading a symlink to a file must fail
  150. func TestReadSymlinkedDirectoryToFile(t *testing.T) {
  151. var err error
  152. var file *os.File
  153. if file, err = os.Create("/tmp/testReadSymlinkToFile"); err != nil {
  154. t.Fatalf("failed to create file: %s", err)
  155. }
  156. file.Close()
  157. if err = os.Symlink("/tmp/testReadSymlinkToFile", "/tmp/fileLinkTest"); err != nil {
  158. t.Errorf("failed to create symlink: %s", err)
  159. }
  160. var path string
  161. if path, err = ReadSymlinkedDirectory("/tmp/fileLinkTest"); err == nil {
  162. t.Fatalf("ReadSymlinkedDirectory on a symlink to a file should've failed")
  163. }
  164. if path != "" {
  165. t.Fatalf("path should've been empty: %s", path)
  166. }
  167. if err = os.Remove("/tmp/testReadSymlinkToFile"); err != nil {
  168. t.Errorf("failed to remove file: %s", err)
  169. }
  170. if err = os.Remove("/tmp/fileLinkTest"); err != nil {
  171. t.Errorf("failed to remove symlink: %s", err)
  172. }
  173. }
  174. func TestWildcardMatches(t *testing.T) {
  175. match, _ := Matches("fileutils.go", []string{"*"})
  176. if match != true {
  177. t.Errorf("failed to get a wildcard match, got %v", match)
  178. }
  179. }
  180. // A simple pattern match should return true.
  181. func TestPatternMatches(t *testing.T) {
  182. match, _ := Matches("fileutils.go", []string{"*.go"})
  183. if match != true {
  184. t.Errorf("failed to get a match, got %v", match)
  185. }
  186. }
  187. // An exclusion followed by an inclusion should return true.
  188. func TestExclusionPatternMatchesPatternBefore(t *testing.T) {
  189. match, _ := Matches("fileutils.go", []string{"!fileutils.go", "*.go"})
  190. if match != true {
  191. t.Errorf("failed to get true match on exclusion pattern, got %v", match)
  192. }
  193. }
  194. // A folder pattern followed by an exception should return false.
  195. func TestPatternMatchesFolderExclusions(t *testing.T) {
  196. match, _ := Matches("docs/README.md", []string{"docs", "!docs/README.md"})
  197. if match != false {
  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 TestPatternMatchesFolderWithSlashExclusions(t *testing.T) {
  203. match, _ := Matches("docs/README.md", []string{"docs/", "!docs/README.md"})
  204. if match != false {
  205. t.Errorf("failed to get a false match on exclusion pattern, got %v", match)
  206. }
  207. }
  208. // A folder pattern followed by an exception should return false.
  209. func TestPatternMatchesFolderWildcardExclusions(t *testing.T) {
  210. match, _ := Matches("docs/README.md", []string{"docs/*", "!docs/README.md"})
  211. if match != false {
  212. t.Errorf("failed to get a false match on exclusion pattern, got %v", match)
  213. }
  214. }
  215. // A pattern followed by an exclusion should return false.
  216. func TestExclusionPatternMatchesPatternAfter(t *testing.T) {
  217. match, _ := Matches("fileutils.go", []string{"*.go", "!fileutils.go"})
  218. if match != false {
  219. t.Errorf("failed to get false match on exclusion pattern, got %v", match)
  220. }
  221. }
  222. // A filename evaluating to . should return false.
  223. func TestExclusionPatternMatchesWholeDirectory(t *testing.T) {
  224. match, _ := Matches(".", []string{"*.go"})
  225. if match != false {
  226. t.Errorf("failed to get false match on ., got %v", match)
  227. }
  228. }
  229. // A single ! pattern should return an error.
  230. func TestSingleExclamationError(t *testing.T) {
  231. _, err := Matches("fileutils.go", []string{"!"})
  232. if err == nil {
  233. t.Errorf("failed to get an error for a single exclamation point, got %v", err)
  234. }
  235. }
  236. // A string preceded with a ! should return true from Exclusion.
  237. func TestExclusion(t *testing.T) {
  238. exclusion := exclusion("!")
  239. if !exclusion {
  240. t.Errorf("failed to get true for a single !, got %v", exclusion)
  241. }
  242. }
  243. // Matches with no patterns
  244. func TestMatchesWithNoPatterns(t *testing.T) {
  245. matches, err := Matches("/any/path/there", []string{})
  246. if err != nil {
  247. t.Fatal(err)
  248. }
  249. if matches {
  250. t.Fatalf("Should not have match anything")
  251. }
  252. }
  253. // Matches with malformed patterns
  254. func TestMatchesWithMalformedPatterns(t *testing.T) {
  255. matches, err := Matches("/any/path/there", []string{"["})
  256. if err == nil {
  257. t.Fatal("Should have failed because of a malformed syntax in the pattern")
  258. }
  259. if matches {
  260. t.Fatalf("Should not have match anything")
  261. }
  262. }
  263. // An empty string should return true from Empty.
  264. func TestEmpty(t *testing.T) {
  265. empty := empty("")
  266. if !empty {
  267. t.Errorf("failed to get true for an empty string, got %v", empty)
  268. }
  269. }
  270. func TestCleanPatterns(t *testing.T) {
  271. cleaned, _, _, _ := CleanPatterns([]string{"docs", "config"})
  272. if len(cleaned) != 2 {
  273. t.Errorf("expected 2 element slice, got %v", len(cleaned))
  274. }
  275. }
  276. func TestCleanPatternsStripEmptyPatterns(t *testing.T) {
  277. cleaned, _, _, _ := CleanPatterns([]string{"docs", "config", ""})
  278. if len(cleaned) != 2 {
  279. t.Errorf("expected 2 element slice, got %v", len(cleaned))
  280. }
  281. }
  282. func TestCleanPatternsExceptionFlag(t *testing.T) {
  283. _, _, exceptions, _ := CleanPatterns([]string{"docs", "!docs/README.md"})
  284. if !exceptions {
  285. t.Errorf("expected exceptions to be true, got %v", exceptions)
  286. }
  287. }
  288. func TestCleanPatternsLeadingSpaceTrimmed(t *testing.T) {
  289. _, _, exceptions, _ := CleanPatterns([]string{"docs", " !docs/README.md"})
  290. if !exceptions {
  291. t.Errorf("expected exceptions to be true, got %v", exceptions)
  292. }
  293. }
  294. func TestCleanPatternsTrailingSpaceTrimmed(t *testing.T) {
  295. _, _, exceptions, _ := CleanPatterns([]string{"docs", "!docs/README.md "})
  296. if !exceptions {
  297. t.Errorf("expected exceptions to be true, got %v", exceptions)
  298. }
  299. }
  300. func TestCleanPatternsErrorSingleException(t *testing.T) {
  301. _, _, _, err := CleanPatterns([]string{"!"})
  302. if err == nil {
  303. t.Errorf("expected error on single exclamation point, got %v", err)
  304. }
  305. }
  306. func TestCleanPatternsFolderSplit(t *testing.T) {
  307. _, dirs, _, _ := CleanPatterns([]string{"docs/config/CONFIG.md"})
  308. if dirs[0][0] != "docs" {
  309. t.Errorf("expected first element in dirs slice to be docs, got %v", dirs[0][1])
  310. }
  311. if dirs[0][1] != "config" {
  312. t.Errorf("expected first element in dirs slice to be config, got %v", dirs[0][1])
  313. }
  314. }
  315. func TestCreateIfNotExistsDir(t *testing.T) {
  316. tempFolder, err := ioutil.TempDir("", "docker-fileutils-test")
  317. if err != nil {
  318. t.Fatal(err)
  319. }
  320. defer os.RemoveAll(tempFolder)
  321. folderToCreate := filepath.Join(tempFolder, "tocreate")
  322. if err := CreateIfNotExists(folderToCreate, true); err != nil {
  323. t.Fatal(err)
  324. }
  325. fileinfo, err := os.Stat(folderToCreate)
  326. if err != nil {
  327. t.Fatalf("Should have create a folder, got %v", err)
  328. }
  329. if !fileinfo.IsDir() {
  330. t.Fatalf("Should have been a dir, seems it's not")
  331. }
  332. }
  333. func TestCreateIfNotExistsFile(t *testing.T) {
  334. tempFolder, err := ioutil.TempDir("", "docker-fileutils-test")
  335. if err != nil {
  336. t.Fatal(err)
  337. }
  338. defer os.RemoveAll(tempFolder)
  339. fileToCreate := filepath.Join(tempFolder, "file/to/create")
  340. if err := CreateIfNotExists(fileToCreate, false); err != nil {
  341. t.Fatal(err)
  342. }
  343. fileinfo, err := os.Stat(fileToCreate)
  344. if err != nil {
  345. t.Fatalf("Should have create a file, got %v", err)
  346. }
  347. if fileinfo.IsDir() {
  348. t.Fatalf("Should have been a file, seems it's not")
  349. }
  350. }