1*d4514f0bSApple OSS Distributions## 2*d4514f0bSApple OSS Distributions# Copyright (c) 2023 Apple Inc. All rights reserved. 3*d4514f0bSApple OSS Distributions# 4*d4514f0bSApple OSS Distributions# @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 5*d4514f0bSApple OSS Distributions# 6*d4514f0bSApple OSS Distributions# This file contains Original Code and/or Modifications of Original Code 7*d4514f0bSApple OSS Distributions# as defined in and that are subject to the Apple Public Source License 8*d4514f0bSApple OSS Distributions# Version 2.0 (the 'License'). You may not use this file except in 9*d4514f0bSApple OSS Distributions# compliance with the License. The rights granted to you under the License 10*d4514f0bSApple OSS Distributions# may not be used to create, or enable the creation or redistribution of, 11*d4514f0bSApple OSS Distributions# unlawful or unlicensed copies of an Apple operating system, or to 12*d4514f0bSApple OSS Distributions# circumvent, violate, or enable the circumvention or violation of, any 13*d4514f0bSApple OSS Distributions# terms of an Apple operating system software license agreement. 14*d4514f0bSApple OSS Distributions# 15*d4514f0bSApple OSS Distributions# Please obtain a copy of the License at 16*d4514f0bSApple OSS Distributions# http://www.opensource.apple.com/apsl/ and read it before using this file. 17*d4514f0bSApple OSS Distributions# 18*d4514f0bSApple OSS Distributions# The Original Code and all software distributed under the License are 19*d4514f0bSApple OSS Distributions# distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 20*d4514f0bSApple OSS Distributions# EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 21*d4514f0bSApple OSS Distributions# INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 22*d4514f0bSApple OSS Distributions# FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 23*d4514f0bSApple OSS Distributions# Please see the License for the specific language governing rights and 24*d4514f0bSApple OSS Distributions# limitations under the License. 25*d4514f0bSApple OSS Distributions# 26*d4514f0bSApple OSS Distributions# @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 27*d4514f0bSApple OSS Distributions## 28*d4514f0bSApple OSS Distributions 29*d4514f0bSApple OSS Distributions""" Source code coverage report 30*d4514f0bSApple OSS Distributions 31*d4514f0bSApple OSS Distributions This module tracks source code coverage if requried modules are installed. 32*d4514f0bSApple OSS Distributions Coverage tracking is disabled when the `coverage` library is missing. 33*d4514f0bSApple OSS Distributions""" 34*d4514f0bSApple OSS Distributions 35*d4514f0bSApple OSS Distributionsimport contextlib 36*d4514f0bSApple OSS Distributions 37*d4514f0bSApple OSS Distributionstry: 38*d4514f0bSApple OSS Distributions import coverage 39*d4514f0bSApple OSS Distributions COVERAGE_ENABLED = True 40*d4514f0bSApple OSS Distributionsexcept ImportError: 41*d4514f0bSApple OSS Distributions COVERAGE_ENABLED = False 42*d4514f0bSApple OSS Distributions 43*d4514f0bSApple OSS Distributions 44*d4514f0bSApple OSS Distributionsdef cov_start(): 45*d4514f0bSApple OSS Distributions """ Start coverage tracking. """ 46*d4514f0bSApple OSS Distributions 47*d4514f0bSApple OSS Distributions if not COVERAGE_ENABLED: 48*d4514f0bSApple OSS Distributions return None 49*d4514f0bSApple OSS Distributions 50*d4514f0bSApple OSS Distributions cov = coverage.Coverage(auto_data=True) 51*d4514f0bSApple OSS Distributions cov.load() 52*d4514f0bSApple OSS Distributions cov.start() 53*d4514f0bSApple OSS Distributions 54*d4514f0bSApple OSS Distributions return cov 55*d4514f0bSApple OSS Distributions 56*d4514f0bSApple OSS Distributionsdef cov_stop(cov): 57*d4514f0bSApple OSS Distributions """ Stop coverage tracking. """ 58*d4514f0bSApple OSS Distributions 59*d4514f0bSApple OSS Distributions if not cov: 60*d4514f0bSApple OSS Distributions return 61*d4514f0bSApple OSS Distributions 62*d4514f0bSApple OSS Distributions cov.stop() 63*d4514f0bSApple OSS Distributions cov.save() 64*d4514f0bSApple OSS Distributions 65*d4514f0bSApple OSS Distributionsdef cov_html(directory): 66*d4514f0bSApple OSS Distributions """ Produce HRML report from current coverage data. """ 67*d4514f0bSApple OSS Distributions 68*d4514f0bSApple OSS Distributions if not COVERAGE_ENABLED: 69*d4514f0bSApple OSS Distributions print("Coverage module not installed.") 70*d4514f0bSApple OSS Distributions return 71*d4514f0bSApple OSS Distributions 72*d4514f0bSApple OSS Distributions cov = coverage.Coverage(auto_data=True) 73*d4514f0bSApple OSS Distributions cov.load() 74*d4514f0bSApple OSS Distributions cov.html_report(directory=directory, 75*d4514f0bSApple OSS Distributions include=[ 76*d4514f0bSApple OSS Distributions '*/lldbmacros/*' 77*d4514f0bSApple OSS Distributions ], 78*d4514f0bSApple OSS Distributions omit=[ 79*d4514f0bSApple OSS Distributions '*/lldbmacros/tests/*' 80*d4514f0bSApple OSS Distributions ]) 81*d4514f0bSApple OSS Distributions 82*d4514f0bSApple OSS Distributions 83*d4514f0bSApple OSS Distributionsclass CoverageContext(contextlib.AbstractContextManager): 84*d4514f0bSApple OSS Distributions """ Coverage tracking context manager. """ 85*d4514f0bSApple OSS Distributions 86*d4514f0bSApple OSS Distributions def __init__(self): 87*d4514f0bSApple OSS Distributions self._cov = None 88*d4514f0bSApple OSS Distributions 89*d4514f0bSApple OSS Distributions def __enter__(self): 90*d4514f0bSApple OSS Distributions self._cov = cov_start() 91*d4514f0bSApple OSS Distributions return super().__enter__() 92*d4514f0bSApple OSS Distributions 93*d4514f0bSApple OSS Distributions def __exit__(self, __exc_type, __exc_value, __traceback): 94*d4514f0bSApple OSS Distributions cov_stop(self._cov) 95*d4514f0bSApple OSS Distributions return super().__exit__(__exc_type, __exc_value, __traceback) 96