gerrit-linkbot/gerrit_linkbot/test_gerrit_linkbot.py
2024-08-06 23:54:15 -07:00

122 lines
4.1 KiB
Python

import gerrit_linkbot
from gerrit_linkbot import RULES, CLMeta, CommentMeta, CLMention, IssueMatch, Service, gerrit, forgejo, testdata
def test_roundtrips():
cls = CommentMeta(cls=[
CLMention('https://gerrit.lix.systems/c/lix/+/1312', 1312, 'comment')
],
cl_meta={1312: CLMeta('meow meow')})
result = gerrit_linkbot.parse_comment(gerrit_linkbot.make_comment(cls))
assert result
assert result == cls
def test_reasonable_comment():
cls = CommentMeta(cls=[
CLMention('https://gerrit.lix.systems/c/lix/+/1312', 1312, 'comment')
],
cl_meta={1312: CLMeta('meow meow')})
assert gerrit_linkbot.make_comment(cls) == \
'<!-- GERRIT_LINKBOT: {"cls": [{"backlink": "https://gerrit.lix.systems/c/lix/+/1312", "number": 1312, "kind": "comment"}], "cl_meta": {"1312": {"change_title": "meow meow"}}}\n' \
'-->\n' \
'\n' \
'This issue was mentioned on Gerrit on the following CLs:\n' \
'* comment in [cl/1312](https://gerrit.lix.systems/c/lix/+/1312) ("meow meow")\n'
def test_posts_comment():
gerrit_api = gerrit.FakeAPI(testdata)
forgejo_api = forgejo.FakeAPI(testdata)
service = Service(gerrit_api, forgejo_api)
service.step()
assert forgejo_api.posted_comments == [
forgejo.PostedComment(
locator=forgejo.CommentLocator(
owner='lix-project',
repo='lix',
issue_id=361,
),
comment_id=10000,
comment='<!-- GERRIT_LINKBOT: {"cls": [{"backlink": '
'"https://gerrit.lix.systems/c/lix/+/1367", "number": 1367, '
'"kind": "commit message"}], "cl_meta": {"1367": '
'{"change_title": "repl: implement tab completing :colon '
'commands"}}}\n'
'-->\n'
'\n'
'This issue was mentioned on Gerrit on the following CLs:\n'
'* commit message in '
'[cl/1367](https://gerrit.lix.systems/c/lix/+/1367) ("repl: '
'implement tab completing :colon commands")\n',
),
forgejo.EditedComment(
locator=forgejo.CommentLocator(
owner='lix-project',
repo='lix',
issue_id=361,
),
comment='<!-- GERRIT_LINKBOT: {"cls": [{"backlink": '
'"https://gerrit.lix.systems/c/lix/+/1367", "number": 1367, '
'"kind": "commit message"}, {"backlink": '
'"https://gerrit.lix.systems/c/lix/+/1367", "number": 1367, '
'"kind": "comment"}], "cl_meta": {"1367": {"change_title": '
'"repl: implement tab completing :colon commands"}}}\n'
'-->\n'
'\n'
'This issue was mentioned on Gerrit on the following CLs:\n'
'* commit message in '
'[cl/1367](https://gerrit.lix.systems/c/lix/+/1367) ("repl: '
'implement tab completing :colon commands")\n'
'* comment in '
'[cl/1367](https://gerrit.lix.systems/c/lix/+/1367) ("repl: '
'implement tab completing :colon commands")\n',
edit_comment_id=10000,
),
]
def test_rules():
def flatten(xss):
return [x for xs in xss for x in xs]
def apply_rules(text: str):
return [x for x in flatten(rule.apply(text) for rule in RULES) if x]
assert apply_rules('meow #12 43') == [
IssueMatch(
issue_num=12,
project_name=None,
),
]
assert apply_rules(
'meow a#123 #123 #345 https://git.lix.systems/meow/nya/issues/86 https://git.lix.systems/lix-project/meows/issues/98'
) == [
IssueMatch(
issue_num=123,
project_name=None,
),
IssueMatch(
issue_num=345,
project_name=None,
),
IssueMatch(
issue_num=98,
project_name='meows',
),
]
assert apply_rules('lix#123 fj#583 lix#1337gj') == [
IssueMatch(
issue_num=123,
project_name=None,
),
IssueMatch(
issue_num=583,
project_name=None,
)
]