gitea源码

merge_test.go 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package pull
  4. import (
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func Test_expandDefaultMergeMessage(t *testing.T) {
  9. type args struct {
  10. template string
  11. vars map[string]string
  12. }
  13. tests := []struct {
  14. name string
  15. args args
  16. want string
  17. wantBody string
  18. }{
  19. {
  20. name: "single line",
  21. args: args{
  22. template: "Merge ${PullRequestTitle}",
  23. vars: map[string]string{
  24. "PullRequestTitle": "PullRequestTitle",
  25. "PullRequestDescription": "Pull\nRequest\nDescription\n",
  26. },
  27. },
  28. want: "Merge PullRequestTitle",
  29. wantBody: "",
  30. },
  31. {
  32. name: "multiple lines",
  33. args: args{
  34. template: "Merge ${PullRequestTitle}\nDescription:\n\n${PullRequestDescription}\n",
  35. vars: map[string]string{
  36. "PullRequestTitle": "PullRequestTitle",
  37. "PullRequestDescription": "Pull\nRequest\nDescription\n",
  38. },
  39. },
  40. want: "Merge PullRequestTitle",
  41. wantBody: "Description:\n\nPull\nRequest\nDescription\n",
  42. },
  43. {
  44. name: "leading newlines",
  45. args: args{
  46. template: "\n\n\nMerge ${PullRequestTitle}\n\n\nDescription:\n\n${PullRequestDescription}\n",
  47. vars: map[string]string{
  48. "PullRequestTitle": "PullRequestTitle",
  49. "PullRequestDescription": "Pull\nRequest\nDescription\n",
  50. },
  51. },
  52. want: "Merge PullRequestTitle",
  53. wantBody: "Description:\n\nPull\nRequest\nDescription\n",
  54. },
  55. }
  56. for _, tt := range tests {
  57. t.Run(tt.name, func(t *testing.T) {
  58. got, got1 := expandDefaultMergeMessage(tt.args.template, tt.args.vars)
  59. assert.Equalf(t, tt.want, got, "expandDefaultMergeMessage(%v, %v)", tt.args.template, tt.args.vars)
  60. assert.Equalf(t, tt.wantBody, got1, "expandDefaultMergeMessage(%v, %v)", tt.args.template, tt.args.vars)
  61. })
  62. }
  63. }
  64. func TestAddCommitMessageTailer(t *testing.T) {
  65. // add tailer for empty message
  66. assert.Equal(t, "\n\nTest-tailer: TestValue", AddCommitMessageTailer("", "Test-tailer", "TestValue"))
  67. // add tailer for message without newlines
  68. assert.Equal(t, "title\n\nTest-tailer: TestValue", AddCommitMessageTailer("title", "Test-tailer", "TestValue"))
  69. assert.Equal(t, "title\n\nNot tailer: xxx\n\nTest-tailer: TestValue", AddCommitMessageTailer("title\n\nNot tailer: xxx", "Test-tailer", "TestValue"))
  70. assert.Equal(t, "title\n\nNotTailer: xxx\n\nTest-tailer: TestValue", AddCommitMessageTailer("title\n\nNotTailer: xxx", "Test-tailer", "TestValue"))
  71. assert.Equal(t, "title\n\nnot-tailer: xxx\n\nTest-tailer: TestValue", AddCommitMessageTailer("title\n\nnot-tailer: xxx", "Test-tailer", "TestValue"))
  72. // add tailer for message with one EOL
  73. assert.Equal(t, "title\n\nTest-tailer: TestValue", AddCommitMessageTailer("title\n", "Test-tailer", "TestValue"))
  74. // add tailer for message with two EOLs
  75. assert.Equal(t, "title\n\nTest-tailer: TestValue", AddCommitMessageTailer("title\n\n", "Test-tailer", "TestValue"))
  76. // add tailer for message with existing tailer (won't duplicate)
  77. assert.Equal(t, "title\n\nTest-tailer: TestValue", AddCommitMessageTailer("title\n\nTest-tailer: TestValue", "Test-tailer", "TestValue"))
  78. assert.Equal(t, "title\n\nTest-tailer: TestValue\n", AddCommitMessageTailer("title\n\nTest-tailer: TestValue\n", "Test-tailer", "TestValue"))
  79. // add tailer for message with existing tailer and different value (will append)
  80. assert.Equal(t, "title\n\nTest-tailer: v1\nTest-tailer: v2", AddCommitMessageTailer("title\n\nTest-tailer: v1", "Test-tailer", "v2"))
  81. assert.Equal(t, "title\n\nTest-tailer: v1\nTest-tailer: v2", AddCommitMessageTailer("title\n\nTest-tailer: v1\n", "Test-tailer", "v2"))
  82. }