mirror of
https://github.com/go-gitea/gitea.git
synced 2026-10-28 16:47:01 +09:00
test: consolidate Go tests (#39962)
Consolidate all Go tests. Coverage is identical to before. Go tests are around 5% faster while being 7% smaller. No non-test code is touched.
This commit is contained in:
1 parent
ce7364e215
commit
8abda62312
388 files changed
+18410
-29561
No files matched your search
@@ -10,65 +10,36 @@ import (
|
||||
)
|
||||
|
||||
func TestDiffSliceBasic(t *testing.T) {
|
||||
// Typical integer cases
|
||||
t.Run("additions", func(t *testing.T) {
|
||||
added, removed := DiffSlice([]int{1, 2}, []int{1, 2, 3})
|
||||
assert.Equal(t, []int{3}, added)
|
||||
assert.Empty(t, removed)
|
||||
})
|
||||
|
||||
t.Run("removals", func(t *testing.T) {
|
||||
added, removed := DiffSlice([]int{1, 2, 3}, []int{1, 2})
|
||||
assert.Empty(t, added)
|
||||
assert.Equal(t, []int{3}, removed)
|
||||
})
|
||||
|
||||
t.Run("no changes", func(t *testing.T) {
|
||||
added, removed := DiffSlice([]int{1, 2}, []int{1, 2})
|
||||
assert.Empty(t, added)
|
||||
assert.Empty(t, removed)
|
||||
})
|
||||
|
||||
t.Run("empty slices", func(t *testing.T) {
|
||||
added, removed := DiffSlice([]int{}, []int{})
|
||||
assert.Empty(t, added)
|
||||
assert.Empty(t, removed)
|
||||
})
|
||||
|
||||
t.Run("overlapping elements", func(t *testing.T) {
|
||||
added, removed := DiffSlice([]int{1, 2, 4}, []int{2, 3, 4})
|
||||
assert.Equal(t, []int{3}, added)
|
||||
assert.Equal(t, []int{1}, removed)
|
||||
})
|
||||
}
|
||||
|
||||
func TestDiffSliceOrderAndDuplicates(t *testing.T) {
|
||||
oldSlice := []int{1, 2, 2, 3}
|
||||
newSlice := []int{2, 4, 2, 5}
|
||||
|
||||
added, removed := DiffSlice(oldSlice, newSlice)
|
||||
assert.Equal(t, []int{4, 5}, added)
|
||||
assert.Equal(t, []int{1, 3}, removed)
|
||||
}
|
||||
|
||||
func TestDiffSliceDeduplicatesOutput(t *testing.T) {
|
||||
// Test case from issue: newSlice contains [4, 4, 5] and oldSlice is [1]
|
||||
// added should return [4, 5], not [4, 4, 5]
|
||||
t.Run("deduplicates added", func(t *testing.T) {
|
||||
added, removed := DiffSlice([]int{1}, []int{4, 4, 5})
|
||||
assert.Equal(t, []int{4, 5}, added)
|
||||
assert.Equal(t, []int{1}, removed)
|
||||
})
|
||||
|
||||
t.Run("deduplicates removed", func(t *testing.T) {
|
||||
added, removed := DiffSlice([]int{1, 1, 2}, []int{3})
|
||||
assert.Equal(t, []int{3}, added)
|
||||
assert.Equal(t, []int{1, 2}, removed)
|
||||
})
|
||||
|
||||
t.Run("deduplicates both", func(t *testing.T) {
|
||||
added, removed := DiffSlice([]int{1, 1, 2, 2}, []int{3, 3, 4, 4})
|
||||
assert.Equal(t, []int{3, 4}, added)
|
||||
assert.Equal(t, []int{1, 2}, removed)
|
||||
})
|
||||
for _, testCase := range []struct {
|
||||
name string
|
||||
oldSlice, newSlice []int
|
||||
expectedAdded, expectedRemoved []int
|
||||
}{
|
||||
// Typical integer cases
|
||||
{"additions", []int{1, 2}, []int{1, 2, 3}, []int{3}, nil},
|
||||
{"removals", []int{1, 2, 3}, []int{1, 2}, nil, []int{3}},
|
||||
{"no changes", []int{1, 2}, []int{1, 2}, nil, nil},
|
||||
{"empty slices", []int{}, []int{}, nil, nil},
|
||||
{"overlapping elements", []int{1, 2, 4}, []int{2, 3, 4}, []int{3}, []int{1}},
|
||||
{"order and duplicates", []int{1, 2, 2, 3}, []int{2, 4, 2, 5}, []int{4, 5}, []int{1, 3}},
|
||||
// Test case from issue: newSlice contains [4, 4, 5] and oldSlice is [1]
|
||||
// added should return [4, 5], not [4, 4, 5]
|
||||
{"deduplicates added", []int{1}, []int{4, 4, 5}, []int{4, 5}, []int{1}},
|
||||
{"deduplicates removed", []int{1, 1, 2}, []int{3}, []int{3}, []int{1, 2}},
|
||||
{"deduplicates both", []int{1, 1, 2, 2}, []int{3, 3, 4, 4}, []int{3, 4}, []int{1, 2}},
|
||||
} {
|
||||
t.Run(testCase.name, func(t *testing.T) {
|
||||
added, removed := DiffSlice(testCase.oldSlice, testCase.newSlice)
|
||||
if testCase.expectedAdded == nil {
|
||||
assert.Empty(t, added)
|
||||
} else {
|
||||
assert.Equal(t, testCase.expectedAdded, added)
|
||||
}
|
||||
if testCase.expectedRemoved == nil {
|
||||
assert.Empty(t, removed)
|
||||
} else {
|
||||
assert.Equal(t, testCase.expectedRemoved, removed)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -12,24 +12,16 @@ import (
|
||||
)
|
||||
|
||||
func TestFileBackedBuffer(t *testing.T) {
|
||||
cases := []struct {
|
||||
MaxMemorySize int
|
||||
Data string
|
||||
}{
|
||||
{5, "test"},
|
||||
{5, "testtest"},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
buf := New(c.MaxMemorySize, t.TempDir())
|
||||
_, err := io.Copy(buf, strings.NewReader(c.Data))
|
||||
for _, content := range []string{"test", "testtest"} {
|
||||
buf := New(5, t.TempDir())
|
||||
_, err := io.Copy(buf, strings.NewReader(content))
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.EqualValues(t, len(c.Data), buf.Size())
|
||||
assert.EqualValues(t, len(content), buf.Size())
|
||||
|
||||
data, err := io.ReadAll(buf)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, c.Data, string(data))
|
||||
assert.Equal(t, content, string(data))
|
||||
|
||||
assert.NoError(t, buf.Close())
|
||||
}
|
||||
|
||||
+15
-20
@@ -26,28 +26,23 @@ func TestReadWithLimit(t *testing.T) {
|
||||
bs := []byte("0123456789abcdef")
|
||||
|
||||
// normal test
|
||||
buf, err := readWithLimit(bytes.NewBuffer(bs), 5, 2)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, []byte("01"), buf)
|
||||
|
||||
buf, err = readWithLimit(bytes.NewBuffer(bs), 5, 5)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, []byte("01234"), buf)
|
||||
|
||||
buf, err = readWithLimit(bytes.NewBuffer(bs), 5, 6)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, []byte("012345"), buf)
|
||||
|
||||
buf, err = readWithLimit(bytes.NewBuffer(bs), 5, len(bs))
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, []byte("0123456789abcdef"), buf)
|
||||
|
||||
buf, err = readWithLimit(bytes.NewBuffer(bs), 5, 100)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, []byte("0123456789abcdef"), buf)
|
||||
for _, testCase := range []struct {
|
||||
limit int
|
||||
expected string
|
||||
}{
|
||||
{2, "01"},
|
||||
{5, "01234"},
|
||||
{6, "012345"},
|
||||
{len(bs), "0123456789abcdef"},
|
||||
{100, "0123456789abcdef"},
|
||||
} {
|
||||
buf, err := readWithLimit(bytes.NewBuffer(bs), 5, testCase.limit)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, []byte(testCase.expected), buf)
|
||||
}
|
||||
|
||||
// test with error
|
||||
buf, err = readWithLimit(&readerWithError{bytes.NewBuffer(bs)}, 5, 10)
|
||||
buf, err := readWithLimit(&readerWithError{bytes.NewBuffer(bs)}, 5, 10)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, []byte("0123456789"), buf)
|
||||
|
||||
|
||||
@@ -25,11 +25,6 @@ func TestKeygen(t *testing.T) {
|
||||
|
||||
assert.Regexp(t, "^-----BEGIN RSA PRIVATE KEY-----.*", priv)
|
||||
assert.Regexp(t, "^-----BEGIN PUBLIC KEY-----.*", pub)
|
||||
}
|
||||
|
||||
func TestSignUsingKeys(t *testing.T) {
|
||||
priv, pub, err := GenerateKeyPair(2048)
|
||||
assert.NoError(t, err)
|
||||
|
||||
privPem, _ := pem.Decode([]byte(priv))
|
||||
if privPem == nil || privPem.Type != "RSA PRIVATE KEY" {
|
||||
|
||||
@@ -159,34 +159,19 @@ func TestCleanPath(t *testing.T) {
|
||||
{[]string{`a`}, `a`},
|
||||
{[]string{`/a/`}, `a`},
|
||||
{[]string{`../a/`, `../b`, `c/..`, `d`}, `a/b/d`},
|
||||
{[]string{`a\..\b`}, `a\..\b`},
|
||||
{[]string{`a`, ``, `b`}, `a/b`},
|
||||
{[]string{`a`, `..`, `b`}, `a/b`},
|
||||
{[]string{`lfs`, `repo/..`, `user/../path`}, `lfs/path`},
|
||||
}
|
||||
for _, c := range cases {
|
||||
assert.Equal(t, c.expected, PathJoinRel(c.elems...), "case: %v", c.elems)
|
||||
}
|
||||
|
||||
cases = []struct {
|
||||
elems []string
|
||||
expected string
|
||||
}{
|
||||
{[]string{}, ``},
|
||||
{[]string{``}, ``},
|
||||
{[]string{`..`}, `.`},
|
||||
{[]string{`a`}, `a`},
|
||||
{[]string{`/a/`}, `a`},
|
||||
{[]string{`../a/`, `../b`, `c/..`, `d`}, `a/b/d`},
|
||||
{[]string{`a\..\b`}, `b`},
|
||||
{[]string{`a`, ``, `b`}, `a/b`},
|
||||
{[]string{`a`, `..`, `b`}, `a/b`},
|
||||
{[]string{`lfs`, `repo/..`, `user/../path`}, `lfs/path`},
|
||||
}
|
||||
for _, c := range cases {
|
||||
assert.Equal(t, c.expected, PathJoinRelX(c.elems...), "case: %v", c.elems)
|
||||
}
|
||||
|
||||
backslashPath := []string{`a\..\b`}
|
||||
assert.Equal(t, `a\..\b`, PathJoinRel(backslashPath...), "case: %v", backslashPath)
|
||||
assert.Equal(t, `b`, PathJoinRelX(backslashPath...), "case: %v", backslashPath)
|
||||
|
||||
// for POSIX only, but the result is similar on Windows, because the first element must be an absolute path
|
||||
if consts.IsWindows {
|
||||
cases = []struct {
|
||||
|
||||
@@ -27,7 +27,6 @@ func TestSliceSortedEqual(t *testing.T) {
|
||||
assert.True(t, SliceSortedEqual([]int{}, []int{}))
|
||||
assert.True(t, SliceSortedEqual([]int(nil), nil))
|
||||
assert.True(t, SliceSortedEqual([]int(nil), []int{}))
|
||||
assert.True(t, SliceSortedEqual([]int{}, []int{}))
|
||||
|
||||
assert.True(t, SliceSortedEqual([]string{"2", "0", "2", "3"}, []string{"2", "0", "2", "3"}))
|
||||
assert.True(t, SliceSortedEqual([]float64{2, 0, 2, 3}, []float64{2, 0, 2, 3}))
|
||||
@@ -47,7 +46,6 @@ func TestSliceRemoveAll(t *testing.T) {
|
||||
assert.ElementsMatch(t, []int{2, 0, 2, 3}, SliceRemoveAll([]int{2, 0, 2, 3}, 4))
|
||||
assert.Empty(t, SliceRemoveAll([]int{}, 0))
|
||||
assert.ElementsMatch(t, []int(nil), SliceRemoveAll([]int(nil), 0))
|
||||
assert.Empty(t, SliceRemoveAll([]int{}, 0))
|
||||
|
||||
assert.ElementsMatch(t, []string{"2", "2", "3"}, SliceRemoveAll([]string{"2", "0", "2", "3"}, "0"))
|
||||
assert.ElementsMatch(t, []float64{2, 2, 3}, SliceRemoveAll([]float64{2, 0, 2, 3}, 0))
|
||||
|
||||
+25
-34
@@ -10,40 +10,31 @@ import (
|
||||
)
|
||||
|
||||
func TestOnceValue(t *testing.T) {
|
||||
t.Run("RepeatCall", func(t *testing.T) {
|
||||
callCount := 0
|
||||
o := OnceValue[int]{Func: func() int {
|
||||
callCount++
|
||||
return 42
|
||||
}}
|
||||
assert.Equal(t, 42, o.Value())
|
||||
assert.Equal(t, 42, o.Value())
|
||||
assert.Equal(t, 1, callCount)
|
||||
o.Reset()
|
||||
assert.Equal(t, 42, o.Value())
|
||||
assert.Equal(t, 2, callCount)
|
||||
assert.Equal(t, 42, o.Value())
|
||||
assert.Equal(t, 2, callCount)
|
||||
})
|
||||
|
||||
t.Run("Panic", func(t *testing.T) {
|
||||
callCount := 0
|
||||
doPanic := true
|
||||
o := OnceValue[int]{Func: func() int {
|
||||
callCount++
|
||||
for _, name := range []string{"RepeatCall", "Panic"} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
callCount := 0
|
||||
doPanic := name == "Panic"
|
||||
o := OnceValue[int]{Func: func() int {
|
||||
callCount++
|
||||
if doPanic {
|
||||
panic("some error")
|
||||
}
|
||||
return 42
|
||||
}}
|
||||
if doPanic {
|
||||
panic("some error")
|
||||
assert.PanicsWithValue(t, "some error", func() { o.Value() })
|
||||
assert.PanicsWithValue(t, "some error", func() { o.Value() })
|
||||
} else {
|
||||
assert.Equal(t, 42, o.Value())
|
||||
assert.Equal(t, 42, o.Value())
|
||||
}
|
||||
return 42
|
||||
}}
|
||||
assert.PanicsWithValue(t, "some error", func() { o.Value() })
|
||||
assert.PanicsWithValue(t, "some error", func() { o.Value() })
|
||||
assert.Equal(t, 1, callCount)
|
||||
doPanic = false
|
||||
o.Reset()
|
||||
assert.Equal(t, 42, o.Value())
|
||||
assert.Equal(t, 2, callCount)
|
||||
assert.Equal(t, 42, o.Value())
|
||||
assert.Equal(t, 2, callCount)
|
||||
})
|
||||
assert.Equal(t, 1, callCount)
|
||||
doPanic = false
|
||||
o.Reset()
|
||||
assert.Equal(t, 42, o.Value())
|
||||
assert.Equal(t, 2, callCount)
|
||||
assert.Equal(t, 42, o.Value())
|
||||
assert.Equal(t, 2, callCount)
|
||||
})
|
||||
}
|
||||
}
|
||||
+10
-29
@@ -40,26 +40,13 @@ func Test_NormalizeEOL(t *testing.T) {
|
||||
"Ends without EOLtoo.",
|
||||
}
|
||||
|
||||
buildEOLData := func(data []string, eol string) []byte {
|
||||
return []byte(strings.Join(data, eol))
|
||||
for _, data := range [][]string{data1, data2} {
|
||||
unix := []byte(strings.Join(data, "\n"))
|
||||
for _, input := range [][]byte{[]byte(strings.Join(data, "\r\n")), []byte(strings.Join(data, "\r")), unix} {
|
||||
assert.Equal(t, unix, NormalizeEOL(input))
|
||||
}
|
||||
}
|
||||
|
||||
dos := buildEOLData(data1, "\r\n")
|
||||
unix := buildEOLData(data1, "\n")
|
||||
mac := buildEOLData(data1, "\r")
|
||||
|
||||
assert.Equal(t, unix, NormalizeEOL(dos))
|
||||
assert.Equal(t, unix, NormalizeEOL(mac))
|
||||
assert.Equal(t, unix, NormalizeEOL(unix))
|
||||
|
||||
dos = buildEOLData(data2, "\r\n")
|
||||
unix = buildEOLData(data2, "\n")
|
||||
mac = buildEOLData(data2, "\r")
|
||||
|
||||
assert.Equal(t, unix, NormalizeEOL(dos))
|
||||
assert.Equal(t, unix, NormalizeEOL(mac))
|
||||
assert.Equal(t, unix, NormalizeEOL(unix))
|
||||
|
||||
assert.Equal(t, []byte("one liner"), NormalizeEOL([]byte("one liner")))
|
||||
assert.Equal(t, []byte("\n"), NormalizeEOL([]byte("\n")))
|
||||
assert.Equal(t, []byte("\ntwo liner"), NormalizeEOL([]byte("\ntwo liner")))
|
||||
@@ -103,17 +90,11 @@ func Test_RandomString(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_RandomBytes(t *testing.T) {
|
||||
bytes1 := CryptoRandomBytes(32)
|
||||
|
||||
bytes2 := CryptoRandomBytes(32)
|
||||
|
||||
assert.NotEqual(t, bytes1, bytes2)
|
||||
|
||||
bytes3 := CryptoRandomBytes(256)
|
||||
|
||||
bytes4 := CryptoRandomBytes(256)
|
||||
|
||||
assert.NotEqual(t, bytes3, bytes4)
|
||||
for _, length := range []int64{32, 256} {
|
||||
bytes1 := CryptoRandomBytes(length)
|
||||
bytes2 := CryptoRandomBytes(length)
|
||||
assert.NotEqual(t, bytes1, bytes2)
|
||||
}
|
||||
}
|
||||
|
||||
// Test case for any function which accepts and returns a single string.
|
||||
|
||||
Reference in new issue
Block a user