A fork of Gitea (see branch `mj`) adding Majority Judgment Polls 𐄷 over Issues and Merge Requests. https://git.mieuxvoter.fr
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.

132 lines
4.9 KiB

  1. // Copyright 2017 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. "path/filepath"
  7. "strings"
  8. "testing"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func TestCommitsCount(t *testing.T) {
  12. bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
  13. commitsCount, err := CommitsCount(bareRepo1Path, "8006ff9adbf0cb94da7dad9e537e53817f9fa5c0")
  14. assert.NoError(t, err)
  15. assert.Equal(t, int64(3), commitsCount)
  16. }
  17. func TestGetFullCommitID(t *testing.T) {
  18. bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
  19. id, err := GetFullCommitID(bareRepo1Path, "8006ff9a")
  20. assert.NoError(t, err)
  21. assert.Equal(t, "8006ff9adbf0cb94da7dad9e537e53817f9fa5c0", id)
  22. }
  23. func TestGetFullCommitIDError(t *testing.T) {
  24. bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
  25. id, err := GetFullCommitID(bareRepo1Path, "unknown")
  26. assert.Empty(t, id)
  27. if assert.Error(t, err) {
  28. assert.EqualError(t, err, "object does not exist [id: unknown, rel_path: ]")
  29. }
  30. }
  31. func TestCommitFromReader(t *testing.T) {
  32. commitString := `feaf4ba6bc635fec442f46ddd4512416ec43c2c2 commit 1074
  33. tree f1a6cb52b2d16773290cefe49ad0684b50a4f930
  34. parent 37991dec2c8e592043f47155ce4808d4580f9123
  35. author silverwind <me@silverwind.io> 1563741793 +0200
  36. committer silverwind <me@silverwind.io> 1563741793 +0200
  37. gpgsig -----BEGIN PGP SIGNATURE-----
  38. ` + " " + `
  39. iQIzBAABCAAdFiEEWPb2jX6FS2mqyJRQLmK0HJOGlEMFAl00zmEACgkQLmK0HJOG
  40. lEMDFBAAhQKKqLD1VICygJMEB8t1gBmNLgvziOLfpX4KPWdPtBk3v/QJ7OrfMrVK
  41. xlC4ZZyx6yMm1Q7GzmuWykmZQJ9HMaHJ49KAbh5MMjjV/+OoQw9coIdo8nagRUld
  42. vX8QHzNZ6Agx77xHuDJZgdHKpQK3TrMDsxzoYYMvlqoLJIDXE1Sp7KYNy12nhdRg
  43. R6NXNmW8oMZuxglkmUwayMiPS+N4zNYqv0CXYzlEqCOgq9MJUcAMHt+KpiST+sm6
  44. FWkJ9D+biNPyQ9QKf1AE4BdZia4lHfPYU/C/DEL/a5xQuuop/zMQZoGaIA4p2zGQ
  45. /maqYxEIM/yRBQpT1jlODKPJrMEgx7SgY2hRU47YZ4fj6350fb6fNBtiiMAfJbjL
  46. S3Gh85E9fm3hJaNSPKAaJFYL1Ya2svuWfgHj677C56UcmYis7fhiiy1aJuYdHnSm
  47. sD53z/f0J+We4VZjY+pidvA9BGZPFVdR3wd3xGs8/oH6UWaLJAMGkLG6dDb3qDLm
  48. 1LFZwsX8sdD32i1SiWanYQYSYMyFWr0awi4xdoMtYCL7uKBYtwtPyvq3cj4IrJlb
  49. mfeFhT57UbE4qukTDIQ0Y0WM40UYRTakRaDY7ubhXgLgx09Cnp9XTVMsHgT6j9/i
  50. 1pxsB104XLWjQHTjr1JtiaBQEwFh9r2OKTcpvaLcbNtYpo7CzOs=
  51. =FRsO
  52. -----END PGP SIGNATURE-----
  53. empty commit`
  54. sha := SHA1{0xfe, 0xaf, 0x4b, 0xa6, 0xbc, 0x63, 0x5f, 0xec, 0x44, 0x2f, 0x46, 0xdd, 0xd4, 0x51, 0x24, 0x16, 0xec, 0x43, 0xc2, 0xc2}
  55. gitRepo, err := OpenRepository(filepath.Join(testReposDir, "repo1_bare"))
  56. assert.NoError(t, err)
  57. assert.NotNil(t, gitRepo)
  58. commitFromReader, err := CommitFromReader(gitRepo, sha, strings.NewReader(commitString))
  59. assert.NoError(t, err)
  60. if !assert.NotNil(t, commitFromReader) {
  61. return
  62. }
  63. assert.EqualValues(t, sha, commitFromReader.ID)
  64. assert.EqualValues(t, `-----BEGIN PGP SIGNATURE-----
  65. iQIzBAABCAAdFiEEWPb2jX6FS2mqyJRQLmK0HJOGlEMFAl00zmEACgkQLmK0HJOG
  66. lEMDFBAAhQKKqLD1VICygJMEB8t1gBmNLgvziOLfpX4KPWdPtBk3v/QJ7OrfMrVK
  67. xlC4ZZyx6yMm1Q7GzmuWykmZQJ9HMaHJ49KAbh5MMjjV/+OoQw9coIdo8nagRUld
  68. vX8QHzNZ6Agx77xHuDJZgdHKpQK3TrMDsxzoYYMvlqoLJIDXE1Sp7KYNy12nhdRg
  69. R6NXNmW8oMZuxglkmUwayMiPS+N4zNYqv0CXYzlEqCOgq9MJUcAMHt+KpiST+sm6
  70. FWkJ9D+biNPyQ9QKf1AE4BdZia4lHfPYU/C/DEL/a5xQuuop/zMQZoGaIA4p2zGQ
  71. /maqYxEIM/yRBQpT1jlODKPJrMEgx7SgY2hRU47YZ4fj6350fb6fNBtiiMAfJbjL
  72. S3Gh85E9fm3hJaNSPKAaJFYL1Ya2svuWfgHj677C56UcmYis7fhiiy1aJuYdHnSm
  73. sD53z/f0J+We4VZjY+pidvA9BGZPFVdR3wd3xGs8/oH6UWaLJAMGkLG6dDb3qDLm
  74. 1LFZwsX8sdD32i1SiWanYQYSYMyFWr0awi4xdoMtYCL7uKBYtwtPyvq3cj4IrJlb
  75. mfeFhT57UbE4qukTDIQ0Y0WM40UYRTakRaDY7ubhXgLgx09Cnp9XTVMsHgT6j9/i
  76. 1pxsB104XLWjQHTjr1JtiaBQEwFh9r2OKTcpvaLcbNtYpo7CzOs=
  77. =FRsO
  78. -----END PGP SIGNATURE-----
  79. `, commitFromReader.Signature.Signature)
  80. assert.EqualValues(t, `tree f1a6cb52b2d16773290cefe49ad0684b50a4f930
  81. parent 37991dec2c8e592043f47155ce4808d4580f9123
  82. author silverwind <me@silverwind.io> 1563741793 +0200
  83. committer silverwind <me@silverwind.io> 1563741793 +0200
  84. empty commit`, commitFromReader.Signature.Payload)
  85. assert.EqualValues(t, "silverwind <me@silverwind.io>", commitFromReader.Author.String())
  86. commitFromReader2, err := CommitFromReader(gitRepo, sha, strings.NewReader(commitString+"\n\n"))
  87. assert.NoError(t, err)
  88. commitFromReader.CommitMessage += "\n\n"
  89. commitFromReader.Signature.Payload += "\n\n"
  90. assert.EqualValues(t, commitFromReader, commitFromReader2)
  91. }
  92. func TestHasPreviousCommit(t *testing.T) {
  93. bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
  94. repo, err := OpenRepository(bareRepo1Path)
  95. assert.NoError(t, err)
  96. commit, err := repo.GetCommit("8006ff9adbf0cb94da7dad9e537e53817f9fa5c0")
  97. assert.NoError(t, err)
  98. parentSHA := MustIDFromString("8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2")
  99. notParentSHA := MustIDFromString("2839944139e0de9737a044f78b0e4b40d989a9e3")
  100. haz, err := commit.HasPreviousCommit(parentSHA)
  101. assert.NoError(t, err)
  102. assert.True(t, haz)
  103. hazNot, err := commit.HasPreviousCommit(notParentSHA)
  104. assert.NoError(t, err)
  105. assert.False(t, hazNot)
  106. selfNot, err := commit.HasPreviousCommit(commit.ID)
  107. assert.NoError(t, err)
  108. assert.False(t, selfNot)
  109. }