object_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package git
  2. import (
  3. "crypto/sha1"
  4. "os"
  5. "testing"
  6. "time"
  7. "github.com/cheekybits/is"
  8. "github.com/kylelemons/godebug/diff"
  9. )
  10. func TestUnpackObject_blob(t *testing.T) {
  11. is := is.New(t)
  12. tcases := []struct {
  13. Fname string
  14. Object *Object
  15. }{
  16. // blobs
  17. {
  18. Fname: "tests/blob/1f7a7a472abf3dd9643fd615f6da379c4acb3e3a",
  19. Object: &Object{
  20. Type: BlobT,
  21. Size: 10,
  22. blob: newBlob([]byte("version 2\n")),
  23. },
  24. },
  25. {
  26. Fname: "tests/blob/d670460b4b4aece5915caf5c68d12f560a9fe3e4",
  27. Object: &Object{
  28. Type: BlobT,
  29. Size: 13,
  30. blob: newBlob([]byte("test content\n")),
  31. },
  32. },
  33. // trees
  34. {
  35. Fname: "tests/tree/3c4e9cd789d88d8d89c1073707c3585e41b0e614",
  36. Object: &Object{Type: TreeT, Size: 101,
  37. tree: []Tree{
  38. {"40000", "bak", shaFromStr("\xd82\x9f\xc1̓\x87\x80\xffݟ\x94\xe0\xd3d\xe0\xeat\xf5y")},
  39. {"100644", "new.txt", shaFromStr("\xfaI\xb0w\x97#\x91\xadX\x03pP\xf2\xa7_t\xe3g\x1e\x92")},
  40. {"100644", "test.txt", shaFromStr("\x1fzzG*\xbf=\xd9d?\xd6\x15\xf6\xda7\x9cJ\xcb>:")},
  41. },
  42. },
  43. },
  44. {
  45. Fname: "tests/tree/0155eb4229851634a0f03eb265b69f5a2d56f341",
  46. Object: &Object{Type: TreeT, Size: 71,
  47. tree: []Tree{
  48. {"100644", "new.txt", shaFromStr("\xfaI\xb0w\x97#\x91\xadX\x03pP\xf2\xa7_t\xe3g\x1e\x92")},
  49. {"100644", "test.txt", shaFromStr("\x1fzzG*\xbf=\xd9d?\xd6\x15\xf6\xda7\x9cJ\xcb>:")},
  50. },
  51. },
  52. },
  53. // commit
  54. {
  55. Fname: "tests/commit/de70159e4a5842aed0aae9380e3006e909c8feb4",
  56. Object: &Object{Type: CommitT, Size: 165,
  57. commit: &Commit{
  58. Tree: "d8329fc1cc938780ffdd9f94e0d364e0ea74f579",
  59. Author: &Stamp{Name: "Henry", Email: "cryptix@riseup.net", When: time.Unix(1438988455, 0)},
  60. Committer: &Stamp{Name: "Henry", Email: "cryptix@riseup.net", When: time.Unix(1438988455, 0)},
  61. Message: "first commit",
  62. },
  63. },
  64. },
  65. {
  66. Fname: "tests/commit/ad8fdc888c6f6caed63af0fb08484901e4e7e41e",
  67. Object: &Object{Type: CommitT, Size: 165,
  68. commit: &Commit{
  69. Tree: "d8329fc1cc938780ffdd9f94e0d364e0ea74f579",
  70. Author: &Stamp{Name: "Henry", Email: "cryptix@riseup.net", When: time.Unix(1438995813, 0)},
  71. Committer: &Stamp{Name: "Henry", Email: "cryptix@riseup.net", When: time.Unix(1438995813, 0)},
  72. Message: "first commit",
  73. },
  74. },
  75. // TODO(cryptix): add example with Parent
  76. },
  77. }
  78. for _, tc := range tcases {
  79. f, err := os.Open(tc.Fname)
  80. is.Nil(err)
  81. obj, err := DecodeObject(f)
  82. is.Nil(err)
  83. diff := diff.Diff(obj.String(), tc.Object.String())
  84. is.Equal(diff, "")
  85. is.Nil(f.Close())
  86. }
  87. }
  88. func shaFromStr(str string) [sha1.Size]byte {
  89. var sha [sha1.Size]byte
  90. if len(str) != 20 {
  91. panic("illegal input")
  92. }
  93. var convert []byte = []byte(str)
  94. copy(sha[:], convert)
  95. return sha
  96. }