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.

repo_blob_test.go 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. defer r.Close()
  17. testCases := []struct {
  18. OID string
  19. Data []byte
  20. }{
  21. {"e2129701f1a4d54dc44f03c93bca0a2aec7c5449", []byte("file1\n")},
  22. {"6c493ff740f9380390d5c9ddef4af18697ac9375", []byte("file2\n")},
  23. }
  24. for _, testCase := range testCases {
  25. blob, err := r.GetBlob(testCase.OID)
  26. assert.NoError(t, err)
  27. dataReader, err := blob.DataAsync()
  28. assert.NoError(t, err)
  29. defer dataReader.Close()
  30. data, err := ioutil.ReadAll(dataReader)
  31. assert.NoError(t, err)
  32. assert.Equal(t, testCase.Data, data)
  33. }
  34. }
  35. func TestRepository_GetBlob_NotExist(t *testing.T) {
  36. repoPath := filepath.Join(testReposDir, "repo1_bare")
  37. r, err := OpenRepository(repoPath)
  38. assert.NoError(t, err)
  39. defer r.Close()
  40. testCase := "0000000000000000000000000000000000000000"
  41. testError := ErrNotExist{testCase, ""}
  42. blob, err := r.GetBlob(testCase)
  43. assert.Nil(t, blob)
  44. assert.EqualError(t, err, testError.Error())
  45. }
  46. func TestRepository_GetBlob_NoId(t *testing.T) {
  47. repoPath := filepath.Join(testReposDir, "repo1_bare")
  48. r, err := OpenRepository(repoPath)
  49. assert.NoError(t, err)
  50. defer r.Close()
  51. testCase := ""
  52. testError := fmt.Errorf("Length must be 40: %s", testCase)
  53. blob, err := r.GetBlob(testCase)
  54. assert.Nil(t, blob)
  55. assert.EqualError(t, err, testError.Error())
  56. }