本站源代码
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

68 行
1.6KB

  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. package git
  5. import (
  6. "fmt"
  7. "io/ioutil"
  8. "path/filepath"
  9. "testing"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func TestRepository_GetBlob_Found(t *testing.T) {
  13. repoPath := filepath.Join(testReposDir, "repo1_bare")
  14. r, err := OpenRepository(repoPath)
  15. assert.NoError(t, err)
  16. testCases := []struct {
  17. OID string
  18. Data []byte
  19. }{
  20. {"e2129701f1a4d54dc44f03c93bca0a2aec7c5449", []byte("file1\n")},
  21. {"6c493ff740f9380390d5c9ddef4af18697ac9375", []byte("file2\n")},
  22. }
  23. for _, testCase := range testCases {
  24. blob, err := r.GetBlob(testCase.OID)
  25. assert.NoError(t, err)
  26. dataReader, err := blob.DataAsync()
  27. assert.NoError(t, err)
  28. defer dataReader.Close()
  29. data, err := ioutil.ReadAll(dataReader)
  30. assert.NoError(t, err)
  31. assert.Equal(t, testCase.Data, data)
  32. }
  33. }
  34. func TestRepository_GetBlob_NotExist(t *testing.T) {
  35. repoPath := filepath.Join(testReposDir, "repo1_bare")
  36. r, err := OpenRepository(repoPath)
  37. assert.NoError(t, err)
  38. testCase := "0000000000000000000000000000000000000000"
  39. testError := ErrNotExist{testCase, ""}
  40. blob, err := r.GetBlob(testCase)
  41. assert.Nil(t, blob)
  42. assert.EqualError(t, err, testError.Error())
  43. }
  44. func TestRepository_GetBlob_NoId(t *testing.T) {
  45. repoPath := filepath.Join(testReposDir, "repo1_bare")
  46. r, err := OpenRepository(repoPath)
  47. assert.NoError(t, err)
  48. testCase := ""
  49. testError := fmt.Errorf("Length must be 40: %s", testCase)
  50. blob, err := r.GetBlob(testCase)
  51. assert.Nil(t, blob)
  52. assert.EqualError(t, err, testError.Error())
  53. }
上海开阖软件有限公司 沪ICP备12045867号-1