2017-05-21 23:14:56 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2017-07-23 21:24:44 -04:00
|
|
|
from os.path import dirname, basename
|
|
|
|
from importlib import import_module
|
2017-05-25 16:52:01 -04:00
|
|
|
import argparse
|
2017-05-21 23:14:56 -04:00
|
|
|
import os
|
2017-08-15 13:41:45 -04:00
|
|
|
import sys
|
2017-07-23 21:24:44 -04:00
|
|
|
import glob
|
2017-05-21 23:14:56 -04:00
|
|
|
import unittest
|
2017-05-31 13:01:39 -04:00
|
|
|
import logging
|
2017-05-21 23:14:56 -04:00
|
|
|
|
2017-05-25 16:52:01 -04:00
|
|
|
|
2017-07-23 21:24:44 -04:00
|
|
|
def load_tests_from_modules(names, template):
|
|
|
|
loader = unittest.defaultTestLoader
|
|
|
|
test_suites = []
|
2017-05-21 23:14:56 -04:00
|
|
|
|
2017-07-23 21:24:44 -04:00
|
|
|
for name in names:
|
|
|
|
module = import_module(template.format(name=name))
|
|
|
|
test_suites.append(loader.loadTestsFromModule(module))
|
|
|
|
|
|
|
|
return test_suites
|
|
|
|
|
|
|
|
|
|
|
|
def parse_args(available_bots):
|
|
|
|
description = """
|
|
|
|
Script to run test_<bot_name>.py files in the
|
|
|
|
zulip_bot/zulip_bots/bots/<bot_name> directories.
|
2017-05-31 13:01:39 -04:00
|
|
|
|
2017-07-23 21:24:44 -04:00
|
|
|
Running tests for all bots:
|
2017-05-21 23:14:56 -04:00
|
|
|
|
2017-07-23 21:24:44 -04:00
|
|
|
./test-bots
|
2017-05-21 23:14:56 -04:00
|
|
|
|
2017-07-23 21:24:44 -04:00
|
|
|
Running tests for specific bots:
|
2017-06-07 15:11:21 -04:00
|
|
|
|
2017-07-23 21:24:44 -04:00
|
|
|
./test-bots define thesaurus
|
|
|
|
|
|
|
|
Running tests for all bots excluding certain bots (the
|
|
|
|
following command would run tests for all bots except
|
|
|
|
the tests for xkcd and wikipedia bots):
|
|
|
|
|
|
|
|
./test-bots --exclude xkcd wikipedia
|
|
|
|
"""
|
2017-06-07 15:11:21 -04:00
|
|
|
parser = argparse.ArgumentParser(description=description)
|
2017-07-23 21:24:44 -04:00
|
|
|
|
|
|
|
parser.add_argument('bots_to_test',
|
|
|
|
metavar='bot',
|
|
|
|
nargs='*',
|
|
|
|
default=available_bots,
|
2017-06-07 15:11:21 -04:00
|
|
|
help='specific bots to test (default is all)')
|
2017-07-23 21:24:44 -04:00
|
|
|
|
|
|
|
parser.add_argument('--exclude',
|
|
|
|
metavar='bot',
|
|
|
|
nargs='*',
|
|
|
|
default=[],
|
|
|
|
help='bot(s) to exclude')
|
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2017-07-24 20:33:09 -04:00
|
|
|
current_dir = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
bots_dir = os.path.join(current_dir, '..', 'zulip_bots/zulip_bots/bots')
|
|
|
|
glob_pattern = bots_dir + '/*/test_*.py'
|
2017-07-23 21:24:44 -04:00
|
|
|
test_modules = glob.glob(glob_pattern)
|
|
|
|
|
|
|
|
# get only the names of bots that have tests
|
|
|
|
available_bots = map(lambda path: basename(dirname(path)), test_modules)
|
|
|
|
|
|
|
|
options = parse_args(available_bots)
|
|
|
|
|
|
|
|
bots_to_test = filter(lambda bot: bot not in options.exclude,
|
|
|
|
options.bots_to_test)
|
|
|
|
test_suites = load_tests_from_modules(
|
|
|
|
bots_to_test,
|
|
|
|
template='zulip_bots.bots.{name}.test_{name}'
|
|
|
|
)
|
|
|
|
|
|
|
|
suite = unittest.TestSuite(test_suites)
|
2017-06-07 15:11:21 -04:00
|
|
|
runner = unittest.TextTestRunner(verbosity=2)
|
2017-08-15 13:41:45 -04:00
|
|
|
result = runner.run(suite)
|
|
|
|
if result.failures or result.errors:
|
|
|
|
sys.exit(1)
|
2017-06-07 15:11:21 -04:00
|
|
|
|
2017-07-23 21:24:44 -04:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|