本站源代码
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.

323 lines
10KB

  1. // Copyright 2018 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 markup
  5. import (
  6. "fmt"
  7. "strconv"
  8. "strings"
  9. "testing"
  10. "code.gitea.io/gitea/modules/setting"
  11. "code.gitea.io/gitea/modules/util"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. const AppURL = "http://localhost:3000/"
  15. const Repo = "gogits/gogs"
  16. const AppSubURL = AppURL + Repo + "/"
  17. // alphanumLink an HTML link to an alphanumeric-style issue
  18. func alphanumIssueLink(baseURL, class, name string) string {
  19. return link(util.URLJoin(baseURL, name), class, name)
  20. }
  21. // numericLink an HTML to a numeric-style issue
  22. func numericIssueLink(baseURL, class string, index int) string {
  23. return link(util.URLJoin(baseURL, strconv.Itoa(index)), class, fmt.Sprintf("#%d", index))
  24. }
  25. // link an HTML link
  26. func link(href, class, contents string) string {
  27. if class != "" {
  28. class = " class=\"" + class + "\""
  29. }
  30. return fmt.Sprintf("<a href=\"%s\"%s>%s</a>", href, class, contents)
  31. }
  32. var numericMetas = map[string]string{
  33. "format": "https://someurl.com/{user}/{repo}/{index}",
  34. "user": "someUser",
  35. "repo": "someRepo",
  36. "style": IssueNameStyleNumeric,
  37. }
  38. var alphanumericMetas = map[string]string{
  39. "format": "https://someurl.com/{user}/{repo}/{index}",
  40. "user": "someUser",
  41. "repo": "someRepo",
  42. "style": IssueNameStyleAlphanumeric,
  43. }
  44. // these values should match the Repo const above
  45. var localMetas = map[string]string{
  46. "user": "gogits",
  47. "repo": "gogs",
  48. }
  49. func TestRender_IssueIndexPattern(t *testing.T) {
  50. // numeric: render inputs without valid mentions
  51. test := func(s string) {
  52. testRenderIssueIndexPattern(t, s, s, nil)
  53. testRenderIssueIndexPattern(t, s, s, &postProcessCtx{metas: numericMetas})
  54. }
  55. // should not render anything when there are no mentions
  56. test("")
  57. test("this is a test")
  58. test("test 123 123 1234")
  59. test("#")
  60. test("# # #")
  61. test("# 123")
  62. test("#abcd")
  63. test("test#1234")
  64. test("#1234test")
  65. test(" test #1234test")
  66. test("/home/gitea/#1234")
  67. // should not render issue mention without leading space
  68. test("test#54321 issue")
  69. // should not render issue mention without trailing space
  70. test("test #54321issue")
  71. }
  72. func TestRender_IssueIndexPattern2(t *testing.T) {
  73. setting.AppURL = AppURL
  74. setting.AppSubURL = AppSubURL
  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(util.URLJoin(setting.AppSubURL, "issues"), "issue", index)
  80. }
  81. expectedNil := fmt.Sprintf(expectedFmt, links...)
  82. testRenderIssueIndexPattern(t, s, expectedNil, &postProcessCtx{metas: localMetas})
  83. for i, index := range indices {
  84. links[i] = numericIssueLink("https://someurl.com/someUser/someRepo/", "issue", index)
  85. }
  86. expectedNum := fmt.Sprintf(expectedFmt, links...)
  87. testRenderIssueIndexPattern(t, s, expectedNum, &postProcessCtx{metas: 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. test("fixes issue #1234.", "fixes issue %s.", 1234)
  94. // should render mentions in parentheses / brackets
  95. test("(#54321 issue)", "(%s issue)", 54321)
  96. test("[#54321 issue]", "[%s issue]", 54321)
  97. test("test (#9801 extra) issue", "test (%s extra) issue", 9801)
  98. test("test (#1)", "test (%s)", 1)
  99. // should render multiple issue mentions in the same line
  100. test("#54321 #1243", "%s %s", 54321, 1243)
  101. test("wow (#54321 #1243)", "wow (%s %s)", 54321, 1243)
  102. test("(#4)(#5)", "(%s)(%s)", 4, 5)
  103. test("#1 (#4321) test", "%s (%s) test", 1, 4321)
  104. // should render with :
  105. test("#1234: test", "%s: test", 1234)
  106. test("wow (#54321: test)", "wow (%s: test)", 54321)
  107. }
  108. func TestRender_IssueIndexPattern3(t *testing.T) {
  109. setting.AppURL = AppURL
  110. setting.AppSubURL = AppSubURL
  111. // alphanumeric: render inputs without valid mentions
  112. test := func(s string) {
  113. testRenderIssueIndexPattern(t, s, s, &postProcessCtx{metas: alphanumericMetas})
  114. }
  115. test("")
  116. test("this is a test")
  117. test("test 123 123 1234")
  118. test("#")
  119. test("# 123")
  120. test("#abcd")
  121. test("test #123")
  122. test("abc-1234") // issue prefix must be capital
  123. test("ABc-1234") // issue prefix must be _all_ capital
  124. test("ABCDEFGHIJK-1234") // the limit is 10 characters in the prefix
  125. test("ABC1234") // dash is required
  126. test("test ABC- test") // number is required
  127. test("test -1234 test") // prefix is required
  128. test("testABC-123 test") // leading space is required
  129. test("test ABC-123test") // trailing space is required
  130. test("ABC-0123") // no leading zero
  131. }
  132. func TestRender_IssueIndexPattern4(t *testing.T) {
  133. setting.AppURL = AppURL
  134. setting.AppSubURL = AppSubURL
  135. // alphanumeric: render inputs with valid mentions
  136. test := func(s, expectedFmt string, names ...string) {
  137. links := make([]interface{}, len(names))
  138. for i, name := range names {
  139. links[i] = alphanumIssueLink("https://someurl.com/someUser/someRepo/", "issue", name)
  140. }
  141. expected := fmt.Sprintf(expectedFmt, links...)
  142. testRenderIssueIndexPattern(t, s, expected, &postProcessCtx{metas: alphanumericMetas})
  143. }
  144. test("OTT-1234 test", "%s test", "OTT-1234")
  145. test("test T-12 issue", "test %s issue", "T-12")
  146. test("test issue ABCDEFGHIJ-1234567890", "test issue %s", "ABCDEFGHIJ-1234567890")
  147. }
  148. func testRenderIssueIndexPattern(t *testing.T, input, expected string, ctx *postProcessCtx) {
  149. if ctx == nil {
  150. ctx = new(postProcessCtx)
  151. }
  152. ctx.procs = []processor{issueIndexPatternProcessor}
  153. if ctx.urlPrefix == "" {
  154. ctx.urlPrefix = AppSubURL
  155. }
  156. res, err := ctx.postProcess([]byte(input))
  157. assert.NoError(t, err)
  158. assert.Equal(t, expected, string(res))
  159. }
  160. func TestRender_AutoLink(t *testing.T) {
  161. setting.AppURL = AppURL
  162. setting.AppSubURL = AppSubURL
  163. test := func(input, expected string) {
  164. buffer, err := PostProcess([]byte(input), setting.AppSubURL, localMetas, false)
  165. assert.Equal(t, err, nil)
  166. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
  167. buffer, err = PostProcess([]byte(input), setting.AppSubURL, localMetas, true)
  168. assert.Equal(t, err, nil)
  169. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
  170. }
  171. // render valid issue URLs
  172. test(util.URLJoin(setting.AppSubURL, "issues", "3333"),
  173. numericIssueLink(util.URLJoin(setting.AppSubURL, "issues"), "issue", 3333))
  174. // render valid commit URLs
  175. tmp := util.URLJoin(AppSubURL, "commit", "d8a994ef243349f321568f9e36d5c3f444b99cae")
  176. test(tmp, "<a href=\""+tmp+"\" class=\"commit\"><code class=\"nohighlight\">d8a994ef24</code></a>")
  177. tmp += "#diff-2"
  178. test(tmp, "<a href=\""+tmp+"\" class=\"commit\"><code class=\"nohighlight\">d8a994ef24 (diff-2)</code></a>")
  179. // render other commit URLs
  180. tmp = "https://external-link.gitea.io/go-gitea/gitea/commit/d8a994ef243349f321568f9e36d5c3f444b99cae#diff-2"
  181. test(tmp, "<a href=\""+tmp+"\" class=\"commit\"><code class=\"nohighlight\">d8a994ef24 (diff-2)</code></a>")
  182. }
  183. func TestRender_FullIssueURLs(t *testing.T) {
  184. setting.AppURL = AppURL
  185. setting.AppSubURL = AppSubURL
  186. test := func(input, expected string) {
  187. ctx := new(postProcessCtx)
  188. ctx.procs = []processor{fullIssuePatternProcessor}
  189. if ctx.urlPrefix == "" {
  190. ctx.urlPrefix = AppSubURL
  191. }
  192. ctx.metas = localMetas
  193. result, err := ctx.postProcess([]byte(input))
  194. assert.NoError(t, err)
  195. assert.Equal(t, expected, string(result))
  196. }
  197. test("Here is a link https://git.osgeo.org/gogs/postgis/postgis/pulls/6",
  198. "Here is a link https://git.osgeo.org/gogs/postgis/postgis/pulls/6")
  199. test("Look here http://localhost:3000/person/repo/issues/4",
  200. `Look here <a href="http://localhost:3000/person/repo/issues/4" class="issue">person/repo#4</a>`)
  201. test("http://localhost:3000/person/repo/issues/4#issuecomment-1234",
  202. `<a href="http://localhost:3000/person/repo/issues/4#issuecomment-1234" class="issue">person/repo#4</a>`)
  203. test("http://localhost:3000/gogits/gogs/issues/4",
  204. `<a href="http://localhost:3000/gogits/gogs/issues/4" class="issue">#4</a>`)
  205. }
  206. func TestRegExp_sha1CurrentPattern(t *testing.T) {
  207. trueTestCases := []string{
  208. "d8a994ef243349f321568f9e36d5c3f444b99cae",
  209. "abcdefabcdefabcdefabcdefabcdefabcdefabcd",
  210. "(abcdefabcdefabcdefabcdefabcdefabcdefabcd)",
  211. "[abcdefabcdefabcdefabcdefabcdefabcdefabcd]",
  212. "abcdefabcdefabcdefabcdefabcdefabcdefabcd.",
  213. }
  214. falseTestCases := []string{
  215. "test",
  216. "abcdefg",
  217. "e59ff077-2d03-4e6b-964d-63fbaea81f",
  218. "abcdefghijklmnopqrstuvwxyzabcdefghijklmn",
  219. "abcdefghijklmnopqrstuvwxyzabcdefghijklmO",
  220. }
  221. for _, testCase := range trueTestCases {
  222. assert.True(t, sha1CurrentPattern.MatchString(testCase))
  223. }
  224. for _, testCase := range falseTestCases {
  225. assert.False(t, sha1CurrentPattern.MatchString(testCase))
  226. }
  227. }
  228. func TestRegExp_anySHA1Pattern(t *testing.T) {
  229. testCases := map[string][]string{
  230. "https://github.com/jquery/jquery/blob/a644101ed04d0beacea864ce805e0c4f86ba1cd1/test/unit/event.js#L2703": {
  231. "a644101ed04d0beacea864ce805e0c4f86ba1cd1",
  232. "/test/unit/event.js",
  233. "#L2703",
  234. },
  235. "https://github.com/jquery/jquery/blob/a644101ed04d0beacea864ce805e0c4f86ba1cd1/test/unit/event.js": {
  236. "a644101ed04d0beacea864ce805e0c4f86ba1cd1",
  237. "/test/unit/event.js",
  238. "",
  239. },
  240. "https://github.com/jquery/jquery/commit/0705be475092aede1eddae01319ec931fb9c65fc": {
  241. "0705be475092aede1eddae01319ec931fb9c65fc",
  242. "",
  243. "",
  244. },
  245. "https://github.com/jquery/jquery/tree/0705be475092aede1eddae01319ec931fb9c65fc/src": {
  246. "0705be475092aede1eddae01319ec931fb9c65fc",
  247. "/src",
  248. "",
  249. },
  250. "https://try.gogs.io/gogs/gogs/commit/d8a994ef243349f321568f9e36d5c3f444b99cae#diff-2": {
  251. "d8a994ef243349f321568f9e36d5c3f444b99cae",
  252. "",
  253. "#diff-2",
  254. },
  255. }
  256. for k, v := range testCases {
  257. assert.Equal(t, anySHA1Pattern.FindStringSubmatch(k)[1:], v)
  258. }
  259. }
  260. func TestRegExp_shortLinkPattern(t *testing.T) {
  261. trueTestCases := []string{
  262. "[[stuff]]",
  263. "[[]]",
  264. "[[stuff|title=Difficult name with spaces*!]]",
  265. }
  266. falseTestCases := []string{
  267. "test",
  268. "abcdefg",
  269. "[[]",
  270. "[[",
  271. "[]",
  272. "]]",
  273. "abcdefghijklmnopqrstuvwxyz",
  274. }
  275. for _, testCase := range trueTestCases {
  276. assert.True(t, shortLinkPattern.MatchString(testCase))
  277. }
  278. for _, testCase := range falseTestCases {
  279. assert.False(t, shortLinkPattern.MatchString(testCase))
  280. }
  281. }
上海开阖软件有限公司 沪ICP备12045867号-1