2019-03-27 04:33:00 -05:00
|
|
|
// Copyright 2015 The Gogs Authors. All rights reserved.
|
2019-04-19 07:17:27 -05:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2019-03-27 04:33:00 -05:00
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
2021-04-11 10:45:29 -05:00
|
|
|
"path/filepath"
|
2019-03-27 04:33:00 -05:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBlob_Data(t *testing.T) {
|
2021-04-11 10:45:29 -05:00
|
|
|
output := "file2\n"
|
|
|
|
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
|
|
|
|
repo, err := OpenRepository(bareRepo1Path)
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
t.Fatal()
|
|
|
|
}
|
2019-11-13 01:01:19 -06:00
|
|
|
defer repo.Close()
|
|
|
|
|
2021-04-11 10:45:29 -05:00
|
|
|
testBlob, err := repo.GetBlob("6c493ff740f9380390d5c9ddef4af18697ac9375")
|
2019-04-19 07:17:27 -05:00
|
|
|
assert.NoError(t, err)
|
2019-03-27 04:33:00 -05:00
|
|
|
|
2019-04-19 07:17:27 -05:00
|
|
|
r, err := testBlob.DataAsync()
|
2019-03-27 04:33:00 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
require.NotNil(t, r)
|
|
|
|
|
|
|
|
data, err := ioutil.ReadAll(r)
|
2021-05-09 20:27:03 -05:00
|
|
|
assert.NoError(t, r.Close())
|
|
|
|
|
2019-03-27 04:33:00 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, output, string(data))
|
|
|
|
}
|
|
|
|
|
|
|
|
func Benchmark_Blob_Data(b *testing.B) {
|
2021-04-11 10:45:29 -05:00
|
|
|
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
|
|
|
|
repo, err := OpenRepository(bareRepo1Path)
|
2019-04-19 07:17:27 -05:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
2019-11-13 01:01:19 -06:00
|
|
|
defer repo.Close()
|
|
|
|
|
2021-04-11 10:45:29 -05:00
|
|
|
testBlob, err := repo.GetBlob("6c493ff740f9380390d5c9ddef4af18697ac9375")
|
2019-04-19 07:17:27 -05:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
2019-03-27 04:33:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2019-04-19 07:17:27 -05:00
|
|
|
r, err := testBlob.DataAsync()
|
|
|
|
if err != nil {
|
2019-03-27 04:33:00 -05:00
|
|
|
b.Fatal(err)
|
|
|
|
}
|
2019-04-19 07:17:27 -05:00
|
|
|
ioutil.ReadAll(r)
|
2021-05-09 20:27:03 -05:00
|
|
|
_ = r.Close()
|
2019-03-27 04:33:00 -05:00
|
|
|
}
|
|
|
|
}
|