1// Copyright 2015 The Gogs Authors. All rights reserved. 2// Copyright 2019 The Gitea Authors. All rights reserved. 3// Use of this source code is governed by a MIT-style 4// license that can be found in the LICENSE file. 5 6package git 7 8import ( 9 "io" 10 "path/filepath" 11 "testing" 12 13 "github.com/stretchr/testify/assert" 14 "github.com/stretchr/testify/require" 15) 16 17func TestBlob_Data(t *testing.T) { 18 output := "file2\n" 19 bareRepo1Path := filepath.Join(testReposDir, "repo1_bare") 20 repo, err := OpenRepository(bareRepo1Path) 21 if !assert.NoError(t, err) { 22 t.Fatal() 23 } 24 defer repo.Close() 25 26 testBlob, err := repo.GetBlob("6c493ff740f9380390d5c9ddef4af18697ac9375") 27 assert.NoError(t, err) 28 29 r, err := testBlob.DataAsync() 30 assert.NoError(t, err) 31 require.NotNil(t, r) 32 33 data, err := io.ReadAll(r) 34 assert.NoError(t, r.Close()) 35 36 assert.NoError(t, err) 37 assert.Equal(t, output, string(data)) 38} 39 40func Benchmark_Blob_Data(b *testing.B) { 41 bareRepo1Path := filepath.Join(testReposDir, "repo1_bare") 42 repo, err := OpenRepository(bareRepo1Path) 43 if err != nil { 44 b.Fatal(err) 45 } 46 defer repo.Close() 47 48 testBlob, err := repo.GetBlob("6c493ff740f9380390d5c9ddef4af18697ac9375") 49 if err != nil { 50 b.Fatal(err) 51 } 52 53 for i := 0; i < b.N; i++ { 54 r, err := testBlob.DataAsync() 55 if err != nil { 56 b.Fatal(err) 57 } 58 io.ReadAll(r) 59 _ = r.Close() 60 } 61} 62