Skip to content

Commit 831f797

Browse files
authored
Merge pull request #2216 from sopel-irc/reddit-bare-comments
reddit, test: even more of the comments link format is optional
2 parents 8f84de7 + 7aeed0c commit 831f797

File tree

2 files changed

+89
-77
lines changed

2 files changed

+89
-77
lines changed

sopel/modules/reddit.py

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

0 commit comments

Comments
 (0)