1#!/usr/bin/env vpython3 2# Copyright 2017 The Chromium Authors 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5"""Ensure files in the directory are thoroughly tested.""" 6 7import importlib 8import io 9import os 10import sys 11import unittest 12 13import coverage # pylint: disable=import-error 14 15# The files need to have sufficient coverages. 16COVERED_FILES = [ 17 'compatible_utils.py', 'deploy_to_fuchsia.py', 'flash_device.py', 18 'log_manager.py', 'publish_package.py', 'serve_repo.py', 'test_server.py' 19] 20 21# The files will be tested without coverage requirements. 22TESTED_FILES = ['common.py', 'ffx_emulator.py'] 23 24 25def main(): 26 """Gather coverage data, ensure included files are 100% covered.""" 27 28 # Fuchsia tests not supported on Windows 29 if os.name == 'nt': 30 return 0 31 32 cov = coverage.coverage(data_file=None, 33 include=COVERED_FILES, 34 config_file=True) 35 cov.start() 36 37 for file in COVERED_FILES + TESTED_FILES: 38 print('Testing ' + file + ' ...') 39 # pylint: disable=import-outside-toplevel 40 # import tests after coverage start to also cover definition lines. 41 module = importlib.import_module(file.replace('.py', '_unittests')) 42 # pylint: enable=import-outside-toplevel 43 44 tests = unittest.TestLoader().loadTestsFromModule(module) 45 if not unittest.TextTestRunner().run(tests).wasSuccessful(): 46 return 1 47 48 cov.stop() 49 outf = io.StringIO() 50 percentage = cov.report(file=outf, show_missing=True) 51 if int(percentage) != 100: 52 print(outf.getvalue()) 53 print('FATAL: Insufficient coverage (%.f%%)' % int(percentage)) 54 return 1 55 return 0 56 57 58if __name__ == '__main__': 59 sys.exit(main()) 60