ignore_test.go 21 KB

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