From 51be32fa125c77cd2bc20a0bd7f6c5ab19ee6b26 Mon Sep 17 00:00:00 2001
From: Kslr <hi@kslr.org>
Date: Sat, 20 Jun 2020 03:07:58 +0800
Subject: [PATCH] actions add coverage

---
 .github/workflows/coverage.yml | 37 +++++++++++++++++++++++++++++++
 testing/coverage/coverall2     | 40 ++++++++++++++++++++++++++++++++++
 2 files changed, 77 insertions(+)
 create mode 100644 .github/workflows/coverage.yml
 create mode 100755 testing/coverage/coverall2

diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml
new file mode 100644
index 00000000..9db22245
--- /dev/null
+++ b/.github/workflows/coverage.yml
@@ -0,0 +1,37 @@
+name: Coverage
+
+on:
+  push:
+    branches: [ master ]
+  pull_request:
+    branches: [ master ]
+
+jobs:
+
+  build:
+    name: Coverage
+    runs-on: ubuntu-latest
+
+    steps:
+
+      - name: Set up Go 1.x
+        uses: actions/setup-go@v2
+        with:
+          go-version: ^1.14
+        id: go
+
+      - name: Check out code into the Go module directory
+        uses: actions/checkout@v2
+
+      - name: Get dependencies
+        run: |
+          go get -v -t -d ./...
+
+      - name: Run Coverage
+        run: ./testing/coverage/coverall2
+
+      - name: Upload coverage to Codecov
+        uses: codecov/codecov-action@v1
+        with:
+          file: ./coverage.txt
+          fail_ci_if_error: true
diff --git a/testing/coverage/coverall2 b/testing/coverage/coverall2
new file mode 100755
index 00000000..57cffc4c
--- /dev/null
+++ b/testing/coverage/coverall2
@@ -0,0 +1,40 @@
+#!/bin/bash
+
+COVERAGE_FILE=${PWD}/coverage.txt
+COV_SORTED=${PWD}/coverallsorted.out
+
+touch "$COVERAGE_FILE"
+
+function test_package {
+  DIR=".$1"
+  DEP=$(go list -f '{{ join .Deps "\n" }}' "$DIR" | grep v2ray | tr '\n' ',')
+  DEP=${DEP}$DIR
+  RND_NAME=$(openssl rand -hex 16)
+  COV_PROFILE=${RND_NAME}.out
+  go test -coverprofile="$COV_PROFILE" -coverpkg="$DEP" "$DIR" || return
+}
+
+TEST_FILES=(./*_test.go)
+if [ -f "${TEST_FILES[0]}" ]; then
+  test_package ""
+fi
+
+while IFS= read -r -d '' DIR
+do
+  TEST_FILES=("$DIR"/*_test.go)
+  if [ -f "${TEST_FILES[0]}" ]; then
+    test_package "/$DIR"
+  fi
+done <   <(find ./* -type d ! -path "*.git*" ! -path "*vendor*" ! -path "*external*" -print0)
+
+# merge out
+while IFS= read -r -d '' OUT_FILE
+do
+  echo "Merging file ${OUT_FILE}"
+  < "${OUT_FILE}" grep -v "mode: set" >> "$COVERAGE_FILE"
+done <   <(find ./* -name "*.out" -print0)
+
+< "$COVERAGE_FILE" sort -t: -k1 | grep -vw "testing" | grep -v ".pb.go" | grep -vw "vendor" | grep -vw "external" > "$COV_SORTED"
+echo "mode: set" | cat - "${COV_SORTED}" > "${COVERAGE_FILE}"
+
+bash <(curl -s https://codecov.io/bash) || echo 'Codecov failed to upload'
\ No newline at end of file