|
| 1 | +// Copyright 2018 The Gitea Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package git |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + "io/ioutil" |
| 10 | + "path/filepath" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | +) |
| 15 | + |
| 16 | +func TestRepository_GetBlob_Found(t *testing.T) { |
| 17 | + repoPath := filepath.Join(testReposDir, "repo1_bare") |
| 18 | + r, err := OpenRepository(repoPath) |
| 19 | + assert.NoError(t, err) |
| 20 | + |
| 21 | + testCases := []struct { |
| 22 | + OID string |
| 23 | + Data []byte |
| 24 | + }{ |
| 25 | + {"e2129701f1a4d54dc44f03c93bca0a2aec7c5449", []byte("file1\n")}, |
| 26 | + {"6c493ff740f9380390d5c9ddef4af18697ac9375", []byte("file2\n")}, |
| 27 | + } |
| 28 | + |
| 29 | + for _, testCase := range testCases { |
| 30 | + blob, err := r.GetBlob(testCase.OID) |
| 31 | + assert.NoError(t, err) |
| 32 | + |
| 33 | + dataReader, err := blob.Data() |
| 34 | + assert.NoError(t, err) |
| 35 | + |
| 36 | + data, err := ioutil.ReadAll(dataReader) |
| 37 | + assert.NoError(t, err) |
| 38 | + assert.Equal(t, testCase.Data, data) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func TestRepository_GetBlob_NotExist(t *testing.T) { |
| 43 | + repoPath := filepath.Join(testReposDir, "repo1_bare") |
| 44 | + r, err := OpenRepository(repoPath) |
| 45 | + assert.NoError(t, err) |
| 46 | + |
| 47 | + testCase := "0000000000000000000000000000000000000000" |
| 48 | + testError := ErrNotExist{testCase, ""} |
| 49 | + |
| 50 | + blob, err := r.GetBlob(testCase) |
| 51 | + assert.Nil(t, blob) |
| 52 | + assert.EqualError(t, err, testError.Error()) |
| 53 | +} |
| 54 | + |
| 55 | +func TestRepository_GetBlob_NoId(t *testing.T) { |
| 56 | + repoPath := filepath.Join(testReposDir, "repo1_bare") |
| 57 | + r, err := OpenRepository(repoPath) |
| 58 | + assert.NoError(t, err) |
| 59 | + |
| 60 | + testCase := "" |
| 61 | + testError := fmt.Errorf("Length must be 40: %s", testCase) |
| 62 | + |
| 63 | + blob, err := r.GetBlob(testCase) |
| 64 | + assert.Nil(t, blob) |
| 65 | + assert.EqualError(t, err, testError.Error()) |
| 66 | +} |
0 commit comments