Skip to content

Commit

Permalink
ci: Fixes
Browse files Browse the repository at this point in the history
Credits to @maggie44 for making me add staticcheck.
See #407

Co-authored-by: maggie0002 <64841595+maggie0002@users.noreply.github.com>
  • Loading branch information
nhooyr and maggie44 committed Oct 10, 2023
1 parent c45cd4c commit 118ea68
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 172 deletions.
39 changes: 0 additions & 39 deletions .github/workflows/ci.yaml

This file was deleted.

39 changes: 39 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: ci
on: [push, pull_request]
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }}
cancel-in-progress: true

jobs:
fmt:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v4
with:
go-version-file: ./go.mod
- run: ./ci/fmt.sh

lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: go version
- uses: actions/setup-go@v4
with:
go-version-file: ./go.mod
- run: ./ci/lint.sh

test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v4
with:
go-version-file: ./go.mod
- run: ./ci/test.sh
- uses: actions/upload-artifact@v2
if: always()
with:
name: coverage.html
path: ./ci/out/coverage.html
12 changes: 0 additions & 12 deletions ci/all.sh

This file was deleted.

14 changes: 0 additions & 14 deletions ci/container/Dockerfile

This file was deleted.

50 changes: 15 additions & 35 deletions ci/fmt.sh
Original file line number Diff line number Diff line change
@@ -1,38 +1,18 @@
#!/usr/bin/env bash
set -euo pipefail
#!/bin/sh
set -eu
cd -- "$(dirname "$0")/.."

main() {
cd "$(dirname "$0")/.."
go mod tidy
gofmt -w -s .
go run golang.org/x/tools/cmd/goimports@latest -w "-local=$(go list -m)" .

go mod tidy
gofmt -w -s .
goimports -w "-local=$(go list -m)" .
npx prettier@3.0.3 \
--write \
--log-level=warn \
--print-width=90 \
--no-semi \
--single-quote \
--arrow-parens=avoid \
$(git ls-files "*.yml" "*.md" "*.js" "*.css" "*.html")

prettier \
--write \
--print-width=120 \
--no-semi \
--trailing-comma=all \
--loglevel=warn \
--arrow-parens=avoid \
$(git ls-files "*.yml" "*.md" "*.js" "*.css" "*.html")
shfmt -i 2 -w -s -sr $(git ls-files "*.sh")

stringer -type=opcode,MessageType,StatusCode -output=stringer.go

if [[ ${CI-} ]]; then
assert_no_changes
fi
}

assert_no_changes() {
if [[ $(git ls-files --other --modified --exclude-standard) ]]; then
git -c color.ui=always --no-pager diff
echo
echo "Please run the following locally:"
echo " ./ci/fmt.sh"
exit 1
fi
}

main "$@"
go run golang.org/x/tools/cmd/stringer@latest -type=opcode,MessageType,StatusCode -output=stringer.go
24 changes: 11 additions & 13 deletions ci/lint.sh
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
#!/usr/bin/env bash
set -euo pipefail
#!/bin/sh
set -eu
cd -- "$(dirname "$0")/.."

main() {
cd "$(dirname "$0")/.."
go vet ./...
GOOS=js GOARCH=wasm go vet ./...

go vet ./...
GOOS=js GOARCH=wasm go vet ./...
go install golang.org/x/lint/golint@latest
golint -set_exit_status ./...
GOOS=js GOARCH=wasm golint -set_exit_status ./...

golint -set_exit_status ./...
GOOS=js GOARCH=wasm golint -set_exit_status ./...

shellcheck --exclude=SC2046 $(git ls-files "*.sh")
}

main "$@"
go install honnef.co/go/tools/cmd/staticcheck@latest
staticcheck ./...
GOOS=js GOARCH=wasm staticcheck ./...
33 changes: 11 additions & 22 deletions ci/test.sh
Original file line number Diff line number Diff line change
@@ -1,25 +1,14 @@
#!/usr/bin/env bash
set -euo pipefail
#!/bin/sh
set -eu
cd -- "$(dirname "$0")/.."

main() {
cd "$(dirname "$0")/.."
go install github.com/agnivade/wasmbrowsertest@latest
go test --race --timeout=1h --covermode=atomic --coverprofile=ci/out/coverage.prof --coverpkg=./... "$@" ./...
sed -i.bak '/stringer\.go/d' ci/out/coverage.prof
sed -i.bak '/nhooyr.io\/websocket\/internal\/test/d' ci/out/coverage.prof
sed -i.bak '/examples/d' ci/out/coverage.prof

go test -timeout=30m -covermode=atomic -coverprofile=ci/out/coverage.prof -coverpkg=./... "$@" ./...
sed -i.bak '/stringer\.go/d' ci/out/coverage.prof
sed -i.bak '/nhooyr.io\/websocket\/internal\/test/d' ci/out/coverage.prof
sed -i.bak '/examples/d' ci/out/coverage.prof
# Last line is the total coverage.
go tool cover -func ci/out/coverage.prof | tail -n1

# Last line is the total coverage.
go tool cover -func ci/out/coverage.prof | tail -n1

go tool cover -html=ci/out/coverage.prof -o=ci/out/coverage.html

if [[ ${CI-} && ${GITHUB_REF-} == *master ]]; then
local deployDir
deployDir="$(mktemp -d)"
cp ci/out/coverage.html "$deployDir/index.html"
netlify deploy --prod "--dir=$deployDir"
fi
}

main "$@"
go tool cover -html=ci/out/coverage.prof -o=ci/out/coverage.html
8 changes: 4 additions & 4 deletions examples/chat/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ body {
margin: 0 0 0 10px;
}

#publish-form input[type="text"] {
#publish-form input[type='text'] {
flex-grow: 1;

-moz-appearance: none;
Expand All @@ -64,18 +64,18 @@ body {
border: 1px solid #ccc;
}

#publish-form input[type="submit"] {
#publish-form input[type='submit'] {
color: white;
background-color: black;
border-radius: 5px;
padding: 5px 10px;
border: none;
}

#publish-form input[type="submit"]:hover {
#publish-form input[type='submit']:hover {
background-color: red;
}

#publish-form input[type="submit"]:active {
#publish-form input[type='submit']:active {
background-color: red;
}
2 changes: 1 addition & 1 deletion examples/chat/index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!DOCTYPE html>
<!doctype html>
<html lang="en-CA">
<head>
<meta charset="UTF-8" />
Expand Down
36 changes: 18 additions & 18 deletions examples/chat/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@
function dial() {
const conn = new WebSocket(`ws://${location.host}/subscribe`)

conn.addEventListener("close", ev => {
conn.addEventListener('close', ev => {
appendLog(`WebSocket Disconnected code: ${ev.code}, reason: ${ev.reason}`, true)
if (ev.code !== 1001) {
appendLog("Reconnecting in 1s", true)
appendLog('Reconnecting in 1s', true)
setTimeout(dial, 1000)
}
})
conn.addEventListener("open", ev => {
console.info("websocket connected")
conn.addEventListener('open', ev => {
console.info('websocket connected')
})

// This is where we handle messages received.
conn.addEventListener("message", ev => {
if (typeof ev.data !== "string") {
console.error("unexpected message type", typeof ev.data)
conn.addEventListener('message', ev => {
if (typeof ev.data !== 'string') {
console.error('unexpected message type', typeof ev.data)
return
}
const p = appendLog(ev.data)
Expand All @@ -32,38 +32,38 @@
}
dial()

const messageLog = document.getElementById("message-log")
const publishForm = document.getElementById("publish-form")
const messageInput = document.getElementById("message-input")
const messageLog = document.getElementById('message-log')
const publishForm = document.getElementById('publish-form')
const messageInput = document.getElementById('message-input')

// appendLog appends the passed text to messageLog.
function appendLog(text, error) {
const p = document.createElement("p")
const p = document.createElement('p')
// Adding a timestamp to each message makes the log easier to read.
p.innerText = `${new Date().toLocaleTimeString()}: ${text}`
if (error) {
p.style.color = "red"
p.style.fontStyle = "bold"
p.style.color = 'red'
p.style.fontStyle = 'bold'
}
messageLog.append(p)
return p
}
appendLog("Submit a message to get started!")
appendLog('Submit a message to get started!')

// onsubmit publishes the message from the user when the form is submitted.
publishForm.onsubmit = async ev => {
ev.preventDefault()

const msg = messageInput.value
if (msg === "") {
if (msg === '') {
return
}
messageInput.value = ""
messageInput.value = ''

expectingMessage = true
try {
const resp = await fetch("/publish", {
method: "POST",
const resp = await fetch('/publish', {
method: 'POST',
body: msg,
})
if (resp.status !== 202) {
Expand Down
18 changes: 4 additions & 14 deletions make.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
#!/bin/sh
set -eu
cd -- "$(dirname "$0")"

cd "$(dirname "$0")"

fmt() {
go mod tidy
gofmt -s -w .
goimports -w "-local=$(go list -m)" .
}

if ! command -v wasmbrowsertest >/dev/null; then
go install github.com/agnivade/wasmbrowsertest@latest
fi

fmt
go test -race --timeout=1h ./... "$@"
./ci/fmt.sh
./ci/lint.sh
./ci/test.sh

0 comments on commit 118ea68

Please sign in to comment.