mirror of
https://github.com/go-gitea/gitea
synced 2024-11-18 21:39:30 +01:00
4eb2a29910
The 4 functions are duplicated, especially as interface methods. I think we just need to keep `MustID` the only one and remove other 3. ``` MustID(b []byte) ObjectID MustIDFromString(s string) ObjectID NewID(b []byte) (ObjectID, error) NewIDFromString(s string) (ObjectID, error) ``` Introduced the new interfrace method `ComputeHash` which will replace the interface `HasherInterface`. Now we don't need to keep two interfaces. Reintroduced `git.NewIDFromString` and `git.MustIDFromString`. The new function will detect the hash length to decide which objectformat of it. If it's 40, then it's SHA1. If it's 64, then it's SHA256. This will be right if the commitID is a full one. So the parameter should be always a full commit id. @AdamMajer Please review.
109 lines
2.9 KiB
Go
109 lines
2.9 KiB
Go
// Copyright 2021 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
//go:build !gogit
|
|
|
|
package git
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestParseTreeEntriesLong(t *testing.T) {
|
|
objectFormat := Sha1ObjectFormat
|
|
|
|
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(objectFormat, []byte(testCase.Input))
|
|
assert.NoError(t, err)
|
|
assert.Len(t, entries, len(testCase.Expected))
|
|
for i, entry := range entries {
|
|
assert.EqualValues(t, testCase.Expected[i], entry)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestParseTreeEntriesShort(t *testing.T) {
|
|
objectFormat := Sha1ObjectFormat
|
|
|
|
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(objectFormat, []byte(testCase.Input))
|
|
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
|
|
entries, err := ParseTreeEntries(Sha1ObjectFormat, []byte("100644 blob ea0d83c9081af9500ac9f804101b3fd0a5c293af"))
|
|
assert.Error(t, err)
|
|
assert.Len(t, entries, 0)
|
|
}
|