1*c2e18aaaSAndroid Build Coastguard Worker#!/usr/bin/env python3 2*c2e18aaaSAndroid Build Coastguard Worker# 3*c2e18aaaSAndroid Build Coastguard Worker# Copyright 2019 The Android Open Source Project 4*c2e18aaaSAndroid Build Coastguard Worker# 5*c2e18aaaSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); 6*c2e18aaaSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License. 7*c2e18aaaSAndroid Build Coastguard Worker# You may obtain a copy of the License at 8*c2e18aaaSAndroid Build Coastguard Worker# 9*c2e18aaaSAndroid Build Coastguard Worker# http://www.apache.org/licenses/LICENSE-2.0 10*c2e18aaaSAndroid Build Coastguard Worker# 11*c2e18aaaSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software 12*c2e18aaaSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, 13*c2e18aaaSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14*c2e18aaaSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and 15*c2e18aaaSAndroid Build Coastguard Worker# limitations under the License. 16*c2e18aaaSAndroid Build Coastguard Worker"""Main entrypoint for all of aidegen's unittest.""" 17*c2e18aaaSAndroid Build Coastguard Worker 18*c2e18aaaSAndroid Build Coastguard Workerimport logging 19*c2e18aaaSAndroid Build Coastguard Workerimport os 20*c2e18aaaSAndroid Build Coastguard Workerimport sys 21*c2e18aaaSAndroid Build Coastguard Workerimport unittest 22*c2e18aaaSAndroid Build Coastguard Workerfrom importlib import import_module 23*c2e18aaaSAndroid Build Coastguard Worker 24*c2e18aaaSAndroid Build Coastguard Worker# Setup logging to be silent so unittests can pass through TF. 25*c2e18aaaSAndroid Build Coastguard Workerlogging.disable(logging.ERROR) 26*c2e18aaaSAndroid Build Coastguard Worker 27*c2e18aaaSAndroid Build Coastguard Worker 28*c2e18aaaSAndroid Build Coastguard Workerdef get_test_modules(): 29*c2e18aaaSAndroid Build Coastguard Worker """Returns a list of testable modules. 30*c2e18aaaSAndroid Build Coastguard Worker 31*c2e18aaaSAndroid Build Coastguard Worker Finds all the test files (*_unittest.py) and gets their relative 32*c2e18aaaSAndroid Build Coastguard Worker paths (internal/lib/utils_test.py) and translate it to an import path and 33*c2e18aaaSAndroid Build Coastguard Worker strip the py ext (internal.lib.utils_test). 34*c2e18aaaSAndroid Build Coastguard Worker 35*c2e18aaaSAndroid Build Coastguard Worker Returns: 36*c2e18aaaSAndroid Build Coastguard Worker List of strings (the testable module import path). 37*c2e18aaaSAndroid Build Coastguard Worker """ 38*c2e18aaaSAndroid Build Coastguard Worker testable_modules = [] 39*c2e18aaaSAndroid Build Coastguard Worker package = os.path.dirname(os.path.realpath(__file__)) 40*c2e18aaaSAndroid Build Coastguard Worker base_path = os.path.dirname(package) 41*c2e18aaaSAndroid Build Coastguard Worker 42*c2e18aaaSAndroid Build Coastguard Worker for dirpath, _, files in os.walk(package): 43*c2e18aaaSAndroid Build Coastguard Worker for _file in files: 44*c2e18aaaSAndroid Build Coastguard Worker if _file.endswith("_unittest.py"): 45*c2e18aaaSAndroid Build Coastguard Worker # Now transform it into a relative import path. 46*c2e18aaaSAndroid Build Coastguard Worker full_file_path = os.path.join(dirpath, _file) 47*c2e18aaaSAndroid Build Coastguard Worker rel_file_path = os.path.relpath(full_file_path, base_path) 48*c2e18aaaSAndroid Build Coastguard Worker rel_file_path, _ = os.path.splitext(rel_file_path) 49*c2e18aaaSAndroid Build Coastguard Worker rel_file_path = rel_file_path.replace(os.sep, ".") 50*c2e18aaaSAndroid Build Coastguard Worker testable_modules.append(rel_file_path) 51*c2e18aaaSAndroid Build Coastguard Worker 52*c2e18aaaSAndroid Build Coastguard Worker return testable_modules 53*c2e18aaaSAndroid Build Coastguard Worker 54*c2e18aaaSAndroid Build Coastguard Worker 55*c2e18aaaSAndroid Build Coastguard Workerdef main(): 56*c2e18aaaSAndroid Build Coastguard Worker """Main unittest entry.""" 57*c2e18aaaSAndroid Build Coastguard Worker test_modules = get_test_modules() 58*c2e18aaaSAndroid Build Coastguard Worker for mod in test_modules: 59*c2e18aaaSAndroid Build Coastguard Worker import_module(mod) 60*c2e18aaaSAndroid Build Coastguard Worker 61*c2e18aaaSAndroid Build Coastguard Worker loader = unittest.defaultTestLoader 62*c2e18aaaSAndroid Build Coastguard Worker test_suite = loader.loadTestsFromNames(test_modules) 63*c2e18aaaSAndroid Build Coastguard Worker runner = unittest.TextTestRunner(verbosity=2) 64*c2e18aaaSAndroid Build Coastguard Worker result = runner.run(test_suite) 65*c2e18aaaSAndroid Build Coastguard Worker sys.exit(not result.wasSuccessful()) 66*c2e18aaaSAndroid Build Coastguard Worker 67*c2e18aaaSAndroid Build Coastguard Worker 68*c2e18aaaSAndroid Build Coastguard Workerif __name__ == '__main__': 69*c2e18aaaSAndroid Build Coastguard Worker main() 70