• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 = [
23    'common.py', 'ffx_emulator.py', 'modification_waiter.py',
24    'serial_boot_device.py'
25]
26
27
28def main():
29    """Gather coverage data, ensure included files are 100% covered."""
30
31    # Fuchsia tests not supported on Windows
32    if os.name == 'nt':
33        return 0
34
35    cov = coverage.coverage(data_file=None,
36                            include=COVERED_FILES,
37                            config_file=True)
38    cov.start()
39
40    for file in COVERED_FILES + TESTED_FILES:
41        print('Testing ' + file + ' ...')
42        # pylint: disable=import-outside-toplevel
43        # import tests after coverage start to also cover definition lines.
44        module = importlib.import_module(file.replace('.py', '_unittests'))
45        # pylint: enable=import-outside-toplevel
46
47        tests = unittest.TestLoader().loadTestsFromModule(module)
48        if not unittest.TextTestRunner().run(tests).wasSuccessful():
49            return 1
50
51    cov.stop()
52    outf = io.StringIO()
53    percentage = cov.report(file=outf, show_missing=True)
54    if int(percentage) != 100:
55        print(outf.getvalue())
56        print('FATAL: Insufficient coverage (%.f%%)' % int(percentage))
57        return 1
58    return 0
59
60
61if __name__ == '__main__':
62    sys.exit(main())
63