ignore_test.go 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package ignore
  7. import (
  8. "bytes"
  9. "fmt"
  10. "io"
  11. "io/ioutil"
  12. "os"
  13. "path/filepath"
  14. "runtime"
  15. "strings"
  16. "testing"
  17. "time"
  18. "github.com/syncthing/syncthing/lib/fs"
  19. "github.com/syncthing/syncthing/lib/osutil"
  20. )
  21. func TestIgnore(t *testing.T) {
  22. pats := New(fs.NewFilesystem(fs.FilesystemTypeBasic, "testdata"), WithCache(true))
  23. err := pats.Load(".stignore")
  24. if err != nil {
  25. t.Fatal(err)
  26. }
  27. var tests = []struct {
  28. f string
  29. r bool
  30. }{
  31. {"afile", false},
  32. {"bfile", true},
  33. {"cfile", false},
  34. {"dfile", false},
  35. {"efile", true},
  36. {"ffile", true},
  37. {"dir1", false},
  38. {filepath.Join("dir1", "cfile"), true},
  39. {filepath.Join("dir1", "dfile"), false},
  40. {filepath.Join("dir1", "efile"), true},
  41. {filepath.Join("dir1", "ffile"), false},
  42. {"dir2", false},
  43. {filepath.Join("dir2", "cfile"), false},
  44. {filepath.Join("dir2", "dfile"), true},
  45. {filepath.Join("dir2", "efile"), true},
  46. {filepath.Join("dir2", "ffile"), false},
  47. {filepath.Join("dir3"), true},
  48. {filepath.Join("dir3", "afile"), true},
  49. {"lost+found", true},
  50. }
  51. for i, tc := range tests {
  52. if r := pats.Match(tc.f); r.IsIgnored() != 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 := New(fs.NewFilesystem(fs.FilesystemTypeBasic, "."), WithCache(true))
  66. err := pats.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.IsIgnored() != tc.r {
  88. t.Errorf("Incorrect match for %s: %v != %v", tc.f, r, tc.r)
  89. }
  90. }
  91. }
  92. func TestFlagOrder(t *testing.T) {
  93. stignore := `
  94. ## Ok cases
  95. (?i)(?d)!ign1
  96. (?d)(?i)!ign2
  97. (?i)!(?d)ign3
  98. (?d)!(?i)ign4
  99. !(?i)(?d)ign5
  100. !(?d)(?i)ign6
  101. ## Bad cases
  102. !!(?i)(?d)ign7
  103. (?i)(?i)(?d)ign8
  104. (?i)(?d)(?d)!ign9
  105. (?d)(?d)!ign10
  106. `
  107. pats := New(fs.NewFilesystem(fs.FilesystemTypeBasic, "."), WithCache(true))
  108. err := pats.Parse(bytes.NewBufferString(stignore), ".stignore")
  109. if err != nil {
  110. t.Fatal(err)
  111. }
  112. for i := 1; i < 7; i++ {
  113. pat := fmt.Sprintf("ign%d", i)
  114. if r := pats.Match(pat); r.IsIgnored() || r.IsDeletable() {
  115. t.Errorf("incorrect %s", pat)
  116. }
  117. }
  118. for i := 7; i < 10; i++ {
  119. pat := fmt.Sprintf("ign%d", i)
  120. if r := pats.Match(pat); r.IsDeletable() {
  121. t.Errorf("incorrect %s", pat)
  122. }
  123. }
  124. if r := pats.Match("(?d)!ign10"); !r.IsIgnored() {
  125. t.Errorf("incorrect")
  126. }
  127. }
  128. func TestDeletables(t *testing.T) {
  129. stignore := `
  130. (?d)ign1
  131. (?d)(?i)ign2
  132. (?i)(?d)ign3
  133. !(?d)ign4
  134. !ign5
  135. !(?i)(?d)ign6
  136. ign7
  137. (?i)ign8
  138. `
  139. pats := New(fs.NewFilesystem(fs.FilesystemTypeBasic, "."), WithCache(true))
  140. err := pats.Parse(bytes.NewBufferString(stignore), ".stignore")
  141. if err != nil {
  142. t.Fatal(err)
  143. }
  144. var tests = []struct {
  145. f string
  146. i bool
  147. d bool
  148. }{
  149. {"ign1", true, true},
  150. {"ign2", true, true},
  151. {"ign3", true, true},
  152. {"ign4", false, false},
  153. {"ign5", false, false},
  154. {"ign6", false, false},
  155. {"ign7", true, false},
  156. {"ign8", true, false},
  157. }
  158. for _, tc := range tests {
  159. if r := pats.Match(tc.f); r.IsIgnored() != tc.i || r.IsDeletable() != tc.d {
  160. t.Errorf("Incorrect match for %s: %v != Result{%t, %t}", tc.f, r, tc.i, tc.d)
  161. }
  162. }
  163. }
  164. func TestBadPatterns(t *testing.T) {
  165. var badPatterns = []string{
  166. "[",
  167. "/[",
  168. "**/[",
  169. "#include nonexistent",
  170. "#include .stignore",
  171. }
  172. for _, pat := range badPatterns {
  173. err := New(fs.NewFilesystem(fs.FilesystemTypeBasic, "."), WithCache(true)).Parse(bytes.NewBufferString(pat), ".stignore")
  174. if err == nil {
  175. t.Errorf("No error for pattern %q", pat)
  176. }
  177. if !IsParseError(err) {
  178. t.Error("Should have been a parse error:", err)
  179. }
  180. }
  181. }
  182. func TestCaseSensitivity(t *testing.T) {
  183. ign := New(fs.NewFilesystem(fs.FilesystemTypeBasic, "."), WithCache(true))
  184. err := ign.Parse(bytes.NewBufferString("test"), ".stignore")
  185. if err != nil {
  186. t.Error(err)
  187. }
  188. match := []string{"test"}
  189. dontMatch := []string{"foo"}
  190. switch runtime.GOOS {
  191. case "darwin", "windows":
  192. match = append(match, "TEST", "Test", "tESt")
  193. default:
  194. dontMatch = append(dontMatch, "TEST", "Test", "tESt")
  195. }
  196. for _, tc := range match {
  197. if !ign.Match(tc).IsIgnored() {
  198. t.Errorf("Incorrect match for %q: should be matched", tc)
  199. }
  200. }
  201. for _, tc := range dontMatch {
  202. if ign.Match(tc).IsIgnored() {
  203. t.Errorf("Incorrect match for %q: should not be matched", tc)
  204. }
  205. }
  206. }
  207. func TestCaching(t *testing.T) {
  208. dir, err := ioutil.TempDir("", "")
  209. if err != nil {
  210. t.Fatal(err)
  211. }
  212. fs := fs.NewFilesystem(fs.FilesystemTypeBasic, dir)
  213. fd1, err := osutil.TempFile(fs, "", "")
  214. if err != nil {
  215. t.Fatal(err)
  216. }
  217. fd2, err := osutil.TempFile(fs, "", "")
  218. if err != nil {
  219. t.Fatal(err)
  220. }
  221. defer fd1.Close()
  222. defer fd2.Close()
  223. defer fs.Remove(fd1.Name())
  224. defer fs.Remove(fd2.Name())
  225. _, err = fd1.Write([]byte("/x/\n#include " + filepath.Base(fd2.Name()) + "\n"))
  226. if err != nil {
  227. t.Fatal(err)
  228. }
  229. fd2.Write([]byte("/y/\n"))
  230. pats := New(fs, WithCache(true))
  231. err = pats.Load(fd1.Name())
  232. if err != nil {
  233. t.Fatal(err)
  234. }
  235. if pats.matches.len() != 0 {
  236. t.Fatal("Expected empty cache")
  237. }
  238. // Cache some outcomes
  239. for _, letter := range []string{"a", "b", "x", "y"} {
  240. pats.Match(letter)
  241. }
  242. if pats.matches.len() != 4 {
  243. t.Fatal("Expected 4 cached results")
  244. }
  245. // Reload file, expect old outcomes to be preserved
  246. err = pats.Load(fd1.Name())
  247. if err != nil {
  248. t.Fatal(err)
  249. }
  250. if pats.matches.len() != 4 {
  251. t.Fatal("Expected 4 cached results")
  252. }
  253. // Modify the include file, expect empty cache. Ensure the timestamp on
  254. // the file changes.
  255. fd2.Write([]byte("/z/\n"))
  256. fd2.Sync()
  257. fakeTime := time.Now().Add(5 * time.Second)
  258. fs.Chtimes(fd2.Name(), fakeTime, fakeTime)
  259. err = pats.Load(fd1.Name())
  260. if err != nil {
  261. t.Fatal(err)
  262. }
  263. if pats.matches.len() != 0 {
  264. t.Fatal("Expected 0 cached results")
  265. }
  266. // Cache some outcomes again
  267. for _, letter := range []string{"b", "x", "y"} {
  268. pats.Match(letter)
  269. }
  270. // Verify that outcomes preserved on next load
  271. err = pats.Load(fd1.Name())
  272. if err != nil {
  273. t.Fatal(err)
  274. }
  275. if pats.matches.len() != 3 {
  276. t.Fatal("Expected 3 cached results")
  277. }
  278. // Modify the root file, expect cache to be invalidated
  279. fd1.Write([]byte("/a/\n"))
  280. fd1.Sync()
  281. fakeTime = time.Now().Add(5 * time.Second)
  282. fs.Chtimes(fd1.Name(), fakeTime, fakeTime)
  283. err = pats.Load(fd1.Name())
  284. if err != nil {
  285. t.Fatal(err)
  286. }
  287. if pats.matches.len() != 0 {
  288. t.Fatal("Expected cache invalidation")
  289. }
  290. // Cache some outcomes again
  291. for _, letter := range []string{"b", "x", "y"} {
  292. pats.Match(letter)
  293. }
  294. // Verify that outcomes provided on next load
  295. err = pats.Load(fd1.Name())
  296. if err != nil {
  297. t.Fatal(err)
  298. }
  299. if pats.matches.len() != 3 {
  300. t.Fatal("Expected 3 cached results")
  301. }
  302. }
  303. func TestCommentsAndBlankLines(t *testing.T) {
  304. stignore := `
  305. // foo
  306. //bar
  307. //!baz
  308. //#dex
  309. // ips
  310. `
  311. pats := New(fs.NewFilesystem(fs.FilesystemTypeBasic, "."), WithCache(true))
  312. err := pats.Parse(bytes.NewBufferString(stignore), ".stignore")
  313. if err != nil {
  314. t.Error(err)
  315. }
  316. if len(pats.patterns) > 0 {
  317. t.Errorf("Expected no patterns")
  318. }
  319. }
  320. var result Result
  321. func BenchmarkMatch(b *testing.B) {
  322. stignore := `
  323. .frog
  324. .frog*
  325. .frogfox
  326. .whale
  327. .whale/*
  328. .dolphin
  329. .dolphin/*
  330. ~ferret~.*
  331. .ferret.*
  332. flamingo.*
  333. flamingo
  334. *.crow
  335. *.crow
  336. `
  337. pats := New(fs.NewFilesystem(fs.FilesystemTypeBasic, "."))
  338. err := pats.Parse(bytes.NewBufferString(stignore), ".stignore")
  339. if err != nil {
  340. b.Error(err)
  341. }
  342. b.ResetTimer()
  343. for i := 0; i < b.N; i++ {
  344. result = pats.Match("filename")
  345. }
  346. }
  347. func BenchmarkMatchCached(b *testing.B) {
  348. stignore := `
  349. .frog
  350. .frog*
  351. .frogfox
  352. .whale
  353. .whale/*
  354. .dolphin
  355. .dolphin/*
  356. ~ferret~.*
  357. .ferret.*
  358. flamingo.*
  359. flamingo
  360. *.crow
  361. *.crow
  362. `
  363. // Caches per file, hence write the patterns to a file.
  364. dir, err := ioutil.TempDir("", "")
  365. if err != nil {
  366. b.Fatal(err)
  367. }
  368. fs := fs.NewFilesystem(fs.FilesystemTypeBasic, dir)
  369. fd, err := osutil.TempFile(fs, "", "")
  370. if err != nil {
  371. b.Fatal(err)
  372. }
  373. _, err = fd.Write([]byte(stignore))
  374. defer fd.Close()
  375. defer fs.Remove(fd.Name())
  376. if err != nil {
  377. b.Fatal(err)
  378. }
  379. // Load the patterns
  380. pats := New(fs, WithCache(true))
  381. err = pats.Load(fd.Name())
  382. if err != nil {
  383. b.Fatal(err)
  384. }
  385. // Cache the outcome for "filename"
  386. pats.Match("filename")
  387. // This load should now load the cached outcomes as the set of patterns
  388. // has not changed.
  389. err = pats.Load(fd.Name())
  390. if err != nil {
  391. b.Fatal(err)
  392. }
  393. b.ResetTimer()
  394. for i := 0; i < b.N; i++ {
  395. result = pats.Match("filename")
  396. }
  397. }
  398. func TestCacheReload(t *testing.T) {
  399. dir, err := ioutil.TempDir("", "")
  400. if err != nil {
  401. t.Fatal(err)
  402. }
  403. fs := fs.NewFilesystem(fs.FilesystemTypeBasic, dir)
  404. fd, err := osutil.TempFile(fs, "", "")
  405. if err != nil {
  406. t.Fatal(err)
  407. }
  408. defer fd.Close()
  409. defer fs.Remove(fd.Name())
  410. // Ignore file matches f1 and f2
  411. _, err = fd.Write([]byte("f1\nf2\n"))
  412. if err != nil {
  413. t.Fatal(err)
  414. }
  415. pats := New(fs, WithCache(true))
  416. err = pats.Load(fd.Name())
  417. if err != nil {
  418. t.Fatal(err)
  419. }
  420. // Verify that both are ignored
  421. if !pats.Match("f1").IsIgnored() {
  422. t.Error("Unexpected non-match for f1")
  423. }
  424. if !pats.Match("f2").IsIgnored() {
  425. t.Error("Unexpected non-match for f2")
  426. }
  427. if pats.Match("f3").IsIgnored() {
  428. t.Error("Unexpected match for f3")
  429. }
  430. // Rewrite file to match f1 and f3
  431. err = fd.Truncate(0)
  432. if err != nil {
  433. t.Fatal(err)
  434. }
  435. _, err = fd.Seek(0, io.SeekStart)
  436. if err != nil {
  437. t.Fatal(err)
  438. }
  439. _, err = fd.Write([]byte("f1\nf3\n"))
  440. if err != nil {
  441. t.Fatal(err)
  442. }
  443. fd.Sync()
  444. fakeTime := time.Now().Add(5 * time.Second)
  445. fs.Chtimes(fd.Name(), fakeTime, fakeTime)
  446. err = pats.Load(fd.Name())
  447. if err != nil {
  448. t.Fatal(err)
  449. }
  450. // Verify that the new patterns are in effect
  451. if !pats.Match("f1").IsIgnored() {
  452. t.Error("Unexpected non-match for f1")
  453. }
  454. if pats.Match("f2").IsIgnored() {
  455. t.Error("Unexpected match for f2")
  456. }
  457. if !pats.Match("f3").IsIgnored() {
  458. t.Error("Unexpected non-match for f3")
  459. }
  460. }
  461. func TestHash(t *testing.T) {
  462. p1 := New(fs.NewFilesystem(fs.FilesystemTypeBasic, "."), WithCache(true))
  463. err := p1.Load("testdata/.stignore")
  464. if err != nil {
  465. t.Fatal(err)
  466. }
  467. // Same list of patterns as testdata/.stignore, after expansion
  468. stignore := `
  469. dir2/dfile
  470. dir3
  471. bfile
  472. dir1/cfile
  473. **/efile
  474. /ffile
  475. lost+found
  476. `
  477. p2 := New(fs.NewFilesystem(fs.FilesystemTypeBasic, "."), WithCache(true))
  478. err = p2.Parse(bytes.NewBufferString(stignore), ".stignore")
  479. if err != nil {
  480. t.Fatal(err)
  481. }
  482. // Not same list of patterns
  483. stignore = `
  484. dir2/dfile
  485. dir3
  486. bfile
  487. dir1/cfile
  488. /ffile
  489. lost+found
  490. `
  491. p3 := New(fs.NewFilesystem(fs.FilesystemTypeBasic, "."), WithCache(true))
  492. err = p3.Parse(bytes.NewBufferString(stignore), ".stignore")
  493. if err != nil {
  494. t.Fatal(err)
  495. }
  496. if p1.Hash() == "" {
  497. t.Error("p1 hash blank")
  498. }
  499. if p2.Hash() == "" {
  500. t.Error("p2 hash blank")
  501. }
  502. if p3.Hash() == "" {
  503. t.Error("p3 hash blank")
  504. }
  505. if p1.Hash() != p2.Hash() {
  506. t.Error("p1-p2 hashes differ")
  507. }
  508. if p1.Hash() == p3.Hash() {
  509. t.Error("p1-p3 hashes same")
  510. }
  511. }
  512. func TestHashOfEmpty(t *testing.T) {
  513. p1 := New(fs.NewFilesystem(fs.FilesystemTypeBasic, "."), WithCache(true))
  514. err := p1.Load("testdata/.stignore")
  515. if err != nil {
  516. t.Fatal(err)
  517. }
  518. firstHash := p1.Hash()
  519. // Reloading with a non-existent file should empty the patterns and
  520. // recalculate the hash.
  521. // e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 is
  522. // the sah256 of nothing.
  523. p1.Load("file/does/not/exist")
  524. secondHash := p1.Hash()
  525. if firstHash == secondHash {
  526. t.Error("hash did not change")
  527. }
  528. if secondHash != "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" {
  529. t.Error("second hash is not hash of empty string")
  530. }
  531. if len(p1.patterns) != 0 {
  532. t.Error("there are more than zero patterns")
  533. }
  534. }
  535. func TestWindowsPatterns(t *testing.T) {
  536. // We should accept patterns as both a/b and a\b and match that against
  537. // both kinds of slash as well.
  538. if runtime.GOOS != "windows" {
  539. t.Skip("Windows specific test")
  540. return
  541. }
  542. stignore := `
  543. a/b
  544. c\d
  545. `
  546. pats := New(fs.NewFilesystem(fs.FilesystemTypeBasic, "."), WithCache(true))
  547. err := pats.Parse(bytes.NewBufferString(stignore), ".stignore")
  548. if err != nil {
  549. t.Fatal(err)
  550. }
  551. tests := []string{`a\b`, `c\d`}
  552. for _, pat := range tests {
  553. if !pats.Match(pat).IsIgnored() {
  554. t.Errorf("Should match %s", pat)
  555. }
  556. }
  557. }
  558. func TestAutomaticCaseInsensitivity(t *testing.T) {
  559. // We should do case insensitive matching by default on some platforms.
  560. if runtime.GOOS != "windows" && runtime.GOOS != "darwin" {
  561. t.Skip("Windows/Mac specific test")
  562. return
  563. }
  564. stignore := `
  565. A/B
  566. c/d
  567. `
  568. pats := New(fs.NewFilesystem(fs.FilesystemTypeBasic, "."), WithCache(true))
  569. err := pats.Parse(bytes.NewBufferString(stignore), ".stignore")
  570. if err != nil {
  571. t.Fatal(err)
  572. }
  573. tests := []string{`a/B`, `C/d`}
  574. for _, pat := range tests {
  575. if !pats.Match(pat).IsIgnored() {
  576. t.Errorf("Should match %s", pat)
  577. }
  578. }
  579. }
  580. func TestCommas(t *testing.T) {
  581. stignore := `
  582. foo,bar.txt
  583. {baz,quux}.txt
  584. `
  585. pats := New(fs.NewFilesystem(fs.FilesystemTypeBasic, "."), WithCache(true))
  586. err := pats.Parse(bytes.NewBufferString(stignore), ".stignore")
  587. if err != nil {
  588. t.Fatal(err)
  589. }
  590. tests := []struct {
  591. name string
  592. match bool
  593. }{
  594. {"foo.txt", false},
  595. {"bar.txt", false},
  596. {"foo,bar.txt", true},
  597. {"baz.txt", true},
  598. {"quux.txt", true},
  599. {"baz,quux.txt", false},
  600. }
  601. for _, tc := range tests {
  602. if pats.Match(tc.name).IsIgnored() != tc.match {
  603. t.Errorf("Match of %s was %v, should be %v", tc.name, !tc.match, tc.match)
  604. }
  605. }
  606. }
  607. func TestIssue3164(t *testing.T) {
  608. stignore := `
  609. (?d)(?i)*.part
  610. (?d)(?i)/foo
  611. (?d)(?i)**/bar
  612. `
  613. pats := New(fs.NewFilesystem(fs.FilesystemTypeBasic, "."), WithCache(true))
  614. err := pats.Parse(bytes.NewBufferString(stignore), ".stignore")
  615. if err != nil {
  616. t.Fatal(err)
  617. }
  618. expanded := pats.Patterns()
  619. t.Log(expanded)
  620. expected := []string{
  621. "(?d)(?i)*.part",
  622. "(?d)(?i)**/*.part",
  623. "(?d)(?i)*.part/**",
  624. "(?d)(?i)**/*.part/**",
  625. "(?d)(?i)/foo",
  626. "(?d)(?i)/foo/**",
  627. "(?d)(?i)**/bar",
  628. "(?d)(?i)bar",
  629. "(?d)(?i)**/bar/**",
  630. "(?d)(?i)bar/**",
  631. }
  632. if len(expanded) != len(expected) {
  633. t.Errorf("Unmatched count: %d != %d", len(expanded), len(expected))
  634. }
  635. for i := range expanded {
  636. if expanded[i] != expected[i] {
  637. t.Errorf("Pattern %d does not match: %s != %s", i, expanded[i], expected[i])
  638. }
  639. }
  640. }
  641. func TestIssue3174(t *testing.T) {
  642. stignore := `
  643. *ä*
  644. `
  645. pats := New(fs.NewFilesystem(fs.FilesystemTypeBasic, "."), WithCache(true))
  646. err := pats.Parse(bytes.NewBufferString(stignore), ".stignore")
  647. if err != nil {
  648. t.Fatal(err)
  649. }
  650. if !pats.Match("åäö").IsIgnored() {
  651. t.Error("Should match")
  652. }
  653. }
  654. func TestIssue3639(t *testing.T) {
  655. stignore := `
  656. foo/
  657. `
  658. pats := New(fs.NewFilesystem(fs.FilesystemTypeBasic, "."), WithCache(true))
  659. err := pats.Parse(bytes.NewBufferString(stignore), ".stignore")
  660. if err != nil {
  661. t.Fatal(err)
  662. }
  663. if !pats.Match("foo/bar").IsIgnored() {
  664. t.Error("Should match 'foo/bar'")
  665. }
  666. if pats.Match("foo").IsIgnored() {
  667. t.Error("Should not match 'foo'")
  668. }
  669. }
  670. func TestIssue3674(t *testing.T) {
  671. stignore := `
  672. a*b
  673. a**c
  674. `
  675. testcases := []struct {
  676. file string
  677. matches bool
  678. }{
  679. {"ab", true},
  680. {"asdfb", true},
  681. {"ac", true},
  682. {"asdfc", true},
  683. {"as/db", false},
  684. {"as/dc", true},
  685. }
  686. pats := New(fs.NewFilesystem(fs.FilesystemTypeBasic, "."), WithCache(true))
  687. err := pats.Parse(bytes.NewBufferString(stignore), ".stignore")
  688. if err != nil {
  689. t.Fatal(err)
  690. }
  691. for _, tc := range testcases {
  692. res := pats.Match(tc.file).IsIgnored()
  693. if res != tc.matches {
  694. t.Errorf("Matches(%q) == %v, expected %v", tc.file, res, tc.matches)
  695. }
  696. }
  697. }
  698. func TestGobwasGlobIssue18(t *testing.T) {
  699. stignore := `
  700. a?b
  701. bb?
  702. `
  703. testcases := []struct {
  704. file string
  705. matches bool
  706. }{
  707. {"ab", false},
  708. {"acb", true},
  709. {"asdb", false},
  710. {"bb", false},
  711. {"bba", true},
  712. {"bbaa", false},
  713. }
  714. pats := New(fs.NewFilesystem(fs.FilesystemTypeBasic, "."), WithCache(true))
  715. err := pats.Parse(bytes.NewBufferString(stignore), ".stignore")
  716. if err != nil {
  717. t.Fatal(err)
  718. }
  719. for _, tc := range testcases {
  720. res := pats.Match(tc.file).IsIgnored()
  721. if res != tc.matches {
  722. t.Errorf("Matches(%q) == %v, expected %v", tc.file, res, tc.matches)
  723. }
  724. }
  725. }
  726. func TestRoot(t *testing.T) {
  727. stignore := `
  728. !/a
  729. /*
  730. `
  731. testcases := []struct {
  732. file string
  733. matches bool
  734. }{
  735. {".", false},
  736. {"a", false},
  737. {"b", true},
  738. }
  739. pats := New(fs.NewFilesystem(fs.FilesystemTypeBasic, "."), WithCache(true))
  740. err := pats.Parse(bytes.NewBufferString(stignore), ".stignore")
  741. if err != nil {
  742. t.Fatal(err)
  743. }
  744. for _, tc := range testcases {
  745. res := pats.Match(tc.file).IsIgnored()
  746. if res != tc.matches {
  747. t.Errorf("Matches(%q) == %v, expected %v", tc.file, res, tc.matches)
  748. }
  749. }
  750. }
  751. func TestLines(t *testing.T) {
  752. stignore := `
  753. #include testdata/excludes
  754. !/a
  755. /*
  756. !/a
  757. `
  758. pats := New(fs.NewFilesystem(fs.FilesystemTypeBasic, "."), WithCache(true))
  759. err := pats.Parse(bytes.NewBufferString(stignore), ".stignore")
  760. if err != nil {
  761. t.Fatal(err)
  762. }
  763. expectedLines := []string{
  764. "",
  765. "#include testdata/excludes",
  766. "",
  767. "!/a",
  768. "/*",
  769. "!/a",
  770. "",
  771. }
  772. lines := pats.Lines()
  773. if len(lines) != len(expectedLines) {
  774. t.Fatalf("len(Lines()) == %d, expected %d", len(lines), len(expectedLines))
  775. }
  776. for i := range lines {
  777. if lines[i] != expectedLines[i] {
  778. t.Fatalf("Lines()[%d] == %s, expected %s", i, lines[i], expectedLines[i])
  779. }
  780. }
  781. }
  782. func TestDuplicateLines(t *testing.T) {
  783. stignore := `
  784. !/a
  785. /*
  786. !/a
  787. `
  788. stignoreFiltered := `
  789. !/a
  790. /*
  791. `
  792. pats := New(fs.NewFilesystem(fs.FilesystemTypeBasic, "testdata"), WithCache(true))
  793. err := pats.Parse(bytes.NewBufferString(stignore), ".stignore")
  794. if err != nil {
  795. t.Fatal(err)
  796. }
  797. patsLen := len(pats.patterns)
  798. err = pats.Parse(bytes.NewBufferString(stignoreFiltered), ".stignore")
  799. if err != nil {
  800. t.Fatal(err)
  801. }
  802. if patsLen != len(pats.patterns) {
  803. t.Fatalf("Parsed patterns differ when manually removing duplicate lines")
  804. }
  805. }
  806. func TestIssue4680(t *testing.T) {
  807. stignore := `
  808. #snapshot
  809. `
  810. testcases := []struct {
  811. file string
  812. matches bool
  813. }{
  814. {"#snapshot", true},
  815. {"#snapshot/foo", true},
  816. }
  817. pats := New(fs.NewFilesystem(fs.FilesystemTypeBasic, "."), WithCache(true))
  818. err := pats.Parse(bytes.NewBufferString(stignore), ".stignore")
  819. if err != nil {
  820. t.Fatal(err)
  821. }
  822. for _, tc := range testcases {
  823. res := pats.Match(tc.file).IsIgnored()
  824. if res != tc.matches {
  825. t.Errorf("Matches(%q) == %v, expected %v", tc.file, res, tc.matches)
  826. }
  827. }
  828. }
  829. func TestIssue4689(t *testing.T) {
  830. stignore := `// orig`
  831. pats := New(fs.NewFilesystem(fs.FilesystemTypeBasic, "."), WithCache(true))
  832. err := pats.Parse(bytes.NewBufferString(stignore), ".stignore")
  833. if err != nil {
  834. t.Fatal(err)
  835. }
  836. if lines := pats.Lines(); len(lines) != 1 || lines[0] != "// orig" {
  837. t.Fatalf("wrong lines parsing original comment:\n%q", lines)
  838. }
  839. stignore = `// new`
  840. err = pats.Parse(bytes.NewBufferString(stignore), ".stignore")
  841. if err != nil {
  842. t.Fatal(err)
  843. }
  844. if lines := pats.Lines(); len(lines) != 1 || lines[0] != "// new" {
  845. t.Fatalf("wrong lines parsing changed comment:\n%v", lines)
  846. }
  847. }
  848. func TestIssue4901(t *testing.T) {
  849. dir, err := ioutil.TempDir("", "")
  850. if err != nil {
  851. t.Fatal(err)
  852. }
  853. defer os.RemoveAll(dir)
  854. stignore := `
  855. #include unicorn-lazor-death
  856. puppy
  857. `
  858. if err := ioutil.WriteFile(filepath.Join(dir, ".stignore"), []byte(stignore), 0777); err != nil {
  859. t.Fatalf(err.Error())
  860. }
  861. pats := New(fs.NewFilesystem(fs.FilesystemTypeBasic, dir), WithCache(true))
  862. // Cache does not suddenly make the load succeed.
  863. for i := 0; i < 2; i++ {
  864. err := pats.Load(".stignore")
  865. if err == nil {
  866. t.Fatal("expected an error")
  867. }
  868. if fs.IsNotExist(err) {
  869. t.Fatal("unexpected error type")
  870. }
  871. if !IsParseError(err) {
  872. t.Fatal("failure to load included file should be a parse error")
  873. }
  874. }
  875. if err := ioutil.WriteFile(filepath.Join(dir, "unicorn-lazor-death"), []byte(" "), 0777); err != nil {
  876. t.Fatalf(err.Error())
  877. }
  878. err = pats.Load(".stignore")
  879. if err != nil {
  880. t.Fatalf("unexpected error: %s", err.Error())
  881. }
  882. }
  883. // TestIssue5009 checks that ignored dirs are only skipped if there are no include patterns.
  884. // https://github.com/syncthing/syncthing/issues/5009 (rc-only bug)
  885. func TestIssue5009(t *testing.T) {
  886. pats := New(fs.NewFilesystem(fs.FilesystemTypeBasic, "."), WithCache(true))
  887. stignore := `
  888. ign1
  889. i*2
  890. `
  891. if err := pats.Parse(bytes.NewBufferString(stignore), ".stignore"); err != nil {
  892. t.Fatal(err)
  893. }
  894. if !pats.skipIgnoredDirs {
  895. t.Error("skipIgnoredDirs should be true without includes")
  896. }
  897. stignore = `
  898. !iex2
  899. !ign1/ex
  900. ign1
  901. i*2
  902. !ign2
  903. `
  904. if err := pats.Parse(bytes.NewBufferString(stignore), ".stignore"); err != nil {
  905. t.Fatal(err)
  906. }
  907. if pats.skipIgnoredDirs {
  908. t.Error("skipIgnoredDirs should not be true with includes")
  909. }
  910. }
  911. func TestSpecialChars(t *testing.T) {
  912. pats := New(fs.NewFilesystem(fs.FilesystemTypeBasic, "."), WithCache(true))
  913. stignore := `(?i)/#recycle
  914. (?i)/#nosync
  915. (?i)/$Recycle.bin
  916. (?i)/$RECYCLE.BIN
  917. (?i)/System Volume Information`
  918. if err := pats.Parse(bytes.NewBufferString(stignore), ".stignore"); err != nil {
  919. t.Fatal(err)
  920. }
  921. cases := []string{
  922. "#nosync",
  923. "$RECYCLE.BIN",
  924. filepath.FromSlash("$RECYCLE.BIN/S-1-5-18/desktop.ini"),
  925. }
  926. for _, c := range cases {
  927. if !pats.Match(c).IsIgnored() {
  928. t.Errorf("%q should be ignored", c)
  929. }
  930. }
  931. }
  932. func TestPartialIncludeLine(t *testing.T) {
  933. // Loading a partial #include line (no file mentioned) should error but not crash.
  934. pats := New(fs.NewFilesystem(fs.FilesystemTypeBasic, "."), WithCache(true))
  935. cases := []string{
  936. "#include",
  937. "#include\n",
  938. "#include ",
  939. "#include \n",
  940. "#include \n\n\n",
  941. }
  942. for _, tc := range cases {
  943. err := pats.Parse(bytes.NewBufferString(tc), ".stignore")
  944. if err == nil {
  945. t.Fatal("should error out")
  946. }
  947. if !IsParseError(err) {
  948. t.Fatal("failure to load included file should be a parse error")
  949. }
  950. }
  951. }
  952. func TestSkipIgnoredDirs(t *testing.T) {
  953. tcs := []struct {
  954. pattern string
  955. expected bool
  956. }{
  957. {`!/test`, true},
  958. {`!/t[eih]t`, true},
  959. {`!/t*t`, true},
  960. {`!/t?t`, true},
  961. {`!/**`, true},
  962. {`!/parent/test`, false},
  963. {`!/parent/t[eih]t`, false},
  964. {`!/parent/t*t`, false},
  965. {`!/parent/t?t`, false},
  966. {`!/**.mp3`, false},
  967. {`!/pa*nt/test`, false},
  968. {`!/pa[sdf]nt/t[eih]t`, false},
  969. {`!/lowest/pa[sdf]nt/test`, false},
  970. {`!/lo*st/parent/test`, false},
  971. {`/pa*nt/test`, true},
  972. {`test`, true},
  973. {`*`, true},
  974. }
  975. for _, tc := range tcs {
  976. pats, err := parseLine(tc.pattern)
  977. if err != nil {
  978. t.Error(err)
  979. }
  980. for _, pat := range pats {
  981. if got := pat.allowsSkippingIgnoredDirs(); got != tc.expected {
  982. t.Errorf(`Pattern "%v": got %v, expected %v`, pat, got, tc.expected)
  983. }
  984. }
  985. }
  986. pats := New(fs.NewFilesystem(fs.FilesystemTypeBasic, "testdata"), WithCache(true))
  987. stignore := `
  988. /foo/ign*
  989. !/f*
  990. !/bar
  991. *
  992. `
  993. if err := pats.Parse(bytes.NewBufferString(stignore), ".stignore"); err != nil {
  994. t.Fatal(err)
  995. }
  996. if !pats.SkipIgnoredDirs() {
  997. t.Error("SkipIgnoredDirs should be true")
  998. }
  999. stignore = `
  1000. !/foo/ign*
  1001. *
  1002. `
  1003. if err := pats.Parse(bytes.NewBufferString(stignore), ".stignore"); err != nil {
  1004. t.Fatal(err)
  1005. }
  1006. if pats.SkipIgnoredDirs() {
  1007. t.Error("SkipIgnoredDirs should be false")
  1008. }
  1009. }
  1010. func TestEmptyPatterns(t *testing.T) {
  1011. // These patterns are all invalid and should be rejected as such (without panicking...)
  1012. tcs := []string{
  1013. "!",
  1014. "(?d)",
  1015. "(?i)",
  1016. }
  1017. for _, tc := range tcs {
  1018. m := New(fs.NewFilesystem(fs.FilesystemTypeFake, ""))
  1019. err := m.Parse(strings.NewReader(tc), ".stignore")
  1020. if err == nil {
  1021. t.Error("Should reject invalid pattern", tc)
  1022. }
  1023. if !IsParseError(err) {
  1024. t.Fatal("bad pattern should be a parse error")
  1025. }
  1026. }
  1027. }