fileutils_test.go 9.9 KB

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