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.

release_test.go 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package repo
  4. import (
  5. "testing"
  6. repo_model "code.gitea.io/gitea/models/repo"
  7. "code.gitea.io/gitea/models/unittest"
  8. "code.gitea.io/gitea/modules/test"
  9. "code.gitea.io/gitea/modules/web"
  10. "code.gitea.io/gitea/services/forms"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. func TestNewReleasePost(t *testing.T) {
  14. for _, testCase := range []struct {
  15. RepoID int64
  16. UserID int64
  17. TagName string
  18. Form forms.NewReleaseForm
  19. }{
  20. {
  21. RepoID: 1,
  22. UserID: 2,
  23. TagName: "v1.1", // pre-existing tag
  24. Form: forms.NewReleaseForm{
  25. TagName: "newtag",
  26. Target: "master",
  27. Title: "title",
  28. Content: "content",
  29. },
  30. },
  31. {
  32. RepoID: 1,
  33. UserID: 2,
  34. TagName: "newtag",
  35. Form: forms.NewReleaseForm{
  36. TagName: "newtag",
  37. Target: "master",
  38. Title: "title",
  39. Content: "content",
  40. },
  41. },
  42. } {
  43. unittest.PrepareTestEnv(t)
  44. ctx := test.MockContext(t, "user2/repo1/releases/new")
  45. test.LoadUser(t, ctx, 2)
  46. test.LoadRepo(t, ctx, 1)
  47. test.LoadGitRepo(t, ctx)
  48. web.SetForm(ctx, &testCase.Form)
  49. NewReleasePost(ctx)
  50. unittest.AssertExistsAndLoadBean(t, &repo_model.Release{
  51. RepoID: 1,
  52. PublisherID: 2,
  53. TagName: testCase.Form.TagName,
  54. Target: testCase.Form.Target,
  55. Title: testCase.Form.Title,
  56. Note: testCase.Form.Content,
  57. }, unittest.Cond("is_draft=?", len(testCase.Form.Draft) > 0))
  58. ctx.Repo.GitRepo.Close()
  59. }
  60. }
  61. func TestNewReleasesList(t *testing.T) {
  62. unittest.PrepareTestEnv(t)
  63. ctx := test.MockContext(t, "user2/repo-release/releases")
  64. test.LoadUser(t, ctx, 2)
  65. test.LoadRepo(t, ctx, 57)
  66. test.LoadGitRepo(t, ctx)
  67. t.Cleanup(func() { ctx.Repo.GitRepo.Close() })
  68. Releases(ctx)
  69. releases := ctx.Data["Releases"].([]*repo_model.Release)
  70. type computedFields struct {
  71. NumCommitsBehind int64
  72. TargetBehind string
  73. }
  74. expectedComputation := map[string]computedFields{
  75. "v1.0": {
  76. NumCommitsBehind: 3,
  77. TargetBehind: "main",
  78. },
  79. "v1.1": {
  80. NumCommitsBehind: 1,
  81. TargetBehind: "main",
  82. },
  83. "v2.0": {
  84. NumCommitsBehind: 0,
  85. TargetBehind: "main",
  86. },
  87. "non-existing-target-branch": {
  88. NumCommitsBehind: 1,
  89. TargetBehind: "main",
  90. },
  91. "empty-target-branch": {
  92. NumCommitsBehind: 1,
  93. TargetBehind: "main",
  94. },
  95. }
  96. for _, r := range releases {
  97. actual := computedFields{
  98. NumCommitsBehind: r.NumCommitsBehind,
  99. TargetBehind: r.TargetBehind,
  100. }
  101. assert.Equal(t, expectedComputation[r.TagName], actual, "wrong computed fields for %s: %#v", r.TagName, r)
  102. }
  103. }