Skip to content

Commit 7a64dc4

Browse files
committed
Improve CI and add prettier
1 parent 01721c9 commit 7a64dc4

File tree

11 files changed

+55
-51
lines changed

11 files changed

+55
-51
lines changed

.circleci/config.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ jobs:
4545
# Fallback to using the latest cache if no exact match is found.
4646
- go-
4747
- run: ./ci/test.sh
48+
- store_artifacts:
49+
path: out
4850
- save_cache:
4951
paths:
5052
- /go

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ http.HandlerFunc(func (w http.ResponseWriter, r *http.Request) {
4242

4343
ctx, cancel := context.WithTimeout(r.Context(), time.Second*10)
4444
defer cancel()
45-
45+
4646
var v interface{}
4747
err = wsjson.Read(ctx, c, &v)
4848
if err != nil {
4949
// ...
5050
}
51-
51+
5252
log.Printf("received: %v", v)
53-
53+
5454
c.Close(websocket.StatusNormalClosure, "")
5555
})
5656
```
@@ -93,7 +93,7 @@ c.Close(websocket.StatusNormalClosure, "")
9393
## Comparison
9494

9595
Before the comparison, I want to point out that both gorilla/websocket and gobwas/ws were
96-
extremely useful in implementing the WebSocket protocol correctly so *big thanks* to the
96+
extremely useful in implementing the WebSocket protocol correctly so _big thanks_ to the
9797
authors of both. In particular, I made sure to go through the issue tracker of gorilla/websocket
9898
to ensure I implemented details correctly and understood how people were using WebSockets in
9999
production.
@@ -111,7 +111,7 @@ Just compare the godoc of
111111
The API for nhooyr/websocket has been designed such that there is only one way to do things
112112
which makes it easy to use correctly. Not only is the API simpler, the implementation is
113113
only 1700 lines whereas gorilla/websocket is at 3500 lines. That's more code to maintain,
114-
more code to test, more code to document and more surface area for bugs.
114+
more code to test, more code to document and more surface area for bugs.
115115

116116
Moreover, nhooyr/websocket has support for newer Go idioms such as context.Context and
117117
also uses net/http's Client and ResponseWriter directly for WebSocket handshakes.
@@ -151,7 +151,7 @@ and clarity.
151151

152152
This library is fantastic in terms of performance. The author put in significant
153153
effort to ensure its speed and I have applied as many of its optimizations as
154-
I could into nhooyr/websocket. Definitely check out his fantastic [blog post](https://medium.freecodecamp.org/million-websockets-and-go-cc58418460bb)
154+
I could into nhooyr/websocket. Definitely check out his fantastic [blog post](https://medium.freecodecamp.org/million-websockets-and-go-cc58418460bb)
155155
about performant WebSocket servers.
156156

157157
If you want a library that gives you absolute control over everything, this is the library,

ci/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
out

ci/fmt.sh

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,30 @@ set -euo pipefail
44
cd "$(dirname "${0}")"
55
source ./lib.sh
66

7-
unstaged_files() {
8-
git ls-files --other --modified --exclude-standard
9-
}
7+
# Unfortunately, this is the only way to ensure go.mod and go.sum are correct.
8+
# See https://github.com/golang/go/issues/27005
9+
go list ./... > /dev/null
10+
go mod tidy
1011

11-
gen() {
12-
# Unfortunately, this is the only way to ensure go.mod and go.sum are correct.
13-
# See https://github.com/golang/go/issues/27005
14-
go list ./... > /dev/null
15-
go mod tidy
12+
go generate ./...
1613

17-
go generate ./...
18-
}
14+
gofmt -w -s .
15+
go run go.coder.com/go-tools/cmd/goimports -w "-local=$(go list -m)" .
16+
go run mvdan.cc/sh/cmd/shfmt -i 2 -w -s -sr .
17+
# shellcheck disable=SC2046
18+
npx prettier \
19+
--write \
20+
--print-width 120 \
21+
--no-semi \
22+
--trailing-comma all \
23+
--loglevel silent \
24+
$(git ls-files "*.yaml" "*.yml" "*.md")
1925

20-
fmt() {
21-
gofmt -w -s .
22-
go run go.coder.com/go-tools/cmd/goimports -w "-local=$(go list -m)" .
23-
go run mvdan.cc/sh/cmd/shfmt -i 2 -w -s -sr .
26+
unstaged_files() {
27+
git ls-files --other --modified --exclude-standard
2428
}
2529

26-
gen
27-
fmt
28-
29-
if [[ $CI && $(unstaged_files) != "" ]]; then
30+
if [[ ${CI:-} && $(unstaged_files) != "" ]]; then
3031
echo
3132
echo "Files either need generation or are formatted incorrectly."
3233
echo "Please run:"

ci/lib.sh

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
set -euo pipefail
44

5-
# Ensures $CI can be used if it's set or not.
6-
export CI=${CI:-}
7-
if [[ $CI ]]; then
5+
if [[ ${CI:-} ]]; then
86
export GOFLAGS=-mod=readonly
97
export DEBIAN_FRONTEND=noninteractive
108
fi

ci/lint.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ set -euo pipefail
44
cd "$(dirname "${0}")"
55
source ./lib.sh
66

7-
if [[ $CI ]]; then
7+
if [[ ${CI:-} ]]; then
88
apt-get update -qq
99
apt-get install -qq shellcheck > /dev/null
1010
fi

ci/out/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

ci/run.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ set -euo pipefail
66
cd "$(dirname "${0}")"
77
source ./lib.sh
88

9-
./fmt.sh
10-
./lint.sh
11-
./test.sh
9+
./ci/fmt.sh
10+
./ci/lint.sh
11+
./ci/test.sh

ci/test.sh

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,20 @@ set -euo pipefail
44
cd "$(dirname "${0}")"
55
source ./lib.sh
66

7-
echo "This step includes benchmarks for race detection and coverage purposes
8-
but the numbers will be misleading. please see the bench step or ./bench.sh for
9-
more accurate numbers."
10-
echo
11-
12-
if [[ $CI ]]; then
7+
if [[ ${CI:-} ]]; then
138
apt-get update -qq
149
apt-get install -qq python-pip > /dev/null
1510
# Need to add pip install directory to $PATH.
1611
export PATH="/home/circleci/.local/bin:$PATH"
1712
pip install -qqq autobahntestsuite
1813
fi
1914

20-
go test -race -coverprofile=ci/out/coverage.prof --vet=off -bench=. -coverpkg=./... "$@" ./...
21-
go tool cover -func=ci/out/coverage.prof
15+
# If you'd like to modify the args to go test, just run go test directly, this script is meant
16+
# for running tests at the end to get coverage and test under the race detector.
17+
go test -race -vet=off -coverprofile=ci/out/coverage.prof -coverpkg=./... ./...
2218

23-
if [[ $CI ]]; then
19+
if [[ ${CI:-} ]]; then
2420
bash <(curl -s https://codecov.io/bash) -f ci/out/coverage.prof
2521
else
2622
go tool cover -html=ci/out/coverage.prof -o=ci/out/coverage.html
27-
28-
echo
29-
echo "Please open ci/out/coverage.html to see detailed test coverage stats."
3023
fi

docs/CONTRIBUTING.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,29 @@ Please capitalize the first word in the commit message title.
1212

1313
The commit message title should use the verb tense + phrase that completes the blank in
1414

15-
> This change modifies websocket to ___________
15+
> This change modifies websocket to \_\_\_\_\_\_\_\_\_
1616
1717
Be sure to link to an existing issue if one exists. In general, try creating an issue
1818
before making a PR to get some discussion going and to make sure you do not spend time
1919
on a PR that may be rejected.
2020

21-
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.
21+
You can run tests normally with `go test`.
22+
You'll need the [Autobahn Test suite pip package](https://github.com/crossbario/autobahn-testsuite).
2223

23-
See [../ci/lint.sh](../ci/lint.sh) and [../ci/lint.sh](../ci/test.sh) for the
24+
On submission please check if CI has passed and if not, please correct your code such that it does.
25+
If necessary, you may run CI locally with the `ci/run.sh` script.
26+
You'll only need [shellcheck](https://github.com/koalaman/shellcheck#installing).
27+
28+
For coverage details locally, please see `ci/out/coverage.html` after running `ci/run.sh` or `ci/test.sh`.
29+
For remote coverage, you can use either [codecov](https://codecov.io/gh/nhooyr/websocket) or download the
30+
`coverage.html` generated by the go tooling as an artifact on CI.
31+
32+
You can also run any of the steps individually. All of them are scripts in the `ci` directory.
33+
34+
See [../ci/lint.sh](../ci/lint.sh) and [../ci/test.sh](../ci/test.sh) for the
2435
installation of shellcheck and the Autobahn test suite on Debian or Ubuntu.
2536

2637
For Go, please refer to the [offical docs](https://golang.org/doc/install).
2738

28-
You can benchmark the library with `./ci/benchmark.sh`. You only need Go to run that script.
39+
You can benchmark the library with `./ci/benchmark.sh`. You only need Go to run that script. Benchmark
40+
profiles generated by that script are also available for every CI job as artifacts.

0 commit comments

Comments
 (0)