1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3#
4# Copyright 2017, Data61
5# Commonwealth Scientific and Industrial Research Organisation (CSIRO)
6# ABN 41 687 119 230.
7#
8# This software may be distributed and modified according to the terms of
9# the BSD 2-Clause license. Note that NO WARRANTY is provided.
10# See "LICENSE_BSD2.txt" for details.
11#
12# @TAG(DATA61_BSD)
13#
14
15'''
16This script is a quick way to execute the tests for all CAmkES modules.
17'''
18
19from __future__ import absolute_import, division, print_function, \
20    unicode_literals
21from concurrencytest import ConcurrentTestSuite, fork_for_tests
22import capdl
23import camkes.ast
24import camkes.internal
25import camkes.parser
26import camkes.runner
27import camkes.templates
28
29import argparse, multiprocessing, os, subprocess, sys, unittest
30
31ME = os.path.abspath(__file__)
32
33# Available tests. The keys to this dictionary should be names of packages containing tests
34# packages must be imported in this file and the directory with tests must be a valid
35# python module (--> contains an __init__.py)
36TESTS = ['ast', 'internal', 'parser','runner','templates']
37
38def main(argv):
39    parser = argparse.ArgumentParser(prog=argv[0],
40        description='run CAmkES tests')
41    parser.add_argument('--jobs', '-j', nargs='?', type=int,
42        help='parallelise test execution')
43    parser.add_argument('--verbosity', '-v', default=1, type=int, help="Verbosity to run tests. 0 = quiet. 1 = default. 2 = verbose")
44    parser.add_argument('test', nargs='*', choices=TESTS+['all'], default='all', help='run a specific category of tests')
45    parser.add_argument('--capdl-python', help='Deprecated. Using this argument has no effect.')
46    options = parser.parse_args(argv[1:])
47
48    if options.jobs is None:
49        # Maximum parallelism.
50        options.jobs = multiprocessing.cpu_count()
51
52    # work out which tests to run
53    if options.test == 'all' or 'all' in options.test:
54        test_packages = TESTS
55    else:
56        test_packages = options.test
57
58    # load the tests we want to run
59    loader = unittest.TestLoader()
60    test_suite = unittest.TestSuite()
61    for v in test_packages:
62        test_suite.addTests(loader.discover('camkes.' + v, top_level_dir=os.path.dirname(ME)))
63
64    concurrent_suite = ConcurrentTestSuite(test_suite, fork_for_tests(options.jobs))
65    runner = unittest.TextTestRunner(verbosity=options.verbosity)
66    result = runner.run(concurrent_suite)
67    if result.wasSuccessful():
68        return 0
69    return 1
70
71if __name__ == '__main__':
72    sys.exit(main(sys.argv))
73