You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

markdown_test.go 6.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package markdown_test
  2. import (
  3. "bytes"
  4. "fmt"
  5. "net/url"
  6. "path"
  7. "strconv"
  8. "testing"
  9. . "code.gitea.io/gitea/modules/markdown"
  10. "code.gitea.io/gitea/modules/setting"
  11. "github.com/russross/blackfriday"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. const urlPrefix = "/prefix"
  15. var numericMetas = map[string]string{
  16. "format": "https://someurl.com/{user}/{repo}/{index}",
  17. "user": "someUser",
  18. "repo": "someRepo",
  19. "style": IssueNameStyleNumeric,
  20. }
  21. var alphanumericMetas = map[string]string{
  22. "format": "https://someurl.com/{user}/{repo}/{index}",
  23. "user": "someUser",
  24. "repo": "someRepo",
  25. "style": IssueNameStyleAlphanumeric,
  26. }
  27. // numericLink an HTML to a numeric-style issue
  28. func numericIssueLink(baseURL string, index int) string {
  29. u, _ := url.Parse(baseURL)
  30. u.Path = path.Join(u.Path, strconv.Itoa(index))
  31. return link(u.String(), fmt.Sprintf("#%d", index))
  32. }
  33. // alphanumLink an HTML link to an alphanumeric-style issue
  34. func alphanumIssueLink(baseURL string, name string) string {
  35. u, _ := url.Parse(baseURL)
  36. u.Path = path.Join(u.Path, name)
  37. return link(u.String(), name)
  38. }
  39. // urlContentsLink an HTML link whose contents is the target URL
  40. func urlContentsLink(href string) string {
  41. return link(href, href)
  42. }
  43. // link an HTML link
  44. func link(href, contents string) string {
  45. return fmt.Sprintf("<a href=\"%s\">%s</a>", href, contents)
  46. }
  47. func testRenderIssueIndexPattern(t *testing.T, input, expected string, metas map[string]string) {
  48. assert.Equal(t, expected,
  49. string(RenderIssueIndexPattern([]byte(input), urlPrefix, metas)))
  50. }
  51. func TestRenderIssueIndexPattern(t *testing.T) {
  52. // numeric: render inputs without valid mentions
  53. test := func(s string) {
  54. testRenderIssueIndexPattern(t, s, s, nil)
  55. testRenderIssueIndexPattern(t, s, s, numericMetas)
  56. }
  57. // should not render anything when there are no mentions
  58. test("")
  59. test("this is a test")
  60. test("test 123 123 1234")
  61. test("#")
  62. test("# # #")
  63. test("# 123")
  64. test("#abcd")
  65. test("##1234")
  66. test("test#1234")
  67. test("#1234test")
  68. test(" test #1234test")
  69. // should not render issue mention without leading space
  70. test("test#54321 issue")
  71. // should not render issue mention without trailing space
  72. test("test #54321issue")
  73. }
  74. func TestRenderIssueIndexPattern2(t *testing.T) {
  75. // numeric: render inputs with valid mentions
  76. test := func(s, expectedFmt string, indices ...int) {
  77. links := make([]interface{}, len(indices))
  78. for i, index := range indices {
  79. links[i] = numericIssueLink(path.Join(urlPrefix, "issues"), index)
  80. }
  81. expectedNil := fmt.Sprintf(expectedFmt, links...)
  82. testRenderIssueIndexPattern(t, s, expectedNil, nil)
  83. for i, index := range indices {
  84. links[i] = numericIssueLink("https://someurl.com/someUser/someRepo/", index)
  85. }
  86. expectedNum := fmt.Sprintf(expectedFmt, links...)
  87. testRenderIssueIndexPattern(t, s, expectedNum, numericMetas)
  88. }
  89. // should render freestanding mentions
  90. test("#1234 test", "%s test", 1234)
  91. test("test #8 issue", "test %s issue", 8)
  92. test("test issue #1234", "test issue %s", 1234)
  93. // should render mentions in parentheses
  94. test("(#54321 issue)", "(%s issue)", 54321)
  95. test("test (#9801 extra) issue", "test (%s extra) issue", 9801)
  96. test("test (#1)", "test (%s)", 1)
  97. // should render multiple issue mentions in the same line
  98. test("#54321 #1243", "%s %s", 54321, 1243)
  99. test("wow (#54321 #1243)", "wow (%s %s)", 54321, 1243)
  100. test("(#4)(#5)", "(%s)(%s)", 4, 5)
  101. test("#1 (#4321) test", "%s (%s) test", 1, 4321)
  102. }
  103. func TestRenderIssueIndexPattern3(t *testing.T) {
  104. // alphanumeric: render inputs without valid mentions
  105. test := func(s string) {
  106. testRenderIssueIndexPattern(t, s, s, alphanumericMetas)
  107. }
  108. test("")
  109. test("this is a test")
  110. test("test 123 123 1234")
  111. test("#")
  112. test("##1234")
  113. test("# 123")
  114. test("#abcd")
  115. test("test #123")
  116. test("abc-1234") // issue prefix must be capital
  117. test("ABc-1234") // issue prefix must be _all_ capital
  118. test("ABCDEFGHIJK-1234") // the limit is 10 characters in the prefix
  119. test("ABC1234") // dash is required
  120. test("test ABC- test") // number is required
  121. test("test -1234 test") // prefix is required
  122. test("testABC-123 test") // leading space is required
  123. test("test ABC-123test") // trailing space is required
  124. test("ABC-0123") // no leading zero
  125. }
  126. func TestRenderIssueIndexPattern4(t *testing.T) {
  127. // alphanumeric: render inputs with valid mentions
  128. test := func(s, expectedFmt string, names ...string) {
  129. links := make([]interface{}, len(names))
  130. for i, name := range names {
  131. links[i] = alphanumIssueLink("https://someurl.com/someUser/someRepo/", name)
  132. }
  133. expected := fmt.Sprintf(expectedFmt, links...)
  134. testRenderIssueIndexPattern(t, s, expected, alphanumericMetas)
  135. }
  136. test("OTT-1234 test", "%s test", "OTT-1234")
  137. test("test T-12 issue", "test %s issue", "T-12")
  138. test("test issue ABCDEFGHIJ-1234567890", "test issue %s", "ABCDEFGHIJ-1234567890")
  139. }
  140. func TestRenderer_AutoLink(t *testing.T) {
  141. setting.AppURL = "http://localhost:3000/"
  142. htmlFlags := blackfriday.HTML_SKIP_STYLE | blackfriday.HTML_OMIT_CONTENTS
  143. renderer := &Renderer{
  144. Renderer: blackfriday.HtmlRenderer(htmlFlags, "", ""),
  145. }
  146. test := func(input, expected string) {
  147. buffer := new(bytes.Buffer)
  148. renderer.AutoLink(buffer, []byte(input), blackfriday.LINK_TYPE_NORMAL)
  149. assert.Equal(t, expected, buffer.String())
  150. }
  151. // render valid issue URLs
  152. test("http://localhost:3000/user/repo/issues/3333",
  153. numericIssueLink("http://localhost:3000/user/repo/issues/", 3333))
  154. // render, but not change, invalid issue URLs
  155. test("http://1111/2222/ssss-issues/3333?param=blah&blahh=333",
  156. urlContentsLink("http://1111/2222/ssss-issues/3333?param=blah&amp;blahh=333"))
  157. test("http://test.com/issues/33333", urlContentsLink("http://test.com/issues/33333"))
  158. test("https://issues/333", urlContentsLink("https://issues/333"))
  159. // render valid commit URLs
  160. test("http://localhost:3000/user/project/commit/d8a994ef243349f321568f9e36d5c3f444b99cae",
  161. " <code><a href=\"http://localhost:3000/user/project/commit/d8a994ef243349f321568f9e36d5c3f444b99cae\">d8a994ef24</a></code>")
  162. test("http://localhost:3000/user/project/commit/d8a994ef243349f321568f9e36d5c3f444b99cae#diff-2",
  163. " <code><a href=\"http://localhost:3000/user/project/commit/d8a994ef243349f321568f9e36d5c3f444b99cae#diff-2\">d8a994ef24</a></code>")
  164. // render other commit URLs
  165. test("https://external-link.gogs.io/gogs/gogs/commit/d8a994ef243349f321568f9e36d5c3f444b99cae#diff-2",
  166. urlContentsLink("https://external-link.gogs.io/gogs/gogs/commit/d8a994ef243349f321568f9e36d5c3f444b99cae#diff-2"))
  167. test("https://commit/d8a994ef243349f321568f9e36d5c3f444b99cae",
  168. urlContentsLink("https://commit/d8a994ef243349f321568f9e36d5c3f444b99cae"))
  169. }