fileutils_test.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. package fileutils
  2. import (
  3. "os"
  4. "testing"
  5. )
  6. // Reading a symlink to a directory must return the directory
  7. func TestReadSymlinkedDirectoryExistingDirectory(t *testing.T) {
  8. var err error
  9. if err = os.Mkdir("/tmp/testReadSymlinkToExistingDirectory", 0777); err != nil {
  10. t.Errorf("failed to create directory: %s", err)
  11. }
  12. if err = os.Symlink("/tmp/testReadSymlinkToExistingDirectory", "/tmp/dirLinkTest"); err != nil {
  13. t.Errorf("failed to create symlink: %s", err)
  14. }
  15. var path string
  16. if path, err = ReadSymlinkedDirectory("/tmp/dirLinkTest"); err != nil {
  17. t.Fatalf("failed to read symlink to directory: %s", err)
  18. }
  19. if path != "/tmp/testReadSymlinkToExistingDirectory" {
  20. t.Fatalf("symlink returned unexpected directory: %s", path)
  21. }
  22. if err = os.Remove("/tmp/testReadSymlinkToExistingDirectory"); err != nil {
  23. t.Errorf("failed to remove temporary directory: %s", err)
  24. }
  25. if err = os.Remove("/tmp/dirLinkTest"); err != nil {
  26. t.Errorf("failed to remove symlink: %s", err)
  27. }
  28. }
  29. // Reading a non-existing symlink must fail
  30. func TestReadSymlinkedDirectoryNonExistingSymlink(t *testing.T) {
  31. var path string
  32. var err error
  33. if path, err = ReadSymlinkedDirectory("/tmp/test/foo/Non/ExistingPath"); err == nil {
  34. t.Fatalf("error expected for non-existing symlink")
  35. }
  36. if path != "" {
  37. t.Fatalf("expected empty path, but '%s' was returned", path)
  38. }
  39. }
  40. // Reading a symlink to a file must fail
  41. func TestReadSymlinkedDirectoryToFile(t *testing.T) {
  42. var err error
  43. var file *os.File
  44. if file, err = os.Create("/tmp/testReadSymlinkToFile"); err != nil {
  45. t.Fatalf("failed to create file: %s", err)
  46. }
  47. file.Close()
  48. if err = os.Symlink("/tmp/testReadSymlinkToFile", "/tmp/fileLinkTest"); err != nil {
  49. t.Errorf("failed to create symlink: %s", err)
  50. }
  51. var path string
  52. if path, err = ReadSymlinkedDirectory("/tmp/fileLinkTest"); err == nil {
  53. t.Fatalf("ReadSymlinkedDirectory on a symlink to a file should've failed")
  54. }
  55. if path != "" {
  56. t.Fatalf("path should've been empty: %s", path)
  57. }
  58. if err = os.Remove("/tmp/testReadSymlinkToFile"); err != nil {
  59. t.Errorf("failed to remove file: %s", err)
  60. }
  61. if err = os.Remove("/tmp/fileLinkTest"); err != nil {
  62. t.Errorf("failed to remove symlink: %s", err)
  63. }
  64. }
  65. func TestWildcardMatches(t *testing.T) {
  66. match, _ := Matches("fileutils.go", []string{"*"})
  67. if match != true {
  68. t.Errorf("failed to get a wildcard match, got %v", match)
  69. }
  70. }
  71. // A simple pattern match should return true.
  72. func TestPatternMatches(t *testing.T) {
  73. match, _ := Matches("fileutils.go", []string{"*.go"})
  74. if match != true {
  75. t.Errorf("failed to get a match, got %v", match)
  76. }
  77. }
  78. // An exclusion followed by an inclusion should return true.
  79. func TestExclusionPatternMatchesPatternBefore(t *testing.T) {
  80. match, _ := Matches("fileutils.go", []string{"!fileutils.go", "*.go"})
  81. if match != true {
  82. t.Errorf("failed to get true match on exclusion pattern, got %v", match)
  83. }
  84. }
  85. // A folder pattern followed by an exception should return false.
  86. func TestPatternMatchesFolderExclusions(t *testing.T) {
  87. match, _ := Matches("docs/README.md", []string{"docs", "!docs/README.md"})
  88. if match != false {
  89. t.Errorf("failed to get a false match on exclusion pattern, got %v", match)
  90. }
  91. }
  92. // A folder pattern followed by an exception should return false.
  93. func TestPatternMatchesFolderWithSlashExclusions(t *testing.T) {
  94. match, _ := Matches("docs/README.md", []string{"docs/", "!docs/README.md"})
  95. if match != false {
  96. t.Errorf("failed to get a false match on exclusion pattern, got %v", match)
  97. }
  98. }
  99. // A folder pattern followed by an exception should return false.
  100. func TestPatternMatchesFolderWildcardExclusions(t *testing.T) {
  101. match, _ := Matches("docs/README.md", []string{"docs/*", "!docs/README.md"})
  102. if match != false {
  103. t.Errorf("failed to get a false match on exclusion pattern, got %v", match)
  104. }
  105. }
  106. // A pattern followed by an exclusion should return false.
  107. func TestExclusionPatternMatchesPatternAfter(t *testing.T) {
  108. match, _ := Matches("fileutils.go", []string{"*.go", "!fileutils.go"})
  109. if match != false {
  110. t.Errorf("failed to get false match on exclusion pattern, got %v", match)
  111. }
  112. }
  113. // A filename evaluating to . should return false.
  114. func TestExclusionPatternMatchesWholeDirectory(t *testing.T) {
  115. match, _ := Matches(".", []string{"*.go"})
  116. if match != false {
  117. t.Errorf("failed to get false match on ., got %v", match)
  118. }
  119. }
  120. // A single ! pattern should return an error.
  121. func TestSingleExclamationError(t *testing.T) {
  122. _, err := Matches("fileutils.go", []string{"!"})
  123. if err == nil {
  124. t.Errorf("failed to get an error for a single exclamation point, got %v", err)
  125. }
  126. }
  127. // A string preceded with a ! should return true from Exclusion.
  128. func TestExclusion(t *testing.T) {
  129. exclusion := Exclusion("!")
  130. if !exclusion {
  131. t.Errorf("failed to get true for a single !, got %v", exclusion)
  132. }
  133. }
  134. // An empty string should return true from Empty.
  135. func TestEmpty(t *testing.T) {
  136. empty := Empty("")
  137. if !empty {
  138. t.Errorf("failed to get true for an empty string, got %v", empty)
  139. }
  140. }
  141. func TestCleanPatterns(t *testing.T) {
  142. cleaned, _, _, _ := CleanPatterns([]string{"docs", "config"})
  143. if len(cleaned) != 2 {
  144. t.Errorf("expected 2 element slice, got %v", len(cleaned))
  145. }
  146. }
  147. func TestCleanPatternsStripEmptyPatterns(t *testing.T) {
  148. cleaned, _, _, _ := CleanPatterns([]string{"docs", "config", ""})
  149. if len(cleaned) != 2 {
  150. t.Errorf("expected 2 element slice, got %v", len(cleaned))
  151. }
  152. }
  153. func TestCleanPatternsExceptionFlag(t *testing.T) {
  154. _, _, exceptions, _ := CleanPatterns([]string{"docs", "!docs/README.md"})
  155. if !exceptions {
  156. t.Errorf("expected exceptions to be true, got %v", exceptions)
  157. }
  158. }
  159. func TestCleanPatternsLeadingSpaceTrimmed(t *testing.T) {
  160. _, _, exceptions, _ := CleanPatterns([]string{"docs", " !docs/README.md"})
  161. if !exceptions {
  162. t.Errorf("expected exceptions to be true, got %v", exceptions)
  163. }
  164. }
  165. func TestCleanPatternsTrailingSpaceTrimmed(t *testing.T) {
  166. _, _, exceptions, _ := CleanPatterns([]string{"docs", "!docs/README.md "})
  167. if !exceptions {
  168. t.Errorf("expected exceptions to be true, got %v", exceptions)
  169. }
  170. }
  171. func TestCleanPatternsErrorSingleException(t *testing.T) {
  172. _, _, _, err := CleanPatterns([]string{"!"})
  173. if err == nil {
  174. t.Errorf("expected error on single exclamation point, got %v", err)
  175. }
  176. }
  177. func TestCleanPatternsFolderSplit(t *testing.T) {
  178. _, dirs, _, _ := CleanPatterns([]string{"docs/config/CONFIG.md"})
  179. if dirs[0][0] != "docs" {
  180. t.Errorf("expected first element in dirs slice to be docs, got %v", dirs[0][1])
  181. }
  182. if dirs[0][1] != "config" {
  183. t.Errorf("expected first element in dirs slice to be config, got %v", dirs[0][1])
  184. }
  185. }