2017-12-31 09:51:24 -05:00
|
|
|
#!/usr/bin/env python3
|
2017-05-21 23:14:56 -04:00
|
|
|
|
2017-07-23 21:24:44 -04:00
|
|
|
from os.path import dirname, basename
|
|
|
|
from importlib import import_module
|
2017-08-21 09:03:08 -04:00
|
|
|
|
2017-05-21 23:14:56 -04:00
|
|
|
import os
|
2017-08-15 13:41:45 -04:00
|
|
|
import sys
|
2017-08-21 09:03:08 -04:00
|
|
|
import argparse
|
2017-07-23 21:24:44 -04:00
|
|
|
import glob
|
2017-05-21 23:14:56 -04:00
|
|
|
import unittest
|
2017-11-14 05:25:24 -05:00
|
|
|
from unittest import TestCase, TestSuite
|
2017-05-25 16:52:01 -04:00
|
|
|
|
2018-05-26 18:30:04 -04:00
|
|
|
def parse_args():
|
2017-07-23 21:24:44 -04:00
|
|
|
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-08-21 09:03:08 -04:00
|
|
|
Running all tests:
|
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-08-16 19:19:47 -04:00
|
|
|
./test-bots define xkcd
|
2017-07-23 21:24:44 -04:00
|
|
|
|
2017-08-21 09:03:08 -04:00
|
|
|
Running all tests excluding certain bots (the
|
2017-07-23 21:24:44 -04:00
|
|
|
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='*',
|
2017-08-21 09:03:08 -04:00
|
|
|
default=[],
|
2017-06-07 15:11:21 -04:00
|
|
|
help='specific bots to test (default is all)')
|
2017-08-21 09:03:08 -04:00
|
|
|
parser.add_argument('--coverage',
|
|
|
|
nargs='?',
|
|
|
|
const=True,
|
|
|
|
default=False,
|
|
|
|
help='compute test coverage (--coverage combine to combine with previous reports)')
|
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-08-21 09:03:08 -04:00
|
|
|
TOOLS_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
os.chdir(os.path.dirname(TOOLS_DIR))
|
|
|
|
sys.path.insert(0, TOOLS_DIR)
|
|
|
|
bots_dir = os.path.join(TOOLS_DIR, '..', 'zulip_bots/zulip_bots/bots')
|
2017-07-24 20:33:09 -04:00
|
|
|
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)
|
2018-05-26 18:30:04 -04:00
|
|
|
|
|
|
|
options = parse_args()
|
2017-07-23 21:24:44 -04:00
|
|
|
|
2017-08-21 09:03:08 -04:00
|
|
|
if options.coverage:
|
|
|
|
import coverage
|
|
|
|
cov = coverage.Coverage(config_file="tools/.coveragerc")
|
|
|
|
if options.coverage == 'combine':
|
|
|
|
cov.load()
|
|
|
|
cov.start()
|
|
|
|
|
|
|
|
if options.bots_to_test:
|
2018-05-26 19:39:22 -04:00
|
|
|
specified_bots = options.bots_to_test
|
2017-08-21 09:03:08 -04:00
|
|
|
else:
|
2018-05-26 19:39:22 -04:00
|
|
|
specified_bots = available_bots
|
|
|
|
|
|
|
|
bots_to_test = filter(lambda bot: bot not in options.exclude, specified_bots)
|
2018-05-26 19:33:26 -04:00
|
|
|
|
|
|
|
# Should add a check here that __init__.py is absent if test_*.py is present?
|
|
|
|
|
|
|
|
# Codecov seems to work only when using loader.discover. It failed to
|
|
|
|
# capture line executions for functions like loader.loadTestFromModule
|
|
|
|
# or loader.loadTestFromNames.
|
|
|
|
top_level = "zulip_bots/zulip_bots/bots/"
|
|
|
|
loader = unittest.defaultTestLoader
|
|
|
|
test_suites = [loader.discover(top_level + name, top_level_dir=top_level)
|
|
|
|
for name in bots_to_test]
|
2017-07-23 21:24:44 -04:00
|
|
|
|
2017-11-14 05:25:24 -05:00
|
|
|
def filter_tests(tests):
|
|
|
|
# type: (Union[TestSuite, TestCase]) -> TestSuite
|
|
|
|
filtered_tests = TestSuite()
|
|
|
|
for test in tests:
|
|
|
|
if isinstance(test, TestCase):
|
|
|
|
# Exclude test base class from being tested.
|
2017-12-14 05:24:11 -05:00
|
|
|
if test.__class__.__name__ != 'BotTestCase':
|
2017-11-14 05:25:24 -05:00
|
|
|
filtered_tests.addTest(test)
|
|
|
|
else:
|
|
|
|
filtered_tests.addTest(filter_tests(test))
|
|
|
|
return filtered_tests
|
|
|
|
test_suites = filter_tests(test_suites)
|
|
|
|
|
2017-07-23 21:24:44 -04:00
|
|
|
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-08-21 09:03:08 -04:00
|
|
|
if not result.failures and options.coverage:
|
|
|
|
cov.stop()
|
|
|
|
cov.data_suffix = False # Disable suffix so that filename is .coverage
|
|
|
|
cov.save()
|
|
|
|
cov.html_report()
|
|
|
|
print("HTML report saved under directory 'htmlcov'.")
|
2017-07-23 21:24:44 -04:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|