Skip to content

Commit

Permalink
Call skills correctly depending on function or class skills (#820)
Browse files Browse the repository at this point in the history
* Call skills correctly depending on function or class skills

* Remove unused import

* Better argument inspection

* Remove deprecated method

* Add test for class based skills
  • Loading branch information
jacobtomlinson committed Jan 28, 2019
1 parent 6f226ad commit fef6f81
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
6 changes: 5 additions & 1 deletion opsdroid/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import weakref
import asyncio
import contextlib
import inspect

from opsdroid.const import DEFAULT_CONFIG_PATH
from opsdroid.memory import Memory
Expand Down Expand Up @@ -312,7 +313,10 @@ async def run_skill(self, skill, config, message):
# halt the application. If a skill throws an exception it just doesn't
# give a response to the user, so an error response should be given.
try:
await skill(self, config, message)
if len(inspect.signature(skill).parameters.keys()) > 1:
await skill(self, config, message)
else:
await skill(message)
except Exception:
if message:
await message.respond(_("Whoops there has been an error"))
Expand Down
19 changes: 19 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,12 @@ async def mockedskill(opsdroid, config, message):
mockedskill.config = {}
return mockedskill

async def getMockMethodSkill(self):
async def mockedskill(message):
await message.respond("Test")
mockedskill.config = {}
return mockedskill

async def test_handle_signal(self):
with OpsDroid() as opsdroid:
opsdroid._running = True
Expand Down Expand Up @@ -293,6 +299,19 @@ async def test_parse_regex(self):
await task
self.assertTrue(mock_connector.respond.called)

async def test_parse_regex_method_skill(self):
with OpsDroid() as opsdroid:
regex = r"Hello .*"
mock_connector = Connector({}, opsdroid=opsdroid)
mock_connector.respond = amock.CoroutineMock()
skill = await self.getMockMethodSkill()
opsdroid.skills.append(match_regex(regex)(skill))
message = Message("user", "default", mock_connector, "Hello world")
tasks = await opsdroid.parse(message)
for task in tasks:
await task
self.assertTrue(mock_connector.respond.called)

async def test_parse_regex_insensitive(self):
with OpsDroid() as opsdroid:
regex = r"Hello .*"
Expand Down

0 comments on commit fef6f81

Please sign in to comment.