1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
local test_directory = arg[0]:match("^(.*)/") or "."
local h = dofile(test_directory .. "/harness.lua")
local script = arg[1]
h.load(script)
local function render(text)
return h.run({}, { text })
end
local out, ret = render("see #123 near deadbee")
h.equals("an unedited copy leaves issue marks as text", out,
"see #123 near <a href='./?id=deadbee'>deadbee</a>")
h.load_with(script, { { "-- { pattern = \"#(%d+)\"", "{ pattern = \"#(%d+)\"" } })
out, ret = render("see #123 for details")
h.contains("issue mark becomes a link", out, "<a href='https://bugs.example.com/?bug=123'>#123</a>")
h.contains("text around the link survives", out, "see ")
h.contains("text after the link survives", out, " for details")
h.equals("close answers zero", ret, 0)
out = render("fixed in deadbee")
h.contains("seven hex characters link", out, "<a href='./?id=deadbee'>deadbee</a>")
out = render("fixed in deadbe")
h.excludes("six hex characters stay text", out, "<a")
out = render("fixed in 1234567")
h.contains("an all digit run still links", out, "<a href='./?id=1234567'>1234567</a>")
local sha1 = string.rep("0123456789abcdef", 2) .. "01234567"
out = render("commit " .. sha1)
h.contains("a full sha1 name links", out, "'./?id=" .. sha1 .. "'")
local sha256 = string.rep("0123456789abcdef", 4)
out = render("commit " .. sha256)
h.contains("a full sha256 name links", out, "'./?id=" .. sha256 .. "'")
out = render("commit " .. sha256 .. "0")
h.excludes("sixty five hex characters stay text", out, "<a")
out = render("prefixdeadbeef")
h.excludes("a hex run inside a word stays text", out, "<a")
out = render("deadbeefy")
h.excludes("a hex run ending inside a word stays text", out, "<a")
out = render("see #1234567 for details")
h.contains("the issue rule wins an overlap", out,
"<a href='https://bugs.example.com/?bug=1234567'>#1234567</a>")
h.excludes("the overlapped hash is dropped", out, "./?id=")
out = render("#12 and cafebabe12")
h.contains("both rules fire on one message", out, "?bug=12'>#12</a>")
h.contains("the object link also fires", out, "<a href='./?id=cafebabe12'>cafebabe12</a>")
out = h.run({}, { "see #45", "6 and be done" })
h.contains("a match split across writes still links", out, "?bug=456'>#456</a>")
out, ret = render("")
h.equals("an empty message stays empty", out, "")
h.equals("an empty message still answers zero", ret, 0)
out = render("plain words only")
h.equals("a message with no matches passes through", out, "plain words only")
h.load_with(script, { { "local rules = {", "local rules = { { pattern = \"T:(%S+)\", url = \"https://t/%s\" }," } })
out = render("T:a'b%c")
h.equals("a captured value is percent-encoded into the url", out, "<a href='https://t/a%27b%25c'>T:a'b%c</a>")
h.load_with(script, { { "local rules = {", "local rules = { { pattern = \"(%d+\", url = \"https://x/%s\" }," } })
out = render("fixed 42 in deadbee")
h.equals("a malformed rule is skipped and object names still link", out,
"fixed 42 in <a href='./?id=deadbee'>deadbee</a>")
h.load_with(script, { { "local rules = {",
"local rules = { { pattern = \"#(%d+)\", url = \"https://a/%s\" }, { pattern = \"#(%d%d)\", url = \"https://b/%s\" }," } })
out = render("#12")
h.equals("the rule listed first wins a tie", out, "<a href='https://a/12'>#12</a>")
h.load_with(script, { { "local rules = {", "local rules = { { pattern = \"x*\", url = \"https://x/%s\" }," } })
out = render("abc")
h.equals("a rule matching the empty string still reaches the end", (out:gsub("<[^>]*>", "")), "abc")
h.load_with(script, { { "local rules = {", "local rules = { { pattern = \"RFC%d+\", url = \"https://r/%s\" }," } })
out = render("see RFC123")
h.equals("a rule with no capture puts the whole match in the url", out, "see <a href='https://r/RFC123'>RFC123</a>")
h.load_with(script, { { "enabled = true,", "enabled = false," } })
out = render("fixed in deadbee")
h.equals("object names can be left unlinked", out, "fixed in deadbee")
h.load_with(script, {
{ "-- { pattern = \"CVE", "{ pattern = \"CVE" },
{ "-- url = \"https://www.cve.org", " url = \"https://www.cve.org" },
{ "-- { pattern = \"!", "{ pattern = \"!" },
{ "-- url = \"https://gitlab", " url = \"https://gitlab" },
{ "-- { pattern = \"RFC", "{ pattern = \"RFC" },
{ "-- url = \"https://www.rfc-editor", " url = \"https://www.rfc-editor" },
})
out = render("CVE-2024-12345, !42 and RFC 9110")
h.equals("the shipped examples link what they describe", out,
"<a href='https://www.cve.org/CVERecord?id=CVE-2024-12345'>CVE-2024-12345</a>, " ..
"<a href='https://gitlab.example.com/my/repo/-/merge_requests/42'>!42</a> and " ..
"<a href='https://www.rfc-editor.org/rfc/rfc9110'>RFC 9110</a>")
h.finish()
|