github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/git/repo_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 package git 7 8 import ( 9 "fmt" 10 "io" 11 "path/filepath" 12 "testing" 13 14 "github.com/stretchr/testify/assert" 15 ) 16 17 func TestRepository_GetBlob_Found(t *testing.T) { 18 repoPath := filepath.Join(testReposDir, "repo1_bare") 19 r, err := openRepositoryWithDefaultContext(repoPath) 20 assert.NoError(t, err) 21 defer r.Close() 22 23 testCases := []struct { 24 OID string 25 Data []byte 26 }{ 27 {"e2129701f1a4d54dc44f03c93bca0a2aec7c5449", []byte("file1\n")}, 28 {"6c493ff740f9380390d5c9ddef4af18697ac9375", []byte("file2\n")}, 29 } 30 31 for _, testCase := range testCases { 32 blob, err := r.GetBlob(testCase.OID) 33 assert.NoError(t, err) 34 35 dataReader, err := blob.DataAsync() 36 assert.NoError(t, err) 37 38 data, err := io.ReadAll(dataReader) 39 assert.NoError(t, dataReader.Close()) 40 assert.NoError(t, err) 41 assert.Equal(t, testCase.Data, data) 42 } 43 } 44 45 func TestRepository_GetBlob_NotExist(t *testing.T) { 46 repoPath := filepath.Join(testReposDir, "repo1_bare") 47 r, err := openRepositoryWithDefaultContext(repoPath) 48 assert.NoError(t, err) 49 defer r.Close() 50 51 testCase := "0000000000000000000000000000000000000000" 52 testError := ErrNotExist{testCase, ""} 53 54 blob, err := r.GetBlob(testCase) 55 assert.Nil(t, blob) 56 assert.EqualError(t, err, testError.Error()) 57 } 58 59 func TestRepository_GetBlob_NoId(t *testing.T) { 60 repoPath := filepath.Join(testReposDir, "repo1_bare") 61 r, err := openRepositoryWithDefaultContext(repoPath) 62 assert.NoError(t, err) 63 defer r.Close() 64 65 testCase := "" 66 testError := fmt.Errorf("Length must be 40: %s", testCase) 67 68 blob, err := r.GetBlob(testCase) 69 assert.Nil(t, blob) 70 assert.EqualError(t, err, testError.Error()) 71 }