mirror of
https://codeberg.org/davrot/forgejo.git
synced 2025-06-24 20:00:02 +02:00

See #8222 for context. ## git.Blob.NewTruncatedReader This introduce a new `NewTruncatedReader` method to return a blob-reader which silently truncates when the limit is reached (io.EOF will be returned). Since the actual size is also returned `GetBlobContent` can pre-allocate a `[]byte` of the full-size (min of the asked size and the actual size) and call `io.ReadFull(rc, buf)` (instead of `util.ReadWithLimit(dataRc, int(limit))` which is convoluted and not used anywhere else). ### Tests - I added test coverage for Go changes... - [x] in their respective `*_test.go` for unit tests. ### Documentation - [x] I did not document these changes and I do not expect someone else to do it. ### Release notes - [x] I do not want this change to show in the release notes. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8223 Reviewed-by: Lucas <sclu1034@noreply.codeberg.org> Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org> Co-authored-by: oliverpool <git@olivier.pfad.fr> Co-committed-by: oliverpool <git@olivier.pfad.fr>
96 lines
2.2 KiB
Go
96 lines
2.2 KiB
Go
// Copyright 2015 The Gogs Authors. All rights reserved.
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package git
|
|
|
|
import (
|
|
"io"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestBlob_Data(t *testing.T) {
|
|
output := "file2\n"
|
|
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
|
|
repo, err := openRepositoryWithDefaultContext(bareRepo1Path)
|
|
require.NoError(t, err)
|
|
|
|
defer repo.Close()
|
|
|
|
testBlob, err := repo.GetBlob("6c493ff740f9380390d5c9ddef4af18697ac9375")
|
|
require.NoError(t, err)
|
|
|
|
r, err := testBlob.DataAsync()
|
|
require.NoError(t, err)
|
|
require.NotNil(t, r)
|
|
|
|
data, err := io.ReadAll(r)
|
|
require.NoError(t, r.Close())
|
|
|
|
require.NoError(t, err)
|
|
assert.Equal(t, output, string(data))
|
|
}
|
|
|
|
func TestBlob_GetBlobContent(t *testing.T) {
|
|
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
|
|
repo, err := openRepositoryWithDefaultContext(bareRepo1Path)
|
|
require.NoError(t, err)
|
|
|
|
defer repo.Close()
|
|
|
|
testBlob, err := repo.GetBlob("6c493ff740f9380390d5c9ddef4af18697ac9375")
|
|
require.NoError(t, err)
|
|
|
|
r, err := testBlob.GetBlobContent(100)
|
|
require.NoError(t, err)
|
|
require.Equal(t, "file2\n", r)
|
|
|
|
r, err = testBlob.GetBlobContent(-1)
|
|
require.NoError(t, err)
|
|
require.Empty(t, r)
|
|
|
|
r, err = testBlob.GetBlobContent(4)
|
|
require.NoError(t, err)
|
|
require.Equal(t, "file", r)
|
|
|
|
r, err = testBlob.GetBlobContent(6)
|
|
require.NoError(t, err)
|
|
require.Equal(t, "file2\n", r)
|
|
|
|
t.Run("non-existing blob", func(t *testing.T) {
|
|
inexistingBlob, err := repo.GetBlob("00003ff740f9380390d5c9ddef4af18690000000")
|
|
require.NoError(t, err)
|
|
|
|
r, err := inexistingBlob.GetBlobContent(100)
|
|
require.Error(t, err)
|
|
require.IsType(t, ErrNotExist{}, err)
|
|
require.Empty(t, r)
|
|
})
|
|
}
|
|
|
|
func Benchmark_Blob_Data(b *testing.B) {
|
|
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
|
|
repo, err := openRepositoryWithDefaultContext(bareRepo1Path)
|
|
if err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
defer repo.Close()
|
|
|
|
testBlob, err := repo.GetBlob("6c493ff740f9380390d5c9ddef4af18697ac9375")
|
|
if err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
r, err := testBlob.DataAsync()
|
|
if err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
io.ReadAll(r)
|
|
_ = r.Close()
|
|
}
|
|
}
|