mirror of
https://github.com/hanzoai/forge.git
synced 2026-10-30 15:00:39 +00:00
Source files keep their SPDX-License-Identifier and Hanzo copyright line; the Gitea, Gogs and LFS Test Server copyrights they used to repeat now live once in NOTICE, alongside LICENSE. The header lint accepts any copyright line above the SPDX line, so it no longer demands an upstream one. Co-authored-by: Hanzo Dev <[email protected]>
134 lines
3.8 KiB
Go
134 lines
3.8 KiB
Go
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package git
|
|
|
|
import (
|
|
"bufio"
|
|
"io"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestParseTreeEntriesLong(t *testing.T) {
|
|
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{
|
|
{
|
|
ID: MustIDFromString("ea0d83c9081af9500ac9f804101b3fd0a5c293af"),
|
|
name: "README.md",
|
|
entryMode: EntryModeBlob,
|
|
size: 8218,
|
|
sized: true,
|
|
},
|
|
{
|
|
ID: MustIDFromString("037f27dc9d353ae4fd50f0474b2194c593914e35"),
|
|
name: "README_ZH.md",
|
|
entryMode: EntryModeBlob,
|
|
size: 4681,
|
|
sized: true,
|
|
},
|
|
{
|
|
ID: MustIDFromString("9846a94f7e8350a916632929d0fda38c90dd2ca8"),
|
|
name: "SECURITY.md",
|
|
entryMode: EntryModeBlob,
|
|
size: 429,
|
|
sized: true,
|
|
},
|
|
{
|
|
ID: MustIDFromString("84b90550547016f73c5dd3f50dea662389e67b6d"),
|
|
name: "assets",
|
|
entryMode: EntryModeTree,
|
|
sized: true,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
for _, testCase := range testCases {
|
|
entries, err := ParseTreeEntries([]byte(testCase.Input))
|
|
assert.NoError(t, err)
|
|
assert.Len(t, entries, len(testCase.Expected))
|
|
for i, entry := range entries {
|
|
assert.Equal(t, testCase.Expected[i], entry)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestParseTreeEntriesShort(t *testing.T) {
|
|
testCases := []struct {
|
|
Input string
|
|
Expected []*TreeEntry
|
|
}{
|
|
{
|
|
Input: `100644 blob ea0d83c9081af9500ac9f804101b3fd0a5c293af README.md
|
|
040000 tree 84b90550547016f73c5dd3f50dea662389e67b6d assets
|
|
`,
|
|
Expected: []*TreeEntry{
|
|
{
|
|
ID: MustIDFromString("ea0d83c9081af9500ac9f804101b3fd0a5c293af"),
|
|
name: "README.md",
|
|
entryMode: EntryModeBlob,
|
|
},
|
|
{
|
|
ID: MustIDFromString("84b90550547016f73c5dd3f50dea662389e67b6d"),
|
|
name: "assets",
|
|
entryMode: EntryModeTree,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
for _, testCase := range testCases {
|
|
entries, err := ParseTreeEntries([]byte(testCase.Input))
|
|
assert.NoError(t, err)
|
|
assert.Len(t, entries, len(testCase.Expected))
|
|
for i, entry := range entries {
|
|
assert.Equal(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
|
|
entries, err := ParseTreeEntries([]byte("100644 blob ea0d83c9081af9500ac9f804101b3fd0a5c293af"))
|
|
assert.Error(t, err)
|
|
assert.Empty(t, entries)
|
|
}
|
|
|
|
func TestParseCatFileTreeLine(t *testing.T) {
|
|
input := "100644 looooooooooooooooooooooooong-file-name.txt\x0012345678901234567890"
|
|
input += "40755 some-directory\x00abcdefg123abcdefg123"
|
|
|
|
var readCount int
|
|
|
|
buf := bufio.NewReaderSize(strings.NewReader(input), 20) // NewReaderSize has a limit: min buffer size = 16
|
|
mode, name, objID, n, err := ParseCatFileTreeLine(Sha1ObjectFormat, buf)
|
|
readCount += n
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, EntryModeBlob, mode)
|
|
assert.Equal(t, "looooooooooooooooooooooooong-file-name.txt", name)
|
|
assert.Equal(t, "12345678901234567890", string(objID.RawValue()))
|
|
|
|
mode, name, objID, n, err = ParseCatFileTreeLine(Sha1ObjectFormat, buf)
|
|
readCount += n
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, EntryModeTree, mode)
|
|
assert.Equal(t, "some-directory", name)
|
|
assert.Equal(t, "abcdefg123abcdefg123", string(objID.RawValue()))
|
|
|
|
assert.Equal(t, len(input), readCount)
|
|
|
|
_, _, _, n, err = ParseCatFileTreeLine(Sha1ObjectFormat, buf)
|
|
assert.ErrorIs(t, err, io.EOF)
|
|
assert.Zero(t, n)
|
|
}
|