ignore_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
  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_test
  16. import (
  17. "bytes"
  18. "path/filepath"
  19. "runtime"
  20. "testing"
  21. "github.com/syncthing/syncthing/internal/ignore"
  22. )
  23. func TestIgnore(t *testing.T) {
  24. pats, err := ignore.Load("testdata/.stignore")
  25. if err != nil {
  26. t.Fatal(err)
  27. }
  28. var tests = []struct {
  29. f string
  30. r bool
  31. }{
  32. {"afile", false},
  33. {"bfile", true},
  34. {"cfile", false},
  35. {"dfile", false},
  36. {"efile", true},
  37. {"ffile", true},
  38. {"dir1", false},
  39. {filepath.Join("dir1", "cfile"), true},
  40. {filepath.Join("dir1", "dfile"), false},
  41. {filepath.Join("dir1", "efile"), true},
  42. {filepath.Join("dir1", "ffile"), false},
  43. {"dir2", false},
  44. {filepath.Join("dir2", "cfile"), false},
  45. {filepath.Join("dir2", "dfile"), true},
  46. {filepath.Join("dir2", "efile"), true},
  47. {filepath.Join("dir2", "ffile"), false},
  48. {filepath.Join("dir3"), true},
  49. {filepath.Join("dir3", "afile"), true},
  50. }
  51. for i, tc := range tests {
  52. if r := pats.Match(tc.f); r != tc.r {
  53. t.Errorf("Incorrect ignoreFile() #%d (%s); E: %v, A: %v", i, tc.f, tc.r, r)
  54. }
  55. }
  56. }
  57. func TestExcludes(t *testing.T) {
  58. stignore := `
  59. !iex2
  60. !ign1/ex
  61. ign1
  62. i*2
  63. !ign2
  64. `
  65. pats, err := ignore.Parse(bytes.NewBufferString(stignore), ".stignore")
  66. if err != nil {
  67. t.Fatal(err)
  68. }
  69. var tests = []struct {
  70. f string
  71. r bool
  72. }{
  73. {"ign1", true},
  74. {"ign2", true},
  75. {"ibla2", true},
  76. {"iex2", false},
  77. {filepath.Join("ign1", "ign"), true},
  78. {filepath.Join("ign1", "ex"), false},
  79. {filepath.Join("ign1", "iex2"), false},
  80. {filepath.Join("iex2", "ign"), false},
  81. {filepath.Join("foo", "bar", "ign1"), true},
  82. {filepath.Join("foo", "bar", "ign2"), true},
  83. {filepath.Join("foo", "bar", "iex2"), false},
  84. }
  85. for _, tc := range tests {
  86. if r := pats.Match(tc.f); r != tc.r {
  87. t.Errorf("Incorrect match for %s: %v != %v", tc.f, r, tc.r)
  88. }
  89. }
  90. }
  91. func TestBadPatterns(t *testing.T) {
  92. var badPatterns = []string{
  93. "[",
  94. "/[",
  95. "**/[",
  96. "#include nonexistent",
  97. "#include .stignore",
  98. "!#include makesnosense",
  99. }
  100. for _, pat := range badPatterns {
  101. parsed, err := ignore.Parse(bytes.NewBufferString(pat), ".stignore")
  102. if err == nil {
  103. t.Errorf("No error for pattern %q: %v", pat, parsed)
  104. }
  105. }
  106. }
  107. func TestCaseSensitivity(t *testing.T) {
  108. ign, _ := ignore.Parse(bytes.NewBufferString("test"), ".stignore")
  109. match := []string{"test"}
  110. dontMatch := []string{"foo"}
  111. switch runtime.GOOS {
  112. case "darwin", "windows":
  113. match = append(match, "TEST", "Test", "tESt")
  114. default:
  115. dontMatch = append(dontMatch, "TEST", "Test", "tESt")
  116. }
  117. for _, tc := range match {
  118. if !ign.Match(tc) {
  119. t.Errorf("Incorrect match for %q: should be matched", tc)
  120. }
  121. }
  122. for _, tc := range dontMatch {
  123. if ign.Match(tc) {
  124. t.Errorf("Incorrect match for %q: should not be matched", tc)
  125. }
  126. }
  127. }
  128. func TestCommentsAndBlankLines(t *testing.T) {
  129. stignore := `
  130. // foo
  131. //bar
  132. //!baz
  133. //#dex
  134. // ips
  135. `
  136. pats, _ := ignore.Parse(bytes.NewBufferString(stignore), ".stignore")
  137. if len(pats) > 0 {
  138. t.Errorf("Expected no patterns")
  139. }
  140. }
  141. var result bool
  142. func BenchmarkMatch(b *testing.B) {
  143. stignore := `
  144. .frog
  145. .frog*
  146. .frogfox
  147. .whale
  148. .whale/*
  149. .dolphin
  150. .dolphin/*
  151. ~ferret~.*
  152. .ferret.*
  153. flamingo.*
  154. flamingo
  155. *.crow
  156. *.crow
  157. `
  158. pats, _ := ignore.Parse(bytes.NewBufferString(stignore), ".stignore")
  159. b.ResetTimer()
  160. for i := 0; i < b.N; i++ {
  161. result = pats.Match("filename")
  162. }
  163. }