Skip to content

Commit

Permalink
Give useful error message
Browse files Browse the repository at this point in the history
Give proper usage of command when a
command is used without args.

Fixes coala#356
  • Loading branch information
Vamshi99 committed Nov 28, 2017
1 parent 340bf99 commit 0c7fbde
Show file tree
Hide file tree
Showing 10 changed files with 313 additions and 264 deletions.
13 changes: 9 additions & 4 deletions plugins/explain.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,19 @@ class Explain(BotPlugin):
'\n- '.join(MSGS.keys())
)

@re_botcmd(pattern=r'^explain\s+(\w+)(?:\s+to\s+@?([\w-]+))?$',
@re_botcmd(pattern=r'^explain(\s+(\w+)(?:\s+to\s+@?([\w-]+))?)$',
re_cmd_name_help='explain <term>',
flags=re.IGNORECASE)
def explain(self, msg, match):
"""Explain various terms.""" # Ignore QuotesBear
return ('{}'.format('@{}: \n'.format(match.group(2))
if match.group(2) else '') +
if match.group(1):
return ('{}'.format('@{}: \n'.format(match.group(3))
if match.group(3) else '') +
self.MSGS.get(
match.group(1).lower(),
match.group(2).lower(),
self.ERROR_MSG
).format(bot_prefix=self.bot_config.BOT_PREFIX))
else:
return('Invalid command args. Usage: `{} '
'explain <term>`.'.format(
self.bot_config.BOT_PREFIX))
22 changes: 14 additions & 8 deletions plugins/ghetto.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,25 @@ class Ghetto(BotPlugin):
Real talk yo
"""

@re_botcmd(pattern=r'ghetto\s+(.+)',
@re_botcmd(pattern=r'ghetto(\s+(.+))',
re_cmd_name_help='ghetto <sentence>',
flags=re.IGNORECASE)
def ghetto(self, msg, match):
"""
Real talk yo
"""
rq = requests.post('http://www.gizoogle.net/textilizer.php',
data={'translatetext': match.group(1)})
if match.group(1):

translated_text = re.search(
r'<textarea .*;\"/>(.+)</textarea>', rq.text)
if translated_text is not None:
return translated_text.group(1)
rq = requests.post('http://www.gizoogle.net/textilizer.php',
data={'translatetext': match.group(2)})

translated_text = re.search(
r'<textarea .*;\"/>(.+)</textarea>', rq.text)
if translated_text is not None:
return translated_text.group(1)
else:
return 'Shiznit happens!'
else:
return 'Shiznit happens!'
return('Invalid command args. Usage: `{} '
'ghetto <sentence>`'.format(
self.bot_config.BOT_PREFIX))
497 changes: 260 additions & 237 deletions plugins/labhub.py

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions plugins/lmgtfy.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ class Lmgtfy(BotPlugin):
'help you even faster!'
)

@re_botcmd(pattern=r'lmgtfy\s+(.+)',
@re_botcmd(pattern=r'lmgtfy(\s+(.+))',
re_cmd_name_help='lmgtfy <search-string>')
def lmgtfy(self, msg, match):
"""I'm lazy, please google for me.""" # Ignore QuotesBear
link = 'https://www.lmgtfy.com/?q=' + match.group(1)
return self.MSG.format(link)
if match.group(1):
link = 'https://www.lmgtfy.com/?q=' + match.group(2)
return self.MSG.format(link)
else:
return('Invalid command args. Usage: `{} lmgtfy '
'<search-string>`'.format(self.bot_config.BOT_PREFIX))
22 changes: 13 additions & 9 deletions plugins/wolfram_alpha.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ def wa(self, msg, arg):
Query the Computational Knowledge Engine.
"""
ans = ''
res = self.client.query(arg)
try:
for pod in res.pods:
if pod.title in ['Result', 'Results']:
for sub in pod.subpods:
ans += sub.plaintext
except AttributeError:
self.log.info('KeyError triggered on retrieving pods.')
return ans if ans else 'Dunno :('
if arg:
res = self.client.query(arg)
try:
for pod in res.pods:
if pod.title in ['Result', 'Results']:
for sub in pod.subpods:
ans += sub.plaintext
except AttributeError:
self.log.info('KeyError triggered on retrieving pods.')
return ans if ans else 'Dunno :('
else:
return('Invalid command args. Usage: `{} wa '
'<query>`'.format(self.bot_config.BOT_PREFIX))
1 change: 1 addition & 0 deletions tests/explain_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ def test_explain(testbot):
'@meet')
testbot.assertCommand("!please explain review",
"Command \"please\" / \"please explain\" not found.")
testbot.assertCommand("!explain", "Invalid command args.")
1 change: 1 addition & 0 deletions tests/ghetto_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ def test_ghetto(testbot):
m.register_uri('POST', 'http://www.gizoogle.net/textilizer.php',
text='test text which will not match with the regex')
testbot.assertCommand("!ghetto ...", "Shiznit happens!")
testbot.assertCommand("!ghetto", "Invalid command args.")
9 changes: 6 additions & 3 deletions tests/labhub_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def test_invite_cmd(self):

testbot.assertCommand('!invite meet to developers',
':poop:')
testbot.assertCommand('!invite', 'Invalid command args.')

def test_hello_world_callback(self):
teams = {
Expand Down Expand Up @@ -98,7 +99,7 @@ def test_create_issue_cmd(self):
)

testbot.assertCommand('!new issue coala title', 'repository that does not exist')

testbot.assertCommand('!new issue', 'Invalid command ars.')
def test_unassign_cmd(self):
plugins.labhub.GitHub = create_autospec(IGitt.GitHub.GitHub.GitHub)
plugins.labhub.GitLab = create_autospec(IGitt.GitLab.GitLab.GitLab)
Expand Down Expand Up @@ -128,6 +129,7 @@ def test_unassign_cmd(self):

testbot.assertCommand('!unassign https://gitlab.com/ala/am/issues/532',
'Repository not owned by our org.')
testbot.assertCommand('!unassign', 'Invalid command args.')

def test_assign_cmd(self):
plugins.labhub.GitHub = create_autospec(IGitt.GitHub.GitHub.GitHub)
Expand Down Expand Up @@ -215,7 +217,7 @@ def test_assign_cmd(self):
# unknown org
testbot.assertCommand(cmd.format('coa', 'a', '23'),
'Repository not owned by our org.')

testbot.assertCommand('!assign', 'Invalid command args.')
def test_mark_cmd(self):
labhub, testbot = plugin_testbot(plugins.labhub.LabHub, logging.ERROR)
labhub.activate()
Expand All @@ -240,7 +242,7 @@ def test_mark_cmd(self):
mock_mr.labels = ['process/wip']
testbot.assertCommand(cmd.format('pending', 'coala', 'a', '23'),
'marked pending review')

testbot.assertCommand('!mark', 'Invalid command args.')
def test_alive(self):
labhub, testbot = plugin_testbot(plugins.labhub.LabHub, logging.ERROR)
with patch('plugins.labhub.time.sleep') as mock_sleep:
Expand Down Expand Up @@ -277,6 +279,7 @@ def test_alive(self):
testbot.assertCommand('!pr stats 3hours',
'10 PRs opened in last 3 hours\n'
'The community is on fire')
testbot.assertCommand('!pr stats', 'Invalid command args.')

def test_invite_me(self):
teams = {
Expand Down
1 change: 1 addition & 0 deletions tests/lmgtfy_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ def test_lmgtfy(testbot):
text.convert(
Lmgtfy.MSG.format("https://www.lmgtfy.com/?q=py c")
))
testbot.assertCommand('!lmgtfy', 'Invalid command args.')
1 change: 1 addition & 0 deletions tests/wolfram_alpha_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def setUp(self):
@my_vcr.use_cassette('tests/cassettes/wa.yaml')
def test_wa(self):
self.testbot.assertCommand("!wa 2^6", "64")
self.testbot.assertCommand("!wa", "Invalid command args.")
with self.assertLogs() as cm:
self.testbot.assertCommand('!wa this is a sentence',
'Dunno')
Expand Down

0 comments on commit 0c7fbde

Please sign in to comment.