Skip to content

Instantly share code, notes, and snippets.

@kingbuzzman
Last active August 6, 2024 22:38
Show Gist options
  • Save kingbuzzman/7a4e9abb695f2289b2c223744e5d8c9d to your computer and use it in GitHub Desktop.
Save kingbuzzman/7a4e9abb695f2289b2c223744e5d8c9d to your computer and use it in GitHub Desktop.

Random scripts

Get all the lines missing from coverage

git diff --unified=0 origin/master | awk '
  /^diff --git a\// { file=$3; sub(/^a\//, "", file); next }
  /^@@ / {
    split($2, a, ",");
    old_start_line=a[1]; sub(/^-/, "", old_start_line);
    old_num_lines=a[2];
    if (old_num_lines == "") old_num_lines = 1;

    split($3, b, ",");
    new_start_line=b[1]; sub(/^\+/, "", new_start_line);
    new_num_lines=b[2];
    if (new_num_lines == "") new_num_lines = 1;

    for (i = 0; i < old_num_lines; i++) {
      line = file ":" old_start_line + i
      if (!(line in seen)) {
        print line
        seen[line] = 1
      }
    }

    for (i = 0; i < new_num_lines; i++) {
      line = file ":" new_start_line + i
      if (!(line in seen)) {
        print line
        seen[line] = 1
      }
    }
  }
' > /tmp/diff_lines.txt

runtestsp $(cat /tmp/diff_lines.txt | cut -d / -f 1 | grep -v : | uniq | tee >(xargs -n 1 printf ' --cov %s') | xargs)

coverage report -m --format text | while IFS= read -r line; do
    # Extract the file path and the missing line ranges using regular expressions
    if [[ "$line" =~ ^([^\ ]+)[[:space:]]+[0-9]+[[:space:]]+[0-9]+[[:space:]]+[0-9]+[[:space:]]+[0-9]+[[:space:]]+[0-9]+%[[:space:]]+(.+) ]]; then
        filepath="${BASH_REMATCH[1]}"
        missing_ranges="${BASH_REMATCH[2]}"
        
        # Split the missing ranges by comma and process each range
        IFS=',' read -ra ranges <<< "$missing_ranges"
        for range in "${ranges[@]}"; do
            # Handle branch coverage notation (e.g., 1001->1005 or 4097->exit)
            if [[ "$range" == *"->"* ]]; then
                target=$(echo "$range" | cut -d'>' -f2)
                # Skip ranges that target "exit"
                if [[ "$target" == "exit" ]]; then
                    continue
                fi
                # If it's not "exit", just extract the target line number
                range=$(echo "$target" | xargs)
            fi

            # Trim any leading or trailing whitespace from the range
            range=$(echo "$range" | xargs)

            # Check if it's a range (e.g., 40-53) or a single line (e.g., 98)
            if [[ "$range" == *-* ]]; then
                start=$(echo "$range" | cut -d'-' -f1)
                end=$(echo "$range" | cut -d'-' -f2)
                
                # Output each line in the range
                for ((i=start; i<=end; i++)); do
                    echo "$filepath:$i"
                done
            else
                # Output the single line
                echo "$filepath:$range"
            fi
        done
    fi
done > /tmp/coverage_lines.txt

sort /tmp/diff_lines.txt -o /tmp/diff_lines.txt
sort /tmp/coverage_lines.txt -o /tmp/coverage_lines.txt
comm -12 /tmp/diff_lines.txt /tmp/coverage_lines.txt > /tmp/common_lines.txt
if [ ! -s /tmp/common_lines.txt ]; then
  echo "All code was covered."
else
  cat /tmp/common_lines.txt
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment