diff --git a/nix/coverage-html.nix b/nix/coverage-html.nix index 54b7ca0..b539172 100644 --- a/nix/coverage-html.nix +++ b/nix/coverage-html.nix @@ -6,6 +6,8 @@ cmocka, libllvm, pkg-config, + htmlq, + anybadge, }: stdenv.mkDerivation { pname = "where-are-my-friends-coverage-html"; @@ -25,8 +27,10 @@ stdenv.mkDerivation { }; nativeBuildInputs = [ - libllvm pkg-config + libllvm + htmlq + anybadge ]; buildInputs = [ cmocka ]; @@ -55,6 +59,28 @@ stdenv.mkDerivation { llvm-cov show "''${LLVM_COV_ARGS[@]}" + TOTAL_COVERAGE="$( + htmlq 'td > pre' Totals<\/pre>$/d' \ + | head -n 1 \ + | sed -e 's/
[[:space:]]*//' -e 's/%.*$/%/' \
+    )"
+
+    if [[ "$TOTAL_COVERAGE" == *% ]]; then
+      COVERAGE_VALUE="''${TOTAL_COVERAGE%\%}"
+      if [[ "$COVERAGE_VALUE" -ge 80 ]]; then
+        COLOR="green"
+      elif [[ "$COVERAGE_VALUE" -ge 50 ]]; then
+        COLOR="yellow"
+      else
+        COLOR="red"
+      fi
+    else
+      COLOR="lightgrey"
+    fi
+
+    anybadge --label "coverage" --value "$TOTAL_COVERAGE" --color "$COLOR" > coverage-html/coverage.svg
+
     runHook postBuild
   '';