Skip to content

Commit b06374d

Browse files
committed
add more tests
1 parent a4d34dd commit b06374d

File tree

3 files changed

+22
-15
lines changed

3 files changed

+22
-15
lines changed

services/gitdiff/submodule.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,12 @@ func (si *SubmoduleDiffInfo) PopulateURL(diffFile *DiffFile, leftCommit, rightCo
4040
}
4141
}
4242

43-
func (si *SubmoduleDiffInfo) PreviousRefIDLinkHTML(ctx context.Context) template.HTML {
44-
webLink := si.SubmoduleFile.SubmoduleWebLink(ctx, si.PreviousRefID)
43+
func (si *SubmoduleDiffInfo) CommitRefIDLinkHTML(ctx context.Context, commitID string) template.HTML {
44+
webLink := si.SubmoduleFile.SubmoduleWebLink(ctx, commitID)
4545
if webLink == nil {
46-
return htmlutil.HTMLFormat("%s", base.ShortSha(si.PreviousRefID))
46+
return htmlutil.HTMLFormat("%s", base.ShortSha(commitID))
4747
}
48-
return htmlutil.HTMLFormat(`<a href="%s">%s</a>`, webLink.CommitWebLink, base.ShortSha(si.PreviousRefID))
49-
}
50-
51-
func (si *SubmoduleDiffInfo) NewRefIDLinkHTML(ctx context.Context) template.HTML {
52-
webLink := si.SubmoduleFile.SubmoduleWebLink(ctx, si.NewRefID)
53-
if webLink == nil {
54-
return htmlutil.HTMLFormat("%s", base.ShortSha(si.NewRefID))
55-
}
56-
return htmlutil.HTMLFormat(`<a href="%s">%s</a>`, webLink.CommitWebLink, base.ShortSha(si.NewRefID))
48+
return htmlutil.HTMLFormat(`<a href="%s">%s</a>`, webLink.CommitWebLink, base.ShortSha(commitID))
5749
}
5850

5951
func (si *SubmoduleDiffInfo) CompareRefIDLinkHTML(ctx context.Context) template.HTML {

services/gitdiff/submodule_test.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
package gitdiff
55

66
import (
7+
"context"
78
"strings"
89
"testing"
910

1011
"code.gitea.io/gitea/models/db"
12+
"code.gitea.io/gitea/modules/git"
1113
"code.gitea.io/gitea/modules/setting"
1214

1315
"github.com/stretchr/testify/assert"
@@ -217,5 +219,18 @@ index 0000000..68972a9
217219
}
218220

219221
func TestSubmoduleInfo(t *testing.T) {
220-
// TODO: test NewRefIDLinkHTML PreviousRefIDLinkHTML SubmoduleRepoLinkHTML after we get the unifed "RefURL" function
222+
sdi := &SubmoduleDiffInfo{
223+
SubmoduleName: "name",
224+
PreviousRefID: "aaaa",
225+
NewRefID: "bbbb",
226+
}
227+
ctx := context.Background()
228+
assert.EqualValues(t, "1111", sdi.CommitRefIDLinkHTML(ctx, "1111"))
229+
assert.EqualValues(t, "aaaa...bbbb", sdi.CompareRefIDLinkHTML(ctx))
230+
assert.EqualValues(t, "name", sdi.SubmoduleRepoLinkHTML(ctx))
231+
232+
sdi.SubmoduleFile = git.NewCommitSubmoduleFile("https://github.com/owner/repo", "1234")
233+
assert.EqualValues(t, `<a href="https://github.com/owner/repo/commit/1111">1111</a>`, sdi.CommitRefIDLinkHTML(ctx, "1111"))
234+
assert.EqualValues(t, `<a href="https://github.com/owner/repo/compare/aaaa...bbbb">aaaa...bbbb</a>`, sdi.CompareRefIDLinkHTML(ctx))
235+
assert.EqualValues(t, `<a href="https://github.com/owner/repo">name</a>`, sdi.SubmoduleRepoLinkHTML(ctx))
221236
}

templates/repo/diff/box.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,9 @@
201201
<div class="tw-p-3">{{svg "octicon-file-submodule"}} {{$submoduleDiffInfo := $file.SubmoduleDiffInfo -}}
202202
{{- $submoduleName := $submoduleDiffInfo.SubmoduleRepoLinkHTML ctx -}}
203203
{{- if $file.IsDeleted -}}
204-
{{- ctx.Locale.Tr "repo.diff.submodule_deleted" $submoduleName ($submoduleDiffInfo.PreviousRefIDLinkHTML ctx) -}}
204+
{{- ctx.Locale.Tr "repo.diff.submodule_deleted" $submoduleName ($submoduleDiffInfo.CommitRefIDLinkHTML ctx $submoduleDiffInfo.PreviousRefID) -}}
205205
{{- else if $file.IsCreated -}}
206-
{{- ctx.Locale.Tr "repo.diff.submodule_added" $submoduleName ($submoduleDiffInfo.NewRefIDLinkHTML ctx) -}}
206+
{{- ctx.Locale.Tr "repo.diff.submodule_added" $submoduleName ($submoduleDiffInfo.CommitRefIDLinkHTML ctx $submoduleDiffInfo.NewRefID) -}}
207207
{{- else -}}
208208
{{- ctx.Locale.Tr "repo.diff.submodule_updated" $submoduleName ($submoduleDiffInfo.CompareRefIDLinkHTML ctx) -}}
209209
{{end}}

0 commit comments

Comments
 (0)