2015-09-27 17:13:20 +02:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
2016-12-07 12:17:10 +01:00
|
|
|
"fmt"
|
2015-09-27 17:13:20 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2015-12-17 12:52:38 +01:00
|
|
|
"time"
|
2016-04-21 21:06:21 +02:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2016-05-16 18:14:04 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2015-09-27 17:13:20 +02:00
|
|
|
)
|
|
|
|
|
2015-12-17 12:52:38 +01:00
|
|
|
func TestAgeSuffix(t *testing.T) {
|
2016-06-29 18:59:31 +02:00
|
|
|
for _, test := range []struct {
|
2015-12-17 12:52:38 +01:00
|
|
|
in string
|
|
|
|
want float64
|
|
|
|
err bool
|
|
|
|
}{
|
|
|
|
{"0", 0, false},
|
|
|
|
{"", 0, true},
|
|
|
|
{"1ms", float64(time.Millisecond), false},
|
|
|
|
{"1s", float64(time.Second), false},
|
|
|
|
{"1m", float64(time.Minute), false},
|
|
|
|
{"1h", float64(time.Hour), false},
|
|
|
|
{"1d", float64(time.Hour) * 24, false},
|
|
|
|
{"1w", float64(time.Hour) * 24 * 7, false},
|
|
|
|
{"1M", float64(time.Hour) * 24 * 30, false},
|
|
|
|
{"1y", float64(time.Hour) * 24 * 365, false},
|
|
|
|
{"1.5y", float64(time.Hour) * 24 * 365 * 1.5, false},
|
|
|
|
{"-1s", -float64(time.Second), false},
|
|
|
|
{"1.s", float64(time.Second), false},
|
|
|
|
{"1x", 0, true},
|
|
|
|
} {
|
|
|
|
duration, err := ParseDuration(test.in)
|
2016-06-29 18:59:31 +02:00
|
|
|
if test.err {
|
|
|
|
require.Error(t, err)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
2015-12-17 12:52:38 +01:00
|
|
|
}
|
2016-06-29 18:59:31 +02:00
|
|
|
assert.Equal(t, test.want, float64(duration))
|
2015-12-17 12:52:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-27 17:13:20 +02:00
|
|
|
func TestNewFilterDefault(t *testing.T) {
|
|
|
|
f, err := NewFilter()
|
2016-05-16 18:14:04 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.False(t, f.DeleteExcluded)
|
2016-06-03 22:54:27 +02:00
|
|
|
assert.Equal(t, int64(-1), f.MinSize)
|
|
|
|
assert.Equal(t, int64(-1), f.MaxSize)
|
2016-05-16 18:14:04 +02:00
|
|
|
assert.Len(t, f.fileRules.rules, 0)
|
|
|
|
assert.Len(t, f.dirRules.rules, 0)
|
|
|
|
assert.Nil(t, f.files)
|
|
|
|
assert.True(t, f.InActive())
|
2015-09-27 17:13:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// testFile creates a temp file with the contents
|
2016-12-07 14:37:40 +01:00
|
|
|
func testFile(t *testing.T, contents string) string {
|
2015-09-27 17:13:20 +02:00
|
|
|
out, err := ioutil.TempFile("", "filter_test")
|
2016-05-16 18:14:04 +02:00
|
|
|
require.NoError(t, err)
|
2015-11-07 11:25:43 +01:00
|
|
|
defer func() {
|
|
|
|
err := out.Close()
|
2016-06-29 18:59:31 +02:00
|
|
|
require.NoError(t, err)
|
2015-11-07 11:25:43 +01:00
|
|
|
}()
|
2015-09-27 17:13:20 +02:00
|
|
|
_, err = out.Write([]byte(contents))
|
2016-05-16 18:14:04 +02:00
|
|
|
require.NoError(t, err)
|
2015-09-27 17:13:20 +02:00
|
|
|
s := out.Name()
|
2016-12-07 14:37:40 +01:00
|
|
|
return s
|
2015-09-27 17:13:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewFilterFull(t *testing.T) {
|
|
|
|
mins := int64(100 * 1024)
|
|
|
|
maxs := int64(1000 * 1024)
|
|
|
|
isFalse := false
|
|
|
|
isTrue := true
|
|
|
|
|
|
|
|
// Set up the input
|
|
|
|
deleteExcluded = &isTrue
|
2016-12-07 14:37:40 +01:00
|
|
|
filterRule = &[]string{"- filter1", "- filter1b"}
|
|
|
|
filterFrom = &[]string{testFile(t, "#comment\n+ filter2\n- filter3\n")}
|
|
|
|
excludeRule = &[]string{"exclude1"}
|
|
|
|
excludeFrom = &[]string{testFile(t, "#comment\nexclude2\nexclude3\n")}
|
|
|
|
includeRule = &[]string{"include1"}
|
|
|
|
includeFrom = &[]string{testFile(t, "#comment\ninclude2\ninclude3\n")}
|
|
|
|
filesFrom = &[]string{testFile(t, "#comment\nfiles1\nfiles2\n")}
|
2015-09-27 17:13:20 +02:00
|
|
|
minSize = SizeSuffix(mins)
|
|
|
|
maxSize = SizeSuffix(maxs)
|
|
|
|
|
|
|
|
rm := func(p string) {
|
|
|
|
err := os.Remove(p)
|
|
|
|
if err != nil {
|
|
|
|
t.Logf("error removing %q: %v", p, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Reset the input
|
|
|
|
defer func() {
|
2016-12-07 14:37:40 +01:00
|
|
|
rm((*filterFrom)[0])
|
|
|
|
rm((*excludeFrom)[0])
|
|
|
|
rm((*includeFrom)[0])
|
|
|
|
rm((*filesFrom)[0])
|
2016-06-03 22:54:27 +02:00
|
|
|
minSize = -1
|
|
|
|
maxSize = -1
|
2015-09-27 17:13:20 +02:00
|
|
|
deleteExcluded = &isFalse
|
2016-12-07 14:37:40 +01:00
|
|
|
filterRule = nil
|
|
|
|
filterFrom = nil
|
|
|
|
excludeRule = nil
|
|
|
|
excludeFrom = nil
|
|
|
|
includeRule = nil
|
|
|
|
includeFrom = nil
|
|
|
|
filesFrom = nil
|
2015-09-27 17:13:20 +02:00
|
|
|
}()
|
|
|
|
|
|
|
|
f, err := NewFilter()
|
2016-05-16 18:14:04 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.True(t, f.DeleteExcluded)
|
|
|
|
assert.Equal(t, f.MinSize, mins)
|
|
|
|
assert.Equal(t, f.MaxSize, maxs)
|
2015-09-27 17:13:20 +02:00
|
|
|
got := f.DumpFilters()
|
2016-05-16 18:14:04 +02:00
|
|
|
want := `--- File filter rules ---
|
|
|
|
+ (^|/)include1$
|
2015-09-27 17:13:20 +02:00
|
|
|
+ (^|/)include2$
|
|
|
|
+ (^|/)include3$
|
|
|
|
- (^|/)exclude1$
|
|
|
|
- (^|/)exclude2$
|
|
|
|
- (^|/)exclude3$
|
|
|
|
- (^|/)filter1$
|
2016-12-07 14:37:40 +01:00
|
|
|
- (^|/)filter1b$
|
2015-09-27 17:13:20 +02:00
|
|
|
+ (^|/)filter2$
|
2016-01-10 12:42:53 +01:00
|
|
|
- (^|/)filter3$
|
2016-05-16 18:14:04 +02:00
|
|
|
- ^.*$
|
|
|
|
--- Directory filter rules ---
|
|
|
|
+ ^.*$
|
|
|
|
- ^.*$`
|
|
|
|
assert.Equal(t, want, got)
|
|
|
|
assert.Len(t, f.files, 2)
|
2015-09-27 17:13:20 +02:00
|
|
|
for _, name := range []string{"files1", "files2"} {
|
|
|
|
_, ok := f.files[name]
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("Didn't find file %q in f.files", name)
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 18:14:04 +02:00
|
|
|
assert.False(t, f.InActive())
|
2015-09-27 17:13:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type includeTest struct {
|
2015-12-17 12:52:38 +01:00
|
|
|
in string
|
|
|
|
size int64
|
|
|
|
modTime int64
|
|
|
|
want bool
|
2015-09-27 17:13:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func testInclude(t *testing.T, f *Filter, tests []includeTest) {
|
|
|
|
for _, test := range tests {
|
2015-12-17 12:52:38 +01:00
|
|
|
got := f.Include(test.in, test.size, time.Unix(test.modTime, 0))
|
2016-05-16 18:14:04 +02:00
|
|
|
assert.Equal(t, test.want, got, test.in, test.size, test.modTime)
|
2015-09-27 17:13:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-21 21:06:21 +02:00
|
|
|
type includeDirTest struct {
|
|
|
|
in string
|
|
|
|
want bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func testDirInclude(t *testing.T, f *Filter, tests []includeDirTest) {
|
|
|
|
for _, test := range tests {
|
2017-11-09 10:28:36 +01:00
|
|
|
got, err := f.IncludeDirectory(nil)(test.in)
|
|
|
|
require.NoError(t, err)
|
2016-05-16 18:14:04 +02:00
|
|
|
assert.Equal(t, test.want, got, test.in)
|
2016-04-21 21:06:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-27 17:13:20 +02:00
|
|
|
func TestNewFilterIncludeFiles(t *testing.T) {
|
|
|
|
f, err := NewFilter()
|
2016-05-16 18:14:04 +02:00
|
|
|
require.NoError(t, err)
|
2015-11-07 11:25:43 +01:00
|
|
|
err = f.AddFile("file1.jpg")
|
2016-06-29 18:59:31 +02:00
|
|
|
require.NoError(t, err)
|
2015-11-07 11:25:43 +01:00
|
|
|
err = f.AddFile("/file2.jpg")
|
2016-06-29 18:59:31 +02:00
|
|
|
require.NoError(t, err)
|
2016-10-07 12:39:39 +02:00
|
|
|
assert.Equal(t, FilesMap{
|
2016-04-21 21:06:21 +02:00
|
|
|
"file1.jpg": {},
|
|
|
|
"file2.jpg": {},
|
|
|
|
}, f.files)
|
2016-10-07 12:39:39 +02:00
|
|
|
assert.Equal(t, FilesMap{}, f.dirs)
|
2015-09-27 17:13:20 +02:00
|
|
|
testInclude(t, f, []includeTest{
|
2015-12-17 12:52:38 +01:00
|
|
|
{"file1.jpg", 0, 0, true},
|
|
|
|
{"file2.jpg", 1, 0, true},
|
|
|
|
{"potato/file2.jpg", 2, 0, false},
|
|
|
|
{"file3.jpg", 3, 0, false},
|
2015-09-27 17:13:20 +02:00
|
|
|
})
|
2016-06-29 18:59:31 +02:00
|
|
|
assert.False(t, f.InActive())
|
2015-09-27 17:13:20 +02:00
|
|
|
}
|
|
|
|
|
2016-04-21 21:06:21 +02:00
|
|
|
func TestNewFilterIncludeFilesDirs(t *testing.T) {
|
|
|
|
f, err := NewFilter()
|
2016-05-16 18:14:04 +02:00
|
|
|
require.NoError(t, err)
|
2016-04-21 21:06:21 +02:00
|
|
|
for _, path := range []string{
|
|
|
|
"path/to/dir/file1.png",
|
|
|
|
"/path/to/dir/file2.png",
|
|
|
|
"/path/to/file3.png",
|
|
|
|
"/path/to/dir2/file4.png",
|
|
|
|
} {
|
|
|
|
err = f.AddFile(path)
|
2016-06-29 18:59:31 +02:00
|
|
|
require.NoError(t, err)
|
2016-04-21 21:06:21 +02:00
|
|
|
}
|
2016-10-07 12:39:39 +02:00
|
|
|
assert.Equal(t, FilesMap{
|
2016-04-21 21:06:21 +02:00
|
|
|
"path": {},
|
|
|
|
"path/to": {},
|
|
|
|
"path/to/dir": {},
|
|
|
|
"path/to/dir2": {},
|
|
|
|
}, f.dirs)
|
|
|
|
testDirInclude(t, f, []includeDirTest{
|
|
|
|
{"path", true},
|
|
|
|
{"path/to", true},
|
|
|
|
{"path/to/", true},
|
|
|
|
{"/path/to", true},
|
|
|
|
{"/path/to/", true},
|
|
|
|
{"path/to/dir", true},
|
|
|
|
{"path/to/dir2", true},
|
|
|
|
{"path/too", false},
|
|
|
|
{"path/three", false},
|
|
|
|
{"four", false},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-09-27 17:13:20 +02:00
|
|
|
func TestNewFilterMinSize(t *testing.T) {
|
|
|
|
f, err := NewFilter()
|
2016-05-16 18:14:04 +02:00
|
|
|
require.NoError(t, err)
|
2015-09-27 17:13:20 +02:00
|
|
|
f.MinSize = 100
|
|
|
|
testInclude(t, f, []includeTest{
|
2015-12-17 12:52:38 +01:00
|
|
|
{"file1.jpg", 100, 0, true},
|
|
|
|
{"file2.jpg", 101, 0, true},
|
|
|
|
{"potato/file2.jpg", 99, 0, false},
|
2015-09-27 17:13:20 +02:00
|
|
|
})
|
2016-06-29 18:59:31 +02:00
|
|
|
assert.False(t, f.InActive())
|
2015-09-27 17:13:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewFilterMaxSize(t *testing.T) {
|
|
|
|
f, err := NewFilter()
|
2016-05-16 18:14:04 +02:00
|
|
|
require.NoError(t, err)
|
2015-09-27 17:13:20 +02:00
|
|
|
f.MaxSize = 100
|
|
|
|
testInclude(t, f, []includeTest{
|
2015-12-17 12:52:38 +01:00
|
|
|
{"file1.jpg", 100, 0, true},
|
|
|
|
{"file2.jpg", 101, 0, false},
|
|
|
|
{"potato/file2.jpg", 99, 0, true},
|
|
|
|
})
|
2016-06-29 18:59:31 +02:00
|
|
|
assert.False(t, f.InActive())
|
2015-12-17 12:52:38 +01:00
|
|
|
}
|
|
|
|
|
2015-12-17 15:22:43 +01:00
|
|
|
func TestNewFilterMinAndMaxAge(t *testing.T) {
|
2015-12-17 12:52:38 +01:00
|
|
|
f, err := NewFilter()
|
2016-05-16 18:14:04 +02:00
|
|
|
require.NoError(t, err)
|
2015-12-17 15:22:43 +01:00
|
|
|
f.ModTimeFrom = time.Unix(1440000002, 0)
|
|
|
|
f.ModTimeTo = time.Unix(1440000003, 0)
|
2015-12-17 12:52:38 +01:00
|
|
|
testInclude(t, f, []includeTest{
|
2015-12-17 15:22:43 +01:00
|
|
|
{"file1.jpg", 100, 1440000000, false},
|
|
|
|
{"file2.jpg", 101, 1440000001, false},
|
|
|
|
{"file3.jpg", 102, 1440000002, true},
|
|
|
|
{"potato/file1.jpg", 98, 1440000003, true},
|
|
|
|
{"potato/file2.jpg", 99, 1440000004, false},
|
|
|
|
})
|
2016-06-29 18:59:31 +02:00
|
|
|
assert.False(t, f.InActive())
|
2015-12-17 15:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewFilterMinAge(t *testing.T) {
|
|
|
|
f, err := NewFilter()
|
2016-05-16 18:14:04 +02:00
|
|
|
require.NoError(t, err)
|
2015-12-17 15:22:43 +01:00
|
|
|
f.ModTimeTo = time.Unix(1440000002, 0)
|
|
|
|
testInclude(t, f, []includeTest{
|
|
|
|
{"file1.jpg", 100, 1440000000, true},
|
|
|
|
{"file2.jpg", 101, 1440000001, true},
|
|
|
|
{"file3.jpg", 102, 1440000002, true},
|
|
|
|
{"potato/file1.jpg", 98, 1440000003, false},
|
|
|
|
{"potato/file2.jpg", 99, 1440000004, false},
|
|
|
|
})
|
2016-06-29 18:59:31 +02:00
|
|
|
assert.False(t, f.InActive())
|
2015-12-17 15:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewFilterMaxAge(t *testing.T) {
|
|
|
|
f, err := NewFilter()
|
2016-05-16 18:14:04 +02:00
|
|
|
require.NoError(t, err)
|
2015-12-17 15:22:43 +01:00
|
|
|
f.ModTimeFrom = time.Unix(1440000002, 0)
|
|
|
|
testInclude(t, f, []includeTest{
|
|
|
|
{"file1.jpg", 100, 1440000000, false},
|
|
|
|
{"file2.jpg", 101, 1440000001, false},
|
|
|
|
{"file3.jpg", 102, 1440000002, true},
|
|
|
|
{"potato/file1.jpg", 98, 1440000003, true},
|
|
|
|
{"potato/file2.jpg", 99, 1440000004, true},
|
2015-09-27 17:13:20 +02:00
|
|
|
})
|
2016-06-29 18:59:31 +02:00
|
|
|
assert.False(t, f.InActive())
|
2015-09-27 17:13:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewFilterMatches(t *testing.T) {
|
|
|
|
f, err := NewFilter()
|
2016-05-16 18:14:04 +02:00
|
|
|
require.NoError(t, err)
|
2015-09-27 17:13:20 +02:00
|
|
|
add := func(s string) {
|
|
|
|
err := f.AddRule(s)
|
2016-05-16 18:14:04 +02:00
|
|
|
require.NoError(t, err)
|
2015-09-27 17:13:20 +02:00
|
|
|
}
|
|
|
|
add("+ cleared")
|
|
|
|
add("!")
|
2016-05-16 18:14:04 +02:00
|
|
|
add("- /file1.jpg")
|
|
|
|
add("+ /file2.png")
|
|
|
|
add("+ /*.jpg")
|
|
|
|
add("- /*.png")
|
2015-09-27 17:13:20 +02:00
|
|
|
add("- /potato")
|
|
|
|
add("+ /sausage1")
|
|
|
|
add("+ /sausage2*")
|
|
|
|
add("+ /sausage3**")
|
2016-05-16 18:14:04 +02:00
|
|
|
add("+ /a/*.jpg")
|
2015-09-27 17:13:20 +02:00
|
|
|
add("- *")
|
|
|
|
testInclude(t, f, []includeTest{
|
2015-12-17 12:52:38 +01:00
|
|
|
{"cleared", 100, 0, false},
|
|
|
|
{"file1.jpg", 100, 0, false},
|
|
|
|
{"file2.png", 100, 0, true},
|
|
|
|
{"afile2.png", 100, 0, false},
|
|
|
|
{"file3.jpg", 101, 0, true},
|
|
|
|
{"file4.png", 101, 0, false},
|
|
|
|
{"potato", 101, 0, false},
|
|
|
|
{"sausage1", 101, 0, true},
|
|
|
|
{"sausage1/potato", 101, 0, false},
|
|
|
|
{"sausage2potato", 101, 0, true},
|
|
|
|
{"sausage2/potato", 101, 0, false},
|
|
|
|
{"sausage3/potato", 101, 0, true},
|
2016-05-16 18:14:04 +02:00
|
|
|
{"a/one.jpg", 101, 0, true},
|
|
|
|
{"a/one.png", 101, 0, false},
|
2015-12-17 12:52:38 +01:00
|
|
|
{"unicorn", 99, 0, false},
|
2015-09-27 17:13:20 +02:00
|
|
|
})
|
2016-04-21 21:06:21 +02:00
|
|
|
testDirInclude(t, f, []includeDirTest{
|
|
|
|
{"sausage1", false},
|
|
|
|
{"sausage2", false},
|
|
|
|
{"sausage2/sub", false},
|
|
|
|
{"sausage2/sub/dir", false},
|
|
|
|
{"sausage3", true},
|
|
|
|
{"sausage3/sub", true},
|
|
|
|
{"sausage3/sub/dir", true},
|
|
|
|
{"sausage4", false},
|
2016-05-16 18:14:04 +02:00
|
|
|
{"a", true},
|
2016-04-21 21:06:21 +02:00
|
|
|
})
|
2016-06-29 18:59:31 +02:00
|
|
|
assert.False(t, f.InActive())
|
2015-09-27 17:13:20 +02:00
|
|
|
}
|
|
|
|
|
2016-12-07 12:17:10 +01:00
|
|
|
func TestFilterAddDirRuleOrFileRule(t *testing.T) {
|
|
|
|
for _, test := range []struct {
|
|
|
|
included bool
|
|
|
|
glob string
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
false,
|
|
|
|
"potato",
|
|
|
|
`--- File filter rules ---
|
|
|
|
- (^|/)potato$
|
|
|
|
--- Directory filter rules ---`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
true,
|
|
|
|
"potato",
|
|
|
|
`--- File filter rules ---
|
|
|
|
+ (^|/)potato$
|
|
|
|
--- Directory filter rules ---
|
|
|
|
+ ^.*$`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
false,
|
|
|
|
"*",
|
|
|
|
`--- File filter rules ---
|
|
|
|
- (^|/)[^/]*$
|
|
|
|
--- Directory filter rules ---
|
|
|
|
- ^.*$`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
true,
|
|
|
|
"*",
|
|
|
|
`--- File filter rules ---
|
|
|
|
+ (^|/)[^/]*$
|
|
|
|
--- Directory filter rules ---
|
|
|
|
+ ^.*$`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
false,
|
|
|
|
".*{,/**}",
|
|
|
|
`--- File filter rules ---
|
|
|
|
- (^|/)\.[^/]*(|/.*)$
|
|
|
|
--- Directory filter rules ---
|
|
|
|
- (^|/)\.[^/]*(|/.*)$`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
true,
|
|
|
|
"a/b/c/d",
|
|
|
|
`--- File filter rules ---
|
|
|
|
+ (^|/)a/b/c/d$
|
|
|
|
--- Directory filter rules ---
|
|
|
|
+ (^|/)a/b/c/$
|
|
|
|
+ (^|/)a/b/$
|
|
|
|
+ (^|/)a/$`,
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
f, err := NewFilter()
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = f.Add(test.included, test.glob)
|
|
|
|
require.NoError(t, err)
|
|
|
|
got := f.DumpFilters()
|
|
|
|
assert.Equal(t, test.want, got, fmt.Sprintf("Add(%v, %q)", test.included, test.glob))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-27 17:13:20 +02:00
|
|
|
func TestFilterForEachLine(t *testing.T) {
|
|
|
|
file := testFile(t, `; comment
|
|
|
|
one
|
|
|
|
# another comment
|
|
|
|
|
|
|
|
|
|
|
|
two
|
|
|
|
# indented comment
|
|
|
|
three
|
|
|
|
four
|
|
|
|
five
|
|
|
|
six `)
|
2015-11-07 11:25:43 +01:00
|
|
|
defer func() {
|
2016-12-07 14:37:40 +01:00
|
|
|
err := os.Remove(file)
|
2016-06-29 18:59:31 +02:00
|
|
|
require.NoError(t, err)
|
2015-11-07 11:25:43 +01:00
|
|
|
}()
|
2015-09-27 17:13:20 +02:00
|
|
|
lines := []string{}
|
2016-12-07 14:37:40 +01:00
|
|
|
err := forEachLine(file, func(s string) error {
|
2015-09-27 17:13:20 +02:00
|
|
|
lines = append(lines, s)
|
|
|
|
return nil
|
|
|
|
})
|
2016-06-29 18:59:31 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "one,two,three,four,five,six", strings.Join(lines, ","))
|
2015-09-27 17:13:20 +02:00
|
|
|
}
|
2015-10-20 10:16:47 +02:00
|
|
|
|
|
|
|
func TestFilterMatchesFromDocs(t *testing.T) {
|
|
|
|
for _, test := range []struct {
|
|
|
|
glob string
|
|
|
|
included bool
|
|
|
|
file string
|
|
|
|
}{
|
|
|
|
{"file.jpg", true, "file.jpg"},
|
|
|
|
{"file.jpg", true, "directory/file.jpg"},
|
|
|
|
{"file.jpg", false, "afile.jpg"},
|
|
|
|
{"file.jpg", false, "directory/afile.jpg"},
|
|
|
|
{"/file.jpg", true, "file.jpg"},
|
|
|
|
{"/file.jpg", false, "afile.jpg"},
|
|
|
|
{"/file.jpg", false, "directory/file.jpg"},
|
|
|
|
{"*.jpg", true, "file.jpg"},
|
|
|
|
{"*.jpg", true, "directory/file.jpg"},
|
|
|
|
{"*.jpg", false, "file.jpg/anotherfile.png"},
|
|
|
|
{"dir/**", true, "dir/file.jpg"},
|
|
|
|
{"dir/**", true, "dir/dir1/dir2/file.jpg"},
|
|
|
|
{"dir/**", false, "directory/file.jpg"},
|
|
|
|
{"dir/**", false, "adir/file.jpg"},
|
|
|
|
{"l?ss", true, "less"},
|
|
|
|
{"l?ss", true, "lass"},
|
|
|
|
{"l?ss", false, "floss"},
|
|
|
|
{"h[ae]llo", true, "hello"},
|
|
|
|
{"h[ae]llo", true, "hallo"},
|
|
|
|
{"h[ae]llo", false, "hullo"},
|
|
|
|
{"{one,two}_potato", true, "one_potato"},
|
|
|
|
{"{one,two}_potato", true, "two_potato"},
|
|
|
|
{"{one,two}_potato", false, "three_potato"},
|
|
|
|
{"{one,two}_potato", false, "_potato"},
|
|
|
|
{"\\*.jpg", true, "*.jpg"},
|
|
|
|
{"\\\\.jpg", true, "\\.jpg"},
|
|
|
|
{"\\[one\\].jpg", true, "[one].jpg"},
|
|
|
|
} {
|
|
|
|
f, err := NewFilter()
|
2016-05-16 18:14:04 +02:00
|
|
|
require.NoError(t, err)
|
2015-10-20 10:16:47 +02:00
|
|
|
err = f.Add(true, test.glob)
|
2016-05-16 18:14:04 +02:00
|
|
|
require.NoError(t, err)
|
2015-10-20 10:16:47 +02:00
|
|
|
err = f.Add(false, "*")
|
2016-05-16 18:14:04 +02:00
|
|
|
require.NoError(t, err)
|
2015-12-17 12:52:38 +01:00
|
|
|
included := f.Include(test.file, 0, time.Unix(0, 0))
|
2015-10-20 10:16:47 +02:00
|
|
|
if included != test.included {
|
2016-06-29 18:59:31 +02:00
|
|
|
t.Errorf("%q match %q: want %v got %v", test.glob, test.file, test.included, included)
|
2015-10-20 10:16:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|