本站源代码
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.

87 lines
2.4KB

  1. // Copyright 2016 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 models
  5. import (
  6. "container/list"
  7. "testing"
  8. "time"
  9. "code.gitea.io/gitea/modules/git"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func TestCommitToPushCommit(t *testing.T) {
  13. now := time.Now()
  14. sig := &git.Signature{
  15. Email: "example@example.com",
  16. Name: "John Doe",
  17. When: now,
  18. }
  19. const hexString = "0123456789abcdef0123456789abcdef01234567"
  20. sha1, err := git.NewIDFromString(hexString)
  21. assert.NoError(t, err)
  22. pushCommit := CommitToPushCommit(&git.Commit{
  23. ID: sha1,
  24. Author: sig,
  25. Committer: sig,
  26. CommitMessage: "Commit Message",
  27. })
  28. assert.Equal(t, hexString, pushCommit.Sha1)
  29. assert.Equal(t, "Commit Message", pushCommit.Message)
  30. assert.Equal(t, "example@example.com", pushCommit.AuthorEmail)
  31. assert.Equal(t, "John Doe", pushCommit.AuthorName)
  32. assert.Equal(t, "example@example.com", pushCommit.CommitterEmail)
  33. assert.Equal(t, "John Doe", pushCommit.CommitterName)
  34. assert.Equal(t, now, pushCommit.Timestamp)
  35. }
  36. func TestListToPushCommits(t *testing.T) {
  37. now := time.Now()
  38. sig := &git.Signature{
  39. Email: "example@example.com",
  40. Name: "John Doe",
  41. When: now,
  42. }
  43. const hexString1 = "0123456789abcdef0123456789abcdef01234567"
  44. hash1, err := git.NewIDFromString(hexString1)
  45. assert.NoError(t, err)
  46. const hexString2 = "fedcba9876543210fedcba9876543210fedcba98"
  47. hash2, err := git.NewIDFromString(hexString2)
  48. assert.NoError(t, err)
  49. l := list.New()
  50. l.PushBack(&git.Commit{
  51. ID: hash1,
  52. Author: sig,
  53. Committer: sig,
  54. CommitMessage: "Message1",
  55. })
  56. l.PushBack(&git.Commit{
  57. ID: hash2,
  58. Author: sig,
  59. Committer: sig,
  60. CommitMessage: "Message2",
  61. })
  62. pushCommits := ListToPushCommits(l)
  63. assert.Equal(t, 2, pushCommits.Len)
  64. if assert.Len(t, pushCommits.Commits, 2) {
  65. assert.Equal(t, "Message1", pushCommits.Commits[0].Message)
  66. assert.Equal(t, hexString1, pushCommits.Commits[0].Sha1)
  67. assert.Equal(t, "example@example.com", pushCommits.Commits[0].AuthorEmail)
  68. assert.Equal(t, now, pushCommits.Commits[0].Timestamp)
  69. assert.Equal(t, "Message2", pushCommits.Commits[1].Message)
  70. assert.Equal(t, hexString2, pushCommits.Commits[1].Sha1)
  71. assert.Equal(t, "example@example.com", pushCommits.Commits[1].AuthorEmail)
  72. assert.Equal(t, now, pushCommits.Commits[1].Timestamp)
  73. }
  74. }
  75. // TODO TestPushUpdate
上海开阖软件有限公司 沪ICP备12045867号-1