@ -11,11 +11,11 @@ import (
"testing"
"testing"
"code.gitea.io/gitea/models/issues"
"code.gitea.io/gitea/models/issues"
"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/modules/git"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/reqctx"
"code.gitea.io/gitea/modules/reqctx"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/test"
"code.gitea.io/gitea/modules/test"
"code.gitea.io/gitea/modules/translation"
"code.gitea.io/gitea/modules/translation"
@ -47,19 +47,8 @@ mail@domain.com
return strings . ReplaceAll ( s , "<SPACE>" , " " )
return strings . ReplaceAll ( s , "<SPACE>" , " " )
}
}
var testMetas = map [ string ] string {
"user" : "user13" ,
"repo" : "repo11" ,
"repoPath" : "../../tests/gitea-repositories-meta/user13/repo11.git/" ,
"markdownNewLineHardBreak" : "true" ,
"markupAllowShortIssuePattern" : "true" ,
}
func TestMain ( m * testing . M ) {
func TestMain ( m * testing . M ) {
unittest . InitSettingsForTesting ( )
setting . Markdown . RenderOptionsComment . ShortIssuePattern = true
if err := git . InitSimple ( context . Background ( ) ) ; err != nil {
log . Fatal ( "git init failed, err: %v" , err )
}
markup . Init ( & markup . RenderHelperFuncs {
markup . Init ( & markup . RenderHelperFuncs {
IsUsernameMentionable : func ( ctx context . Context , username string ) bool {
IsUsernameMentionable : func ( ctx context . Context , username string ) bool {
return username == "mention-user"
return username == "mention-user"
@ -74,46 +63,52 @@ func newTestRenderUtils(t *testing.T) *RenderUtils {
return NewRenderUtils ( ctx )
return NewRenderUtils ( ctx )
}
}
func TestRenderCommitBody ( t * testing . T ) {
func TestRenderRepoComment ( t * testing . T ) {
defer test . MockVariableValue ( & markup . RenderBehaviorForTesting . DisableAdditionalAttributes , true ) ( )
mockRepo := & repo . Repository {
type args struct {
ID : 1 , OwnerName : "user13" , Name : "repo11" ,
msg string
Owner : & user_model . User { ID : 13 , Name : "user13" } ,
Units : [ ] * repo . RepoUnit { } ,
}
}
tests := [ ] struct {
t . Run ( "RenderCommitBody" , func ( t * testing . T ) {
name string
defer test . MockVariableValue ( & markup . RenderBehaviorForTesting . DisableAdditionalAttributes , true ) ( )
args args
type args struct {
want template . HTML
msg string
} {
}
{
tests := [ ] struct {
name : "multiple lines" ,
name string
args : args {
args args
msg : "first line\nsecond line" ,
want template . HTML
} {
{
name : "multiple lines" ,
args : args {
msg : "first line\nsecond line" ,
} ,
want : "second line" ,
} ,
} ,
want : "second line" ,
{
} ,
name : "multiple lines with leading newlines" ,
{
args : args {
name : "multiple lines with leading newlines" ,
msg : "\n\n\n\nfirst line\nsecond line ",
args : args {
} ,
msg : "\n\n\n\nfirst line\nsecond line" ,
want: " second line",
} ,
} ,
want : "second line" ,
{
} ,
name : "multiple lines with trailing newlines" ,
{
args : args {
name : "multiple lines with trailing newlines ",
msg : "first line\nsecond line\n\n\n ",
args : args {
} ,
msg: "first line\nsecond line\n\n\n ",
want: "second line ",
} ,
} ,
want : "second line" ,
}
} ,
ut := newTestRenderUtils ( t )
}
for _ , tt := range tests {
ut := newTestRenderUtils ( t )
t . Run ( tt . name , func ( t * testing . T ) {
for _ , tt := range tests {
assert . Equalf ( t , tt . want , ut . RenderCommitBody ( tt . args . msg , mockRepo ) , "RenderCommitBody(%v, %v)" , tt . args . msg , nil )
t . Run ( tt . name , func ( t * testing . T ) {
} )
assert . Equalf ( t , tt . want , ut . RenderCommitBody ( tt . args . msg , nil ) , "RenderCommitBody(%v, %v)" , tt . args . msg , nil )
}
} )
}
expected := ` / just / a / path . bin
expected := ` / just / a / path . bin
< a href = "https://example.com/file.bin" > https : //example.com/file.bin</a>
< a href = "https://example.com/file.bin" > https : //example.com/file.bin</a>
[ local link ] ( file . bin )
[ local link ] ( file . bin )
[ remote link ] ( < a href = "https://example.com" > https : //example.com</a>)
[ remote link ] ( < a href = "https://example.com" > https : //example.com</a>)
@ -132,22 +127,22 @@ com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb mit
< a href = "/mention-user" > @ mention - user < / a > test
< a href = "/mention-user" > @ mention - user < / a > test
< a href = "/user13/repo11/issues/123" class = "ref-issue" > # 123 < / a >
< a href = "/user13/repo11/issues/123" class = "ref-issue" > # 123 < / a >
space `
space `
assert . Equal ( t , expected , string ( newTestRenderUtils ( t ) . RenderCommitBody ( testInput ( ) , testMetas ) ) )
assert . Equal ( t , expected , string ( newTestRenderUtils ( t ) . RenderCommitBody ( testInput ( ) , mockRepo ) ) )
}
} )
func TestRenderCommitMessage ( t * testing . T ) {
t . Run ( "RenderCommitMessage" , func ( t * testing . T ) {
expected := ` space <a href="/mention-user" data-markdown-generated-content="">@mention-user</a> `
expected := ` space <a href="/mention-user" data-markdown-generated-content="">@mention-user</a> `
assert . EqualValues ( t , expected , newTestRenderUtils ( t ) . RenderCommitMessage ( testInput ( ) , testMetas ) )
assert . EqualValues ( t , expected , newTestRenderUtils ( t ) . RenderCommitMessage ( testInput ( ) , mockRepo ) )
}
} )
func TestRenderCommitMessageLinkSubject ( t * testing . T ) {
t . Run ( "RenderCommitMessageLinkSubject" , func ( t * testing . T ) {
expected := ` <a href="https://example.com/link" class="muted">space </a><a href="/mention-user" data-markdown-generated-content="">@mention-user</a> `
expected := ` <a href="https://example.com/link" class="muted">space </a><a href="/mention-user" data-markdown-generated-content="">@mention-user</a> `
assert . EqualValues ( t , expected , newTestRenderUtils ( t ) . RenderCommitMessageLinkSubject ( testInput ( ) , "https://example.com/link" , testMetas ) )
assert . EqualValues ( t , expected , newTestRenderUtils ( t ) . RenderCommitMessageLinkSubject ( testInput ( ) , "https://example.com/link" , mockRepo ) )
}
} )
func TestRenderIssueTitle ( t * testing . T ) {
t . Run ( "RenderIssueTitle" , func ( t * testing . T ) {
defer test . MockVariableValue ( & markup . RenderBehaviorForTesting . DisableAdditionalAttributes , true ) ( )
defer test . MockVariableValue ( & markup . RenderBehaviorForTesting . DisableAdditionalAttributes , true ) ( )
expected := ` space @ mention - user < SPACE > < SPACE >
expected := ` space @ mention - user < SPACE > < SPACE >
/ just / a / path . bin
/ just / a / path . bin
https : //example.com/file.bin
https : //example.com/file.bin
[ local link ] ( file . bin )
[ local link ] ( file . bin )
@ -168,8 +163,9 @@ mail@domain.com
< a href = "/user13/repo11/issues/123" class = "ref-issue" > # 123 < / a >
< a href = "/user13/repo11/issues/123" class = "ref-issue" > # 123 < / a >
space < SPACE > < SPACE >
space < SPACE > < SPACE >
`
`
expected = strings . ReplaceAll ( expected , "<SPACE>" , " " )
expected = strings . ReplaceAll ( expected , "<SPACE>" , " " )
assert . Equal ( t , expected , string ( newTestRenderUtils ( t ) . RenderIssueTitle ( testInput ( ) , testMetas ) ) )
assert . Equal ( t , expected , string ( newTestRenderUtils ( t ) . RenderIssueTitle ( testInput ( ) , mockRepo ) ) )
} )
}
}
func TestRenderMarkdownToHtml ( t * testing . T ) {
func TestRenderMarkdownToHtml ( t * testing . T ) {