2019-03-27 12:33:00 +03:00
|
|
|
// Copyright 2015 The Gogs Authors. All rights reserved.
|
2019-04-19 15:17:27 +03:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 21:20:29 +03:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-03-27 12:33:00 +03:00
|
|
|
|
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
2021-09-22 08:38:34 +03:00
|
|
|
"io"
|
2021-04-11 18:45:29 +03:00
|
|
|
"path/filepath"
|
2019-03-27 12:33:00 +03:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBlob_Data(t *testing.T) {
|
2021-04-11 18:45:29 +03:00
|
|
|
output := "file2\n"
|
|
|
|
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
|
2022-03-29 22:13:41 +03:00
|
|
|
repo, err := openRepositoryWithDefaultContext(bareRepo1Path)
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-11-13 10:01:19 +03:00
|
|
|
defer repo.Close()
|
|
|
|
|
2021-04-11 18:45:29 +03:00
|
|
|
testBlob, err := repo.GetBlob("6c493ff740f9380390d5c9ddef4af18697ac9375")
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2019-03-27 12:33:00 +03:00
|
|
|
|
2019-04-19 15:17:27 +03:00
|
|
|
r, err := testBlob.DataAsync()
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2019-03-27 12:33:00 +03:00
|
|
|
require.NotNil(t, r)
|
|
|
|
|
2021-09-22 08:38:34 +03:00
|
|
|
data, err := io.ReadAll(r)
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, r.Close())
|
2021-05-10 04:27:03 +03:00
|
|
|
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2019-03-27 12:33:00 +03:00
|
|
|
assert.Equal(t, output, string(data))
|
|
|
|
}
|
|
|
|
|
|
|
|
func Benchmark_Blob_Data(b *testing.B) {
|
2021-04-11 18:45:29 +03:00
|
|
|
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
|
2022-03-29 22:13:41 +03:00
|
|
|
repo, err := openRepositoryWithDefaultContext(bareRepo1Path)
|
2019-04-19 15:17:27 +03:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
2019-11-13 10:01:19 +03:00
|
|
|
defer repo.Close()
|
|
|
|
|
2021-04-11 18:45:29 +03:00
|
|
|
testBlob, err := repo.GetBlob("6c493ff740f9380390d5c9ddef4af18697ac9375")
|
2019-04-19 15:17:27 +03:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
2019-03-27 12:33:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2019-04-19 15:17:27 +03:00
|
|
|
r, err := testBlob.DataAsync()
|
|
|
|
if err != nil {
|
2019-03-27 12:33:00 +03:00
|
|
|
b.Fatal(err)
|
|
|
|
}
|
2021-09-22 08:38:34 +03:00
|
|
|
io.ReadAll(r)
|
2021-05-10 04:27:03 +03:00
|
|
|
_ = r.Close()
|
2019-03-27 12:33:00 +03:00
|
|
|
}
|
|
|
|
}
|