github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/git/blob_test.go (about) 1 // Copyright 2023 The GitBundle Inc. All rights reserved. 2 // Copyright 2017 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 6 // Copyright 2015 The Gogs Authors. All rights reserved. 7 8 package git 9 10 import ( 11 "io" 12 "path/filepath" 13 "testing" 14 15 "github.com/stretchr/testify/assert" 16 "github.com/stretchr/testify/require" 17 ) 18 19 func TestBlob_Data(t *testing.T) { 20 output := "file2\n" 21 bareRepo1Path := filepath.Join(testReposDir, "repo1_bare") 22 repo, err := openRepositoryWithDefaultContext(bareRepo1Path) 23 if !assert.NoError(t, err) { 24 t.Fatal() 25 } 26 defer repo.Close() 27 28 testBlob, err := repo.GetBlob("6c493ff740f9380390d5c9ddef4af18697ac9375") 29 assert.NoError(t, err) 30 31 r, err := testBlob.DataAsync() 32 assert.NoError(t, err) 33 require.NotNil(t, r) 34 35 data, err := io.ReadAll(r) 36 assert.NoError(t, r.Close()) 37 38 assert.NoError(t, err) 39 assert.Equal(t, output, string(data)) 40 } 41 42 func Benchmark_Blob_Data(b *testing.B) { 43 bareRepo1Path := filepath.Join(testReposDir, "repo1_bare") 44 repo, err := openRepositoryWithDefaultContext(bareRepo1Path) 45 if err != nil { 46 b.Fatal(err) 47 } 48 defer repo.Close() 49 50 testBlob, err := repo.GetBlob("6c493ff740f9380390d5c9ddef4af18697ac9375") 51 if err != nil { 52 b.Fatal(err) 53 } 54 55 for i := 0; i < b.N; i++ { 56 r, err := testBlob.DataAsync() 57 if err != nil { 58 b.Fatal(err) 59 } 60 io.ReadAll(r) 61 _ = r.Close() 62 } 63 }