2021-02-18 05:32:25 +08:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-28 02:20:29 +08:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-02-18 05:32:25 +08:00
|
|
|
|
2021-08-25 00:47:09 +08:00
|
|
|
//go:build !gogit
|
2021-02-18 05:32:25 +08:00
|
|
|
|
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2022-10-08 01:20:53 +08:00
|
|
|
func TestParseTreeEntriesLong(t *testing.T) {
|
2023-12-17 19:56:08 +08:00
|
|
|
objectFormat := Sha1ObjectFormat
|
2023-12-14 05:02:00 +08:00
|
|
|
|
2021-02-18 05:32:25 +08:00
|
|
|
testCases := []struct {
|
|
|
|
Input string
|
|
|
|
Expected []*TreeEntry
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Input: `100644 blob ea0d83c9081af9500ac9f804101b3fd0a5c293af 8218 README.md
|
|
|
|
100644 blob 037f27dc9d353ae4fd50f0474b2194c593914e35 4681 README_ZH.md
|
|
|
|
100644 blob 9846a94f7e8350a916632929d0fda38c90dd2ca8 429 SECURITY.md
|
|
|
|
040000 tree 84b90550547016f73c5dd3f50dea662389e67b6d - assets
|
|
|
|
`,
|
|
|
|
Expected: []*TreeEntry{
|
|
|
|
{
|
2023-12-14 05:02:00 +08:00
|
|
|
ID: objectFormat.MustIDFromString("ea0d83c9081af9500ac9f804101b3fd0a5c293af"),
|
2021-02-18 05:32:25 +08:00
|
|
|
name: "README.md",
|
|
|
|
entryMode: EntryModeBlob,
|
|
|
|
size: 8218,
|
|
|
|
sized: true,
|
|
|
|
},
|
|
|
|
{
|
2023-12-14 05:02:00 +08:00
|
|
|
ID: objectFormat.MustIDFromString("037f27dc9d353ae4fd50f0474b2194c593914e35"),
|
2021-02-18 05:32:25 +08:00
|
|
|
name: "README_ZH.md",
|
|
|
|
entryMode: EntryModeBlob,
|
|
|
|
size: 4681,
|
|
|
|
sized: true,
|
|
|
|
},
|
|
|
|
{
|
2023-12-14 05:02:00 +08:00
|
|
|
ID: objectFormat.MustIDFromString("9846a94f7e8350a916632929d0fda38c90dd2ca8"),
|
2021-02-18 05:32:25 +08:00
|
|
|
name: "SECURITY.md",
|
|
|
|
entryMode: EntryModeBlob,
|
|
|
|
size: 429,
|
|
|
|
sized: true,
|
|
|
|
},
|
|
|
|
{
|
2023-12-14 05:02:00 +08:00
|
|
|
ID: objectFormat.MustIDFromString("84b90550547016f73c5dd3f50dea662389e67b6d"),
|
2021-02-18 05:32:25 +08:00
|
|
|
name: "assets",
|
|
|
|
entryMode: EntryModeTree,
|
|
|
|
sized: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, testCase := range testCases {
|
2023-12-14 05:02:00 +08:00
|
|
|
entries, err := ParseTreeEntries(objectFormat, []byte(testCase.Input))
|
2021-02-18 05:32:25 +08:00
|
|
|
assert.NoError(t, err)
|
2021-06-07 13:27:09 +08:00
|
|
|
assert.Len(t, entries, len(testCase.Expected))
|
2021-02-18 05:32:25 +08:00
|
|
|
for i, entry := range entries {
|
2022-10-08 01:20:53 +08:00
|
|
|
assert.EqualValues(t, testCase.Expected[i], entry)
|
2021-02-18 05:32:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-08 01:20:53 +08:00
|
|
|
|
|
|
|
func TestParseTreeEntriesShort(t *testing.T) {
|
2023-12-17 19:56:08 +08:00
|
|
|
objectFormat := Sha1ObjectFormat
|
2023-12-14 05:02:00 +08:00
|
|
|
|
2022-10-08 01:20:53 +08:00
|
|
|
testCases := []struct {
|
|
|
|
Input string
|
|
|
|
Expected []*TreeEntry
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Input: `100644 blob ea0d83c9081af9500ac9f804101b3fd0a5c293af README.md
|
|
|
|
040000 tree 84b90550547016f73c5dd3f50dea662389e67b6d assets
|
|
|
|
`,
|
|
|
|
Expected: []*TreeEntry{
|
|
|
|
{
|
2023-12-14 05:02:00 +08:00
|
|
|
ID: objectFormat.MustIDFromString("ea0d83c9081af9500ac9f804101b3fd0a5c293af"),
|
2022-10-08 01:20:53 +08:00
|
|
|
name: "README.md",
|
|
|
|
entryMode: EntryModeBlob,
|
|
|
|
},
|
|
|
|
{
|
2023-12-14 05:02:00 +08:00
|
|
|
ID: objectFormat.MustIDFromString("84b90550547016f73c5dd3f50dea662389e67b6d"),
|
2022-10-08 01:20:53 +08:00
|
|
|
name: "assets",
|
|
|
|
entryMode: EntryModeTree,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, testCase := range testCases {
|
2023-12-14 05:02:00 +08:00
|
|
|
entries, err := ParseTreeEntries(objectFormat, []byte(testCase.Input))
|
2022-10-08 01:20:53 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, entries, len(testCase.Expected))
|
|
|
|
for i, entry := range entries {
|
|
|
|
assert.EqualValues(t, testCase.Expected[i], entry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseTreeEntriesInvalid(t *testing.T) {
|
|
|
|
// there was a panic: "runtime error: slice bounds out of range" when the input was invalid: #20315
|
2023-12-17 19:56:08 +08:00
|
|
|
entries, err := ParseTreeEntries(Sha1ObjectFormat, []byte("100644 blob ea0d83c9081af9500ac9f804101b3fd0a5c293af"))
|
2022-10-08 01:20:53 +08:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Len(t, entries, 0)
|
|
|
|
}
|