2013-08-08 16:52:19 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
2017-11-30 18:26:44 +00:00
|
|
|
# Copyright 2013 - 2017, New York University and the TUF contributors
|
|
|
|
|
# SPDX-License-Identifier: MIT OR Apache-2.0
|
|
|
|
|
|
2013-03-05 04:11:34 +00:00
|
|
|
"""
|
|
|
|
|
<Program Name>
|
|
|
|
|
aggregate_tests.py
|
|
|
|
|
|
|
|
|
|
<Author>
|
2014-04-20 20:19:56 +00:00
|
|
|
Konstantin Andrianov.
|
|
|
|
|
Zane Fisher.
|
2013-03-05 04:11:34 +00:00
|
|
|
|
|
|
|
|
<Started>
|
2014-04-20 20:19:56 +00:00
|
|
|
January 26, 2013.
|
2013-08-06 17:42:44 +00:00
|
|
|
|
2013-08-30 18:58:41 +00:00
|
|
|
August 2013.
|
|
|
|
|
Modified previous behavior that explicitly imported individual
|
2013-08-06 17:42:44 +00:00
|
|
|
unit tests. -Zane Fisher
|
2017-01-12 20:20:03 +00:00
|
|
|
|
2013-03-05 04:11:34 +00:00
|
|
|
<Copyright>
|
2018-02-05 16:31:19 +00:00
|
|
|
See LICENSE-MIT OR LICENSE for licensing information.
|
2013-03-05 04:11:34 +00:00
|
|
|
|
|
|
|
|
<Purpose>
|
2013-08-30 18:58:41 +00:00
|
|
|
Run all the unit tests from every .py file beginning with "test_" in
|
|
|
|
|
'tuf/tests'. Use --random to run the tests in random order.
|
2013-03-05 04:11:34 +00:00
|
|
|
"""
|
|
|
|
|
|
2014-04-29 18:27:34 +00:00
|
|
|
# Help with Python 3 compatibility, where the print statement is a function, an
|
|
|
|
|
# implicit relative import is invalid, and the '/' operator performs true
|
|
|
|
|
# division. Example: print 'hello world' raises a 'SyntaxError' exception.
|
|
|
|
|
from __future__ import print_function
|
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
from __future__ import division
|
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
2013-08-26 19:35:28 +00:00
|
|
|
import sys
|
2013-07-31 23:01:19 +00:00
|
|
|
import unittest
|
2013-07-30 18:41:07 +00:00
|
|
|
import glob
|
2013-08-13 22:41:38 +00:00
|
|
|
import random
|
2013-07-30 18:41:07 +00:00
|
|
|
|
2013-08-30 18:58:41 +00:00
|
|
|
# Generate a list of pathnames that match a pattern (i.e., that begin with
|
|
|
|
|
# 'test_' and end with '.py'. A shell-style wildcard is used with glob() to
|
|
|
|
|
# match desired filenames. All the tests matching the pattern will be loaded
|
|
|
|
|
# and run in a test suite.
|
2018-05-02 21:04:39 +00:00
|
|
|
tests_list = glob.glob('test_*.py')
|
2013-07-30 18:41:07 +00:00
|
|
|
|
2018-09-20 14:02:03 +00:00
|
|
|
# A dictionary of test modules that should only run in certain python versions.
|
|
|
|
|
# Carefully consider the impact of only testing these in a given version.
|
|
|
|
|
# test_proxy_use.py: uses a proxy that only runs in Python2.7. TUF's
|
|
|
|
|
# compatibility with proxies is not likely to vary based on the Python version
|
|
|
|
|
# in use, so this is OK for now. See comments in that module.
|
|
|
|
|
# The format here is: if major is included, limit version to that listed here.
|
|
|
|
|
# If minor is included, limit version to that listed here. Skip the test if any
|
|
|
|
|
# such listed constraints don't match the python version currently running.
|
|
|
|
|
VERSION_SPECIFIC_TESTS = {
|
|
|
|
|
'test_proxy_use': {'major': 2, 'minor': 7}} # Run test only if Python2.7
|
|
|
|
|
|
2013-08-30 18:58:41 +00:00
|
|
|
# Remove '.py' from each filename to allow loadTestsFromNames() (called below)
|
|
|
|
|
# to properly load the file as a module.
|
|
|
|
|
tests_without_extension = []
|
|
|
|
|
for test in tests_list:
|
|
|
|
|
test = test[:-3]
|
2018-09-20 14:02:03 +00:00
|
|
|
if test in VERSION_SPECIFIC_TESTS:
|
|
|
|
|
if 'major' in VERSION_SPECIFIC_TESTS[test] \
|
|
|
|
|
and sys.version_info.major != VERSION_SPECIFIC_TESTS[test]['major']:
|
|
|
|
|
continue
|
|
|
|
|
elif 'minor' in VERSION_SPECIFIC_TESTS[test] \
|
|
|
|
|
and sys.version_info.minor != VERSION_SPECIFIC_TESTS[test]['minor']:
|
|
|
|
|
continue
|
2013-08-30 18:58:41 +00:00
|
|
|
tests_without_extension.append(test)
|
2013-07-31 23:01:19 +00:00
|
|
|
|
2014-04-29 18:27:34 +00:00
|
|
|
# Randomize the order in which the tests run. Randomization might catch errors
|
|
|
|
|
# with unit tests that do not properly clean up or restore monkey-patched
|
|
|
|
|
# modules.
|
2014-04-20 20:15:19 +00:00
|
|
|
random.shuffle(tests_without_extension)
|
2013-08-13 22:41:38 +00:00
|
|
|
|
2015-04-27 22:32:09 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
|
suite = unittest.TestLoader().loadTestsFromNames(tests_without_extension)
|
2016-01-21 18:25:58 +00:00
|
|
|
all_tests_passed = unittest.TextTestRunner(verbosity=1).run(suite).wasSuccessful()
|
2015-04-27 22:32:09 +00:00
|
|
|
if not all_tests_passed:
|
|
|
|
|
sys.exit(1)
|
2018-05-02 15:28:20 +00:00
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
sys.exit(0)
|