2014-11-16 20:13:20 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 19:43:32 +00:00
|
|
|
//
|
2015-03-07 20:36:35 +00:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2014-09-04 20:33:01 +00:00
|
|
|
|
2014-10-12 21:35:15 +00:00
|
|
|
package ignore
|
2014-09-04 20:29:53 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2016-04-07 09:34:07 +00:00
|
|
|
"fmt"
|
2019-02-02 11:09:07 +00:00
|
|
|
"io"
|
2014-09-04 20:29:53 +00:00
|
|
|
"path/filepath"
|
2020-01-30 08:58:44 +00:00
|
|
|
"strings"
|
2014-09-04 20:29:53 +00:00
|
|
|
"testing"
|
2016-11-22 21:30:45 +00:00
|
|
|
"time"
|
2017-08-19 14:36:56 +00:00
|
|
|
|
2022-07-28 17:36:39 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/build"
|
2017-08-19 14:36:56 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/fs"
|
2024-01-13 17:58:23 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/ignore/ignoreresult"
|
2017-08-19 14:36:56 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/osutil"
|
2023-05-03 08:25:36 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/rand"
|
2014-09-04 20:29:53 +00:00
|
|
|
)
|
|
|
|
|
2023-05-03 08:25:36 +00:00
|
|
|
var testFiles = map[string]string{
|
|
|
|
".stignore": `#include excludes
|
|
|
|
bfile
|
|
|
|
dir1/cfile
|
|
|
|
**/efile
|
|
|
|
/ffile
|
|
|
|
lost+found
|
|
|
|
`,
|
|
|
|
"excludes": "dir2/dfile\n#include further-excludes\n",
|
|
|
|
"further-excludes": "dir3\n",
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTestFS() fs.Filesystem {
|
|
|
|
testFS := fs.NewFilesystem(fs.FilesystemTypeFake, rand.String(32)+"?content=true&nostfolder=true")
|
|
|
|
|
|
|
|
// Add some data expected by the tests, previously existing on disk.
|
|
|
|
testFS.Mkdir("dir3", 0o777)
|
|
|
|
for name, content := range testFiles {
|
|
|
|
fs.WriteFile(testFS, name, []byte(content), 0o666)
|
|
|
|
}
|
|
|
|
|
|
|
|
return testFS
|
|
|
|
}
|
|
|
|
|
2014-09-04 20:29:53 +00:00
|
|
|
func TestIgnore(t *testing.T) {
|
2023-05-03 08:25:36 +00:00
|
|
|
testFs := newTestFS()
|
|
|
|
|
|
|
|
pats := New(testFs, WithCache(true))
|
2017-08-19 14:36:56 +00:00
|
|
|
err := pats.Load(".stignore")
|
2014-09-04 20:29:53 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2023-05-03 08:25:36 +00:00
|
|
|
tests := []struct {
|
2014-09-04 20:29:53 +00:00
|
|
|
f string
|
|
|
|
r bool
|
|
|
|
}{
|
|
|
|
{"afile", false},
|
|
|
|
{"bfile", true},
|
|
|
|
{"cfile", false},
|
|
|
|
{"dfile", false},
|
|
|
|
{"efile", true},
|
|
|
|
{"ffile", true},
|
|
|
|
|
|
|
|
{"dir1", false},
|
|
|
|
{filepath.Join("dir1", "cfile"), true},
|
|
|
|
{filepath.Join("dir1", "dfile"), false},
|
|
|
|
{filepath.Join("dir1", "efile"), true},
|
|
|
|
{filepath.Join("dir1", "ffile"), false},
|
|
|
|
|
|
|
|
{"dir2", false},
|
|
|
|
{filepath.Join("dir2", "cfile"), false},
|
|
|
|
{filepath.Join("dir2", "dfile"), true},
|
|
|
|
{filepath.Join("dir2", "efile"), true},
|
|
|
|
{filepath.Join("dir2", "ffile"), false},
|
|
|
|
|
|
|
|
{filepath.Join("dir3"), true},
|
|
|
|
{filepath.Join("dir3", "afile"), true},
|
2014-12-07 00:10:32 +00:00
|
|
|
|
|
|
|
{"lost+found", true},
|
2014-09-04 20:29:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range tests {
|
2016-04-07 09:34:07 +00:00
|
|
|
if r := pats.Match(tc.f); r.IsIgnored() != tc.r {
|
2014-09-04 20:29:53 +00:00
|
|
|
t.Errorf("Incorrect ignoreFile() #%d (%s); E: %v, A: %v", i, tc.f, tc.r, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExcludes(t *testing.T) {
|
2023-05-03 08:25:36 +00:00
|
|
|
testFs := newTestFS()
|
|
|
|
|
2014-09-04 20:29:53 +00:00
|
|
|
stignore := `
|
|
|
|
!iex2
|
|
|
|
!ign1/ex
|
|
|
|
ign1
|
|
|
|
i*2
|
|
|
|
!ign2
|
|
|
|
`
|
2023-05-03 08:25:36 +00:00
|
|
|
pats := New(testFs, WithCache(true))
|
2014-12-23 09:05:08 +00:00
|
|
|
err := pats.Parse(bytes.NewBufferString(stignore), ".stignore")
|
2014-09-04 20:29:53 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2023-05-03 08:25:36 +00:00
|
|
|
tests := []struct {
|
2014-09-04 20:29:53 +00:00
|
|
|
f string
|
|
|
|
r bool
|
|
|
|
}{
|
|
|
|
{"ign1", true},
|
|
|
|
{"ign2", true},
|
|
|
|
{"ibla2", true},
|
|
|
|
{"iex2", false},
|
2014-09-05 22:01:34 +00:00
|
|
|
{filepath.Join("ign1", "ign"), true},
|
|
|
|
{filepath.Join("ign1", "ex"), false},
|
|
|
|
{filepath.Join("ign1", "iex2"), false},
|
|
|
|
{filepath.Join("iex2", "ign"), false},
|
|
|
|
{filepath.Join("foo", "bar", "ign1"), true},
|
|
|
|
{filepath.Join("foo", "bar", "ign2"), true},
|
|
|
|
{filepath.Join("foo", "bar", "iex2"), false},
|
2014-09-04 20:29:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
2016-04-07 09:34:07 +00:00
|
|
|
if r := pats.Match(tc.f); r.IsIgnored() != tc.r {
|
2014-09-04 20:29:53 +00:00
|
|
|
t.Errorf("Incorrect match for %s: %v != %v", tc.f, r, tc.r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-07 09:34:07 +00:00
|
|
|
func TestFlagOrder(t *testing.T) {
|
2023-05-03 08:25:36 +00:00
|
|
|
testFs := newTestFS()
|
|
|
|
|
2016-04-07 09:34:07 +00:00
|
|
|
stignore := `
|
|
|
|
## Ok cases
|
|
|
|
(?i)(?d)!ign1
|
|
|
|
(?d)(?i)!ign2
|
|
|
|
(?i)!(?d)ign3
|
|
|
|
(?d)!(?i)ign4
|
|
|
|
!(?i)(?d)ign5
|
|
|
|
!(?d)(?i)ign6
|
|
|
|
## Bad cases
|
|
|
|
!!(?i)(?d)ign7
|
|
|
|
(?i)(?i)(?d)ign8
|
|
|
|
(?i)(?d)(?d)!ign9
|
|
|
|
(?d)(?d)!ign10
|
|
|
|
`
|
2023-05-03 08:25:36 +00:00
|
|
|
pats := New(testFs, WithCache(true))
|
2016-04-07 09:34:07 +00:00
|
|
|
err := pats.Parse(bytes.NewBufferString(stignore), ".stignore")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 1; i < 7; i++ {
|
|
|
|
pat := fmt.Sprintf("ign%d", i)
|
|
|
|
if r := pats.Match(pat); r.IsIgnored() || r.IsDeletable() {
|
|
|
|
t.Errorf("incorrect %s", pat)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for i := 7; i < 10; i++ {
|
|
|
|
pat := fmt.Sprintf("ign%d", i)
|
|
|
|
if r := pats.Match(pat); r.IsDeletable() {
|
|
|
|
t.Errorf("incorrect %s", pat)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if r := pats.Match("(?d)!ign10"); !r.IsIgnored() {
|
|
|
|
t.Errorf("incorrect")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeletables(t *testing.T) {
|
2023-05-03 08:25:36 +00:00
|
|
|
testFs := newTestFS()
|
|
|
|
|
2016-04-07 09:34:07 +00:00
|
|
|
stignore := `
|
|
|
|
(?d)ign1
|
|
|
|
(?d)(?i)ign2
|
|
|
|
(?i)(?d)ign3
|
|
|
|
!(?d)ign4
|
|
|
|
!ign5
|
|
|
|
!(?i)(?d)ign6
|
|
|
|
ign7
|
|
|
|
(?i)ign8
|
|
|
|
`
|
2023-05-03 08:25:36 +00:00
|
|
|
pats := New(testFs, WithCache(true))
|
2016-04-07 09:34:07 +00:00
|
|
|
err := pats.Parse(bytes.NewBufferString(stignore), ".stignore")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2023-05-03 08:25:36 +00:00
|
|
|
tests := []struct {
|
2016-04-07 09:34:07 +00:00
|
|
|
f string
|
|
|
|
i bool
|
|
|
|
d bool
|
|
|
|
}{
|
|
|
|
{"ign1", true, true},
|
|
|
|
{"ign2", true, true},
|
|
|
|
{"ign3", true, true},
|
|
|
|
{"ign4", false, false},
|
|
|
|
{"ign5", false, false},
|
|
|
|
{"ign6", false, false},
|
|
|
|
{"ign7", true, false},
|
|
|
|
{"ign8", true, false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
if r := pats.Match(tc.f); r.IsIgnored() != tc.i || r.IsDeletable() != tc.d {
|
|
|
|
t.Errorf("Incorrect match for %s: %v != Result{%t, %t}", tc.f, r, tc.i, tc.d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-04 20:29:53 +00:00
|
|
|
func TestBadPatterns(t *testing.T) {
|
2023-05-03 08:25:36 +00:00
|
|
|
testFs := newTestFS()
|
|
|
|
|
|
|
|
t.Skip("to fix: bad pattern not happening")
|
|
|
|
badPatterns := []string{
|
2014-09-04 20:29:53 +00:00
|
|
|
"[",
|
|
|
|
"/[",
|
|
|
|
"**/[",
|
|
|
|
"#include nonexistent",
|
|
|
|
"#include .stignore",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, pat := range badPatterns {
|
2023-05-03 08:25:36 +00:00
|
|
|
err := New(testFs, WithCache(true)).Parse(bytes.NewBufferString(pat), ".stignore")
|
2014-09-04 20:29:53 +00:00
|
|
|
if err == nil {
|
2014-12-23 09:05:08 +00:00
|
|
|
t.Errorf("No error for pattern %q", pat)
|
2014-09-04 20:29:53 +00:00
|
|
|
}
|
2020-06-18 09:04:00 +00:00
|
|
|
if !IsParseError(err) {
|
|
|
|
t.Error("Should have been a parse error:", err)
|
|
|
|
}
|
2023-04-20 13:00:55 +00:00
|
|
|
if strings.HasPrefix(pat, "#include") {
|
|
|
|
if fs.IsNotExist(err) {
|
|
|
|
t.Error("Includes should not toss a regular isNotExist error")
|
|
|
|
}
|
|
|
|
}
|
2014-09-04 20:29:53 +00:00
|
|
|
}
|
|
|
|
}
|
2014-09-16 21:14:03 +00:00
|
|
|
|
|
|
|
func TestCaseSensitivity(t *testing.T) {
|
2023-05-03 08:25:36 +00:00
|
|
|
testFs := newTestFS()
|
|
|
|
|
|
|
|
ign := New(testFs, WithCache(true))
|
2014-12-23 09:05:08 +00:00
|
|
|
err := ign.Parse(bytes.NewBufferString("test"), ".stignore")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2014-09-16 21:14:03 +00:00
|
|
|
|
|
|
|
match := []string{"test"}
|
|
|
|
dontMatch := []string{"foo"}
|
|
|
|
|
2022-07-28 17:36:39 +00:00
|
|
|
if build.IsDarwin || build.IsWindows {
|
2014-09-16 21:14:03 +00:00
|
|
|
match = append(match, "TEST", "Test", "tESt")
|
2022-07-28 17:36:39 +00:00
|
|
|
} else {
|
2014-09-16 21:14:03 +00:00
|
|
|
dontMatch = append(dontMatch, "TEST", "Test", "tESt")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range match {
|
2016-04-07 09:34:07 +00:00
|
|
|
if !ign.Match(tc).IsIgnored() {
|
2014-09-16 21:14:03 +00:00
|
|
|
t.Errorf("Incorrect match for %q: should be matched", tc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range dontMatch {
|
2016-04-07 09:34:07 +00:00
|
|
|
if ign.Match(tc).IsIgnored() {
|
2014-09-16 21:14:03 +00:00
|
|
|
t.Errorf("Incorrect match for %q: should not be matched", tc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-09-16 21:22:21 +00:00
|
|
|
|
2014-10-12 21:35:15 +00:00
|
|
|
func TestCaching(t *testing.T) {
|
2023-05-03 08:25:36 +00:00
|
|
|
fs := fs.NewFilesystem(fs.FilesystemTypeFake, rand.String(32)+"?content=true")
|
2017-08-19 14:36:56 +00:00
|
|
|
|
|
|
|
fd1, err := osutil.TempFile(fs, "", "")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fd2, err := osutil.TempFile(fs, "", "")
|
2014-10-12 21:35:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer fd1.Close()
|
|
|
|
defer fd2.Close()
|
2019-02-02 11:16:27 +00:00
|
|
|
defer fs.Remove(fd1.Name())
|
|
|
|
defer fs.Remove(fd2.Name())
|
2014-10-12 21:35:15 +00:00
|
|
|
|
2017-08-19 14:36:56 +00:00
|
|
|
_, err = fd1.Write([]byte("/x/\n#include " + filepath.Base(fd2.Name()) + "\n"))
|
2014-10-12 21:35:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-02-02 11:16:27 +00:00
|
|
|
fd2.Write([]byte("/y/\n"))
|
2014-10-12 21:35:15 +00:00
|
|
|
|
2017-08-19 14:36:56 +00:00
|
|
|
pats := New(fs, WithCache(true))
|
2014-12-23 09:05:08 +00:00
|
|
|
err = pats.Load(fd1.Name())
|
2014-10-12 21:35:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-12-02 22:13:03 +00:00
|
|
|
if pats.matches.len() != 0 {
|
|
|
|
t.Fatal("Expected empty cache")
|
2014-10-12 21:35:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cache some outcomes
|
|
|
|
|
|
|
|
for _, letter := range []string{"a", "b", "x", "y"} {
|
|
|
|
pats.Match(letter)
|
|
|
|
}
|
|
|
|
|
2014-12-02 22:13:03 +00:00
|
|
|
if pats.matches.len() != 4 {
|
2014-10-12 21:35:15 +00:00
|
|
|
t.Fatal("Expected 4 cached results")
|
|
|
|
}
|
|
|
|
|
2014-12-23 09:05:08 +00:00
|
|
|
// Reload file, expect old outcomes to be preserved
|
2014-10-12 21:35:15 +00:00
|
|
|
|
2014-12-23 09:05:08 +00:00
|
|
|
err = pats.Load(fd1.Name())
|
2014-10-12 21:35:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-12-02 22:13:03 +00:00
|
|
|
if pats.matches.len() != 4 {
|
2014-10-12 21:35:15 +00:00
|
|
|
t.Fatal("Expected 4 cached results")
|
|
|
|
}
|
|
|
|
|
2016-11-22 21:30:45 +00:00
|
|
|
// Modify the include file, expect empty cache. Ensure the timestamp on
|
|
|
|
// the file changes.
|
2014-10-12 21:35:15 +00:00
|
|
|
|
2019-02-02 11:16:27 +00:00
|
|
|
fd2.Write([]byte("/z/\n"))
|
|
|
|
fd2.Sync()
|
2016-11-22 21:44:04 +00:00
|
|
|
fakeTime := time.Now().Add(5 * time.Second)
|
2019-02-02 11:16:27 +00:00
|
|
|
fs.Chtimes(fd2.Name(), fakeTime, fakeTime)
|
2014-10-12 21:35:15 +00:00
|
|
|
|
2014-12-23 09:05:08 +00:00
|
|
|
err = pats.Load(fd1.Name())
|
2014-10-12 21:35:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-12-02 22:13:03 +00:00
|
|
|
if pats.matches.len() != 0 {
|
2014-10-12 21:35:15 +00:00
|
|
|
t.Fatal("Expected 0 cached results")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cache some outcomes again
|
|
|
|
|
|
|
|
for _, letter := range []string{"b", "x", "y"} {
|
|
|
|
pats.Match(letter)
|
|
|
|
}
|
|
|
|
|
2016-05-08 10:54:22 +00:00
|
|
|
// Verify that outcomes preserved on next load
|
2014-10-12 21:35:15 +00:00
|
|
|
|
2014-12-23 09:05:08 +00:00
|
|
|
err = pats.Load(fd1.Name())
|
2014-10-12 21:35:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-12-02 22:13:03 +00:00
|
|
|
if pats.matches.len() != 3 {
|
2014-10-12 21:35:15 +00:00
|
|
|
t.Fatal("Expected 3 cached results")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Modify the root file, expect cache to be invalidated
|
|
|
|
|
2019-02-02 11:16:27 +00:00
|
|
|
fd1.Write([]byte("/a/\n"))
|
|
|
|
fd1.Sync()
|
2016-11-22 21:44:04 +00:00
|
|
|
fakeTime = time.Now().Add(5 * time.Second)
|
2019-02-02 11:16:27 +00:00
|
|
|
fs.Chtimes(fd1.Name(), fakeTime, fakeTime)
|
2014-10-12 21:35:15 +00:00
|
|
|
|
2014-12-23 09:05:08 +00:00
|
|
|
err = pats.Load(fd1.Name())
|
2014-10-12 21:35:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-12-02 22:13:03 +00:00
|
|
|
if pats.matches.len() != 0 {
|
2014-10-12 21:35:15 +00:00
|
|
|
t.Fatal("Expected cache invalidation")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cache some outcomes again
|
|
|
|
|
|
|
|
for _, letter := range []string{"b", "x", "y"} {
|
|
|
|
pats.Match(letter)
|
|
|
|
}
|
|
|
|
|
2016-05-08 10:54:22 +00:00
|
|
|
// Verify that outcomes provided on next load
|
2014-10-12 21:35:15 +00:00
|
|
|
|
2014-12-23 09:05:08 +00:00
|
|
|
err = pats.Load(fd1.Name())
|
2014-10-12 21:35:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-12-02 22:13:03 +00:00
|
|
|
if pats.matches.len() != 3 {
|
2014-10-12 21:35:15 +00:00
|
|
|
t.Fatal("Expected 3 cached results")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-16 21:22:21 +00:00
|
|
|
func TestCommentsAndBlankLines(t *testing.T) {
|
2023-05-03 08:25:36 +00:00
|
|
|
testFs := newTestFS()
|
|
|
|
|
2014-09-16 21:22:21 +00:00
|
|
|
stignore := `
|
|
|
|
// foo
|
|
|
|
//bar
|
|
|
|
|
|
|
|
//!baz
|
|
|
|
//#dex
|
|
|
|
|
|
|
|
// ips
|
|
|
|
|
|
|
|
|
|
|
|
`
|
2023-05-03 08:25:36 +00:00
|
|
|
pats := New(testFs, WithCache(true))
|
2014-12-23 09:05:08 +00:00
|
|
|
err := pats.Parse(bytes.NewBufferString(stignore), ".stignore")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2014-10-12 21:35:15 +00:00
|
|
|
if len(pats.patterns) > 0 {
|
2014-09-16 21:22:21 +00:00
|
|
|
t.Errorf("Expected no patterns")
|
|
|
|
}
|
|
|
|
}
|
2014-10-12 12:54:36 +00:00
|
|
|
|
2024-01-13 17:58:23 +00:00
|
|
|
var result ignoreresult.R
|
2014-10-12 12:54:36 +00:00
|
|
|
|
|
|
|
func BenchmarkMatch(b *testing.B) {
|
2023-05-03 08:25:36 +00:00
|
|
|
testFs := newTestFS()
|
|
|
|
|
2014-10-12 12:54:36 +00:00
|
|
|
stignore := `
|
|
|
|
.frog
|
|
|
|
.frog*
|
|
|
|
.frogfox
|
|
|
|
.whale
|
|
|
|
.whale/*
|
|
|
|
.dolphin
|
|
|
|
.dolphin/*
|
|
|
|
~ferret~.*
|
|
|
|
.ferret.*
|
|
|
|
flamingo.*
|
|
|
|
flamingo
|
|
|
|
*.crow
|
|
|
|
*.crow
|
|
|
|
`
|
2023-05-03 08:25:36 +00:00
|
|
|
pats := New(testFs)
|
2014-12-23 09:05:08 +00:00
|
|
|
err := pats.Parse(bytes.NewBufferString(stignore), ".stignore")
|
|
|
|
if err != nil {
|
|
|
|
b.Error(err)
|
|
|
|
}
|
2014-10-12 21:35:15 +00:00
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
result = pats.Match("filename")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkMatchCached(b *testing.B) {
|
|
|
|
stignore := `
|
|
|
|
.frog
|
|
|
|
.frog*
|
|
|
|
.frogfox
|
|
|
|
.whale
|
|
|
|
.whale/*
|
|
|
|
.dolphin
|
|
|
|
.dolphin/*
|
|
|
|
~ferret~.*
|
|
|
|
.ferret.*
|
|
|
|
flamingo.*
|
|
|
|
flamingo
|
|
|
|
*.crow
|
|
|
|
*.crow
|
|
|
|
`
|
|
|
|
// Caches per file, hence write the patterns to a file.
|
|
|
|
|
2023-05-03 08:25:36 +00:00
|
|
|
fs := fs.NewFilesystem(fs.FilesystemTypeFake, rand.String(32)+"?content=true")
|
2017-08-19 14:36:56 +00:00
|
|
|
|
|
|
|
fd, err := osutil.TempFile(fs, "", "")
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = fd.Write([]byte(stignore))
|
2014-10-12 21:35:15 +00:00
|
|
|
defer fd.Close()
|
2019-02-02 11:16:27 +00:00
|
|
|
defer fs.Remove(fd.Name())
|
2014-10-12 21:35:15 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
2014-10-12 12:54:36 +00:00
|
|
|
|
2014-10-12 21:35:15 +00:00
|
|
|
// Load the patterns
|
2017-08-19 14:36:56 +00:00
|
|
|
pats := New(fs, WithCache(true))
|
2014-12-23 09:05:08 +00:00
|
|
|
err = pats.Load(fd.Name())
|
2014-10-12 21:35:15 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
// Cache the outcome for "filename"
|
|
|
|
pats.Match("filename")
|
|
|
|
|
|
|
|
// This load should now load the cached outcomes as the set of patterns
|
|
|
|
// has not changed.
|
2014-12-23 09:05:08 +00:00
|
|
|
err = pats.Load(fd.Name())
|
2014-10-12 21:35:15 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
2014-10-12 12:54:36 +00:00
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
result = pats.Match("filename")
|
|
|
|
}
|
|
|
|
}
|
2014-12-21 08:26:54 +00:00
|
|
|
|
|
|
|
func TestCacheReload(t *testing.T) {
|
2023-05-03 08:25:36 +00:00
|
|
|
fs := fs.NewFilesystem(fs.FilesystemTypeFake, rand.String(32)+"?content=true")
|
2017-08-19 14:36:56 +00:00
|
|
|
|
|
|
|
fd, err := osutil.TempFile(fs, "", "")
|
2014-12-21 08:26:54 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer fd.Close()
|
2019-02-02 11:16:27 +00:00
|
|
|
defer fs.Remove(fd.Name())
|
2014-12-21 08:26:54 +00:00
|
|
|
|
|
|
|
// Ignore file matches f1 and f2
|
|
|
|
|
2017-08-19 14:36:56 +00:00
|
|
|
_, err = fd.Write([]byte("f1\nf2\n"))
|
2014-12-21 08:26:54 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-08-19 14:36:56 +00:00
|
|
|
pats := New(fs, WithCache(true))
|
2014-12-23 09:05:08 +00:00
|
|
|
err = pats.Load(fd.Name())
|
2014-12-21 08:26:54 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that both are ignored
|
|
|
|
|
2016-04-07 09:34:07 +00:00
|
|
|
if !pats.Match("f1").IsIgnored() {
|
2014-12-21 08:26:54 +00:00
|
|
|
t.Error("Unexpected non-match for f1")
|
|
|
|
}
|
2016-04-07 09:34:07 +00:00
|
|
|
if !pats.Match("f2").IsIgnored() {
|
2014-12-21 08:26:54 +00:00
|
|
|
t.Error("Unexpected non-match for f2")
|
|
|
|
}
|
2016-04-07 09:34:07 +00:00
|
|
|
if pats.Match("f3").IsIgnored() {
|
2014-12-21 08:26:54 +00:00
|
|
|
t.Error("Unexpected match for f3")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rewrite file to match f1 and f3
|
|
|
|
|
|
|
|
err = fd.Truncate(0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-02-02 11:09:07 +00:00
|
|
|
_, err = fd.Seek(0, io.SeekStart)
|
2014-12-21 08:26:54 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-08-19 14:36:56 +00:00
|
|
|
_, err = fd.Write([]byte("f1\nf3\n"))
|
2014-12-21 08:26:54 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-02-02 11:16:27 +00:00
|
|
|
fd.Sync()
|
2016-11-22 21:44:04 +00:00
|
|
|
fakeTime := time.Now().Add(5 * time.Second)
|
2019-02-02 11:16:27 +00:00
|
|
|
fs.Chtimes(fd.Name(), fakeTime, fakeTime)
|
2014-12-21 08:26:54 +00:00
|
|
|
|
2014-12-23 09:05:08 +00:00
|
|
|
err = pats.Load(fd.Name())
|
2014-12-21 08:26:54 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that the new patterns are in effect
|
|
|
|
|
2016-04-07 09:34:07 +00:00
|
|
|
if !pats.Match("f1").IsIgnored() {
|
2014-12-21 08:26:54 +00:00
|
|
|
t.Error("Unexpected non-match for f1")
|
|
|
|
}
|
2016-04-07 09:34:07 +00:00
|
|
|
if pats.Match("f2").IsIgnored() {
|
2014-12-21 08:26:54 +00:00
|
|
|
t.Error("Unexpected match for f2")
|
|
|
|
}
|
2016-04-07 09:34:07 +00:00
|
|
|
if !pats.Match("f3").IsIgnored() {
|
2014-12-21 08:26:54 +00:00
|
|
|
t.Error("Unexpected non-match for f3")
|
|
|
|
}
|
|
|
|
}
|
2014-12-23 09:05:08 +00:00
|
|
|
|
|
|
|
func TestHash(t *testing.T) {
|
2023-05-03 08:25:36 +00:00
|
|
|
testFs := newTestFS()
|
|
|
|
|
|
|
|
p1 := New(testFs, WithCache(true))
|
|
|
|
err := p1.Load(".stignore")
|
2014-12-23 09:05:08 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2023-05-03 08:25:36 +00:00
|
|
|
// Same list of patterns as .stignore, after expansion
|
2014-12-23 09:05:08 +00:00
|
|
|
stignore := `
|
|
|
|
dir2/dfile
|
|
|
|
dir3
|
|
|
|
bfile
|
|
|
|
dir1/cfile
|
|
|
|
**/efile
|
|
|
|
/ffile
|
|
|
|
lost+found
|
|
|
|
`
|
2023-05-03 08:25:36 +00:00
|
|
|
p2 := New(testFs, WithCache(true))
|
2014-12-23 09:05:08 +00:00
|
|
|
err = p2.Parse(bytes.NewBufferString(stignore), ".stignore")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not same list of patterns
|
|
|
|
stignore = `
|
|
|
|
dir2/dfile
|
|
|
|
dir3
|
|
|
|
bfile
|
|
|
|
dir1/cfile
|
|
|
|
/ffile
|
|
|
|
lost+found
|
|
|
|
`
|
2023-05-03 08:25:36 +00:00
|
|
|
p3 := New(testFs, WithCache(true))
|
2014-12-23 09:05:08 +00:00
|
|
|
err = p3.Parse(bytes.NewBufferString(stignore), ".stignore")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p1.Hash() == "" {
|
|
|
|
t.Error("p1 hash blank")
|
|
|
|
}
|
|
|
|
if p2.Hash() == "" {
|
|
|
|
t.Error("p2 hash blank")
|
|
|
|
}
|
|
|
|
if p3.Hash() == "" {
|
|
|
|
t.Error("p3 hash blank")
|
|
|
|
}
|
|
|
|
if p1.Hash() != p2.Hash() {
|
|
|
|
t.Error("p1-p2 hashes differ")
|
|
|
|
}
|
|
|
|
if p1.Hash() == p3.Hash() {
|
|
|
|
t.Error("p1-p3 hashes same")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHashOfEmpty(t *testing.T) {
|
2023-05-03 08:25:36 +00:00
|
|
|
testFs := newTestFS()
|
|
|
|
|
|
|
|
p1 := New(testFs, WithCache(true))
|
|
|
|
|
|
|
|
err := p1.Load(".stignore")
|
2014-12-23 09:05:08 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
firstHash := p1.Hash()
|
|
|
|
|
|
|
|
// Reloading with a non-existent file should empty the patterns and
|
2021-03-17 21:22:49 +00:00
|
|
|
// recalculate the hash.
|
|
|
|
// e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 is
|
|
|
|
// the sah256 of nothing.
|
2014-12-23 09:05:08 +00:00
|
|
|
|
2019-02-02 11:16:27 +00:00
|
|
|
p1.Load("file/does/not/exist")
|
2014-12-23 09:05:08 +00:00
|
|
|
secondHash := p1.Hash()
|
|
|
|
|
|
|
|
if firstHash == secondHash {
|
|
|
|
t.Error("hash did not change")
|
|
|
|
}
|
2021-03-17 21:22:49 +00:00
|
|
|
if secondHash != "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" {
|
2014-12-23 09:05:08 +00:00
|
|
|
t.Error("second hash is not hash of empty string")
|
|
|
|
}
|
|
|
|
if len(p1.patterns) != 0 {
|
|
|
|
t.Error("there are more than zero patterns")
|
|
|
|
}
|
|
|
|
}
|
2016-04-02 19:03:24 +00:00
|
|
|
|
|
|
|
func TestWindowsPatterns(t *testing.T) {
|
2023-05-03 08:25:36 +00:00
|
|
|
testFs := newTestFS()
|
|
|
|
|
2016-04-02 19:03:24 +00:00
|
|
|
// We should accept patterns as both a/b and a\b and match that against
|
|
|
|
// both kinds of slash as well.
|
2022-07-28 17:36:39 +00:00
|
|
|
if !build.IsWindows {
|
2016-04-02 19:03:24 +00:00
|
|
|
t.Skip("Windows specific test")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
stignore := `
|
|
|
|
a/b
|
|
|
|
c\d
|
|
|
|
`
|
2023-05-03 08:25:36 +00:00
|
|
|
pats := New(testFs, WithCache(true))
|
|
|
|
|
2016-04-02 19:03:24 +00:00
|
|
|
err := pats.Parse(bytes.NewBufferString(stignore), ".stignore")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []string{`a\b`, `c\d`}
|
|
|
|
for _, pat := range tests {
|
2016-04-07 09:34:07 +00:00
|
|
|
if !pats.Match(pat).IsIgnored() {
|
2016-04-02 19:03:24 +00:00
|
|
|
t.Errorf("Should match %s", pat)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-05 06:35:51 +00:00
|
|
|
|
|
|
|
func TestAutomaticCaseInsensitivity(t *testing.T) {
|
2023-05-03 08:25:36 +00:00
|
|
|
testFs := newTestFS()
|
|
|
|
|
2016-04-05 06:35:51 +00:00
|
|
|
// We should do case insensitive matching by default on some platforms.
|
2022-07-28 17:36:39 +00:00
|
|
|
if !build.IsWindows && !build.IsDarwin {
|
2016-04-05 06:35:51 +00:00
|
|
|
t.Skip("Windows/Mac specific test")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
stignore := `
|
|
|
|
A/B
|
|
|
|
c/d
|
|
|
|
`
|
2023-05-03 08:25:36 +00:00
|
|
|
pats := New(testFs, WithCache(true))
|
|
|
|
|
2016-04-05 06:35:51 +00:00
|
|
|
err := pats.Parse(bytes.NewBufferString(stignore), ".stignore")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []string{`a/B`, `C/d`}
|
|
|
|
for _, pat := range tests {
|
2016-04-07 09:34:07 +00:00
|
|
|
if !pats.Match(pat).IsIgnored() {
|
2016-04-05 06:35:51 +00:00
|
|
|
t.Errorf("Should match %s", pat)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-06 15:45:11 +00:00
|
|
|
|
|
|
|
func TestCommas(t *testing.T) {
|
2023-05-03 08:25:36 +00:00
|
|
|
testFs := newTestFS()
|
|
|
|
|
2016-05-06 15:45:11 +00:00
|
|
|
stignore := `
|
|
|
|
foo,bar.txt
|
|
|
|
{baz,quux}.txt
|
|
|
|
`
|
2023-05-03 08:25:36 +00:00
|
|
|
pats := New(testFs, WithCache(true))
|
|
|
|
|
2016-05-06 15:45:11 +00:00
|
|
|
err := pats.Parse(bytes.NewBufferString(stignore), ".stignore")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
match bool
|
|
|
|
}{
|
|
|
|
{"foo.txt", false},
|
|
|
|
{"bar.txt", false},
|
|
|
|
{"foo,bar.txt", true},
|
|
|
|
{"baz.txt", true},
|
|
|
|
{"quux.txt", true},
|
|
|
|
{"baz,quux.txt", false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
if pats.Match(tc.name).IsIgnored() != tc.match {
|
|
|
|
t.Errorf("Match of %s was %v, should be %v", tc.name, !tc.match, tc.match)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-23 23:32:08 +00:00
|
|
|
|
|
|
|
func TestIssue3164(t *testing.T) {
|
2023-05-03 08:25:36 +00:00
|
|
|
testFs := newTestFS()
|
|
|
|
|
2016-05-23 23:32:08 +00:00
|
|
|
stignore := `
|
|
|
|
(?d)(?i)*.part
|
|
|
|
(?d)(?i)/foo
|
|
|
|
(?d)(?i)**/bar
|
|
|
|
`
|
2023-05-03 08:25:36 +00:00
|
|
|
pats := New(testFs, WithCache(true))
|
|
|
|
|
2016-05-23 23:32:08 +00:00
|
|
|
err := pats.Parse(bytes.NewBufferString(stignore), ".stignore")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expanded := pats.Patterns()
|
|
|
|
t.Log(expanded)
|
|
|
|
expected := []string{
|
|
|
|
"(?d)(?i)*.part",
|
|
|
|
"(?d)(?i)**/*.part",
|
|
|
|
"(?d)(?i)*.part/**",
|
|
|
|
"(?d)(?i)**/*.part/**",
|
|
|
|
"(?d)(?i)/foo",
|
|
|
|
"(?d)(?i)/foo/**",
|
|
|
|
"(?d)(?i)**/bar",
|
|
|
|
"(?d)(?i)bar",
|
|
|
|
"(?d)(?i)**/bar/**",
|
|
|
|
"(?d)(?i)bar/**",
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(expanded) != len(expected) {
|
|
|
|
t.Errorf("Unmatched count: %d != %d", len(expanded), len(expected))
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range expanded {
|
|
|
|
if expanded[i] != expected[i] {
|
|
|
|
t.Errorf("Pattern %d does not match: %s != %s", i, expanded[i], expected[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-28 04:43:54 +00:00
|
|
|
|
|
|
|
func TestIssue3174(t *testing.T) {
|
2023-05-03 08:25:36 +00:00
|
|
|
testFs := newTestFS()
|
|
|
|
|
2016-05-28 04:43:54 +00:00
|
|
|
stignore := `
|
|
|
|
*ä*
|
|
|
|
`
|
2023-05-03 08:25:36 +00:00
|
|
|
pats := New(testFs, WithCache(true))
|
|
|
|
|
2016-05-28 04:43:54 +00:00
|
|
|
err := pats.Parse(bytes.NewBufferString(stignore), ".stignore")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !pats.Match("åäö").IsIgnored() {
|
|
|
|
t.Error("Should match")
|
|
|
|
}
|
|
|
|
}
|
2016-10-03 23:12:55 +00:00
|
|
|
|
|
|
|
func TestIssue3639(t *testing.T) {
|
2023-05-03 08:25:36 +00:00
|
|
|
testFs := newTestFS()
|
|
|
|
|
2016-10-03 23:12:55 +00:00
|
|
|
stignore := `
|
|
|
|
foo/
|
|
|
|
`
|
2023-05-03 08:25:36 +00:00
|
|
|
pats := New(testFs, WithCache(true))
|
|
|
|
|
2016-10-03 23:12:55 +00:00
|
|
|
err := pats.Parse(bytes.NewBufferString(stignore), ".stignore")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !pats.Match("foo/bar").IsIgnored() {
|
|
|
|
t.Error("Should match 'foo/bar'")
|
|
|
|
}
|
|
|
|
|
|
|
|
if pats.Match("foo").IsIgnored() {
|
|
|
|
t.Error("Should not match 'foo'")
|
|
|
|
}
|
|
|
|
}
|
2016-10-21 07:33:40 +00:00
|
|
|
|
|
|
|
func TestIssue3674(t *testing.T) {
|
2023-05-03 08:25:36 +00:00
|
|
|
testFs := newTestFS()
|
|
|
|
|
2016-10-21 07:33:40 +00:00
|
|
|
stignore := `
|
|
|
|
a*b
|
|
|
|
a**c
|
|
|
|
`
|
|
|
|
|
|
|
|
testcases := []struct {
|
|
|
|
file string
|
|
|
|
matches bool
|
|
|
|
}{
|
|
|
|
{"ab", true},
|
|
|
|
{"asdfb", true},
|
|
|
|
{"ac", true},
|
|
|
|
{"asdfc", true},
|
|
|
|
{"as/db", false},
|
|
|
|
{"as/dc", true},
|
|
|
|
}
|
|
|
|
|
2023-05-03 08:25:36 +00:00
|
|
|
pats := New(testFs, WithCache(true))
|
|
|
|
|
2016-10-21 07:33:40 +00:00
|
|
|
err := pats.Parse(bytes.NewBufferString(stignore), ".stignore")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testcases {
|
|
|
|
res := pats.Match(tc.file).IsIgnored()
|
|
|
|
if res != tc.matches {
|
|
|
|
t.Errorf("Matches(%q) == %v, expected %v", tc.file, res, tc.matches)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-23 13:47:31 +00:00
|
|
|
|
|
|
|
func TestGobwasGlobIssue18(t *testing.T) {
|
2023-05-03 08:25:36 +00:00
|
|
|
testFs := newTestFS()
|
|
|
|
|
2016-10-23 13:47:31 +00:00
|
|
|
stignore := `
|
|
|
|
a?b
|
|
|
|
bb?
|
|
|
|
`
|
|
|
|
|
|
|
|
testcases := []struct {
|
|
|
|
file string
|
|
|
|
matches bool
|
|
|
|
}{
|
|
|
|
{"ab", false},
|
|
|
|
{"acb", true},
|
|
|
|
{"asdb", false},
|
|
|
|
{"bb", false},
|
|
|
|
{"bba", true},
|
|
|
|
{"bbaa", false},
|
|
|
|
}
|
|
|
|
|
2023-05-03 08:25:36 +00:00
|
|
|
pats := New(testFs, WithCache(true))
|
|
|
|
|
2016-10-23 13:47:31 +00:00
|
|
|
err := pats.Parse(bytes.NewBufferString(stignore), ".stignore")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testcases {
|
|
|
|
res := pats.Match(tc.file).IsIgnored()
|
|
|
|
if res != tc.matches {
|
|
|
|
t.Errorf("Matches(%q) == %v, expected %v", tc.file, res, tc.matches)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-01 14:00:11 +00:00
|
|
|
|
2017-05-01 16:58:08 +00:00
|
|
|
func TestRoot(t *testing.T) {
|
2023-05-03 08:25:36 +00:00
|
|
|
testFs := newTestFS()
|
|
|
|
|
2017-05-01 16:58:08 +00:00
|
|
|
stignore := `
|
|
|
|
!/a
|
|
|
|
/*
|
|
|
|
`
|
|
|
|
|
|
|
|
testcases := []struct {
|
|
|
|
file string
|
|
|
|
matches bool
|
|
|
|
}{
|
|
|
|
{".", false},
|
|
|
|
{"a", false},
|
|
|
|
{"b", true},
|
|
|
|
}
|
|
|
|
|
2023-05-03 08:25:36 +00:00
|
|
|
pats := New(testFs, WithCache(true))
|
|
|
|
|
2017-05-01 16:58:08 +00:00
|
|
|
err := pats.Parse(bytes.NewBufferString(stignore), ".stignore")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testcases {
|
|
|
|
res := pats.Match(tc.file).IsIgnored()
|
|
|
|
if res != tc.matches {
|
|
|
|
t.Errorf("Matches(%q) == %v, expected %v", tc.file, res, tc.matches)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-06 11:44:11 +00:00
|
|
|
|
|
|
|
func TestLines(t *testing.T) {
|
2023-05-03 08:25:36 +00:00
|
|
|
testFs := newTestFS()
|
|
|
|
|
2017-07-06 11:44:11 +00:00
|
|
|
stignore := `
|
2023-05-03 08:25:36 +00:00
|
|
|
#include excludes
|
2017-08-19 14:36:56 +00:00
|
|
|
|
2017-07-06 11:44:11 +00:00
|
|
|
!/a
|
|
|
|
/*
|
2017-09-04 12:46:19 +00:00
|
|
|
!/a
|
2017-07-06 11:44:11 +00:00
|
|
|
`
|
|
|
|
|
2023-05-03 08:25:36 +00:00
|
|
|
pats := New(testFs, WithCache(true))
|
|
|
|
|
2017-07-06 11:44:11 +00:00
|
|
|
err := pats.Parse(bytes.NewBufferString(stignore), ".stignore")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedLines := []string{
|
|
|
|
"",
|
2023-05-03 08:25:36 +00:00
|
|
|
"#include excludes",
|
2017-07-06 11:44:11 +00:00
|
|
|
"",
|
|
|
|
"!/a",
|
|
|
|
"/*",
|
2017-09-04 12:46:19 +00:00
|
|
|
"!/a",
|
2017-07-06 11:44:11 +00:00
|
|
|
"",
|
|
|
|
}
|
|
|
|
|
|
|
|
lines := pats.Lines()
|
|
|
|
if len(lines) != len(expectedLines) {
|
|
|
|
t.Fatalf("len(Lines()) == %d, expected %d", len(lines), len(expectedLines))
|
|
|
|
}
|
|
|
|
for i := range lines {
|
|
|
|
if lines[i] != expectedLines[i] {
|
|
|
|
t.Fatalf("Lines()[%d] == %s, expected %s", i, lines[i], expectedLines[i])
|
|
|
|
}
|
|
|
|
}
|
2017-09-04 12:46:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDuplicateLines(t *testing.T) {
|
2023-05-03 08:25:36 +00:00
|
|
|
testFs := newTestFS()
|
|
|
|
|
2017-09-04 12:46:19 +00:00
|
|
|
stignore := `
|
|
|
|
!/a
|
|
|
|
/*
|
|
|
|
!/a
|
|
|
|
`
|
|
|
|
stignoreFiltered := `
|
|
|
|
!/a
|
|
|
|
/*
|
|
|
|
`
|
|
|
|
|
2023-05-03 08:25:36 +00:00
|
|
|
pats := New(testFs, WithCache(true))
|
2017-09-04 12:46:19 +00:00
|
|
|
|
|
|
|
err := pats.Parse(bytes.NewBufferString(stignore), ".stignore")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
patsLen := len(pats.patterns)
|
|
|
|
|
|
|
|
err = pats.Parse(bytes.NewBufferString(stignoreFiltered), ".stignore")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-07-06 11:44:11 +00:00
|
|
|
|
2017-09-04 12:46:19 +00:00
|
|
|
if patsLen != len(pats.patterns) {
|
|
|
|
t.Fatalf("Parsed patterns differ when manually removing duplicate lines")
|
|
|
|
}
|
2017-07-06 11:44:11 +00:00
|
|
|
}
|
2018-01-17 16:56:53 +00:00
|
|
|
|
|
|
|
func TestIssue4680(t *testing.T) {
|
2023-05-03 08:25:36 +00:00
|
|
|
testFs := newTestFS()
|
|
|
|
|
2018-01-17 16:56:53 +00:00
|
|
|
stignore := `
|
|
|
|
#snapshot
|
|
|
|
`
|
|
|
|
|
|
|
|
testcases := []struct {
|
|
|
|
file string
|
|
|
|
matches bool
|
|
|
|
}{
|
|
|
|
{"#snapshot", true},
|
|
|
|
{"#snapshot/foo", true},
|
|
|
|
}
|
|
|
|
|
2023-05-03 08:25:36 +00:00
|
|
|
pats := New(testFs, WithCache(true))
|
|
|
|
|
2018-01-17 16:56:53 +00:00
|
|
|
err := pats.Parse(bytes.NewBufferString(stignore), ".stignore")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testcases {
|
|
|
|
res := pats.Match(tc.file).IsIgnored()
|
|
|
|
if res != tc.matches {
|
|
|
|
t.Errorf("Matches(%q) == %v, expected %v", tc.file, res, tc.matches)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-20 07:52:57 +00:00
|
|
|
|
|
|
|
func TestIssue4689(t *testing.T) {
|
2023-05-03 08:25:36 +00:00
|
|
|
testFs := newTestFS()
|
|
|
|
|
2018-01-20 07:52:57 +00:00
|
|
|
stignore := `// orig`
|
|
|
|
|
2023-05-03 08:25:36 +00:00
|
|
|
pats := New(testFs, WithCache(true))
|
|
|
|
|
2018-01-20 07:52:57 +00:00
|
|
|
err := pats.Parse(bytes.NewBufferString(stignore), ".stignore")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if lines := pats.Lines(); len(lines) != 1 || lines[0] != "// orig" {
|
|
|
|
t.Fatalf("wrong lines parsing original comment:\n%q", lines)
|
|
|
|
}
|
|
|
|
|
|
|
|
stignore = `// new`
|
|
|
|
|
|
|
|
err = pats.Parse(bytes.NewBufferString(stignore), ".stignore")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if lines := pats.Lines(); len(lines) != 1 || lines[0] != "// new" {
|
|
|
|
t.Fatalf("wrong lines parsing changed comment:\n%v", lines)
|
|
|
|
}
|
|
|
|
}
|
2018-05-08 21:37:13 +00:00
|
|
|
|
|
|
|
func TestIssue4901(t *testing.T) {
|
2023-05-03 08:25:36 +00:00
|
|
|
testFs := newTestFS()
|
2018-05-08 21:37:13 +00:00
|
|
|
|
|
|
|
stignore := `
|
|
|
|
#include unicorn-lazor-death
|
|
|
|
puppy
|
|
|
|
`
|
|
|
|
|
2023-05-03 08:25:36 +00:00
|
|
|
pats := New(testFs, WithCache(true))
|
|
|
|
|
|
|
|
fd, err := pats.fs.Create(".stignore")
|
|
|
|
if err != nil {
|
2018-05-08 21:37:13 +00:00
|
|
|
t.Fatalf(err.Error())
|
|
|
|
}
|
2023-05-03 08:25:36 +00:00
|
|
|
if _, err := fd.Write([]byte(stignore)); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-05-08 21:37:13 +00:00
|
|
|
|
|
|
|
// Cache does not suddenly make the load succeed.
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
err := pats.Load(".stignore")
|
|
|
|
if err == nil {
|
2020-06-18 09:04:00 +00:00
|
|
|
t.Fatal("expected an error")
|
2018-05-08 21:37:13 +00:00
|
|
|
}
|
2022-12-21 22:42:22 +00:00
|
|
|
if err == fs.ErrNotExist {
|
|
|
|
t.Fatalf("unexpected error type: %T", err)
|
2020-06-18 09:04:00 +00:00
|
|
|
}
|
|
|
|
if !IsParseError(err) {
|
|
|
|
t.Fatal("failure to load included file should be a parse error")
|
2018-05-08 21:37:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-03 08:25:36 +00:00
|
|
|
fd, err = pats.fs.Create("unicorn-lazor-death")
|
|
|
|
if err != nil {
|
2018-05-08 21:37:13 +00:00
|
|
|
t.Fatalf(err.Error())
|
|
|
|
}
|
2023-05-03 08:25:36 +00:00
|
|
|
if _, err := fd.Write([]byte(" ")); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-05-08 21:37:13 +00:00
|
|
|
|
2023-05-03 08:25:36 +00:00
|
|
|
err = pats.Load(".stignore")
|
2018-05-08 21:37:13 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err.Error())
|
|
|
|
}
|
|
|
|
}
|
2018-06-18 06:22:19 +00:00
|
|
|
|
|
|
|
// TestIssue5009 checks that ignored dirs are only skipped if there are no include patterns.
|
|
|
|
// https://github.com/syncthing/syncthing/issues/5009 (rc-only bug)
|
|
|
|
func TestIssue5009(t *testing.T) {
|
2023-05-03 08:25:36 +00:00
|
|
|
testFs := newTestFS()
|
|
|
|
|
|
|
|
pats := New(testFs, WithCache(true))
|
2018-06-18 06:22:19 +00:00
|
|
|
|
|
|
|
stignore := `
|
|
|
|
ign1
|
|
|
|
i*2
|
|
|
|
`
|
|
|
|
if err := pats.Parse(bytes.NewBufferString(stignore), ".stignore"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
lib/ignore: Optimise ignoring directories for filesystem watcher (fixes #9339) (#9340)
This improves the ignore handling so that directories can be fully
ignored (skipped in the watcher) in more cases. Specifically, where the
previous rule was that any complex `!`-pattern would disable skipping
directories, the new rule is that only matches on patterns *after* such
a `!`-pattern disable skipping. That is, the following now does the
intuitive thing:
```
/foo
/bar
!whatever
*
```
- `/foo/**` and `/bar/**` are completely skipped, since there is no
chance anything underneath them could ever be not-ignored
- `!whatever` toggles the "can't skip directories any more" flag
- Anything that matches `*` can't skip directories, because it's
possible we can have `whatever` match something deeper.
To enable this, some refactoring was necessary:
- The "can skip dirs" flag is now a property of the match result, not of
the pattern set as a whole.
- That meant returning a boolean is not good enough, we need to actually
return the entire `Result` (or, like, two booleans but that seemed
uglier and more annoying to use)
- `ShouldIgnore(string) boolean` went away with
`Match(string).IsIgnored()` being the obvious replacement (API
simplification!)
- The watcher then needed to import the `ignore` package (for the
`Result` type), but `fs` imports the watcher and `ignore` imports `fs`.
That's a cycle, so I broke out `Result` into a package of its own so
that it can be safely imported everywhere in things like `type Matcher
interface { Match(string) result.Result }`. There's a fair amount of
stuttering in `result.Result` and maybe we should go with something like
`ignoreresult.R` or so, leaving this open for discussion.
Tests refactored to suit, I think this change is in fact quite well
covered by the existing ones...
Also some noise because a few of the changed files were quite old and
got the `gofumpt` treatment by my editor. Sorry not sorry.
---------
Co-authored-by: Simon Frei <freisim93@gmail.com>
2024-01-15 10:13:22 +00:00
|
|
|
if m := pats.Match("ign2"); !m.CanSkipDir() {
|
|
|
|
t.Error("CanSkipDir should be true without excludes")
|
2018-06-18 06:22:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stignore = `
|
|
|
|
!iex2
|
|
|
|
!ign1/ex
|
|
|
|
ign1
|
|
|
|
i*2
|
|
|
|
!ign2
|
|
|
|
`
|
|
|
|
|
|
|
|
if err := pats.Parse(bytes.NewBufferString(stignore), ".stignore"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
lib/ignore: Optimise ignoring directories for filesystem watcher (fixes #9339) (#9340)
This improves the ignore handling so that directories can be fully
ignored (skipped in the watcher) in more cases. Specifically, where the
previous rule was that any complex `!`-pattern would disable skipping
directories, the new rule is that only matches on patterns *after* such
a `!`-pattern disable skipping. That is, the following now does the
intuitive thing:
```
/foo
/bar
!whatever
*
```
- `/foo/**` and `/bar/**` are completely skipped, since there is no
chance anything underneath them could ever be not-ignored
- `!whatever` toggles the "can't skip directories any more" flag
- Anything that matches `*` can't skip directories, because it's
possible we can have `whatever` match something deeper.
To enable this, some refactoring was necessary:
- The "can skip dirs" flag is now a property of the match result, not of
the pattern set as a whole.
- That meant returning a boolean is not good enough, we need to actually
return the entire `Result` (or, like, two booleans but that seemed
uglier and more annoying to use)
- `ShouldIgnore(string) boolean` went away with
`Match(string).IsIgnored()` being the obvious replacement (API
simplification!)
- The watcher then needed to import the `ignore` package (for the
`Result` type), but `fs` imports the watcher and `ignore` imports `fs`.
That's a cycle, so I broke out `Result` into a package of its own so
that it can be safely imported everywhere in things like `type Matcher
interface { Match(string) result.Result }`. There's a fair amount of
stuttering in `result.Result` and maybe we should go with something like
`ignoreresult.R` or so, leaving this open for discussion.
Tests refactored to suit, I think this change is in fact quite well
covered by the existing ones...
Also some noise because a few of the changed files were quite old and
got the `gofumpt` treatment by my editor. Sorry not sorry.
---------
Co-authored-by: Simon Frei <freisim93@gmail.com>
2024-01-15 10:13:22 +00:00
|
|
|
if m := pats.Match("ign2"); m.CanSkipDir() {
|
|
|
|
t.Error("CanSkipDir should not be true with excludes")
|
2018-06-18 06:22:19 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-28 20:20:11 +00:00
|
|
|
|
|
|
|
func TestSpecialChars(t *testing.T) {
|
2023-05-03 08:25:36 +00:00
|
|
|
testFs := newTestFS()
|
|
|
|
|
|
|
|
pats := New(testFs, WithCache(true))
|
2019-04-28 20:20:11 +00:00
|
|
|
|
|
|
|
stignore := `(?i)/#recycle
|
|
|
|
(?i)/#nosync
|
|
|
|
(?i)/$Recycle.bin
|
|
|
|
(?i)/$RECYCLE.BIN
|
|
|
|
(?i)/System Volume Information`
|
|
|
|
if err := pats.Parse(bytes.NewBufferString(stignore), ".stignore"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []string{
|
|
|
|
"#nosync",
|
|
|
|
"$RECYCLE.BIN",
|
|
|
|
filepath.FromSlash("$RECYCLE.BIN/S-1-5-18/desktop.ini"),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
if !pats.Match(c).IsIgnored() {
|
|
|
|
t.Errorf("%q should be ignored", c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-30 09:36:31 +00:00
|
|
|
|
2022-06-15 19:49:15 +00:00
|
|
|
func TestIntlWildcards(t *testing.T) {
|
2023-05-03 08:25:36 +00:00
|
|
|
testFs := newTestFS()
|
|
|
|
|
|
|
|
pats := New(testFs, WithCache(true))
|
2022-06-15 19:49:15 +00:00
|
|
|
|
|
|
|
stignore := `1000春
|
|
|
|
200?春
|
|
|
|
300[0-9]春
|
|
|
|
400[0-9]?`
|
|
|
|
if err := pats.Parse(bytes.NewBufferString(stignore), ".stignore"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []string{
|
|
|
|
"1000春",
|
|
|
|
"2002春",
|
|
|
|
"3003春",
|
|
|
|
"4004春",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
if !pats.Match(c).IsIgnored() {
|
|
|
|
t.Errorf("%q should be ignored", c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-30 09:36:31 +00:00
|
|
|
func TestPartialIncludeLine(t *testing.T) {
|
2023-05-03 08:25:36 +00:00
|
|
|
testFs := newTestFS()
|
|
|
|
|
2019-08-30 09:36:31 +00:00
|
|
|
// Loading a partial #include line (no file mentioned) should error but not crash.
|
|
|
|
|
2023-05-03 08:25:36 +00:00
|
|
|
pats := New(testFs, WithCache(true))
|
|
|
|
|
2019-08-30 09:36:31 +00:00
|
|
|
cases := []string{
|
|
|
|
"#include",
|
|
|
|
"#include\n",
|
|
|
|
"#include ",
|
|
|
|
"#include \n",
|
|
|
|
"#include \n\n\n",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
2020-06-18 09:04:00 +00:00
|
|
|
err := pats.Parse(bytes.NewBufferString(tc), ".stignore")
|
|
|
|
if err == nil {
|
2019-08-30 09:36:31 +00:00
|
|
|
t.Fatal("should error out")
|
|
|
|
}
|
2020-06-18 09:04:00 +00:00
|
|
|
if !IsParseError(err) {
|
|
|
|
t.Fatal("failure to load included file should be a parse error")
|
|
|
|
}
|
2019-08-30 09:36:31 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-26 07:37:41 +00:00
|
|
|
|
|
|
|
func TestSkipIgnoredDirs(t *testing.T) {
|
2023-05-03 08:25:36 +00:00
|
|
|
testFs := newTestFS()
|
|
|
|
|
2019-11-26 07:37:41 +00:00
|
|
|
tcs := []struct {
|
|
|
|
pattern string
|
|
|
|
expected bool
|
|
|
|
}{
|
|
|
|
{`!/test`, true},
|
|
|
|
{`!/t[eih]t`, true},
|
|
|
|
{`!/t*t`, true},
|
|
|
|
{`!/t?t`, true},
|
|
|
|
{`!/**`, true},
|
2020-04-07 08:23:38 +00:00
|
|
|
{`!/parent/test`, false},
|
|
|
|
{`!/parent/t[eih]t`, false},
|
|
|
|
{`!/parent/t*t`, false},
|
|
|
|
{`!/parent/t?t`, false},
|
2019-11-26 07:37:41 +00:00
|
|
|
{`!/**.mp3`, false},
|
|
|
|
{`!/pa*nt/test`, false},
|
|
|
|
{`!/pa[sdf]nt/t[eih]t`, false},
|
|
|
|
{`!/lowest/pa[sdf]nt/test`, false},
|
|
|
|
{`!/lo*st/parent/test`, false},
|
|
|
|
{`/pa*nt/test`, true},
|
|
|
|
{`test`, true},
|
|
|
|
{`*`, true},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tcs {
|
|
|
|
pats, err := parseLine(tc.pattern)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
for _, pat := range pats {
|
|
|
|
if got := pat.allowsSkippingIgnoredDirs(); got != tc.expected {
|
|
|
|
t.Errorf(`Pattern "%v": got %v, expected %v`, pat, got, tc.expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-03 08:25:36 +00:00
|
|
|
pats := New(testFs, WithCache(true))
|
2019-11-26 07:37:41 +00:00
|
|
|
|
|
|
|
stignore := `
|
|
|
|
/foo/ign*
|
|
|
|
!/f*
|
|
|
|
!/bar
|
|
|
|
*
|
|
|
|
`
|
|
|
|
if err := pats.Parse(bytes.NewBufferString(stignore), ".stignore"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
lib/ignore: Optimise ignoring directories for filesystem watcher (fixes #9339) (#9340)
This improves the ignore handling so that directories can be fully
ignored (skipped in the watcher) in more cases. Specifically, where the
previous rule was that any complex `!`-pattern would disable skipping
directories, the new rule is that only matches on patterns *after* such
a `!`-pattern disable skipping. That is, the following now does the
intuitive thing:
```
/foo
/bar
!whatever
*
```
- `/foo/**` and `/bar/**` are completely skipped, since there is no
chance anything underneath them could ever be not-ignored
- `!whatever` toggles the "can't skip directories any more" flag
- Anything that matches `*` can't skip directories, because it's
possible we can have `whatever` match something deeper.
To enable this, some refactoring was necessary:
- The "can skip dirs" flag is now a property of the match result, not of
the pattern set as a whole.
- That meant returning a boolean is not good enough, we need to actually
return the entire `Result` (or, like, two booleans but that seemed
uglier and more annoying to use)
- `ShouldIgnore(string) boolean` went away with
`Match(string).IsIgnored()` being the obvious replacement (API
simplification!)
- The watcher then needed to import the `ignore` package (for the
`Result` type), but `fs` imports the watcher and `ignore` imports `fs`.
That's a cycle, so I broke out `Result` into a package of its own so
that it can be safely imported everywhere in things like `type Matcher
interface { Match(string) result.Result }`. There's a fair amount of
stuttering in `result.Result` and maybe we should go with something like
`ignoreresult.R` or so, leaving this open for discussion.
Tests refactored to suit, I think this change is in fact quite well
covered by the existing ones...
Also some noise because a few of the changed files were quite old and
got the `gofumpt` treatment by my editor. Sorry not sorry.
---------
Co-authored-by: Simon Frei <freisim93@gmail.com>
2024-01-15 10:13:22 +00:00
|
|
|
if m := pats.Match("whatever"); !m.CanSkipDir() {
|
|
|
|
t.Error("CanSkipDir should be true")
|
2019-11-26 07:37:41 +00:00
|
|
|
}
|
2020-04-07 08:23:38 +00:00
|
|
|
|
|
|
|
stignore = `
|
|
|
|
!/foo/ign*
|
|
|
|
*
|
|
|
|
`
|
|
|
|
if err := pats.Parse(bytes.NewBufferString(stignore), ".stignore"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
lib/ignore: Optimise ignoring directories for filesystem watcher (fixes #9339) (#9340)
This improves the ignore handling so that directories can be fully
ignored (skipped in the watcher) in more cases. Specifically, where the
previous rule was that any complex `!`-pattern would disable skipping
directories, the new rule is that only matches on patterns *after* such
a `!`-pattern disable skipping. That is, the following now does the
intuitive thing:
```
/foo
/bar
!whatever
*
```
- `/foo/**` and `/bar/**` are completely skipped, since there is no
chance anything underneath them could ever be not-ignored
- `!whatever` toggles the "can't skip directories any more" flag
- Anything that matches `*` can't skip directories, because it's
possible we can have `whatever` match something deeper.
To enable this, some refactoring was necessary:
- The "can skip dirs" flag is now a property of the match result, not of
the pattern set as a whole.
- That meant returning a boolean is not good enough, we need to actually
return the entire `Result` (or, like, two booleans but that seemed
uglier and more annoying to use)
- `ShouldIgnore(string) boolean` went away with
`Match(string).IsIgnored()` being the obvious replacement (API
simplification!)
- The watcher then needed to import the `ignore` package (for the
`Result` type), but `fs` imports the watcher and `ignore` imports `fs`.
That's a cycle, so I broke out `Result` into a package of its own so
that it can be safely imported everywhere in things like `type Matcher
interface { Match(string) result.Result }`. There's a fair amount of
stuttering in `result.Result` and maybe we should go with something like
`ignoreresult.R` or so, leaving this open for discussion.
Tests refactored to suit, I think this change is in fact quite well
covered by the existing ones...
Also some noise because a few of the changed files were quite old and
got the `gofumpt` treatment by my editor. Sorry not sorry.
---------
Co-authored-by: Simon Frei <freisim93@gmail.com>
2024-01-15 10:13:22 +00:00
|
|
|
if m := pats.Match("whatever"); m.CanSkipDir() {
|
|
|
|
t.Error("CanSkipDir should be false")
|
2020-04-07 08:23:38 +00:00
|
|
|
}
|
2019-11-26 07:37:41 +00:00
|
|
|
}
|
2020-01-30 08:58:44 +00:00
|
|
|
|
|
|
|
func TestEmptyPatterns(t *testing.T) {
|
2023-05-03 08:25:36 +00:00
|
|
|
testFs := newTestFS()
|
|
|
|
|
2020-01-30 08:58:44 +00:00
|
|
|
// These patterns are all invalid and should be rejected as such (without panicking...)
|
|
|
|
tcs := []string{
|
|
|
|
"!",
|
|
|
|
"(?d)",
|
|
|
|
"(?i)",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tcs {
|
2023-05-03 08:25:36 +00:00
|
|
|
m := New(testFs)
|
2020-01-30 08:58:44 +00:00
|
|
|
err := m.Parse(strings.NewReader(tc), ".stignore")
|
|
|
|
if err == nil {
|
|
|
|
t.Error("Should reject invalid pattern", tc)
|
|
|
|
}
|
2020-06-18 09:04:00 +00:00
|
|
|
if !IsParseError(err) {
|
|
|
|
t.Fatal("bad pattern should be a parse error")
|
|
|
|
}
|
2020-01-30 08:58:44 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-22 08:38:24 +00:00
|
|
|
|
|
|
|
func TestWindowsLineEndings(t *testing.T) {
|
2023-05-03 08:25:36 +00:00
|
|
|
testFs := newTestFS()
|
|
|
|
|
2022-07-28 17:36:39 +00:00
|
|
|
if !build.IsWindows {
|
2021-11-22 08:38:24 +00:00
|
|
|
t.Skip("Windows specific")
|
|
|
|
}
|
|
|
|
lines := "foo\nbar\nbaz\n"
|
|
|
|
|
2023-05-03 08:25:36 +00:00
|
|
|
m := New(testFs)
|
2021-11-22 08:38:24 +00:00
|
|
|
if err := m.Parse(strings.NewReader(lines), ".stignore"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2023-05-03 08:25:36 +00:00
|
|
|
if err := WriteIgnores(testFs, ".stignore", m.Lines()); err != nil {
|
2021-11-22 08:38:24 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2023-05-03 08:25:36 +00:00
|
|
|
fd, err := testFs.Open(".stignore")
|
2021-11-22 08:38:24 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
bs, err := io.ReadAll(fd)
|
|
|
|
fd.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
unixLineEndings := bytes.Count(bs, []byte("\n"))
|
|
|
|
windowsLineEndings := bytes.Count(bs, []byte("\r\n"))
|
|
|
|
if unixLineEndings == 0 || windowsLineEndings != unixLineEndings {
|
|
|
|
t.Error("expected there to be a non-zero number of Windows line endings")
|
|
|
|
}
|
|
|
|
}
|