Skip to content

Commit

Permalink
Add tests, configure tox & travis
Browse files Browse the repository at this point in the history
  • Loading branch information
DonDebonair committed Sep 1, 2017
1 parent 1bccd4a commit a323250
Show file tree
Hide file tree
Showing 10 changed files with 171 additions and 11 deletions.
19 changes: 19 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
sudo: false
language: python
python:
- "3.6"
env:
matrix:
- TOX_ENV=py33
- TOX_ENV=py34
- TOX_ENV=py35
- TOX_ENV=py36
- TOX_ENV=flake8
cache: pip
install:
- "travis_retry pip install setuptools --upgrade"
- "travis_retry pip install tox"
script:
- tox -e $TOX_ENV
after_script:
- cat .tox/$TOX_ENV/log/*.log
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Usage
SLACK_API_TOKEN = 'xox-my-slack-token'
#. Start the bot with ``slack-machine``
#. ...
#. \...
#. Profit!

Writing plugins
Expand Down
5 changes: 1 addition & 4 deletions machine/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ def load_plugins(self):
instance = cls(self._settings, MessagingClient(self._client))
self._register_plugin(plugin, instance)

def _fqn_fn_name(self, plugin, fn_name):
return "{}.{}".format(plugin, fn_name)

def _register_plugin(self, plugin, cls_instance):
if hasattr(cls_instance, 'catch_all'):
self._plugin_actions['catch_all'][plugin] = {
Expand All @@ -61,7 +58,7 @@ def _register_plugin(self, plugin, cls_instance):
self._register_plugin_actions(plugin, fn.metadata, cls_instance, name, fn)

def _register_plugin_actions(self, plugin, metadata, cls_instance, fn_name, fn):
fq_fn_name = self._fqn_fn_name(plugin, fn_name)
fq_fn_name = "{}.{}".format(plugin, fn_name)
for action, config in metadata['plugin_actions'].items():
if action == 'process':
event_type = config['event_type']
Expand Down
9 changes: 7 additions & 2 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
-r requirements.txt
check-manifest==0.35
pyroma==2.2
check-manifest~=0.35
pyroma~=2.2
pytest~=3.2
tox~=2.7
detox~=0.11
flake8~=3.4
twine~=1.5
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
slackclient==1.0.7
slackclient~=1.0
4 changes: 3 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ universal = 1
exclude =
.git,
__pycache__,
dist
dist,
build,
tests
max-line-length=100
58 changes: 56 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from setuptools import setup, find_packages
from codecs import open
import os
import sys
from shutil import rmtree

from setuptools import find_packages, setup, Command

here = os.path.abspath(os.path.dirname(__file__))

Expand All @@ -14,6 +17,54 @@
with open(os.path.join(here, 'requirements.txt'), 'r', 'utf-8') as f:
dependencies = f.read().splitlines()


class PublishCommand(Command):
"""
Support setup.py publish.
Graciously taken from https://github.com/kennethreitz/setup.py
"""

description = 'Build and publish the package.'
user_options = []

@staticmethod
def status(s):
"""Prints things in bold."""
print('\033[1m{0}\033[0m'.format(s))

def initialize_options(self):
pass

def finalize_options(self):
pass

def _remove_builds(self, msg):
try:
self.status(msg)
rmtree(os.path.join(here, 'dist'))
rmtree(os.path.join(here, 'build'))
rmtree(os.path.join(here, '.egg'))
rmtree(os.path.join(here, 'slack_machine.egg-info'))
except FileNotFoundError:
pass

def run(self):
try:
self._remove_builds("Removing previous builds…")
except FileNotFoundError:
pass

self.status("Building Source and Wheel (universal) distribution…")
os.system('{0} setup.py sdist bdist_wheel'.format(sys.executable))

self.status("Uploading the package to PyPi via Twine…")
os.system('twine upload dist/*')

self._remove_builds("Removing builds…")

sys.exit()


setup(
name=about["__title__"],
version=about["__version__"],
Expand Down Expand Up @@ -48,5 +99,8 @@
},
packages=find_packages(),
include_package_data=True,
zip_safe=False
zip_safe=False,
cmdclass={
'publish': PublishCommand,
}
)
Empty file added tests/__init__.py
Empty file.
73 changes: 73 additions & 0 deletions tests/test_decorators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import re
import pytest
from machine.plugins.decorators import process, listen_to, respond_to


@pytest.fixture
def process_f():
@process(event_type='test_event')
def f(event):
pass

return f


@pytest.fixture
def listen_to_f():
@listen_to(r'hello', re.IGNORECASE)
def f(msg):
pass

return f


@pytest.fixture
def respond_to_f():
@respond_to(r'hello', re.IGNORECASE)
def f(msg):
pass

return f


@pytest.fixture
def multi_decorator_f():
@respond_to(r'hello', re.IGNORECASE)
@listen_to(r'hello', re.IGNORECASE)
def f(msg):
pass

return f


def test_process(process_f):
assert hasattr(process_f, 'metadata')
assert 'plugin_actions' in process_f.metadata
assert 'process' in process_f.metadata['plugin_actions']
assert 'event_type' in process_f.metadata['plugin_actions']['process']
assert process_f.metadata['plugin_actions']['process']['event_type'] == 'test_event'


def test_listen_to(listen_to_f):
assert hasattr(listen_to_f, 'metadata')
assert 'plugin_actions' in listen_to_f.metadata
assert 'listen_to' in listen_to_f.metadata['plugin_actions']
assert 'regex' in listen_to_f.metadata['plugin_actions']['listen_to']
assert listen_to_f.metadata['plugin_actions']['listen_to']['regex'] == re.compile(r'hello',
re.IGNORECASE)


def test_respond_to(respond_to_f):
assert hasattr(respond_to_f, 'metadata')
assert 'plugin_actions' in respond_to_f.metadata
assert 'respond_to' in respond_to_f.metadata['plugin_actions']
assert 'regex' in respond_to_f.metadata['plugin_actions']['respond_to']
assert respond_to_f.metadata['plugin_actions']['respond_to']['regex'] == re.compile(r'hello', re.IGNORECASE)


def test_mulitple_decorators(multi_decorator_f):
assert hasattr(multi_decorator_f, 'metadata')
assert 'plugin_actions' in multi_decorator_f.metadata
assert 'respond_to' in multi_decorator_f.metadata['plugin_actions']
assert 'listen_to' in multi_decorator_f.metadata['plugin_actions']
assert 'process' not in multi_decorator_f.metadata['plugin_actions']
10 changes: 10 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[tox]
envlist = py33,py34,py35,py36,flake8

[testenv]
commands = pytest
deps =-r{toxinidir}/requirements-dev.txt

[testenv:flake8]
deps = flake8
commands = flake8 machine/ setup.py

0 comments on commit a323250

Please sign in to comment.