@@ -7,26 +7,37 @@ import (
77 go_context "context"
88 "io"
99 "net/http"
10+ "os"
1011 "path"
1112 "strings"
1213 "testing"
1314
15+ repo_model "code.gitea.io/gitea/models/repo"
16+ "code.gitea.io/gitea/models/unittest"
1417 "code.gitea.io/gitea/modules/markup"
1518 "code.gitea.io/gitea/modules/setting"
1619 api "code.gitea.io/gitea/modules/structs"
1720 "code.gitea.io/gitea/modules/test"
1821 "code.gitea.io/gitea/modules/web"
22+ context_service "code.gitea.io/gitea/services/context"
1923 "code.gitea.io/gitea/services/contexttest"
2024
2125 "github.com/stretchr/testify/assert"
2226)
2327
2428const AppURL = "http://localhost:3000/"
2529
30+ func TestMain (m * testing.M ) {
31+ unittest .MainTest (m , & unittest.TestOptions {
32+ FixtureFiles : []string {"repository.yml" , "user.yml" },
33+ })
34+ os .Exit (m .Run ())
35+ }
36+
2637func testRenderMarkup (t * testing.T , mode string , wiki bool , filePath , text , expectedBody string , expectedCode int ) {
2738 setting .AppURL = AppURL
2839 defer test .MockVariableValue (& markup .RenderBehaviorForTesting .DisableAdditionalAttributes , true )()
29- context := "/gogits/gogs "
40+ context := "/user2/repo1 "
3041 if ! wiki {
3142 context += path .Join ("/src/branch/main" , path .Dir (filePath ))
3243 }
@@ -38,6 +49,8 @@ func testRenderMarkup(t *testing.T, mode string, wiki bool, filePath, text, expe
3849 FilePath : filePath ,
3950 }
4051 ctx , resp := contexttest .MockAPIContext (t , "POST /api/v1/markup" )
52+ ctx .Repo = & context_service.Repository {}
53+ ctx .Repo .Repository = unittest .AssertExistsAndLoadBean (t , & repo_model.Repository {ID : 1 })
4154 web .SetForm (ctx , & options )
4255 Markup (ctx )
4356 assert .Equal (t , expectedBody , resp .Body .String ())
@@ -48,7 +61,7 @@ func testRenderMarkup(t *testing.T, mode string, wiki bool, filePath, text, expe
4861func testRenderMarkdown (t * testing.T , mode string , wiki bool , text , responseBody string , responseCode int ) {
4962 defer test .MockVariableValue (& markup .RenderBehaviorForTesting .DisableAdditionalAttributes , true )()
5063 setting .AppURL = AppURL
51- context := "/gogits/gogs "
64+ context := "/user2/repo1 "
5265 if ! wiki {
5366 context += "/src/branch/main"
5467 }
@@ -67,6 +80,7 @@ func testRenderMarkdown(t *testing.T, mode string, wiki bool, text, responseBody
6780}
6881
6982func TestAPI_RenderGFM (t * testing.T ) {
83+ unittest .PrepareTestEnv (t )
7084 markup .Init (& markup.RenderHelperFuncs {
7185 IsUsernameMentionable : func (ctx go_context.Context , username string ) bool {
7286 return username == "r-lyeh"
@@ -82,20 +96,20 @@ func TestAPI_RenderGFM(t *testing.T) {
8296 // rendered
8397 `<p>Wiki! Enjoy :)</p>
8498<ul>
85- <li><a href="http://localhost:3000/gogits/gogs /wiki/Links" rel="nofollow">Links, Language bindings, Engine bindings</a></li>
86- <li><a href="http://localhost:3000/gogits/gogs /wiki/Tips" rel="nofollow">Tips</a></li>
99+ <li><a href="http://localhost:3000/user2/repo1 /wiki/Links" rel="nofollow">Links, Language bindings, Engine bindings</a></li>
100+ <li><a href="http://localhost:3000/user2/repo1 /wiki/Tips" rel="nofollow">Tips</a></li>
87101<li>Bezier widget (by <a href="http://localhost:3000/r-lyeh" rel="nofollow">@r-lyeh</a>) <a href="https://github.com/ocornut/imgui/issues/786" rel="nofollow">https://github.com/ocornut/imgui/issues/786</a></li>
88102</ul>
89103` ,
90104 // Guard wiki sidebar: special syntax
91105 `[[Guardfile-DSL / Configuring-Guard|Guardfile-DSL---Configuring-Guard]]` ,
92106 // rendered
93- `<p><a href="http://localhost:3000/gogits/gogs /wiki/Guardfile-DSL---Configuring-Guard" rel="nofollow">Guardfile-DSL / Configuring-Guard</a></p>
107+ `<p><a href="http://localhost:3000/user2/repo1 /wiki/Guardfile-DSL---Configuring-Guard" rel="nofollow">Guardfile-DSL / Configuring-Guard</a></p>
94108` ,
95109 // special syntax
96110 `[[Name|Link]]` ,
97111 // rendered
98- `<p><a href="http://localhost:3000/gogits/gogs /wiki/Link" rel="nofollow">Name</a></p>
112+ `<p><a href="http://localhost:3000/user2/repo1 /wiki/Link" rel="nofollow">Name</a></p>
99113` ,
100114 // empty
101115 `` ,
@@ -119,8 +133,8 @@ Here are some links to the most important topics. You can find the full list of
119133<p><strong>Wine Staging</strong> on website <a href="http://wine-staging.com" rel="nofollow">wine-staging.com</a>.</p>
120134<h2 id="user-content-quick-links">Quick Links</h2>
121135<p>Here are some links to the most important topics. You can find the full list of pages at the sidebar.</p>
122- <p><a href="http://localhost:3000/gogits/gogs /wiki/Configuration" rel="nofollow">Configuration</a>
123- <a href="http://localhost:3000/gogits/gogs /wiki/raw/images/icon-bug.png" rel="nofollow"><img src="http://localhost:3000/gogits/gogs /wiki/raw/images/icon-bug.png" title="icon-bug.png" alt="images/icon-bug.png"/></a></p>
136+ <p><a href="http://localhost:3000/user2/repo1 /wiki/Configuration" rel="nofollow">Configuration</a>
137+ <a href="http://localhost:3000/user2/repo1 /wiki/raw/images/icon-bug.png" rel="nofollow"><img src="http://localhost:3000/user2/repo1 /wiki/raw/images/icon-bug.png" title="icon-bug.png" alt="images/icon-bug.png"/></a></p>
124138` ,
125139 }
126140
@@ -143,20 +157,20 @@ Here are some links to the most important topics. You can find the full list of
143157 }
144158
145159 input := "[Link](test.md)\n "
146- testRenderMarkdown (t , "gfm" , false , input , `<p><a href="http://localhost:3000/gogits/gogs /src/branch/main/test.md" rel="nofollow">Link</a>
147- <a href="http://localhost:3000/gogits/gogs /media/branch/main/image.png" target="_blank" rel="nofollow noopener"><img src="http://localhost:3000/gogits/gogs /media/branch/main/image.png" alt="Image"/></a></p>
160+ testRenderMarkdown (t , "gfm" , false , input , `<p><a href="http://localhost:3000/user2/repo1 /src/branch/main/test.md" rel="nofollow">Link</a>
161+ <a href="http://localhost:3000/user2/repo1 /media/branch/main/image.png" target="_blank" rel="nofollow noopener"><img src="http://localhost:3000/user2/repo1 /media/branch/main/image.png" alt="Image"/></a></p>
148162` , http .StatusOK )
149163
150- testRenderMarkdown (t , "gfm" , false , input , `<p><a href="http://localhost:3000/gogits/gogs /src/branch/main/test.md" rel="nofollow">Link</a>
151- <a href="http://localhost:3000/gogits/gogs /media/branch/main/image.png" target="_blank" rel="nofollow noopener"><img src="http://localhost:3000/gogits/gogs /media/branch/main/image.png" alt="Image"/></a></p>
164+ testRenderMarkdown (t , "gfm" , false , input , `<p><a href="http://localhost:3000/user2/repo1 /src/branch/main/test.md" rel="nofollow">Link</a>
165+ <a href="http://localhost:3000/user2/repo1 /media/branch/main/image.png" target="_blank" rel="nofollow noopener"><img src="http://localhost:3000/user2/repo1 /media/branch/main/image.png" alt="Image"/></a></p>
152166` , http .StatusOK )
153167
154- testRenderMarkup (t , "gfm" , false , "" , input , `<p><a href="http://localhost:3000/gogits/gogs /src/branch/main/test.md" rel="nofollow">Link</a>
155- <a href="http://localhost:3000/gogits/gogs /media/branch/main/image.png" target="_blank" rel="nofollow noopener"><img src="http://localhost:3000/gogits/gogs /media/branch/main/image.png" alt="Image"/></a></p>
168+ testRenderMarkup (t , "gfm" , false , "" , input , `<p><a href="http://localhost:3000/user2/repo1 /src/branch/main/test.md" rel="nofollow">Link</a>
169+ <a href="http://localhost:3000/user2/repo1 /media/branch/main/image.png" target="_blank" rel="nofollow noopener"><img src="http://localhost:3000/user2/repo1 /media/branch/main/image.png" alt="Image"/></a></p>
156170` , http .StatusOK )
157171
158- testRenderMarkup (t , "file" , false , "path/new-file.md" , input , `<p><a href="http://localhost:3000/gogits/gogs /src/branch/main/path/test.md" rel="nofollow">Link</a>
159- <a href="http://localhost:3000/gogits/gogs /media/branch/main/path/image.png" target="_blank" rel="nofollow noopener"><img src="http://localhost:3000/gogits/gogs /media/branch/main/path/image.png" alt="Image"/></a></p>
172+ testRenderMarkup (t , "file" , false , "path/new-file.md" , input , `<p><a href="http://localhost:3000/user2/repo1 /src/branch/main/path/test.md" rel="nofollow">Link</a>
173+ <a href="http://localhost:3000/user2/repo1 /media/branch/main/path/image.png" target="_blank" rel="nofollow noopener"><img src="http://localhost:3000/user2/repo1 /media/branch/main/path/image.png" alt="Image"/></a></p>
160174` , http .StatusOK )
161175
162176 testRenderMarkup (t , "file" , false , "path/test.unknown" , "## Test" , "unsupported file to render: \" path/test.unknown\" \n " , http .StatusUnprocessableEntity )
@@ -186,7 +200,7 @@ func TestAPI_RenderSimple(t *testing.T) {
186200 options := api.MarkdownOption {
187201 Mode : "markdown" ,
188202 Text : "" ,
189- Context : "/gogits/gogs " ,
203+ Context : "/user2/repo1 " ,
190204 }
191205 ctx , resp := contexttest .MockAPIContext (t , "POST /api/v1/markdown" )
192206 for i := 0 ; i < len (simpleCases ); i += 2 {
0 commit comments