git-lfs/lfs/util_test.go

120 lines
4.0 KiB
Go
Raw Normal View History

2015-03-19 19:30:55 +00:00
package lfs
2014-08-06 22:04:34 +00:00
import (
"bytes"
"io/ioutil"
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
2015-05-25 18:20:50 +00:00
"github.com/github/git-lfs/vendor/_nuts/github.com/technoweenie/assert"
2014-08-06 22:04:34 +00:00
)
func TestWriterWithCallback(t *testing.T) {
called := 0
calledRead := make([]int64, 0, 2)
2014-08-06 22:04:34 +00:00
reader := &CallbackReader{
2014-08-06 22:04:34 +00:00
TotalSize: 5,
Reader: bytes.NewBufferString("BOOYA"),
C: func(total int64, read int64, current int) error {
2014-08-06 22:04:34 +00:00
called += 1
calledRead = append(calledRead, read)
2014-08-06 22:04:34 +00:00
assert.Equal(t, 5, int(total))
return nil
},
}
readBuf := make([]byte, 3)
n, err := reader.Read(readBuf)
assert.Equal(t, nil, err)
assert.Equal(t, "BOO", string(readBuf[0:n]))
n, err = reader.Read(readBuf)
assert.Equal(t, nil, err)
assert.Equal(t, "YA", string(readBuf[0:n]))
2014-08-06 22:04:34 +00:00
assert.Equal(t, 2, called)
assert.Equal(t, 2, len(calledRead))
assert.Equal(t, 3, int(calledRead[0]))
assert.Equal(t, 5, int(calledRead[1]))
2014-08-06 22:04:34 +00:00
}
func TestCopyWithCallback(t *testing.T) {
buf := bytes.NewBufferString("BOOYA")
called := 0
calledWritten := make([]int64, 0, 2)
n, err := CopyWithCallback(ioutil.Discard, buf, 5, func(total int64, written int64, current int) error {
2014-08-06 22:04:34 +00:00
called += 1
calledWritten = append(calledWritten, written)
assert.Equal(t, 5, int(total))
return nil
})
assert.Equal(t, nil, err)
assert.Equal(t, 5, int(n))
assert.Equal(t, 1, called)
assert.Equal(t, 1, len(calledWritten))
assert.Equal(t, 5, int(calledWritten[0]))
}
2015-07-30 09:57:52 +00:00
type TestIncludeExcludeCase struct {
expectedResult bool
includes []string
excludes []string
}
func TestFilterIncludeExclude(t *testing.T) {
2015-07-30 09:57:52 +00:00
cases := []TestIncludeExcludeCase{
// Null case
TestIncludeExcludeCase{true, nil, nil},
// 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},
// 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*/*"}},
// 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-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)
}
}
}