Skip to content

Instantly share code, notes, and snippets.

@Alexander-Ignition
Created December 30, 2020 11:57
Show Gist options
  • Save Alexander-Ignition/d4bfd4183c5a823726de6c1dc7ccf1e8 to your computer and use it in GitHub Desktop.
Save Alexander-Ignition/d4bfd4183c5a823726de6c1dc7ccf1e8 to your computer and use it in GitHub Desktop.
swift code coverage
import json
import subprocess
from pathlib import Path
def shell(cmd, stdout=None) -> str:
return subprocess.run(cmd.split(' '), stdout=stdout, check=True, text=True).stdout
def get_package() -> dict:
return json.loads(shell('swift package describe --type json', stdout=subprocess.PIPE))
def get_codecov_path(configuration: str) -> Path:
# .build/x86_64-apple-macosx/{configuration}/codecov/XCAudit.json
return Path(shell(f'swift test --show-codecov-path --configuration {configuration}', stdout=subprocess.PIPE)).parent
def code_coverage(configuration: str):
package_name = get_package()['name']
codecov_path = get_codecov_path(configuration=configuration)
# .build/x86_64-apple-macosx/{configuration}/{package_name}PackageTests.xctest/Contents/MacOS/{package_name}PackageTests
binary_path = codecov_path.parent / f'{package_name}PackageTests.xctest/Contents/MacOS/{package_name}PackageTests'
# .build/x86_64-apple-macosx/{configuration}/codecov/default.profdata
prof_data_path = codecov_path / 'default.profdata'
shell(f'xcrun llvm-cov report {binary_path} --format=text -instr-profile={prof_data_path} -ignore-filename-regex=Tests')
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description='Code coverage for swift packages')
parser.add_argument('-c', '--configuration', type=str, default='debug', help='Swift package build configuration (debug|release) [default: debug]')
args = parser.parse_args()
code_coverage(**vars(args))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment