From 002abf1ecbb0ba78037c104fe9dda81beb7bf0c5 Mon Sep 17 00:00:00 2001 From: Anmol Sethi <hi@nhooyr.io> Date: Tue, 23 Jul 2019 20:26:42 -0400 Subject: [PATCH] Switch to CircleCI Closes #107 --- .circleci/config.yml | 64 ++++++++++++++++++++++++++++++++++++++++++ .github/main.workflow | 21 -------------- ci/.codecov.yml | 1 + ci/bench.sh | 16 +++++++++++ ci/bench/Dockerfile | 10 ------- ci/bench/entrypoint.sh | 15 ---------- ci/fmt.sh | 37 ++++++++++++++++++++++++ ci/fmt/Dockerfile | 10 ------- ci/fmt/entrypoint.sh | 36 ------------------------ ci/lib.sh | 16 +++++------ ci/lint.sh | 15 ++++++++++ ci/lint/Dockerfile | 12 -------- ci/lint/entrypoint.sh | 11 -------- ci/run.sh | 64 +++++------------------------------------- ci/test.sh | 30 ++++++++++++++++++++ ci/test/Dockerfile | 14 --------- ci/test/entrypoint.sh | 24 ---------------- docs/CONTRIBUTING.md | 9 +++++- 18 files changed, 185 insertions(+), 220 deletions(-) create mode 100644 .circleci/config.yml delete mode 100644 .github/main.workflow create mode 100755 ci/bench.sh delete mode 100644 ci/bench/Dockerfile delete mode 100755 ci/bench/entrypoint.sh create mode 100755 ci/fmt.sh delete mode 100644 ci/fmt/Dockerfile delete mode 100755 ci/fmt/entrypoint.sh create mode 100755 ci/lint.sh delete mode 100644 ci/lint/Dockerfile delete mode 100755 ci/lint/entrypoint.sh create mode 100755 ci/test.sh delete mode 100644 ci/test/Dockerfile delete mode 100755 ci/test/entrypoint.sh diff --git a/.circleci/config.yml b/.circleci/config.yml new file mode 100644 index 0000000..b34f651 --- /dev/null +++ b/.circleci/config.yml @@ -0,0 +1,64 @@ +version: 2 +jobs: + fmt: + docker: + - image: golang:1 + steps: + - checkout + - restore_cache: + keys: + - go-{{ checksum "go.sum" }} + # Fallback to using the latest cache if no exact match is found. + - go- + - run: ./ci/fmt.sh + - save_cache: + paths: + - /go + - /root/.cache/go-build + key: go-{{ checksum "go.sum" }} + + lint: + docker: + - image: golang:1 + steps: + - checkout + - restore_cache: + keys: + - go-{{ checksum "go.sum" }} + # Fallback to using the latest cache if no exact match is found. + - go- + - run: ./ci/lint.sh + - save_cache: + paths: + - /go + - /root/.cache/go-build + key: go-{{ checksum "go.sum" }} + + test: + docker: + - image: golang:1 + steps: + - checkout + - restore_cache: + keys: + - go-{{ checksum "go.sum" }} + # Fallback to using the latest cache if no exact match is found. + - go- + - run: ./ci/test.sh + - save_cache: + paths: + - /go + - /root/.cache/go-build + key: go-{{ checksum "go.sum" }} + +workflows: + version: 2 + fmt: + jobs: + - fmt + lint: + jobs: + - lint + test: + jobs: + - test diff --git a/.github/main.workflow b/.github/main.workflow deleted file mode 100644 index d56d42e..0000000 --- a/.github/main.workflow +++ /dev/null @@ -1,21 +0,0 @@ -workflow "main" { - on = "push" - resolves = ["fmt", "lint", "test", "bench"] -} - -action "lint" { - uses = "./ci/lint" -} - -action "fmt" { - uses = "./ci/fmt" -} - -action "test" { - uses = "./ci/test" - secrets = ["CODECOV_TOKEN"] -} - -action "bench" { - uses = "./ci/bench" -} diff --git a/ci/.codecov.yml b/ci/.codecov.yml index 7d614ef..f7eec9f 100644 --- a/ci/.codecov.yml +++ b/ci/.codecov.yml @@ -1,3 +1,4 @@ +comment: off coverage: status: # Prevent small changes in coverage from failing CI. diff --git a/ci/bench.sh b/ci/bench.sh new file mode 100755 index 0000000..a3d81b2 --- /dev/null +++ b/ci/bench.sh @@ -0,0 +1,16 @@ +#!/usr/bin/env bash + +set -euo pipefail +cd "$(dirname "${0}")" +source ./lib.sh + +go test --vet=off --run=^$ -bench=. -o=ci/out/websocket.test \ + -cpuprofile=ci/out/cpu.prof \ + -memprofile=ci/out/mem.prof \ + -blockprofile=ci/out/block.prof \ + -mutexprofile=ci/out/mutex.prof \ + . + +echo +echo "Profiles are in ./ci/out/*.prof +Keep in mind that every profiler Go provides is enabled so that may skew the benchmarks." diff --git a/ci/bench/Dockerfile b/ci/bench/Dockerfile deleted file mode 100644 index a2b6c73..0000000 --- a/ci/bench/Dockerfile +++ /dev/null @@ -1,10 +0,0 @@ -FROM golang:1.12 - -LABEL "com.github.actions.name"="bench" -LABEL "com.github.actions.description"="" -LABEL "com.github.actions.icon"="code" -LABEL "com.github.actions.color"="red" - -COPY entrypoint.sh /entrypoint.sh - -CMD ["/entrypoint.sh"] diff --git a/ci/bench/entrypoint.sh b/ci/bench/entrypoint.sh deleted file mode 100755 index a8350c9..0000000 --- a/ci/bench/entrypoint.sh +++ /dev/null @@ -1,15 +0,0 @@ -#!/usr/bin/env bash - -source ci/lib.sh || exit 1 - -go test --vet=off --run=^$ -bench=. -o=ci/out/websocket.test \ - -cpuprofile=ci/out/cpu.prof \ - -memprofile=ci/out/mem.prof \ - -blockprofile=ci/out/block.prof \ - -mutexprofile=ci/out/mutex.prof \ - . - -set +x -echo -echo "profiles are in ./ci/out/*.prof -keep in mind that every profiler Go provides is enabled so that may skew the benchmarks" diff --git a/ci/fmt.sh b/ci/fmt.sh new file mode 100755 index 0000000..52ef3fd --- /dev/null +++ b/ci/fmt.sh @@ -0,0 +1,37 @@ +#!/usr/bin/env bash + +set -euo pipefail +cd "$(dirname "${0}")" +source ./lib.sh + +unstaged_files() { + git ls-files --other --modified --exclude-standard +} + +gen() { + # Unfortunately, this is the only way to ensure go.mod and go.sum are correct. + # See https://github.com/golang/go/issues/27005 + go list ./... > /dev/null + go mod tidy + + go generate ./... +} + +fmt() { + gofmt -w -s . + go run go.coder.com/go-tools/cmd/goimports -w "-local=$(go list -m)" . + go run mvdan.cc/sh/cmd/shfmt -i 2 -w -s -sr . +} + +gen +fmt + +if [[ $CI && $(unstaged_files) != "" ]]; then + echo + echo "Files either need generation or are formatted incorrectly." + echo "Please run:" + echo "./ci/fmt.sh" + echo + git status + exit 1 +fi diff --git a/ci/fmt/Dockerfile b/ci/fmt/Dockerfile deleted file mode 100644 index f829f51..0000000 --- a/ci/fmt/Dockerfile +++ /dev/null @@ -1,10 +0,0 @@ -FROM golang:1.12 - -LABEL "com.github.actions.name"="fmt" -LABEL "com.github.actions.description"="" -LABEL "com.github.actions.icon"="code" -LABEL "com.github.actions.color"="blue" - -COPY entrypoint.sh /entrypoint.sh - -CMD ["/entrypoint.sh"] diff --git a/ci/fmt/entrypoint.sh b/ci/fmt/entrypoint.sh deleted file mode 100755 index f4749ec..0000000 --- a/ci/fmt/entrypoint.sh +++ /dev/null @@ -1,36 +0,0 @@ -#!/usr/bin/env bash - -source ci/lib.sh || exit 1 - -unstaged_files() { - git ls-files --other --modified --exclude-standard -} - -gen() { - # Unfortunately, this is the only way to ensure go.mod and go.sum are correct. - # See https://github.com/golang/go/issues/27005 - go list ./... > /dev/null - go mod tidy - - go generate ./... -} - -fmt() { - gofmt -w -s . - go run go.coder.com/go-tools/cmd/goimports -w "-local=$(go list -m)" . - go run mvdan.cc/sh/cmd/shfmt -w -s -sr . -} - -gen -fmt - -if [[ $CI && $(unstaged_files) != "" ]]; then - set +x - echo - echo "files either need generation or are formatted incorrectly" - echo "please run:" - echo "./ci/run.sh" - echo - git status - exit 1 -fi diff --git a/ci/lib.sh b/ci/lib.sh index 0bc093c..590e790 100644 --- a/ci/lib.sh +++ b/ci/lib.sh @@ -1,14 +1,12 @@ #!/usr/bin/env bash -set -euxo pipefail || exit 1 - -export GO111MODULE=on -export PAGER=cat - -# shellcheck disable=SC2034 -# CI is used by the scripts that source this file. -export CI=${GITHUB_ACTION-} +set -euo pipefail +# Ensures $CI can be used if it's set or not. +export CI=${CI:-} if [[ $CI ]]; then - export GOFLAGS=-mod=readonly + export GOFLAGS=-mod=readonly + export DEBIAN_FRONTEND=noninteractive fi + +cd "$(git rev-parse --show-toplevel)" diff --git a/ci/lint.sh b/ci/lint.sh new file mode 100755 index 0000000..6565545 --- /dev/null +++ b/ci/lint.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash + +set -euo pipefail +cd "$(dirname "${0}")" +source ./lib.sh + +if [[ $CI ]]; then + apt-get update -qq + apt-get install -qq shellcheck > /dev/null +fi + +# shellcheck disable=SC2046 +shellcheck -e SC1091 -x $(git ls-files "*.sh") +go vet ./... +go run golang.org/x/lint/golint -set_exit_status ./... diff --git a/ci/lint/Dockerfile b/ci/lint/Dockerfile deleted file mode 100644 index 3fe06f0..0000000 --- a/ci/lint/Dockerfile +++ /dev/null @@ -1,12 +0,0 @@ -FROM golang:1.12 - -LABEL "com.github.actions.name"="lint" -LABEL "com.github.actions.description"="" -LABEL "com.github.actions.icon"="code" -LABEL "com.github.actions.color"="purple" - -RUN apt-get update && apt-get install -y shellcheck - -COPY entrypoint.sh /entrypoint.sh - -CMD ["/entrypoint.sh"] diff --git a/ci/lint/entrypoint.sh b/ci/lint/entrypoint.sh deleted file mode 100755 index 62f7402..0000000 --- a/ci/lint/entrypoint.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/usr/bin/env bash - -source ci/lib.sh || exit 1 - -( - shopt -s globstar nullglob dotglob - shellcheck ./**/*.sh -) - -go vet ./... -go run golang.org/x/lint/golint -set_exit_status ./... diff --git a/ci/run.sh b/ci/run.sh index 9112c83..53f0427 100755 --- a/ci/run.sh +++ b/ci/run.sh @@ -1,61 +1,11 @@ #!/usr/bin/env bash -# This script is for local testing. See .github for CI. +# This script is for local testing. See .circleci for CI. -cd "$(dirname "${0}")/.." || exit 1 -source ci/lib.sh || exit 1 +set -euo pipefail +cd "$(dirname "${0}")" +source ./lib.sh -function docker_run() { - local DIR="$1" - local IMAGE - IMAGE="$(docker build -q "$DIR")" - docker run \ - -it \ - -v "${PWD}:/repo" \ - -v "$(go env GOPATH):/go" \ - -v "$(go env GOCACHE):/root/.cache/go-build" \ - -w /repo \ - "${IMAGE}" -} - -function help() { - set +x - echo - echo "$0 [-h] <step>" - cat << EOF - -If you do not pass in an explicit step, all steps will be ran in order. -Pass "analyze" as the step to be put into an interactive container to analyze -profiles. -EOF - exit 1 -} - -# Use this to analyze benchmark profiles. -if [[ ${1-} == "analyze" ]]; then - docker run \ - -it \ - -v "${PWD}:/repo" \ - -v "$(go env GOPATH):/go" \ - -v "$(go env GOCACHE):/root/.cache/go-build" \ - -w /repo \ - golang:1.12 -fi - -if [[ ${1-} == "-h" || ${1-} == "--help" || ${1-} == "help" ]]; then - help -fi - -if [[ $# -gt 0 ]]; then - if [[ ! -d "ci/$*" ]]; then - help - fi - - docker_run "ci/$*" - exit 0 -fi - -docker_run ci/fmt -docker_run ci/lint -docker_run ci/test -docker_run ci/bench +./fmt.sh +./lint.sh +./test.sh diff --git a/ci/test.sh b/ci/test.sh new file mode 100755 index 0000000..9cf982a --- /dev/null +++ b/ci/test.sh @@ -0,0 +1,30 @@ +#!/usr/bin/env bash + +set -euo pipefail +cd "$(dirname "${0}")" +source ./lib.sh + +echo "This step includes benchmarks for race detection and coverage purposes +but the numbers will be misleading. please see the bench step or ./bench.sh for +more accurate numbers." +echo + +if [[ $CI ]]; then + apt-get update -qq + apt-get install -qq python-pip > /dev/null + # Need to add pip install directory to $PATH. + export PATH="/home/circleci/.local/bin:$PATH" + pip install -qqq autobahntestsuite +fi + +go test -race -coverprofile=ci/out/coverage.prof --vet=off -bench=. -coverpkg=./... ./... +go tool cover -func=ci/out/coverage.prof + +if [[ $CI ]]; then + bash <(curl -s https://codecov.io/bash) -f ci/out/coverage.prof +else + go tool cover -html=ci/out/coverage.prof -o=ci/out/coverage.html + + echo + echo "Please open ci/out/coverage.html to see detailed test coverage stats." +fi diff --git a/ci/test/Dockerfile b/ci/test/Dockerfile deleted file mode 100644 index a2fe695..0000000 --- a/ci/test/Dockerfile +++ /dev/null @@ -1,14 +0,0 @@ -FROM golang:1.12 - -LABEL "com.github.actions.name"="test" -LABEL "com.github.actions.description"="" -LABEL "com.github.actions.icon"="code" -LABEL "com.github.actions.color"="green" - -RUN apt-get update && \ - apt-get install -y python-pip && \ - pip install autobahntestsuite - -COPY entrypoint.sh /entrypoint.sh - -CMD ["/entrypoint.sh"] diff --git a/ci/test/entrypoint.sh b/ci/test/entrypoint.sh deleted file mode 100755 index 81f504a..0000000 --- a/ci/test/entrypoint.sh +++ /dev/null @@ -1,24 +0,0 @@ -#!/usr/bin/env bash - -source ci/lib.sh || exit 1 - -set +x -echo -echo "this step includes benchmarks for race detection and coverage purposes -but the numbers will be misleading. please see the bench step for more -accurate numbers" -echo -set -x - -go test -race -coverprofile=ci/out/coverage.prof --vet=off -bench=. -coverpkg=./... ./... -go tool cover -func=ci/out/coverage.prof - -if [[ $CI ]]; then - bash <(curl -s https://codecov.io/bash) -f ci/out/coverage.prof -else - go tool cover -html=ci/out/coverage.prof -o=ci/out/coverage.html - - set +x - echo - echo "please open ci/out/coverage.html to see detailed test coverage stats" -fi diff --git a/docs/CONTRIBUTING.md b/docs/CONTRIBUTING.md index 7214915..ade2013 100644 --- a/docs/CONTRIBUTING.md +++ b/docs/CONTRIBUTING.md @@ -18,4 +18,11 @@ Be sure to link to an existing issue if one exists. In general, try creating an before making a PR to get some discussion going and to make sure you do not spend time on a PR that may be rejected. -Run `ci/run.sh` to test your changes. You only need docker and bash to run the tests. +Run `ci/run.sh` to test your changes. You'll need [shellcheck](https://github.com/koalaman/shellcheck#installing), the [Autobahn Test suite pip package](https://github.com/crossbario/autobahn-testsuite) and Go. + +See [../ci/lint.sh](../ci/lint.sh) and [../ci/lint.sh](../ci/test.sh) for the +installation of shellcheck and the Autobahn test suite on Debian or Ubuntu. + +For Go, please refer to the [offical docs](https://golang.org/doc/install). + +You can benchmark the library with `./ci/benchmark.sh`. You only need Go to run that script. -- GitLab