diff --git a/.github/workflows/static.yml b/.github/workflows/static.yml index a78ce1b9..0206dae5 100644 --- a/.github/workflows/static.yml +++ b/.github/workflows/static.yml @@ -37,12 +37,7 @@ jobs: with: go-version-file: ./go.mod - name: Generate coverage and badge - run: | - make test - mkdir -p ./ci/out/static - cp ./ci/out/coverage.html ./ci/out/static/coverage.html - percent=$(go tool cover -func ./ci/out/coverage.prof | tail -n1 | awk '{print $3}' | tr -d '%') - wget -O ./ci/out/static/coverage.svg "https://img.shields.io/badge/coverage-${percent}%25-success" + run: make static - name: Upload artifact uses: actions/upload-pages-artifact@v3 with: diff --git a/Makefile b/Makefile index a3e4a20d..5847ba91 100644 --- a/Makefile +++ b/Makefile @@ -13,6 +13,10 @@ lint: test: ./ci/test.sh +.PHONY: static +static: test + ./ci/static.sh + .PHONY: bench bench: - ./ci/bench.sh \ No newline at end of file + ./ci/bench.sh diff --git a/ci/bench.sh b/ci/bench.sh index 30c06986..dc7d2f32 100755 --- a/ci/bench.sh +++ b/ci/bench.sh @@ -5,16 +5,15 @@ cd -- "$(dirname "$0")/.." go test --run=^$ --bench=. --benchmem "$@" ./... # For profiling add: --memprofile ci/out/prof.mem --cpuprofile ci/out/prof.cpu -o ci/out/websocket.test ( - cd ./internal/thirdparty - go test --run=^$ --bench=. --benchmem "$@" . + cd ./internal/thirdparty + go test --run=^$ --bench=. --benchmem "$@" . - GOARCH=arm64 go test -c -o ../../ci/out/thirdparty-arm64.test "$@" . - if [ "$#" -eq 0 ]; then - if [ "${CI-}" ]; then - sudo apt-get update - sudo apt-get install -y qemu-user-static - ln -s /usr/bin/qemu-aarch64-static /usr/local/bin/qemu-aarch64 - fi - qemu-aarch64 ../../ci/out/thirdparty-arm64.test --test.run=^$ --test.bench=Benchmark_mask --test.benchmem - fi + GOARCH=arm64 go test -c -o ../../ci/out/thirdparty-arm64.test "$@" . + if [ "$#" -eq 0 ]; then + if [ "${CI-}" ]; then + sudo apt-get update + sudo apt-get install -y qemu-user + fi + qemu-aarch64 ../../ci/out/thirdparty-arm64.test --test.run=^$ --test.bench=Benchmark_mask --test.benchmem + fi ) diff --git a/ci/static.sh b/ci/static.sh new file mode 100755 index 00000000..bbb81fa7 --- /dev/null +++ b/ci/static.sh @@ -0,0 +1,20 @@ +#!/usr/bin/env bash +set -euo pipefail + +cd -- "$(dirname "$0")/.." + +if [[ ! -f ./ci/out/profile.txt ]]; then + echo "No coverage profile found, run 'make test' first" + exit 1 +fi + +if [[ ! -f ./ci/out/coverage.html ]]; then + echo "No coverage report found, run 'make test' first" + exit 1 +fi + +rm -rf ./ci/out/static +mkdir -p ./ci/out/static +cp ./ci/out/coverage.html ./ci/out/static/coverage.html +percent=$(go tool cover -func ./ci/out/profile.txt | tail -n1 | awk '{print $3}' | tr -d '%') +curl -fsSL -o ./ci/out/static/coverage.svg "https://img.shields.io/badge/coverage-${percent}%25-success" diff --git a/ci/test.sh b/ci/test.sh index ce109778..c1c09e37 100755 --- a/ci/test.sh +++ b/ci/test.sh @@ -1,35 +1,51 @@ -#!/bin/sh -set -eu +#!/usr/bin/env bash +set -euo pipefail + cd -- "$(dirname "$0")/.." -( - cd ./internal/examples - go test "$@" ./... -) -( - cd ./internal/thirdparty - go test "$@" ./... -) - -( - GOARCH=arm64 go test -c -o ./ci/out/websocket-arm64.test "$@" . - if [ "$#" -eq 0 ]; then +go install github.com/agnivade/wasmbrowsertest@8be019f6c6dceae821467b4c589eb195c2b761ce + +echo "+++ Testing websocket library and generating coverage" +rm -rf ci/out/profile.txt ci/out/profile +coverdir="$(pwd)/ci/out/profile" +mkdir -p "$coverdir" + +coverpkgs=$(go list ./... | grep -v websocket/internal/test | paste -sd, -) + +echo "++++ Running main tests" +# -test.gocoverdir is a test binary flag, so it goes after -args. Each test +# binary runs in its package directory, so the path must be absolute. +go test -race --bench=. --timeout=1h -cover -covermode=atomic -coverpkg="$coverpkgs" "$@" ./... -args -test.gocoverdir="$coverdir" + +echo "+++ Testing examples" +pushd internal/examples +go test "$@" ./... +popd + +echo "+++ Testing thirdparty" +pushd internal/thirdparty +go test "$@" ./... +popd + +if [[ $# -eq 0 ]]; then + echo "+++ Running TestMask for arm64" + + run_test=(go test .) + if [[ $(go env GOARCH) != "arm64" ]]; then if [ "${CI-}" ]; then sudo apt-get update - sudo apt-get install -y qemu-user-static - ln -s /usr/bin/qemu-aarch64-static /usr/local/bin/qemu-aarch64 + sudo apt-get install -y qemu-user fi - qemu-aarch64 ./ci/out/websocket-arm64.test -test.run=TestMask + GOARCH=arm64 go test -c -o ./ci/out/websocket-arm64.test "$@" . + run_test=(qemu-aarch64 ./ci/out/websocket-arm64.test) fi -) - -go install github.com/agnivade/wasmbrowsertest@8be019f6c6dceae821467b4c589eb195c2b761ce -go test --race --bench=. --timeout=1h --covermode=atomic --coverprofile=ci/out/coverage.prof --coverpkg=./... "$@" ./... -sed -i.bak '/stringer\.go/d' ci/out/coverage.prof -sed -i.bak '/github\.com\/coder\/websocket\/internal\/test\//d' ci/out/coverage.prof -sed -i.bak '/examples/d' ci/out/coverage.prof + "${run_test[@]}" -test.run=TestMask +fi -# Last line is the total coverage. -go tool cover -func ci/out/coverage.prof | tail -n1 +echo "++++ Generating test coverage data" +go tool covdata textfmt -i="$coverdir" -o ci/out/profile.txt +sed -i.bak '/stringer\.go/d' ci/out/profile.txt +go tool cover -func ci/out/profile.txt | tail -n1 # Last line is the total coverage. +go tool cover -html=ci/out/profile.txt -o=ci/out/coverage.html -go tool cover -html=ci/out/coverage.prof -o=ci/out/coverage.html +echo "+++ Done"