ignore_test.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  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/ioutil"
  11. "os"
  12. "path/filepath"
  13. "runtime"
  14. "testing"
  15. "time"
  16. "github.com/syncthing/syncthing/lib/fs"
  17. "github.com/syncthing/syncthing/lib/osutil"
  18. )
  19. func TestIgnore(t *testing.T) {
  20. pats := New(fs.NewFilesystem(fs.FilesystemTypeBasic, "testdata"), WithCache(true))
  21. err := pats.Load(".stignore")
  22. if err != nil {
  23. t.Fatal(err)
  24. }
  25. var tests = []struct {
  26. f string
  27. r bool
  28. }{
  29. {"afile", false},
  30. {"bfile", true},
  31. {"cfile", false},
  32. {"dfile", false},
  33. {"efile", true},
  34. {"ffile", true},
  35. {"dir1", false},
  36. {filepath.Join("dir1", "cfile"), true},
  37. {filepath.Join("dir1", "dfile"), false},
  38. {filepath.Join("dir1", "efile"), true},
  39. {filepath.Join("dir1", "ffile"), false},
  40. {"dir2", false},
  41. {filepath.Join("dir2", "cfile"), false},
  42. {filepath.Join("dir2", "dfile"), true},
  43. {filepath.Join("dir2", "efile"), true},
  44. {filepath.Join("dir2", "ffile"), false},
  45. {filepath.Join("dir3"), true},
  46. {filepath.Join("dir3", "afile"), true},
  47. {"lost+found", true},
  48. }
  49. for i, tc := range tests {
  50. if r := pats.Match(tc.f); r.IsIgnored() != tc.r {
  51. t.Errorf("Incorrect ignoreFile() #%d (%s); E: %v, A: %v", i, tc.f, tc.r, r)
  52. }
  53. }
  54. }
  55. func TestExcludes(t *testing.T) {
  56. stignore := `
  57. !iex2
  58. !ign1/ex
  59. ign1
  60. i*2
  61. !ign2
  62. `
  63. pats := New(fs.NewFilesystem(fs.FilesystemTypeBasic, "."), WithCache(true))
  64. err := pats.Parse(bytes.NewBufferString(stignore), ".stignore")
  65. if err != nil {
  66. t.Fatal(err)
  67. }
  68. var tests = []struct {
  69. f string
  70. r bool
  71. }{
  72. {"ign1", true},
  73. {"ign2", true},
  74. {"ibla2", true},
  75. {"iex2", false},
  76. {filepath.Join("ign1", "ign"), true},
  77. {filepath.Join("ign1", "ex"), false},
  78. {filepath.Join("ign1", "iex2"), false},
  79. {filepath.Join("iex2", "ign"), false},
  80. {filepath.Join("foo", "bar", "ign1"), true},
  81. {filepath.Join("foo", "bar", "ign2"), true},
  82. {filepath.Join("foo", "bar", "iex2"), false},
  83. }
  84. for _, tc := range tests {
  85. if r := pats.Match(tc.f); r.IsIgnored() != tc.r {
  86. t.Errorf("Incorrect match for %s: %v != %v", tc.f, r, tc.r)
  87. }
  88. }
  89. }
  90. func TestFlagOrder(t *testing.T) {
  91. stignore := `
  92. ## Ok cases
  93. (?i)(?d)!ign1
  94. (?d)(?i)!ign2
  95. (?i)!(?d)ign3
  96. (?d)!(?i)ign4
  97. !(?i)(?d)ign5
  98. !(?d)(?i)ign6
  99. ## Bad cases
  100. !!(?i)(?d)ign7
  101. (?i)(?i)(?d)ign8
  102. (?i)(?d)(?d)!ign9
  103. (?d)(?d)!ign10
  104. `
  105. pats := New(fs.NewFilesystem(fs.FilesystemTypeBasic, "."), WithCache(true))
  106. err := pats.Parse(bytes.NewBufferString(stignore), ".stignore")
  107. if err != nil {
  108. t.Fatal(err)
  109. }
  110. for i := 1; i < 7; i++ {
  111. pat := fmt.Sprintf("ign%d", i)
  112. if r := pats.Match(pat); r.IsIgnored() || r.IsDeletable() {
  113. t.Errorf("incorrect %s", pat)
  114. }
  115. }
  116. for i := 7; i < 10; i++ {
  117. pat := fmt.Sprintf("ign%d", i)
  118. if r := pats.Match(pat); r.IsDeletable() {
  119. t.Errorf("incorrect %s", pat)
  120. }
  121. }
  122. if r := pats.Match("(?d)!ign10"); !r.IsIgnored() {
  123. t.Errorf("incorrect")
  124. }
  125. }
  126. func TestDeletables(t *testing.T) {
  127. stignore := `
  128. (?d)ign1
  129. (?d)(?i)ign2
  130. (?i)(?d)ign3
  131. !(?d)ign4
  132. !ign5
  133. !(?i)(?d)ign6
  134. ign7
  135. (?i)ign8
  136. `
  137. pats := New(fs.NewFilesystem(fs.FilesystemTypeBasic, "."), WithCache(true))
  138. err := pats.Parse(bytes.NewBufferString(stignore), ".stignore")
  139. if err != nil {
  140. t.Fatal(err)
  141. }
  142. var tests = []struct {
  143. f string
  144. i bool
  145. d bool
  146. }{
  147. {"ign1", true, true},
  148. {"ign2", true, true},
  149. {"ign3", true, true},
  150. {"ign4", false, false},
  151. {"ign5", false, false},
  152. {"ign6", false, false},
  153. {"ign7", true, false},
  154. {"ign8", true, false},
  155. }
  156. for _, tc := range tests {
  157. if r := pats.Match(tc.f); r.IsIgnored() != tc.i || r.IsDeletable() != tc.d {
  158. t.Errorf("Incorrect match for %s: %v != Result{%t, %t}", tc.f, r, tc.i, tc.d)
  159. }
  160. }
  161. }
  162. func TestBadPatterns(t *testing.T) {
  163. var badPatterns = []string{
  164. "[",
  165. "/[",
  166. "**/[",
  167. "#include nonexistent",
  168. "#include .stignore",
  169. "!#include makesnosense",
  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, os.SEEK_SET)
  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 TestIsInternal(t *testing.T) {
  722. cases := []struct {
  723. file string
  724. internal bool
  725. }{
  726. {".stfolder", true},
  727. {".stignore", true},
  728. {".stversions", true},
  729. {".stfolder/foo", true},
  730. {".stignore/foo", true},
  731. {".stversions/foo", true},
  732. {".stfolderfoo", false},
  733. {".stignorefoo", false},
  734. {".stversionsfoo", false},
  735. {"foo.stfolder", false},
  736. {"foo.stignore", false},
  737. {"foo.stversions", false},
  738. {"foo/.stfolder", false},
  739. {"foo/.stignore", false},
  740. {"foo/.stversions", false},
  741. }
  742. for _, tc := range cases {
  743. res := IsInternal(filepath.FromSlash(tc.file))
  744. if res != tc.internal {
  745. t.Errorf("Unexpected result: IsInteral(%q): %v should be %v", tc.file, res, tc.internal)
  746. }
  747. }
  748. }
  749. func TestRoot(t *testing.T) {
  750. stignore := `
  751. !/a
  752. /*
  753. `
  754. testcases := []struct {
  755. file string
  756. matches bool
  757. }{
  758. {".", false},
  759. {"a", false},
  760. {"b", true},
  761. }
  762. pats := New(fs.NewFilesystem(fs.FilesystemTypeBasic, "."), WithCache(true))
  763. err := pats.Parse(bytes.NewBufferString(stignore), ".stignore")
  764. if err != nil {
  765. t.Fatal(err)
  766. }
  767. for _, tc := range testcases {
  768. res := pats.Match(tc.file).IsIgnored()
  769. if res != tc.matches {
  770. t.Errorf("Matches(%q) == %v, expected %v", tc.file, res, tc.matches)
  771. }
  772. }
  773. }
  774. func TestLines(t *testing.T) {
  775. stignore := `
  776. #include testdata/excludes
  777. !/a
  778. /*
  779. `
  780. pats := New(fs.NewFilesystem(fs.FilesystemTypeBasic, "."), WithCache(true))
  781. err := pats.Parse(bytes.NewBufferString(stignore), ".stignore")
  782. if err != nil {
  783. t.Fatal(err)
  784. }
  785. expectedLines := []string{
  786. "",
  787. "#include testdata/excludes",
  788. "",
  789. "!/a",
  790. "/*",
  791. "",
  792. }
  793. lines := pats.Lines()
  794. if len(lines) != len(expectedLines) {
  795. t.Fatalf("len(Lines()) == %d, expected %d", len(lines), len(expectedLines))
  796. }
  797. for i := range lines {
  798. if lines[i] != expectedLines[i] {
  799. t.Fatalf("Lines()[%d] == %s, expected %s", i, lines[i], expectedLines[i])
  800. }
  801. }
  802. }