Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

util_render_test.go 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package templates
  4. import (
  5. "context"
  6. "html/template"
  7. "os"
  8. "testing"
  9. "code.gitea.io/gitea/models/unittest"
  10. "code.gitea.io/gitea/modules/git"
  11. "code.gitea.io/gitea/modules/log"
  12. "code.gitea.io/gitea/modules/markup"
  13. "github.com/stretchr/testify/assert"
  14. )
  15. const testInput = ` space @mention-user
  16. /just/a/path.bin
  17. https://example.com/file.bin
  18. [local link](file.bin)
  19. [remote link](https://example.com)
  20. [[local link|file.bin]]
  21. [[remote link|https://example.com]]
  22. ![local image](image.jpg)
  23. ![remote image](https://example.com/image.jpg)
  24. [[local image|image.jpg]]
  25. [[remote link|https://example.com/image.jpg]]
  26. https://example.com/user/repo/compare/88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb#hash
  27. com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb pare
  28. https://example.com/user/repo/commit/88fc37a3c0a4dda553bdcfc80c178a58247f42fb
  29. com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb mit
  30. :+1:
  31. mail@domain.com
  32. @mention-user test
  33. #123
  34. space
  35. `
  36. var testMetas = map[string]string{
  37. "user": "user13",
  38. "repo": "repo11",
  39. "repoPath": "../../tests/gitea-repositories-meta/user13/repo11.git/",
  40. "mode": "comment",
  41. }
  42. func TestMain(m *testing.M) {
  43. unittest.InitSettings()
  44. if err := git.InitSimple(context.Background()); err != nil {
  45. log.Fatal("git init failed, err: %v", err)
  46. }
  47. markup.Init(&markup.ProcessorHelper{
  48. IsUsernameMentionable: func(ctx context.Context, username string) bool {
  49. return username == "mention-user"
  50. },
  51. })
  52. os.Exit(m.Run())
  53. }
  54. func TestRenderCommitBody(t *testing.T) {
  55. type args struct {
  56. ctx context.Context
  57. msg string
  58. metas map[string]string
  59. }
  60. tests := []struct {
  61. name string
  62. args args
  63. want template.HTML
  64. }{
  65. {
  66. name: "multiple lines",
  67. args: args{
  68. ctx: context.Background(),
  69. msg: "first line\nsecond line",
  70. },
  71. want: "second line",
  72. },
  73. {
  74. name: "multiple lines with leading newlines",
  75. args: args{
  76. ctx: context.Background(),
  77. msg: "\n\n\n\nfirst line\nsecond line",
  78. },
  79. want: "second line",
  80. },
  81. {
  82. name: "multiple lines with trailing newlines",
  83. args: args{
  84. ctx: context.Background(),
  85. msg: "first line\nsecond line\n\n\n",
  86. },
  87. want: "second line",
  88. },
  89. }
  90. for _, tt := range tests {
  91. t.Run(tt.name, func(t *testing.T) {
  92. assert.Equalf(t, tt.want, RenderCommitBody(tt.args.ctx, tt.args.msg, tt.args.metas), "RenderCommitBody(%v, %v, %v)", tt.args.ctx, tt.args.msg, tt.args.metas)
  93. })
  94. }
  95. expected := `/just/a/path.bin
  96. <a href="https://example.com/file.bin" class="link">https://example.com/file.bin</a>
  97. [local link](file.bin)
  98. [remote link](<a href="https://example.com" class="link">https://example.com</a>)
  99. [[local link|file.bin]]
  100. [[remote link|<a href="https://example.com" class="link">https://example.com</a>]]
  101. ![local image](image.jpg)
  102. ![remote image](<a href="https://example.com/image.jpg" class="link">https://example.com/image.jpg</a>)
  103. [[local image|image.jpg]]
  104. [[remote link|<a href="https://example.com/image.jpg" class="link">https://example.com/image.jpg</a>]]
  105. <a href="https://example.com/user/repo/compare/88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb#hash" class="compare"><code class="nohighlight">88fc37a3c0...12fc37a3c0 (hash)</code></a>
  106. com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb pare
  107. <a href="https://example.com/user/repo/commit/88fc37a3c0a4dda553bdcfc80c178a58247f42fb" class="commit"><code class="nohighlight">88fc37a3c0</code></a>
  108. com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb mit
  109. <span class="emoji" aria-label="thumbs up">👍</span>
  110. <a href="mailto:mail@domain.com" class="mailto">mail@domain.com</a>
  111. <a href="http://localhost:3000/mention-user" class="mention">@mention-user</a> test
  112. <a href="http://localhost:3000/user13/repo11/issues/123" class="ref-issue">#123</a>
  113. space`
  114. assert.EqualValues(t, expected, RenderCommitBody(context.Background(), testInput, testMetas))
  115. }
  116. func TestRenderCommitMessage(t *testing.T) {
  117. expected := `space <a href="http://localhost:3000/mention-user" class="mention">@mention-user</a> `
  118. assert.EqualValues(t, expected, RenderCommitMessage(context.Background(), testInput, testMetas))
  119. }
  120. func TestRenderCommitMessageLinkSubject(t *testing.T) {
  121. expected := `<a href="https://example.com/link" class="default-link muted">space </a><a href="http://localhost:3000/mention-user" class="mention">@mention-user</a>`
  122. assert.EqualValues(t, expected, RenderCommitMessageLinkSubject(context.Background(), testInput, "https://example.com/link", testMetas))
  123. }
  124. func TestRenderIssueTitle(t *testing.T) {
  125. expected := ` space @mention-user
  126. /just/a/path.bin
  127. https://example.com/file.bin
  128. [local link](file.bin)
  129. [remote link](https://example.com)
  130. [[local link|file.bin]]
  131. [[remote link|https://example.com]]
  132. ![local image](image.jpg)
  133. ![remote image](https://example.com/image.jpg)
  134. [[local image|image.jpg]]
  135. [[remote link|https://example.com/image.jpg]]
  136. https://example.com/user/repo/compare/88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb#hash
  137. com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb pare
  138. https://example.com/user/repo/commit/88fc37a3c0a4dda553bdcfc80c178a58247f42fb
  139. com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb mit
  140. <span class="emoji" aria-label="thumbs up">👍</span>
  141. mail@domain.com
  142. @mention-user test
  143. <a href="http://localhost:3000/user13/repo11/issues/123" class="ref-issue">#123</a>
  144. space
  145. `
  146. assert.EqualValues(t, expected, RenderIssueTitle(context.Background(), testInput, testMetas))
  147. }
  148. func TestRenderMarkdownToHtml(t *testing.T) {
  149. expected := `<p>space <a href="http://localhost:3000/mention-user" rel="nofollow">@mention-user</a><br/>
  150. /just/a/path.bin
  151. <a href="https://example.com/file.bin" rel="nofollow">https://example.com/file.bin</a>
  152. <a href="/file.bin" rel="nofollow">local link</a>
  153. <a href="https://example.com" rel="nofollow">remote link</a>
  154. <a href="/src/file.bin" rel="nofollow">local link</a>
  155. <a href="https://example.com" rel="nofollow">remote link</a>
  156. <a href="/image.jpg" target="_blank" rel="nofollow noopener"><img src="/image.jpg" alt="local image"/></a>
  157. <a href="https://example.com/image.jpg" target="_blank" rel="nofollow noopener"><img src="https://example.com/image.jpg" alt="remote image"/></a>
  158. <a href="/image.jpg" rel="nofollow"><img src="/image.jpg" title="local image" alt="local image"/></a>
  159. <a href="https://example.com/image.jpg" rel="nofollow"><img src="https://example.com/image.jpg" title="remote link" alt="remote link"/></a>
  160. <a href="https://example.com/user/repo/compare/88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb#hash" rel="nofollow"><code>88fc37a3c0...12fc37a3c0 (hash)</code></a>
  161. com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb pare
  162. <a href="https://example.com/user/repo/commit/88fc37a3c0a4dda553bdcfc80c178a58247f42fb" rel="nofollow"><code>88fc37a3c0</code></a>
  163. com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb mit
  164. <span class="emoji" aria-label="thumbs up">👍</span>
  165. <a href="mailto:mail@domain.com" rel="nofollow">mail@domain.com</a>
  166. <a href="http://localhost:3000/mention-user" rel="nofollow">@mention-user</a> test
  167. #123
  168. space</p>
  169. `
  170. assert.EqualValues(t, expected, RenderMarkdownToHtml(context.Background(), testInput))
  171. }