ignore_test.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package ignore
  16. import (
  17. "bytes"
  18. "io/ioutil"
  19. "os"
  20. "path/filepath"
  21. "runtime"
  22. "testing"
  23. )
  24. func TestIgnore(t *testing.T) {
  25. pats, err := Load("testdata/.stignore", true)
  26. if err != nil {
  27. t.Fatal(err)
  28. }
  29. var tests = []struct {
  30. f string
  31. r bool
  32. }{
  33. {"afile", false},
  34. {"bfile", true},
  35. {"cfile", false},
  36. {"dfile", false},
  37. {"efile", true},
  38. {"ffile", true},
  39. {"dir1", false},
  40. {filepath.Join("dir1", "cfile"), true},
  41. {filepath.Join("dir1", "dfile"), false},
  42. {filepath.Join("dir1", "efile"), true},
  43. {filepath.Join("dir1", "ffile"), false},
  44. {"dir2", false},
  45. {filepath.Join("dir2", "cfile"), false},
  46. {filepath.Join("dir2", "dfile"), true},
  47. {filepath.Join("dir2", "efile"), true},
  48. {filepath.Join("dir2", "ffile"), false},
  49. {filepath.Join("dir3"), true},
  50. {filepath.Join("dir3", "afile"), true},
  51. }
  52. for i, tc := range tests {
  53. if r := pats.Match(tc.f); r != tc.r {
  54. t.Errorf("Incorrect ignoreFile() #%d (%s); E: %v, A: %v", i, tc.f, tc.r, r)
  55. }
  56. }
  57. }
  58. func TestExcludes(t *testing.T) {
  59. stignore := `
  60. !iex2
  61. !ign1/ex
  62. ign1
  63. i*2
  64. !ign2
  65. `
  66. pats, err := Parse(bytes.NewBufferString(stignore), ".stignore")
  67. if err != nil {
  68. t.Fatal(err)
  69. }
  70. var tests = []struct {
  71. f string
  72. r bool
  73. }{
  74. {"ign1", true},
  75. {"ign2", true},
  76. {"ibla2", true},
  77. {"iex2", false},
  78. {filepath.Join("ign1", "ign"), true},
  79. {filepath.Join("ign1", "ex"), false},
  80. {filepath.Join("ign1", "iex2"), false},
  81. {filepath.Join("iex2", "ign"), false},
  82. {filepath.Join("foo", "bar", "ign1"), true},
  83. {filepath.Join("foo", "bar", "ign2"), true},
  84. {filepath.Join("foo", "bar", "iex2"), false},
  85. }
  86. for _, tc := range tests {
  87. if r := pats.Match(tc.f); r != tc.r {
  88. t.Errorf("Incorrect match for %s: %v != %v", tc.f, r, tc.r)
  89. }
  90. }
  91. }
  92. func TestBadPatterns(t *testing.T) {
  93. var badPatterns = []string{
  94. "[",
  95. "/[",
  96. "**/[",
  97. "#include nonexistent",
  98. "#include .stignore",
  99. "!#include makesnosense",
  100. }
  101. for _, pat := range badPatterns {
  102. parsed, err := Parse(bytes.NewBufferString(pat), ".stignore")
  103. if err == nil {
  104. t.Errorf("No error for pattern %q: %v", pat, parsed)
  105. }
  106. }
  107. }
  108. func TestCaseSensitivity(t *testing.T) {
  109. ign, _ := Parse(bytes.NewBufferString("test"), ".stignore")
  110. match := []string{"test"}
  111. dontMatch := []string{"foo"}
  112. switch runtime.GOOS {
  113. case "darwin", "windows":
  114. match = append(match, "TEST", "Test", "tESt")
  115. default:
  116. dontMatch = append(dontMatch, "TEST", "Test", "tESt")
  117. }
  118. for _, tc := range match {
  119. if !ign.Match(tc) {
  120. t.Errorf("Incorrect match for %q: should be matched", tc)
  121. }
  122. }
  123. for _, tc := range dontMatch {
  124. if ign.Match(tc) {
  125. t.Errorf("Incorrect match for %q: should not be matched", tc)
  126. }
  127. }
  128. }
  129. func TestCaching(t *testing.T) {
  130. fd1, err := ioutil.TempFile("", "")
  131. if err != nil {
  132. t.Fatal(err)
  133. }
  134. fd2, err := ioutil.TempFile("", "")
  135. if err != nil {
  136. t.Fatal(err)
  137. }
  138. defer fd1.Close()
  139. defer fd2.Close()
  140. defer os.Remove(fd1.Name())
  141. defer os.Remove(fd2.Name())
  142. _, err = fd1.WriteString("/x/\n#include " + filepath.Base(fd2.Name()) + "\n")
  143. if err != nil {
  144. t.Fatal(err)
  145. }
  146. fd2.WriteString("/y/\n")
  147. pats, err := Load(fd1.Name(), true)
  148. if err != nil {
  149. t.Fatal(err)
  150. }
  151. if pats.oldMatches == nil || len(pats.oldMatches) != 0 {
  152. t.Fatal("Expected empty map")
  153. }
  154. if pats.newMatches == nil || len(pats.newMatches) != 0 {
  155. t.Fatal("Expected empty map")
  156. }
  157. if len(pats.patterns) != 4 {
  158. t.Fatal("Incorrect number of patterns loaded", len(pats.patterns), "!=", 4)
  159. }
  160. // Cache some outcomes
  161. for _, letter := range []string{"a", "b", "x", "y"} {
  162. pats.Match(letter)
  163. }
  164. if len(pats.newMatches) != 4 {
  165. t.Fatal("Expected 4 cached results")
  166. }
  167. // Reload file, expect old outcomes to be provided
  168. pats, err = Load(fd1.Name(), true)
  169. if err != nil {
  170. t.Fatal(err)
  171. }
  172. if len(pats.oldMatches) != 4 {
  173. t.Fatal("Expected 4 cached results")
  174. }
  175. // Match less this time
  176. for _, letter := range []string{"b", "x", "y"} {
  177. pats.Match(letter)
  178. }
  179. if len(pats.newMatches) != 3 {
  180. t.Fatal("Expected 3 cached results")
  181. }
  182. // Reload file, expect the new outcomes to be provided
  183. pats, err = Load(fd1.Name(), true)
  184. if err != nil {
  185. t.Fatal(err)
  186. }
  187. if len(pats.oldMatches) != 3 {
  188. t.Fatal("Expected 3 cached results", len(pats.oldMatches))
  189. }
  190. // Modify the include file, expect empty cache
  191. fd2.WriteString("/z/\n")
  192. pats, err = Load(fd1.Name(), true)
  193. if err != nil {
  194. t.Fatal(err)
  195. }
  196. if len(pats.oldMatches) != 0 {
  197. t.Fatal("Expected 0 cached results")
  198. }
  199. // Cache some outcomes again
  200. for _, letter := range []string{"b", "x", "y"} {
  201. pats.Match(letter)
  202. }
  203. // Verify that outcomes provided on next laod
  204. pats, err = Load(fd1.Name(), true)
  205. if err != nil {
  206. t.Fatal(err)
  207. }
  208. if len(pats.oldMatches) != 3 {
  209. t.Fatal("Expected 3 cached results")
  210. }
  211. // Modify the root file, expect cache to be invalidated
  212. fd1.WriteString("/a/\n")
  213. pats, err = Load(fd1.Name(), true)
  214. if err != nil {
  215. t.Fatal(err)
  216. }
  217. if len(pats.oldMatches) != 0 {
  218. t.Fatal("Expected cache invalidation")
  219. }
  220. // Cache some outcomes again
  221. for _, letter := range []string{"b", "x", "y"} {
  222. pats.Match(letter)
  223. }
  224. // Verify that outcomes provided on next laod
  225. pats, err = Load(fd1.Name(), true)
  226. if err != nil {
  227. t.Fatal(err)
  228. }
  229. if len(pats.oldMatches) != 3 {
  230. t.Fatal("Expected 3 cached results")
  231. }
  232. }
  233. func TestCommentsAndBlankLines(t *testing.T) {
  234. stignore := `
  235. // foo
  236. //bar
  237. //!baz
  238. //#dex
  239. // ips
  240. `
  241. pats, _ := Parse(bytes.NewBufferString(stignore), ".stignore")
  242. if len(pats.patterns) > 0 {
  243. t.Errorf("Expected no patterns")
  244. }
  245. }
  246. var result bool
  247. func BenchmarkMatch(b *testing.B) {
  248. stignore := `
  249. .frog
  250. .frog*
  251. .frogfox
  252. .whale
  253. .whale/*
  254. .dolphin
  255. .dolphin/*
  256. ~ferret~.*
  257. .ferret.*
  258. flamingo.*
  259. flamingo
  260. *.crow
  261. *.crow
  262. `
  263. pats, _ := Parse(bytes.NewBufferString(stignore), ".stignore")
  264. b.ResetTimer()
  265. for i := 0; i < b.N; i++ {
  266. result = pats.Match("filename")
  267. }
  268. }
  269. func BenchmarkMatchCached(b *testing.B) {
  270. stignore := `
  271. .frog
  272. .frog*
  273. .frogfox
  274. .whale
  275. .whale/*
  276. .dolphin
  277. .dolphin/*
  278. ~ferret~.*
  279. .ferret.*
  280. flamingo.*
  281. flamingo
  282. *.crow
  283. *.crow
  284. `
  285. // Caches per file, hence write the patterns to a file.
  286. fd, err := ioutil.TempFile("", "")
  287. if err != nil {
  288. b.Fatal(err)
  289. }
  290. _, err = fd.WriteString(stignore)
  291. defer fd.Close()
  292. defer os.Remove(fd.Name())
  293. if err != nil {
  294. b.Fatal(err)
  295. }
  296. // Load the patterns
  297. pats, err := Load(fd.Name(), true)
  298. if err != nil {
  299. b.Fatal(err)
  300. }
  301. // Cache the outcome for "filename"
  302. pats.Match("filename")
  303. // This load should now load the cached outcomes as the set of patterns
  304. // has not changed.
  305. pats, err = Load(fd.Name(), true)
  306. if err != nil {
  307. b.Fatal(err)
  308. }
  309. b.ResetTimer()
  310. for i := 0; i < b.N; i++ {
  311. result = pats.Match("filename")
  312. }
  313. }