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