Skip to content

Commit ce9bfa2

Browse files
committed
Add test for file-preview link rendering
1 parent 1105468 commit ce9bfa2

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

modules/markup/html_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package markup_test
55

66
import (
77
"context"
8+
"html/template"
89
"io"
910
"os"
1011
"strings"
@@ -13,10 +14,12 @@ import (
1314
"code.gitea.io/gitea/models/unittest"
1415
"code.gitea.io/gitea/modules/emoji"
1516
"code.gitea.io/gitea/modules/git"
17+
"code.gitea.io/gitea/modules/highlight"
1618
"code.gitea.io/gitea/modules/log"
1719
"code.gitea.io/gitea/modules/markup"
1820
"code.gitea.io/gitea/modules/markup/markdown"
1921
"code.gitea.io/gitea/modules/setting"
22+
"code.gitea.io/gitea/modules/translation"
2023
"code.gitea.io/gitea/modules/util"
2124

2225
"github.com/stretchr/testify/assert"
@@ -688,3 +691,57 @@ func TestIsFullURL(t *testing.T) {
688691
assert.True(t, markup.IsFullURLString("mailto:[email protected]"))
689692
assert.False(t, markup.IsFullURLString("/foo:bar"))
690693
}
694+
695+
func TestRender_FilePreview(t *testing.T) {
696+
setting.AppURL = markup.TestAppURL
697+
markup.Init(&markup.ProcessorHelper{
698+
GetRepoFileContent: func(ctx context.Context, ownerName string, repoName string, commitSha string, filePath string) ([]template.HTML, error) {
699+
buf := []byte( "A\nB\nC\nD\n" )
700+
return highlight.PlainText(buf), nil
701+
},
702+
GetLocale: func(ctx context.Context) (translation.Locale, error) {
703+
return translation.NewLocale("en-US"), nil
704+
},
705+
})
706+
707+
sha := "b6dd6210eaebc915fd5be5579c58cce4da2e2579"
708+
commitFilePreview := util.URLJoin(markup.TestRepoURL, "src", "commit", sha, "path", "to", "file.go") + "#L1-L2"
709+
710+
test := func(input, expected string) {
711+
buffer, err := markup.RenderString(&markup.RenderContext{
712+
Ctx: git.DefaultContext,
713+
RelativePath: ".md",
714+
Metas: localMetas,
715+
}, input)
716+
assert.NoError(t, err)
717+
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
718+
}
719+
720+
test(
721+
commitFilePreview,
722+
`<p></p>` +
723+
`<div class="file-preview-box">` +
724+
`<div class="header">` +
725+
`<a href="http://localhost:3000/gogits/gogs/src/commit/b6dd6210eaebc915fd5be5579c58cce4da2e2579/path/to/file.go#L1-L2" class="muted" rel="nofollow">path/to/file.go</a>` +
726+
`<span class="text small grey">` +
727+
`Lines 1 to 2 in <a href="http://localhost:3000/gogits/gogs/src/commit/b6dd6210eaebc915fd5be5579c58cce4da2e2579" class="text black" rel="nofollow">b6dd621</a>` +
728+
`</span>` +
729+
`</div>` +
730+
`<div class="ui table">` +
731+
`<table class="file-preview">` +
732+
`<tbody>` +
733+
`<tr>` +
734+
`<td id="user-content-L1" class="lines-num"><span id="user-content-L1" data-line-number="1"></span></td>` +
735+
`<td rel="L1" class="lines-code chroma"><code class="code-inner">A` + "\n" + `</code></td>` +
736+
`</tr>` +
737+
`<tr>` +
738+
`<td id="user-content-L2" class="lines-num"><span id="user-content-L2" data-line-number="2"></span></td>` +
739+
`<td rel="L2" class="lines-code chroma"><code class="code-inner">B` + "\n" + `</code></td>` +
740+
`</tr>` +
741+
`</tbody>` +
742+
`</table>` +
743+
`</div>` +
744+
`</div>` +
745+
`<p></p>`,
746+
)
747+
}

0 commit comments

Comments
 (0)