2017-05-22 05:14:56 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
from __future__ import absolute_import
|
|
|
|
from __future__ import print_function
|
|
|
|
|
2017-05-25 22:52:01 +02:00
|
|
|
import argparse
|
2017-05-22 05:14:56 +02:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import unittest
|
2017-05-31 19:01:39 +02:00
|
|
|
import logging
|
2017-05-22 05:14:56 +02:00
|
|
|
from unittest import TestCase
|
|
|
|
|
2017-05-25 22:52:01 +02:00
|
|
|
def dir_join(dir1, dir2):
|
|
|
|
# type: (str, str) -> str
|
|
|
|
return os.path.abspath(os.path.join(dir1, dir2))
|
|
|
|
|
2017-05-22 05:14:56 +02:00
|
|
|
|
2017-05-25 22:52:01 +02:00
|
|
|
if __name__ == '__main__':
|
2017-05-31 19:01:39 +02:00
|
|
|
|
2017-05-25 22:52:01 +02:00
|
|
|
description = 'Script to run test_<bot>.py files in bots/<bot> directories'
|
|
|
|
parser = argparse.ArgumentParser(description=description)
|
2017-05-31 19:01:39 +02:00
|
|
|
parser.add_argument('bot_to_test', metavar='bot', nargs='*',
|
|
|
|
help='specific bots to test (default is all)')
|
2017-05-25 22:52:01 +02:00
|
|
|
args = parser.parse_args()
|
2017-05-22 05:14:56 +02:00
|
|
|
|
|
|
|
bots_dir = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
root_dir = dir_join(bots_dir, '..')
|
2017-05-30 08:10:19 +02:00
|
|
|
bots_test_dir = dir_join(bots_dir, '../bots')
|
2017-05-22 05:14:56 +02:00
|
|
|
|
|
|
|
sys.path.insert(0, root_dir)
|
2017-05-30 08:10:19 +02:00
|
|
|
sys.path.insert(0, bots_test_dir)
|
2017-05-22 05:14:56 +02:00
|
|
|
|
2017-05-31 19:01:39 +02:00
|
|
|
def run_tests_in(start_dir):
|
|
|
|
# type: (str) -> None
|
|
|
|
# mypy doesn't recognize the TestLoader attribute, even though the code
|
|
|
|
# is executable
|
2017-06-07 23:02:27 +02:00
|
|
|
loader = unittest.TestLoader() # type: ignore
|
2017-05-31 19:01:39 +02:00
|
|
|
suite = loader.discover(start_dir=start_dir, top_level_dir=root_dir)
|
|
|
|
runner = unittest.TextTestRunner(verbosity=2)
|
|
|
|
# same issue as for TestLoader
|
2017-06-07 23:02:27 +02:00
|
|
|
result = runner.run(suite) # type: ignore
|
2017-05-31 19:01:39 +02:00
|
|
|
if result.errors or result.failures:
|
|
|
|
raise Exception('Test failed!')
|
|
|
|
|
|
|
|
if len(args.bot_to_test) > 0:
|
|
|
|
btd = bots_test_dir
|
|
|
|
available_bots = [b for b in os.listdir(btd) if os.path.isdir(dir_join(btd, b))]
|
|
|
|
tests_incomplete = False
|
|
|
|
for n in args.bot_to_test:
|
|
|
|
if n not in available_bots:
|
|
|
|
logging.warning("Bot with name '%s' is unavailable for testing." % (n))
|
|
|
|
tests_incomplete = True
|
|
|
|
else:
|
|
|
|
run_tests_in(dir_join(bots_test_dir, n))
|
|
|
|
sys.exit(tests_incomplete)
|
|
|
|
else:
|
|
|
|
run_tests_in(bots_test_dir)
|