From 36a2d2f919ee445f0374c210309b336e7c3c1f8e Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Mon, 12 Dec 2022 20:45:21 +0800 Subject: [PATCH] Add a simple test for external renderer (#20033) Fix #16402 --- models/db/iterate_test.go | 2 +- models/fixtures/repo_unit.yml | 6 +++ models/fixtures/repository.yml | 27 ++++++++++++ models/fixtures/user.yml | 2 +- models/repo/repo_list_test.go | 6 +-- routers/web/user/home_test.go | 2 +- .../user30/renderer.git/HEAD | 1 + .../user30/renderer.git/config | 6 +++ .../user30/renderer.git/description | 1 + .../user30/renderer.git/hooks/post-receive | 15 +++++++ .../renderer.git/hooks/post-receive.d/gitea | 2 + .../user30/renderer.git/hooks/pre-receive | 15 +++++++ .../renderer.git/hooks/pre-receive.d/gitea | 2 + .../user30/renderer.git/hooks/update | 14 ++++++ .../user30/renderer.git/hooks/update.d/gitea | 2 + .../user30/renderer.git/info/exclude | 6 +++ .../06/0d5c2acd8bf4b6f14010acd1a73d73392ec46e | Bin 0 -> 56 bytes .../45/14a93050edb2c3165bdd0a3c03be063e879e68 | Bin 0 -> 50 bytes .../c9/61cc4d1ba6b7ee1ba228a9a02b00b7746d8033 | Bin 0 -> 789 bytes .../user30/renderer.git/packed-refs | 2 + tests/integration/api_repo_test.go | 6 +-- tests/integration/markup_external_test.go | 40 ++++++++++++++++++ tests/sqlite.ini.tmpl | 7 +++ tools/external_renderer.go | 23 ++++++++++ 24 files changed, 178 insertions(+), 9 deletions(-) create mode 100644 tests/gitea-repositories-meta/user30/renderer.git/HEAD create mode 100644 tests/gitea-repositories-meta/user30/renderer.git/config create mode 100644 tests/gitea-repositories-meta/user30/renderer.git/description create mode 100644 tests/gitea-repositories-meta/user30/renderer.git/hooks/post-receive create mode 100644 tests/gitea-repositories-meta/user30/renderer.git/hooks/post-receive.d/gitea create mode 100644 tests/gitea-repositories-meta/user30/renderer.git/hooks/pre-receive create mode 100644 tests/gitea-repositories-meta/user30/renderer.git/hooks/pre-receive.d/gitea create mode 100644 tests/gitea-repositories-meta/user30/renderer.git/hooks/update create mode 100644 tests/gitea-repositories-meta/user30/renderer.git/hooks/update.d/gitea create mode 100644 tests/gitea-repositories-meta/user30/renderer.git/info/exclude create mode 100644 tests/gitea-repositories-meta/user30/renderer.git/objects/06/0d5c2acd8bf4b6f14010acd1a73d73392ec46e create mode 100644 tests/gitea-repositories-meta/user30/renderer.git/objects/45/14a93050edb2c3165bdd0a3c03be063e879e68 create mode 100644 tests/gitea-repositories-meta/user30/renderer.git/objects/c9/61cc4d1ba6b7ee1ba228a9a02b00b7746d8033 create mode 100644 tests/gitea-repositories-meta/user30/renderer.git/packed-refs create mode 100644 tests/integration/markup_external_test.go create mode 100644 tools/external_renderer.go diff --git a/models/db/iterate_test.go b/models/db/iterate_test.go index b0ea8b53ef1..57b1bd3cd19 100644 --- a/models/db/iterate_test.go +++ b/models/db/iterate_test.go @@ -25,7 +25,7 @@ func TestIterate(t *testing.T) { return nil }) assert.NoError(t, err) - assert.EqualValues(t, 79, repoCnt) + assert.EqualValues(t, 80, repoCnt) err = db.Iterate(db.DefaultContext, nil, func(ctx context.Context, repoUnit *repo_model.RepoUnit) error { reopUnit2 := repo_model.RepoUnit{ID: repoUnit.ID} diff --git a/models/fixtures/repo_unit.yml b/models/fixtures/repo_unit.yml index 59ab6183407..73db47218bc 100644 --- a/models/fixtures/repo_unit.yml +++ b/models/fixtures/repo_unit.yml @@ -544,3 +544,9 @@ repo_id: 51 type: 2 created_unix: 946684810 + +- + id: 80 + repo_id: 53 + type: 1 + created_unix: 946684810 diff --git a/models/fixtures/repository.yml b/models/fixtures/repository.yml index f09953be7e5..a5a3dd0601f 100644 --- a/models/fixtures/repository.yml +++ b/models/fixtures/repository.yml @@ -1558,3 +1558,30 @@ size: 0 is_fsck_enabled: true close_issues_via_commit_in_any_branch: false + +- + id: 53 + owner_id: 30 + owner_name: user30 + lower_name: renderer + name: renderer + is_archived: false + is_empty: false + is_private: false + num_issues: 0 + num_closed_issues: 0 + num_pulls: 0 + num_closed_pulls: 0 + num_milestones: 0 + num_closed_milestones: 0 + num_watches: 0 + num_projects: 0 + num_closed_projects: 0 + status: 0 + is_fork: false + fork_id: 0 + is_template: false + template_id: 0 + size: 0 + is_fsck_enabled: true + close_issues_via_commit_in_any_branch: false diff --git a/models/fixtures/user.yml b/models/fixtures/user.yml index 0e3348e146a..1b870e8999b 100644 --- a/models/fixtures/user.yml +++ b/models/fixtures/user.yml @@ -1102,7 +1102,7 @@ num_followers: 0 num_following: 0 num_stars: 0 - num_repos: 3 + num_repos: 4 num_teams: 0 num_members: 0 visibility: 0 diff --git a/models/repo/repo_list_test.go b/models/repo/repo_list_test.go index bfbceb7e2ec..d87d70c189a 100644 --- a/models/repo/repo_list_test.go +++ b/models/repo/repo_list_test.go @@ -235,12 +235,12 @@ func TestSearchRepository(t *testing.T) { { name: "AllPublic/PublicRepositoriesOfUserIncludingCollaborative", opts: &repo_model.SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 15, AllPublic: true, Template: util.OptionalBoolFalse}, - count: 28, + count: 29, }, { name: "AllPublic/PublicAndPrivateRepositoriesOfUserIncludingCollaborative", opts: &repo_model.SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 15, Private: true, AllPublic: true, AllLimited: true, Template: util.OptionalBoolFalse}, - count: 33, + count: 34, }, { name: "AllPublic/PublicAndPrivateRepositoriesOfUserIncludingCollaborativeByName", @@ -255,7 +255,7 @@ func TestSearchRepository(t *testing.T) { { name: "AllPublic/PublicRepositoriesOfOrganization", opts: &repo_model.SearchRepoOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 10}, OwnerID: 17, AllPublic: true, Collaborate: util.OptionalBoolFalse, Template: util.OptionalBoolFalse}, - count: 28, + count: 29, }, { name: "AllTemplates", diff --git a/routers/web/user/home_test.go b/routers/web/user/home_test.go index a75b3573a00..534b0b26207 100644 --- a/routers/web/user/home_test.go +++ b/routers/web/user/home_test.go @@ -26,7 +26,7 @@ func TestArchivedIssues(t *testing.T) { // Assume: User 30 has access to two Repos with Issues, one of the Repos being archived. repos, _, _ := repo_model.GetUserRepositories(&repo_model.SearchRepoOptions{Actor: ctx.Doer}) - assert.Len(t, repos, 2) + assert.Len(t, repos, 3) IsArchived := make(map[int64]bool) NumIssues := make(map[int64]int) for _, repo := range repos { diff --git a/tests/gitea-repositories-meta/user30/renderer.git/HEAD b/tests/gitea-repositories-meta/user30/renderer.git/HEAD new file mode 100644 index 00000000000..cb089cd89a7 --- /dev/null +++ b/tests/gitea-repositories-meta/user30/renderer.git/HEAD @@ -0,0 +1 @@ +ref: refs/heads/master diff --git a/tests/gitea-repositories-meta/user30/renderer.git/config b/tests/gitea-repositories-meta/user30/renderer.git/config new file mode 100644 index 00000000000..e6da231579b --- /dev/null +++ b/tests/gitea-repositories-meta/user30/renderer.git/config @@ -0,0 +1,6 @@ +[core] + repositoryformatversion = 0 + filemode = true + bare = true + ignorecase = true + precomposeunicode = true diff --git a/tests/gitea-repositories-meta/user30/renderer.git/description b/tests/gitea-repositories-meta/user30/renderer.git/description new file mode 100644 index 00000000000..04c23973b8a --- /dev/null +++ b/tests/gitea-repositories-meta/user30/renderer.git/description @@ -0,0 +1 @@ +The repository will be used to test third-party renderer in TestExternalMarkupRenderer diff --git a/tests/gitea-repositories-meta/user30/renderer.git/hooks/post-receive b/tests/gitea-repositories-meta/user30/renderer.git/hooks/post-receive new file mode 100644 index 00000000000..f1f2709ddde --- /dev/null +++ b/tests/gitea-repositories-meta/user30/renderer.git/hooks/post-receive @@ -0,0 +1,15 @@ +#!/usr/bin/env bash +data=$(cat) +exitcodes="" +hookname=$(basename $0) +GIT_DIR=${GIT_DIR:-$(dirname $0)} + +for hook in ${GIT_DIR}/hooks/${hookname}.d/*; do +test -x "${hook}" && test -f "${hook}" || continue +echo "${data}" | "${hook}" +exitcodes="${exitcodes} $?" +done + +for i in ${exitcodes}; do +[ ${i} -eq 0 ] || exit ${i} +done diff --git a/tests/gitea-repositories-meta/user30/renderer.git/hooks/post-receive.d/gitea b/tests/gitea-repositories-meta/user30/renderer.git/hooks/post-receive.d/gitea new file mode 100644 index 00000000000..43a948da3a9 --- /dev/null +++ b/tests/gitea-repositories-meta/user30/renderer.git/hooks/post-receive.d/gitea @@ -0,0 +1,2 @@ +#!/usr/bin/env bash +"$GITEA_ROOT/gitea" hook --config="$GITEA_ROOT/$GITEA_CONF" post-receive diff --git a/tests/gitea-repositories-meta/user30/renderer.git/hooks/pre-receive b/tests/gitea-repositories-meta/user30/renderer.git/hooks/pre-receive new file mode 100644 index 00000000000..f1f2709ddde --- /dev/null +++ b/tests/gitea-repositories-meta/user30/renderer.git/hooks/pre-receive @@ -0,0 +1,15 @@ +#!/usr/bin/env bash +data=$(cat) +exitcodes="" +hookname=$(basename $0) +GIT_DIR=${GIT_DIR:-$(dirname $0)} + +for hook in ${GIT_DIR}/hooks/${hookname}.d/*; do +test -x "${hook}" && test -f "${hook}" || continue +echo "${data}" | "${hook}" +exitcodes="${exitcodes} $?" +done + +for i in ${exitcodes}; do +[ ${i} -eq 0 ] || exit ${i} +done diff --git a/tests/gitea-repositories-meta/user30/renderer.git/hooks/pre-receive.d/gitea b/tests/gitea-repositories-meta/user30/renderer.git/hooks/pre-receive.d/gitea new file mode 100644 index 00000000000..49d09406364 --- /dev/null +++ b/tests/gitea-repositories-meta/user30/renderer.git/hooks/pre-receive.d/gitea @@ -0,0 +1,2 @@ +#!/usr/bin/env bash +"$GITEA_ROOT/gitea" hook --config="$GITEA_ROOT/$GITEA_CONF" pre-receive diff --git a/tests/gitea-repositories-meta/user30/renderer.git/hooks/update b/tests/gitea-repositories-meta/user30/renderer.git/hooks/update new file mode 100644 index 00000000000..df5bd27f106 --- /dev/null +++ b/tests/gitea-repositories-meta/user30/renderer.git/hooks/update @@ -0,0 +1,14 @@ +#!/usr/bin/env bash +exitcodes="" +hookname=$(basename $0) +GIT_DIR=${GIT_DIR:-$(dirname $0)} + +for hook in ${GIT_DIR}/hooks/${hookname}.d/*; do +test -x "${hook}" && test -f "${hook}" || continue +"${hook}" $1 $2 $3 +exitcodes="${exitcodes} $?" +done + +for i in ${exitcodes}; do +[ ${i} -eq 0 ] || exit ${i} +done diff --git a/tests/gitea-repositories-meta/user30/renderer.git/hooks/update.d/gitea b/tests/gitea-repositories-meta/user30/renderer.git/hooks/update.d/gitea new file mode 100644 index 00000000000..38101c24266 --- /dev/null +++ b/tests/gitea-repositories-meta/user30/renderer.git/hooks/update.d/gitea @@ -0,0 +1,2 @@ +#!/usr/bin/env bash +"$GITEA_ROOT/gitea" hook --config="$GITEA_ROOT/$GITEA_CONF" update $1 $2 $3 diff --git a/tests/gitea-repositories-meta/user30/renderer.git/info/exclude b/tests/gitea-repositories-meta/user30/renderer.git/info/exclude new file mode 100644 index 00000000000..a5196d1be8f --- /dev/null +++ b/tests/gitea-repositories-meta/user30/renderer.git/info/exclude @@ -0,0 +1,6 @@ +# git ls-files --others --exclude-from=.git/info/exclude +# Lines that start with '#' are comments. +# For a project mostly in C, the following would be a good set of +# exclude patterns (uncomment them if you want to use them): +# *.[oa] +# *~ diff --git a/tests/gitea-repositories-meta/user30/renderer.git/objects/06/0d5c2acd8bf4b6f14010acd1a73d73392ec46e b/tests/gitea-repositories-meta/user30/renderer.git/objects/06/0d5c2acd8bf4b6f14010acd1a73d73392ec46e new file mode 100644 index 0000000000000000000000000000000000000000..994f25602cd6d8b6a48735b7520ee0dc1f6c307b GIT binary patch literal 56 zcmV-80LTA$0V^p=O;s?qWH2-^Ff%bx2y%6F@paY9D9O!Xa1~i;5b$=>VX^4DTsF-6 O*zDTpWdHz1$qy%Vr5Mrx literal 0 HcmV?d00001 diff --git a/tests/gitea-repositories-meta/user30/renderer.git/objects/45/14a93050edb2c3165bdd0a3c03be063e879e68 b/tests/gitea-repositories-meta/user30/renderer.git/objects/45/14a93050edb2c3165bdd0a3c03be063e879e68 new file mode 100644 index 0000000000000000000000000000000000000000..b1fff27753835c235faa542aeeb6708413501109 GIT binary patch literal 50 zcmV-20L}k+0ZYosPf{>8XRt}hEVJX{EJ-acQAn*QNiE7t%uy&x%}YrwN-g5D(FaQd I08PXWv=3tx761SM literal 0 HcmV?d00001 diff --git a/tests/gitea-repositories-meta/user30/renderer.git/objects/c9/61cc4d1ba6b7ee1ba228a9a02b00b7746d8033 b/tests/gitea-repositories-meta/user30/renderer.git/objects/c9/61cc4d1ba6b7ee1ba228a9a02b00b7746d8033 new file mode 100644 index 0000000000000000000000000000000000000000..66488767ae90c8665d6a1ce53faa21a44d6421d9 GIT binary patch literal 789 zcmV+w1M2*E0iBdd6RJiOg|p^Y^qxwXS{keJQelum6a#up&z~m`^d0ZaKV(IR{ye{jOY!1cC%%%V!vh8I{gc z*^l5bcijiXRoa8!-(PL!?v-_a7HwKJzu~_9190MIBXTS+ZUD_nEK7aqH*?DV*Ksiv z&#C~w{x!-Ts!9jzD2 z_XdAlFzj`?wdZzI%W6@3|8fdoI3llLm>|NZy5Am&(qDU$+gDRbcjWL2FDGcxmT+Lr zW;S5sCFrAKt4n$QxJjW*0m%`^hlS>2K(Cj%*3coTqgpC;yKOu?8Cplmgo~cYEp2GD zuM*i^8yyrc(N?QA1PT!QgADnzEbIR35UGbkbKP~h3GKo{?VV(sa{G9g$DU11*+p+# z{gBNn`rKML*`(1FxW~`okU%0jrS=UPd{0~-G96qUuaEroxD~j(+(I*Ev}adVzA7kA z`AVei*)@oao)l=vLsxG7vcIzD=Um@k(mNRIv@iStKN%*v#gpt}Ee-qEr8b%EWVLP5 zjZEe^?ly)e3K)w!Kk4E^xOZAus#65kvHCPurWKy8bBcV+@V!t+L^?I7=aQ?DPsYrv zo4wT_hTJGHiSSKN`T^3K_xPB^Cw9-4ZY00RNGgq&=476$3=d(jd&xoERPSZxTA`D3 z7}9k`0l}`<4h@)92n^ZK;{E&FSm|ZqCkDF0(t2Cc##>WKJUT6Bt449p#K(hKS`U&) zm;x7=VO7;-P3v@ZRIyvZd$Old<{bUp4Xf7Vve367%MKO3Xj))P%^5CF`3iOJy<92K z(aS-&B;3l`F~gZ2$I{P@NX|UcFY7QjAsXwfd>n>oqV*$vRz&Nu&wU#DcF`O64+Y{S znUiaT3=-DiZ48*)JR=*;3ER|a;Jf$^swEt=h TRdwGtFpT{@!xQxrb9G4{#m9=~ literal 0 HcmV?d00001 diff --git a/tests/gitea-repositories-meta/user30/renderer.git/packed-refs b/tests/gitea-repositories-meta/user30/renderer.git/packed-refs new file mode 100644 index 00000000000..63f8af0f129 --- /dev/null +++ b/tests/gitea-repositories-meta/user30/renderer.git/packed-refs @@ -0,0 +1,2 @@ +# pack-refs with: peeled fully-peeled sorted +c961cc4d1ba6b7ee1ba228a9a02b00b7746d8033 refs/heads/master diff --git a/tests/integration/api_repo_test.go b/tests/integration/api_repo_test.go index c84416104ca..76850fb8274 100644 --- a/tests/integration/api_repo_test.go +++ b/tests/integration/api_repo_test.go @@ -81,9 +81,9 @@ func TestAPISearchRepo(t *testing.T) { }{ { name: "RepositoriesMax50", requestURL: "/api/v1/repos/search?limit=50&private=false", expectedResults: expectedResults{ - nil: {count: 30}, - user: {count: 30}, - user2: {count: 30}, + nil: {count: 31}, + user: {count: 31}, + user2: {count: 31}, }, }, { diff --git a/tests/integration/markup_external_test.go b/tests/integration/markup_external_test.go new file mode 100644 index 00000000000..6ea0226ec62 --- /dev/null +++ b/tests/integration/markup_external_test.go @@ -0,0 +1,40 @@ +// Copyright 2022 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package integration + +import ( + "bytes" + "io" + "net/http" + "strings" + "testing" + + "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/tests" + + "github.com/stretchr/testify/assert" +) + +func TestExternalMarkupRenderer(t *testing.T) { + defer tests.PrepareTestEnv(t)() + if !setting.Database.UseSQLite3 { + t.Skip() + return + } + + const repoURL = "user30/renderer" + req := NewRequest(t, "GET", repoURL+"/src/branch/master/README.html") + resp := MakeRequest(t, req, http.StatusOK) + assert.EqualValues(t, "text/html; charset=UTF-8", resp.Header()["Content-Type"][0]) + + bs, err := io.ReadAll(resp.Body) + assert.NoError(t, err) + + doc := NewHTMLParser(t, bytes.NewBuffer(bs)) + div := doc.Find("div.file-view") + data, err := div.Html() + assert.NoError(t, err) + assert.EqualValues(t, "
\n\ttest external renderer\n
", strings.TrimSpace(data)) +} diff --git a/tests/sqlite.ini.tmpl b/tests/sqlite.ini.tmpl index f5e8895e063..f6f8c70ed99 100644 --- a/tests/sqlite.ini.tmpl +++ b/tests/sqlite.ini.tmpl @@ -107,3 +107,10 @@ PATH = tests/{{TEST_TYPE}}/gitea-{{TEST_TYPE}}-sqlite/data/lfs [packages] ENABLED = true + +[markup.html] +ENABLED = true +FILE_EXTENSIONS = .html +RENDER_COMMAND = `go run tools/external_renderer.go` +IS_INPUT_FILE = false +RENDER_CONTENT_MODE=sanitized diff --git a/tools/external_renderer.go b/tools/external_renderer.go new file mode 100644 index 00000000000..c1fcc78f50b --- /dev/null +++ b/tools/external_renderer.go @@ -0,0 +1,23 @@ +// Copyright 2017 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +// gobuild: external_renderer + +package main + +import ( + "fmt" + "io" + "os" +) + +func main() { + if len(os.Args) > 1 { + fmt.Print(os.Args[1]) + } else { + _, err := io.Copy(os.Stdout, os.Stdin) + if err != nil { + fmt.Println(err) + } + } +}