You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.6 KiB

  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.Data()
  27. assert.NoError(t, err)
  28. data, err := ioutil.ReadAll(dataReader)
  29. assert.NoError(t, err)
  30. assert.Equal(t, testCase.Data, data)
  31. }
  32. }
  33. func TestRepository_GetBlob_NotExist(t *testing.T) {
  34. repoPath := filepath.Join(testReposDir, "repo1_bare")
  35. r, err := OpenRepository(repoPath)
  36. assert.NoError(t, err)
  37. testCase := "0000000000000000000000000000000000000000"
  38. testError := ErrNotExist{testCase, ""}
  39. blob, err := r.GetBlob(testCase)
  40. assert.Nil(t, blob)
  41. assert.EqualError(t, err, testError.Error())
  42. }
  43. func TestRepository_GetBlob_NoId(t *testing.T) {
  44. repoPath := filepath.Join(testReposDir, "repo1_bare")
  45. r, err := OpenRepository(repoPath)
  46. assert.NoError(t, err)
  47. testCase := ""
  48. testError := fmt.Errorf("Length must be 40: %s", testCase)
  49. blob, err := r.GetBlob(testCase)
  50. assert.Nil(t, blob)
  51. assert.EqualError(t, err, testError.Error())
  52. }