Browse Source

feat: Adding more scripts

Matthias Ladkau 1 year ago
parent
commit
6309991931
2 changed files with 82 additions and 0 deletions
  1. 6 0
      bin/check-go
  2. 76 0
      bin/cover-go

+ 6 - 0
bin/check-go

@@ -0,0 +1,6 @@
+#!/bin/bash
+echo Running go vet ...
+go vet ./...
+echo Running golint ...
+golint -set_exit_status ./...
+echo Done.

+ 76 - 0
bin/cover-go

@@ -0,0 +1,76 @@
+#!/bin/bash
+# Generate test coverage statistics for all Go packages.
+
+workdir=.cover
+
+mkdir -p $workdir
+
+profile="$workdir/cover.out"
+mode=count
+args=$*
+
+# Used for saving results when specifying --save-html
+
+if [[ $args == *--save-html* ]]; then
+    for lastarg; do true; done
+    outfile=$lastarg
+    echo "Outfile: $outfile"
+fi
+
+generate_cover_data() {
+    rm -rf "$workdir"
+    mkdir "$workdir"
+
+    for pkg in "$@"; do
+        f="$workdir/$(echo $pkg | tr / -).cover"
+        
+        go test -covermode="$mode" -coverprofile="$f" "$pkg"
+        
+        ret=$?
+        if [ $ret -ne 0 ]; then
+            if [[ $args == *--save-html* ]]; then
+                echo '<svg width="88" height="20" xmlns="http://www.w3.org/2000/svg"><g shape-rendering="crispEdges"><path fill="#555" d="M0 0h41v20H0z"/><path fill="#fc1" d="M41 0h40v20H41z"/></g><g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11"><text x="20.5" y="14">tests</text><text x="60" y="14">fail</text></g></svg>' > $outfile.svg
+            fi
+
+            exit $ret
+        fi
+    done
+
+    echo "mode: $mode" >"$profile"
+    grep -h -v "^mode:" "$workdir"/*.cover >>"$profile"
+}
+
+show_cover_report() {
+    go tool cover -${1}="$profile" -o $profile.${1}
+    go tool cover -${1}="$profile"
+}
+
+save_cover_report() {
+
+    coverage=`go tool cover -func=.cover/cover.out | tee $outfile.txt | grep -En "^total:" | grep -o '[0-9]*.[0-9]*%$'`
+
+    echo $?
+
+    go tool cover -${1}="$profile" -o $outfile.html
+
+    if [ "$coverage" == "100.0%" ]; then
+        echo '<svg width="110" height="20" xmlns="http://www.w3.org/2000/svg"><g shape-rendering="crispEdges"><path fill="#555" d="M0 0h61v20H0z"/><path fill="#4c1" d="M61 0h50v20H61z"/></g><g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11"><text x="30.5" y="14">coverage</text><text x="85" y="14">'$coverage'</text></g></svg>' > $outfile.svg
+    else
+        echo '<svg width="110" height="20" xmlns="http://www.w3.org/2000/svg"><g shape-rendering="crispEdges"><path fill="#555" d="M0 0h61v20H0z"/><path fill="#fc1" d="M61 0h50v20H61z"/></g><g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11"><text x="30.5" y="14">coverage</text><text x="85" y="14">'$coverage'</text></g></svg>' > $outfile.svg
+    fi
+}
+
+generate_cover_data $(go list ./...)
+
+show_cover_report func
+case "$1" in
+"")
+    ;;
+--html)
+    show_cover_report html ;;
+--save-html)
+    save_cover_report html ;;
+*)
+    echo >&2 "error: invalid option: $1"; exit 1 ;;
+esac
+