Last active
June 8, 2023 01:32
-
-
Save shu-yusa/4b42f944d5dc11b4a2b7f00a2cea6bec to your computer and use it in GitHub Desktop.
Covert output from `phpunit --coverage-text` into the table form in Markdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This script is used to convert the output from "phpunit --coverage-text" | |
# into a markdown formatted table. It parses the coverage data, | |
# then generates a table with class names, statement counts, misses, and coverages. | |
# The output is designed to be easily readable and suitable for inclusion in | |
# markdown-based documents or reports. | |
# Ignore output until reaching coverage report | |
while IFS= read -r line | |
do | |
if [[ $line == *"Summary"* ]]; then | |
read _ # skip overall class coverage | |
read _ # skip overall method coverage | |
break | |
fi | |
done | |
echo "| Class | Stmts | Miss | Cover |" | |
echo "|-------|-------|------|-------|" | |
# Calculate overall coverage | |
read overall_coverage | |
total_stmts=$(echo $overall_coverage | awk -F'[()]' '{print $2}' | awk -F/ '{print $2}') | |
total_miss=$(echo $overall_coverage | awk -F'[()]' '{print $2}' | awk -F/ '{print $2 - $1}') | |
total_coverage=$(echo $overall_coverage | awk -F'[:(]' '{print $2}' | tr -d ' ') | |
echo "| **TOTAL** | **$total_stmts** | **$total_miss** | **$total_coverage** |" | |
while IFS= read -r line | |
do | |
# If line starts with a letter (assumed to be a class name) | |
if [[ $line =~ ^[A-Za-z] ]]; then | |
class_name=$(echo $line | cut -d' ' -f1) | |
read coverage_info | |
stmts=$(echo $coverage_info | awk -F'[()]' '{print $4}' | awk -F/ '{print $2}') | |
miss=$(echo $coverage_info | awk -F'[()]' '{print $4}' | awk -F/ '{print $2 - $1}') | |
coverage=$(echo $coverage_info | awk -F'[:(]' '{print $4}' | tr -d ' ') | |
# Skip if coverage is empty or 100% | |
if [ -z "$coverage" ] || [ "$coverage" = "100.00%" ]; then | |
continue | |
fi | |
echo "| $class_name | $stmts | $miss | $coverage |" | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example:
With parsing coverage
In Markdown, it looks like