From 44909f6e2c8f94b3153cb5114078e4eebe65a4a8 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Tue, 26 Nov 2024 03:04:55 +0800 Subject: [PATCH] Fix markup render regression and fix some tests (#32640) Fix #32639, https://github.com/go-gitea/gitea/issues/32608#issuecomment-2497918210 By the way, fix some incorrect SQLs (use single quote but not double quote) --- models/activities/action.go | 4 +-- models/activities/action_test.go | 2 +- models/issues/label_test.go | 2 +- modules/indexer/code/indexer_test.go | 2 -- routers/api/v1/misc/markup_test.go | 48 ++++++++++++++++++---------- routers/common/markup.go | 2 ++ 6 files changed, 37 insertions(+), 23 deletions(-) diff --git a/models/activities/action.go b/models/activities/action.go index e74deef1df..546d4340ae 100644 --- a/models/activities/action.go +++ b/models/activities/action.go @@ -770,7 +770,7 @@ func DeleteIssueActions(ctx context.Context, repoID, issueID, issueIndex int64) // CountActionCreatedUnixString count actions where created_unix is an empty string func CountActionCreatedUnixString(ctx context.Context) (int64, error) { if setting.Database.Type.IsSQLite3() { - return db.GetEngine(ctx).Where(`created_unix = ""`).Count(new(Action)) + return db.GetEngine(ctx).Where(`created_unix = ''`).Count(new(Action)) } return 0, nil } @@ -778,7 +778,7 @@ func CountActionCreatedUnixString(ctx context.Context) (int64, error) { // FixActionCreatedUnixString set created_unix to zero if it is an empty string func FixActionCreatedUnixString(ctx context.Context) (int64, error) { if setting.Database.Type.IsSQLite3() { - res, err := db.GetEngine(ctx).Exec(`UPDATE action SET created_unix = 0 WHERE created_unix = ""`) + res, err := db.GetEngine(ctx).Exec(`UPDATE action SET created_unix = 0 WHERE created_unix = ''`) if err != nil { return 0, err } diff --git a/models/activities/action_test.go b/models/activities/action_test.go index e5dee33ae0..64330ebbb3 100644 --- a/models/activities/action_test.go +++ b/models/activities/action_test.go @@ -256,7 +256,7 @@ func TestConsistencyUpdateAction(t *testing.T) { unittest.AssertExistsAndLoadBean(t, &activities_model.Action{ ID: int64(id), }) - _, err := db.GetEngine(db.DefaultContext).Exec(`UPDATE action SET created_unix = "" WHERE id = ?`, id) + _, err := db.GetEngine(db.DefaultContext).Exec(`UPDATE action SET created_unix = '' WHERE id = ?`, id) assert.NoError(t, err) actions := make([]*activities_model.Action, 0, 1) // diff --git a/models/issues/label_test.go b/models/issues/label_test.go index a0cc8e6d75..c2ff084c23 100644 --- a/models/issues/label_test.go +++ b/models/issues/label_test.go @@ -406,7 +406,7 @@ func TestDeleteIssueLabel(t *testing.T) { PosterID: doerID, IssueID: issueID, LabelID: labelID, - }, `content=""`) + }, `content=''`) label = unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: labelID}) assert.EqualValues(t, expectedNumIssues, label.NumIssues) assert.EqualValues(t, expectedNumClosedIssues, label.NumClosedIssues) diff --git a/modules/indexer/code/indexer_test.go b/modules/indexer/code/indexer_test.go index 78fbe7f792..d04088531a 100644 --- a/modules/indexer/code/indexer_test.go +++ b/modules/indexer/code/indexer_test.go @@ -22,8 +22,6 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - - _ "github.com/mattn/go-sqlite3" ) type codeSearchResult struct { diff --git a/routers/api/v1/misc/markup_test.go b/routers/api/v1/misc/markup_test.go index 921e7b2750..6063e54cdc 100644 --- a/routers/api/v1/misc/markup_test.go +++ b/routers/api/v1/misc/markup_test.go @@ -7,15 +7,19 @@ import ( go_context "context" "io" "net/http" + "os" "path" "strings" "testing" + repo_model "code.gitea.io/gitea/models/repo" + "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/test" "code.gitea.io/gitea/modules/web" + context_service "code.gitea.io/gitea/services/context" "code.gitea.io/gitea/services/contexttest" "github.com/stretchr/testify/assert" @@ -23,10 +27,17 @@ import ( const AppURL = "http://localhost:3000/" +func TestMain(m *testing.M) { + unittest.MainTest(m, &unittest.TestOptions{ + FixtureFiles: []string{"repository.yml", "user.yml"}, + }) + os.Exit(m.Run()) +} + func testRenderMarkup(t *testing.T, mode string, wiki bool, filePath, text, expectedBody string, expectedCode int) { setting.AppURL = AppURL defer test.MockVariableValue(&markup.RenderBehaviorForTesting.DisableAdditionalAttributes, true)() - context := "/gogits/gogs" + context := "/user2/repo1" if !wiki { context += path.Join("/src/branch/main", path.Dir(filePath)) } @@ -38,6 +49,8 @@ func testRenderMarkup(t *testing.T, mode string, wiki bool, filePath, text, expe FilePath: filePath, } ctx, resp := contexttest.MockAPIContext(t, "POST /api/v1/markup") + ctx.Repo = &context_service.Repository{} + ctx.Repo.Repository = unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) web.SetForm(ctx, &options) Markup(ctx) assert.Equal(t, expectedBody, resp.Body.String()) @@ -48,7 +61,7 @@ func testRenderMarkup(t *testing.T, mode string, wiki bool, filePath, text, expe func testRenderMarkdown(t *testing.T, mode string, wiki bool, text, responseBody string, responseCode int) { defer test.MockVariableValue(&markup.RenderBehaviorForTesting.DisableAdditionalAttributes, true)() setting.AppURL = AppURL - context := "/gogits/gogs" + context := "/user2/repo1" if !wiki { context += "/src/branch/main" } @@ -67,6 +80,7 @@ func testRenderMarkdown(t *testing.T, mode string, wiki bool, text, responseBody } func TestAPI_RenderGFM(t *testing.T) { + unittest.PrepareTestEnv(t) markup.Init(&markup.RenderHelperFuncs{ IsUsernameMentionable: func(ctx go_context.Context, username string) bool { return username == "r-lyeh" @@ -82,20 +96,20 @@ func TestAPI_RenderGFM(t *testing.T) { // rendered `

Wiki! Enjoy :)

`, // Guard wiki sidebar: special syntax `[[Guardfile-DSL / Configuring-Guard|Guardfile-DSL---Configuring-Guard]]`, // rendered - `

Guardfile-DSL / Configuring-Guard

+ `

Guardfile-DSL / Configuring-Guard

`, // special syntax `[[Name|Link]]`, // rendered - `

Name

+ `

Name

`, // empty ``, @@ -119,8 +133,8 @@ Here are some links to the most important topics. You can find the full list of

Wine Staging on website wine-staging.com.

Here are some links to the most important topics. You can find the full list of pages at the sidebar.

-

Configuration -images/icon-bug.png

+

Configuration +images/icon-bug.png

`, } @@ -143,20 +157,20 @@ Here are some links to the most important topics. You can find the full list of } input := "[Link](test.md)\n![Image](image.png)" - testRenderMarkdown(t, "gfm", false, input, `

Link -Image

+ testRenderMarkdown(t, "gfm", false, input, `

Link +Image

`, http.StatusOK) - testRenderMarkdown(t, "gfm", false, input, `

Link -Image

+ testRenderMarkdown(t, "gfm", false, input, `

Link +Image

`, http.StatusOK) - testRenderMarkup(t, "gfm", false, "", input, `

Link -Image

+ testRenderMarkup(t, "gfm", false, "", input, `

Link +Image

`, http.StatusOK) - testRenderMarkup(t, "file", false, "path/new-file.md", input, `

Link -Image

+ testRenderMarkup(t, "file", false, "path/new-file.md", input, `

Link +Image

`, http.StatusOK) 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) { options := api.MarkdownOption{ Mode: "markdown", Text: "", - Context: "/gogits/gogs", + Context: "/user2/repo1", } ctx, resp := contexttest.MockAPIContext(t, "POST /api/v1/markdown") for i := 0; i < len(simpleCases); i += 2 { diff --git a/routers/common/markup.go b/routers/common/markup.go index e3e6d9cfcf..533b546a2a 100644 --- a/routers/common/markup.go +++ b/routers/common/markup.go @@ -77,8 +77,10 @@ func RenderMarkup(ctx *context.Base, ctxRepo *context.Repository, mode, text, ur rctx = rctx.WithMarkupType(markdown.MarkupName) case "comment": rctx = renderhelper.NewRenderContextRepoComment(ctx, repoModel, renderhelper.RepoCommentOptions{DeprecatedOwnerName: repoOwnerName, DeprecatedRepoName: repoName}) + rctx = rctx.WithMarkupType(markdown.MarkupName) case "wiki": rctx = renderhelper.NewRenderContextRepoWiki(ctx, repoModel, renderhelper.RepoWikiOptions{DeprecatedOwnerName: repoOwnerName, DeprecatedRepoName: repoName}) + rctx = rctx.WithMarkupType(markdown.MarkupName) case "file": rctx = renderhelper.NewRenderContextRepoFile(ctx, repoModel, renderhelper.RepoFileOptions{ DeprecatedOwnerName: repoOwnerName, DeprecatedRepoName: repoName,