Skip to content

Commit 19d7fde

Browse files
committed
Backport pull request #2216
reddit, test: even more of the comments link format is optional
1 parent 4fb17d4 commit 19d7fde

File tree

2 files changed

+90
-78
lines changed

2 files changed

+90
-78
lines changed

sopel/modules/reddit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
subreddit_url = r'%s/r/([\w-]+)/?$' % domain
4242
post_or_comment_url = (
4343
domain +
44-
r'/r/\S+?/comments/(?P<submission>[\w-]+)'
44+
r'(?:/r/\S+?)?/comments/(?P<submission>[\w-]+)'
4545
r'(?:/?(?:[\w%]+/(?P<comment>[\w-]+))?)'
4646
)
4747
short_post_url = r'https?://(redd\.it|reddit\.com)/(?P<submission>[\w-]+)/?$'
Lines changed: 89 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,89 @@
1-
# coding=utf-8
2-
"""Tests for Sopel's ``reddit`` plugin"""
3-
from __future__ import absolute_import, division, print_function, unicode_literals
4-
5-
import pytest
6-
7-
from sopel.trigger import PreTrigger
8-
9-
10-
TMP_CONFIG = """
11-
[core]
12-
owner = Admin
13-
nick = Sopel
14-
enable =
15-
reddit
16-
host = irc.libera.chat
17-
"""
18-
19-
20-
@pytest.fixture
21-
def bot(botfactory, configfactory):
22-
settings = configfactory('default.ini', TMP_CONFIG)
23-
return botfactory.preloaded(settings, ['reddit'])
24-
25-
26-
MATCHING_URLS = (
27-
# URLs the reddit plugin is expected to handle
28-
# Should match ONCE each, no more, no less
29-
'https://redd.it/123456',
30-
'https://redd.it/123456/',
31-
'https://reddit.com/123456',
32-
'https://reddit.com/123456/',
33-
'https://reddit.com/r/subname',
34-
'https://reddit.com/r/subname/',
35-
'https://www.reddit.com/r/subname',
36-
'https://www.reddit.com/r/subname/',
37-
'https://reddit.com/r/subname/comments/123456',
38-
'https://reddit.com/r/subname/comments/123456/',
39-
'https://reddit.com/r/subname/comments/123456?param=value',
40-
'https://reddit.com/r/subname/comments/123456/?param=value',
41-
'https://www.reddit.com/r/subname/comments/123456',
42-
'https://www.reddit.com/r/subname/comments/123456/',
43-
'https://reddit.com/r/subname/comments/123456/post_title_slug/234567',
44-
'https://reddit.com/r/subname/comments/123456/post_title_slug/234567/',
45-
'https://www.reddit.com/r/subname/comments/123456/post_title_slug/234567',
46-
'https://www.reddit.com/r/subname/comments/123456/post_title_slug/234567/',
47-
'https://reddit.com/r/subname/comments/123456/post_title_slug/234567/?context=1337',
48-
'https://www.reddit.com/r/subname/comments/123456/post_title_slug/234567/?context=1337',
49-
)
50-
51-
52-
NON_MATCHING_URLS = (
53-
# we don't allow for query parameters on subreddit links (yet?)
54-
'https://reddit.com/r/subname?param=value',
55-
'https://reddit.com/r/subname/?param=value',
56-
'https://www.reddit.com/r/subname?param=value',
57-
'https://www.reddit.com/r/subname/?param=value',
58-
# "shortlink" style allegedly seen in the wild, but not currently recognized
59-
'https://reddit.com/comments/123456',
60-
'https://reddit.com/comments/123456/',
61-
)
62-
63-
64-
@pytest.mark.parametrize('link', MATCHING_URLS)
65-
def test_url_matching(link, bot):
66-
line = PreTrigger(bot.nick, ':[email protected] PRIVMSG #channel {}'.format(link))
67-
matches = bot.rules.get_triggered_rules(bot, line)
68-
69-
assert len([match for match in matches if match[0].get_plugin_name() == 'reddit']) == 1
70-
71-
72-
@pytest.mark.parametrize('link', NON_MATCHING_URLS)
73-
def test_url_non_matching(link, bot):
74-
line = PreTrigger(bot.nick, ':[email protected] PRIVMSG #channel {}'.format(link))
75-
matches = bot.rules.get_triggered_rules(bot, line)
76-
77-
assert len([match for match in matches if match[0].get_plugin_name() == 'reddit']) == 0
1+
# coding=utf-8
2+
"""Tests for Sopel's ``reddit`` plugin"""
3+
from __future__ import absolute_import, division, print_function, unicode_literals
4+
5+
import pytest
6+
7+
from sopel.trigger import PreTrigger
8+
9+
10+
TMP_CONFIG = """
11+
[core]
12+
owner = Admin
13+
nick = Sopel
14+
enable =
15+
reddit
16+
host = irc.libera.chat
17+
"""
18+
19+
20+
@pytest.fixture
21+
def bot(botfactory, configfactory):
22+
settings = configfactory('default.ini', TMP_CONFIG)
23+
return botfactory.preloaded(settings, ['reddit'])
24+
25+
26+
MATCHING_URLS = (
27+
# URLs the reddit plugin is expected to handle
28+
# Should match ONCE each, no more, no less
29+
('auto_subreddit_info', 'https://reddit.com/r/subname'),
30+
('auto_subreddit_info', 'https://reddit.com/r/subname/'),
31+
('auto_subreddit_info', 'https://www.reddit.com/r/subname'),
32+
('auto_subreddit_info', 'https://www.reddit.com/r/subname/'),
33+
('post_or_comment_info', 'https://redd.it/123456'),
34+
('post_or_comment_info', 'https://redd.it/123456/'),
35+
('post_or_comment_info', 'https://reddit.com/123456'),
36+
('post_or_comment_info', 'https://reddit.com/123456/'),
37+
('post_or_comment_info', 'https://reddit.com/comments/123456'),
38+
('post_or_comment_info', 'https://reddit.com/comments/123456/'),
39+
('post_or_comment_info', 'https://www.reddit.com/comments/123456'),
40+
('post_or_comment_info', 'https://www.reddit.com/comments/123456/'),
41+
('post_or_comment_info', 'https://reddit.com/r/subname/comments/123456'),
42+
('post_or_comment_info', 'https://reddit.com/r/subname/comments/123456/'),
43+
('post_or_comment_info', 'https://www.reddit.com/comments/123456?param=value'),
44+
('post_or_comment_info', 'https://www.reddit.com/comments/123456/?param=value'),
45+
('post_or_comment_info', 'https://reddit.com/r/subname/comments/123456?param=value'),
46+
('post_or_comment_info', 'https://reddit.com/r/subname/comments/123456/?param=value'),
47+
('post_or_comment_info', 'https://www.reddit.com/r/subname/comments/123456'),
48+
('post_or_comment_info', 'https://www.reddit.com/r/subname/comments/123456/'),
49+
('post_or_comment_info', 'https://reddit.com/r/subname/comments/123456/post_title_slug/234567'),
50+
('post_or_comment_info', 'https://reddit.com/r/subname/comments/123456/post_title_slug/234567/'),
51+
('post_or_comment_info', 'https://www.reddit.com/r/subname/comments/123456/post_title_slug/234567'),
52+
('post_or_comment_info', 'https://www.reddit.com/r/subname/comments/123456/post_title_slug/234567/'),
53+
('post_or_comment_info', 'https://reddit.com/r/subname/comments/123456/post_title_slug/234567/?context=1337'),
54+
('post_or_comment_info', 'https://www.reddit.com/r/subname/comments/123456/post_title_slug/234567/?context=1337'),
55+
)
56+
57+
58+
NON_MATCHING_URLS = (
59+
# we don't allow for query parameters on subreddit links (yet?)
60+
'https://reddit.com/r/subname?param=value',
61+
'https://reddit.com/r/subname/?param=value',
62+
'https://www.reddit.com/r/subname?param=value',
63+
'https://www.reddit.com/r/subname/?param=value',
64+
)
65+
66+
67+
@pytest.mark.parametrize('rule_name, link', MATCHING_URLS)
68+
def test_url_matching(link, rule_name, bot):
69+
line = PreTrigger(bot.nick, ':[email protected] PRIVMSG #channel {}'.format(link))
70+
matched_rules = [
71+
# we can ignore matches that don't come from the plugin under test
72+
match[0] for match in bot.rules.get_triggered_rules(bot, line)
73+
if match[0].get_plugin_name() == 'reddit'
74+
]
75+
76+
assert len(matched_rules) == 1
77+
assert matched_rules[0].get_rule_label() == rule_name
78+
79+
80+
@pytest.mark.parametrize('link', NON_MATCHING_URLS)
81+
def test_url_non_matching(link, bot):
82+
line = PreTrigger(bot.nick, ':[email protected] PRIVMSG #channel {}'.format(link))
83+
matched_rules = [
84+
# we can ignore matches that don't come from the plugin under test
85+
match[0] for match in bot.rules.get_triggered_rules(bot, line)
86+
if match[0].get_plugin_name() == 'reddit'
87+
]
88+
89+
assert len(matched_rules) == 0

0 commit comments

Comments
 (0)