2015-03-19 19:30:55 +00:00
|
|
|
package lfs
|
2014-08-06 22:04:34 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2015-07-30 09:57:52 +00:00
|
|
|
"strings"
|
2014-08-06 22:04:34 +00:00
|
|
|
"testing"
|
2015-05-13 19:43:41 +00:00
|
|
|
|
2016-05-06 14:04:47 +00:00
|
|
|
"github.com/github/git-lfs/progress"
|
2016-05-25 16:43:10 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2014-08-06 22:04:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestWriterWithCallback(t *testing.T) {
|
|
|
|
called := 0
|
2014-08-07 17:27:06 +00:00
|
|
|
calledRead := make([]int64, 0, 2)
|
2014-08-06 22:04:34 +00:00
|
|
|
|
2016-05-06 14:04:47 +00:00
|
|
|
reader := &progress.CallbackReader{
|
2014-08-06 22:04:34 +00:00
|
|
|
TotalSize: 5,
|
2014-08-07 17:27:06 +00:00
|
|
|
Reader: bytes.NewBufferString("BOOYA"),
|
2015-04-24 15:56:22 +00:00
|
|
|
C: func(total int64, read int64, current int) error {
|
2014-08-06 22:04:34 +00:00
|
|
|
called += 1
|
2014-08-07 17:27:06 +00:00
|
|
|
calledRead = append(calledRead, read)
|
2014-08-06 22:04:34 +00:00
|
|
|
assert.Equal(t, 5, int(total))
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2014-08-07 17:27:06 +00:00
|
|
|
readBuf := make([]byte, 3)
|
|
|
|
n, err := reader.Read(readBuf)
|
2016-05-25 16:43:10 +00:00
|
|
|
assert.Nil(t, err)
|
2014-08-07 17:27:06 +00:00
|
|
|
assert.Equal(t, "BOO", string(readBuf[0:n]))
|
|
|
|
|
|
|
|
n, err = reader.Read(readBuf)
|
2016-05-25 16:43:10 +00:00
|
|
|
assert.Nil(t, err)
|
2014-08-07 17:27:06 +00:00
|
|
|
assert.Equal(t, "YA", string(readBuf[0:n]))
|
2014-08-06 22:04:34 +00:00
|
|
|
|
|
|
|
assert.Equal(t, 2, called)
|
2016-05-25 16:43:10 +00:00
|
|
|
assert.Len(t, calledRead, 2)
|
2014-08-07 17:27:06 +00:00
|
|
|
assert.Equal(t, 3, int(calledRead[0]))
|
|
|
|
assert.Equal(t, 5, int(calledRead[1]))
|
2014-08-06 22:04:34 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 09:57:52 +00:00
|
|
|
type TestIncludeExcludeCase struct {
|
|
|
|
expectedResult bool
|
|
|
|
includes []string
|
|
|
|
excludes []string
|
|
|
|
}
|
|
|
|
|
2015-07-28 11:59:53 +00:00
|
|
|
func TestFilterIncludeExclude(t *testing.T) {
|
|
|
|
|
2015-07-30 09:57:52 +00:00
|
|
|
cases := []TestIncludeExcludeCase{
|
|
|
|
// Null case
|
|
|
|
TestIncludeExcludeCase{true, nil, nil},
|
2015-07-28 11:59:53 +00:00
|
|
|
// Inclusion
|
2015-07-30 09:57:52 +00:00
|
|
|
TestIncludeExcludeCase{true, []string{"test/filename.dat"}, nil},
|
|
|
|
TestIncludeExcludeCase{true, []string{"test/filename.dat"}, nil},
|
|
|
|
TestIncludeExcludeCase{false, []string{"blank", "something", "foo"}, nil},
|
|
|
|
TestIncludeExcludeCase{false, []string{"test/notfilename.dat"}, nil},
|
|
|
|
TestIncludeExcludeCase{true, []string{"test"}, nil},
|
|
|
|
TestIncludeExcludeCase{true, []string{"test/*"}, nil},
|
|
|
|
TestIncludeExcludeCase{false, []string{"nottest"}, nil},
|
|
|
|
TestIncludeExcludeCase{false, []string{"nottest/*"}, nil},
|
|
|
|
TestIncludeExcludeCase{true, []string{"test/fil*"}, nil},
|
|
|
|
TestIncludeExcludeCase{false, []string{"test/g*"}, nil},
|
|
|
|
TestIncludeExcludeCase{true, []string{"tes*/*"}, nil},
|
2015-07-28 11:59:53 +00:00
|
|
|
// Exclusion
|
2015-07-30 09:57:52 +00:00
|
|
|
TestIncludeExcludeCase{false, nil, []string{"test/filename.dat"}},
|
|
|
|
TestIncludeExcludeCase{false, nil, []string{"blank", "something", "test/filename.dat", "foo"}},
|
|
|
|
TestIncludeExcludeCase{true, nil, []string{"blank", "something", "foo"}},
|
|
|
|
TestIncludeExcludeCase{true, nil, []string{"test/notfilename.dat"}},
|
|
|
|
TestIncludeExcludeCase{false, nil, []string{"test"}},
|
|
|
|
TestIncludeExcludeCase{false, nil, []string{"test/*"}},
|
|
|
|
TestIncludeExcludeCase{true, nil, []string{"nottest"}},
|
|
|
|
TestIncludeExcludeCase{true, nil, []string{"nottest/*"}},
|
|
|
|
TestIncludeExcludeCase{false, nil, []string{"test/fil*"}},
|
|
|
|
TestIncludeExcludeCase{true, nil, []string{"test/g*"}},
|
|
|
|
TestIncludeExcludeCase{false, nil, []string{"tes*/*"}},
|
2015-07-28 11:59:53 +00:00
|
|
|
|
|
|
|
// Both
|
2015-07-30 09:57:52 +00:00
|
|
|
TestIncludeExcludeCase{true, []string{"test/filename.dat"}, []string{"test/notfilename.dat"}},
|
|
|
|
TestIncludeExcludeCase{false, []string{"test"}, []string{"test/filename.dat"}},
|
|
|
|
TestIncludeExcludeCase{true, []string{"test/*"}, []string{"test/notfile*"}},
|
|
|
|
TestIncludeExcludeCase{false, []string{"test/*"}, []string{"test/file*"}},
|
|
|
|
TestIncludeExcludeCase{false, []string{"another/*", "test/*"}, []string{"test/notfilename.dat", "test/filename.dat"}},
|
|
|
|
}
|
2015-07-28 11:59:53 +00:00
|
|
|
|
2015-07-30 09:57:52 +00:00
|
|
|
for _, c := range cases {
|
|
|
|
assert.Equal(t, c.expectedResult, FilenamePassesIncludeExcludeFilter("test/filename.dat", c.includes, c.excludes), c)
|
|
|
|
if IsWindows() {
|
|
|
|
// also test with \ path separators, tolerate mixed separators
|
|
|
|
for i, inc := range c.includes {
|
|
|
|
c.includes[i] = strings.Replace(inc, "/", "\\", -1)
|
|
|
|
}
|
|
|
|
for i, ex := range c.excludes {
|
|
|
|
c.excludes[i] = strings.Replace(ex, "/", "\\", -1)
|
|
|
|
}
|
|
|
|
assert.Equal(t, c.expectedResult, FilenamePassesIncludeExcludeFilter("test/filename.dat", c.includes, c.excludes), c)
|
|
|
|
}
|
2015-07-28 11:59:53 +00:00
|
|
|
}
|
|
|
|
}
|