Skip to content

Commit 68b718b

Browse files
committed
fix some tests
1 parent a1795f9 commit 68b718b

File tree

3 files changed

+24
-19
lines changed

3 files changed

+24
-19
lines changed

modules/markup/html.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -817,31 +817,31 @@ func issueIndexPatternProcessor(ctx *RenderContext, node *html.Node) {
817817

818818
next := node.NextSibling
819819

820-
_, exttrack := ctx.Metas["format"]
821-
notNumericStyle := ctx.Metas["style"] != IssueNameStyleNumeric
822-
foundNumeric, refNumeric := references.FindRenderizableReferenceNumeric(node.Data, exttrack && notNumericStyle)
823-
824820
for node != nil && node != next {
821+
_, hasExtTrackFormat := ctx.Metas["format"]
822+
823+
// Repos with external issue trackers might still need to reference local PRs
824+
// We need to concern with the first one that shows up in the text, whichever it is
825+
isNumericStyle := ctx.Metas["style"] == "" || ctx.Metas["style"] == IssueNameStyleNumeric
826+
foundNumeric, refNumeric := references.FindRenderizableReferenceNumeric(node.Data, hasExtTrackFormat && !isNumericStyle)
827+
825828
switch ctx.Metas["style"] {
826-
case IssueNameStyleNumeric:
827-
found = foundNumeric
828-
ref = refNumeric
829+
case "", IssueNameStyleNumeric:
830+
found, ref = foundNumeric, refNumeric
829831
case IssueNameStyleAlphanumeric:
830832
found, ref = references.FindRenderizableReferenceAlphanumeric(node.Data)
831833
case IssueNameStyleRegexp:
832-
// TODO: Compile only once, at regexp definition time
833-
pattern, err := regexp.Compile(ctx.Metas["regexp"])
834-
if err == nil {
834+
pattern, err := regexp.Compile(ctx.Metas["regexp"]) // TODO: Compile only once, at regexp definition time
835+
if err != nil {
835836
return
836837
}
837838
found, ref = references.FindRenderizableReferenceRegexp(node.Data, pattern)
838839
}
839840

840841
// Repos with external issue trackers might still need to reference local PRs
841842
// We need to concern with the first one that shows up in the text, whichever it is
842-
if exttrack && notNumericStyle {
843-
// If numeric (PR) was found and it was BEFORE the notNumeric
844-
// pattern, use that
843+
if hasExtTrackFormat && !isNumericStyle {
844+
// If numeric (PR) was found, and it was BEFORE the non-numeric pattern, use that
845845
if foundNumeric && refNumeric.RefLocation.Start < ref.RefLocation.Start {
846846
found = foundNumeric
847847
ref = refNumeric
@@ -853,7 +853,7 @@ func issueIndexPatternProcessor(ctx *RenderContext, node *html.Node) {
853853

854854
var link *html.Node
855855
reftext := node.Data[ref.RefLocation.Start:ref.RefLocation.End]
856-
if exttrack && !ref.IsPull {
856+
if hasExtTrackFormat && !ref.IsPull {
857857
ctx.Metas["index"] = ref.Issue
858858

859859
res, err := vars.Expand(ctx.Metas["format"], ctx.Metas)
@@ -886,7 +886,7 @@ func issueIndexPatternProcessor(ctx *RenderContext, node *html.Node) {
886886

887887
// Decorate action keywords if actionable
888888
var keyword *html.Node
889-
if references.IsXrefActionable(ref, exttrack) {
889+
if references.IsXrefActionable(ref, hasExtTrackFormat) {
890890
keyword = createKeyword(node.Data[ref.ActionLocation.Start:ref.ActionLocation.End])
891891
} else {
892892
keyword = &html.Node{

modules/markup/html_internal_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,13 @@ func TestRender_IssueIndexPattern5(t *testing.T) {
211211
links[i] = externalIssueLink("https://someurl.com/someUser/someRepo/", "ref-issue ref-external-issue", name)
212212
}
213213

214+
metas := map[string]string{}
215+
for k, v := range regexpMetas {
216+
metas[k] = v
217+
}
218+
metas["regexp"] = pattern
214219
expected := fmt.Sprintf(expectedFmt, links...)
215-
testRenderIssueIndexPattern(t, s, expected, &RenderContext{Metas: regexpMetas})
220+
testRenderIssueIndexPattern(t, s, expected, &RenderContext{Metas: metas})
216221
}
217222

218223
test("abc ISSUE-123 def", "abc %s def",
@@ -242,7 +247,7 @@ func testRenderIssueIndexPattern(t *testing.T, input, expected string, ctx *Rend
242247
var buf strings.Builder
243248
err := postProcess(ctx, []processor{issueIndexPatternProcessor}, strings.NewReader(input), &buf)
244249
assert.NoError(t, err)
245-
assert.Equal(t, expected, buf.String())
250+
assert.Equal(t, expected, buf.String(), "input=%q", input)
246251
}
247252

248253
func TestRender_AutoLink(t *testing.T) {

modules/references/references.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,14 +354,14 @@ func FindRenderizableReferenceNumeric(content string, prOnly bool) (bool, *Rende
354354
// FindRenderizableReferenceRegexp returns the first regexp unvalidated references found in a string.
355355
func FindRenderizableReferenceRegexp(content string, pattern *regexp.Regexp) (bool, *RenderizableReference) {
356356
match := pattern.FindStringSubmatchIndex(content)
357-
if match == nil {
357+
if len(match) < 4 {
358358
return false, nil
359359
}
360360

361361
action, location := findActionKeywords([]byte(content), match[2])
362362

363363
return true, &RenderizableReference{
364-
Issue: string(content[match[2]:match[3]]),
364+
Issue: content[match[2]:match[3]],
365365
RefLocation: &RefSpan{Start: match[0], End: match[1]},
366366
Action: action,
367367
ActionLocation: location,

0 commit comments

Comments
 (0)