Skip to content

Commit 2e46b12

Browse files
committed
bin: add scripts for ci & git hooks
1 parent a52c04b commit 2e46b12

17 files changed

+269
-29
lines changed

.travis.yml

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,30 +33,22 @@ before_script:
3333
- pushd test/cs-demos && git lfs pull -I '*' && popd
3434

3535
script:
36-
- go build ./...
36+
# Compile
37+
- bin/build.sh
3738

3839
# Make sure generated code is up-to-date
39-
- go generate && go generate ./sendtables
40-
- diff_output=$(git diff)
41-
- if [[ "$diff_output" != "" ]]; then echo "Generated code is not up-to-date" && echo "$diff_output" && exit 1; fi
40+
- bin/check-interfaces-generated.sh
4241

4342
# Lint changed code
44-
- if [[ "$TRAVIS_COMMIT_RANGE" != "" ]]; then commit_range=${TRAVIS_COMMIT_RANGE/.../..} && git diff $commit_range > /dev/null && base_rev=$commit_range || true; fi
45-
- if [[ "$base_rev" = "" ]]; then base_rev='origin/master'; fi && echo "Linting changes between/since $base_rev"
46-
- golangci-lint run --new-from-rev $base_rev
43+
- bin/lint-changes.sh
4744

48-
# Get list of tests to run with race detection.
49-
# We don't want to run the entire demo set with it because it's large and race tests take a lot longer.
50-
- race_tests=$(go test -list . | grep -v "TestDemoSet\|Benchmark" | head -n -1 | awk -vORS=\| '{ print $1 }' | sed 's/|$/\n/')
5145
# Run race tests
52-
- go test -v -race -run "$race_tests" -timeout 15m
46+
- bin/race-tests.sh
5347

54-
# We run all tests again to get full coverage, technically unnecessary tho
55-
- coverpkg_ignore='/(msg|fake)'
56-
# coverpkg for multiple mains is broken in 1.12, so we need to exclude the examples from coverage
57-
# https://github.com/golang/go/issues/30374
58-
- if ! [[ "$TRAVIS_GO_VERSION" =~ ^1\.11 ]]; then coverpkg_ignore="${coverpkg_ignore}|/examples"; fi
59-
- go test -v -timeout 30m -coverprofile=coverage.txt -coverpkg=$(go list ./... | grep -v -E ${coverpkg_ignore} | awk -vORS=, '{ print $1 }' | sed 's/,$/\n/') ./...
48+
# Coverage
49+
# Note: We run ALL tests again to get full coverage
50+
# Race tests are too slow and skip the regression set
51+
- bin/coverage.sh
6052

6153
after_success:
6254
- bash <(curl -s https://codecov.io/bash)

README.md

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -159,18 +159,41 @@ If your project is using this library feel free to submit a PR or send a message
159159

160160
## Development
161161

162-
### Running tests
162+
### Git hooks
163163

164-
To run tests [Git LFS](https://git-lfs.github.com) is required.
164+
To install some (optional, but quite handy) `pre-commit` and `pre-push` hooks, you can run the following script.
165165

166-
```sh
167-
git submodule init
168-
git submodule update
169-
pushd test/cs-demos && git lfs pull -I '*' && popd
170-
go test
171-
```
166+
bin/git-hooks/link-git-hooks.sh
167+
168+
#### `pre-commit`:
169+
- check if [interfaces have been updated](#generating-interfaces)
170+
- build the code
171+
- run unit tests
172+
173+
#### `pre-push`:
174+
- run regression tests
175+
176+
### Testing
177+
178+
#### Unit tests
179+
180+
For any new features, [Test Driven Development](https://medium.com/@pierreprinetti/test-driven-development-in-go-baeab5adb468) should be practiced where possible.
181+
However, due to some design flaws in some parts of the code it's currently not always practical to do so.
182+
183+
Running unit tests:
184+
185+
bin/unit-tests.sh
186+
# or (identical)
187+
go test -short ./...
188+
189+
#### Regression tests
190+
191+
For the full regression suite you will need to download the test demo-set.
192+
For this, [Git LFS](https://git-lfs.github.com) is required.
193+
194+
Downloading demos + running regression tests:
172195

173-
Here's a cool [gist of a pre-commit hook](https://gist.github.com/micvbang/4c8cb1f24cfe04d1a0dfab010eb851d8) to run tests before each commit. You can put this inside the `.git/hooks` directory to avoid committing/pushing code with build errors or failing tests.
196+
bin/regression-tests.sh
174197

175198
### Debugging
176199

@@ -187,7 +210,7 @@ We generate interfaces such as `IGameState` from structs to make it easier to ke
187210
For this we use [@vburenin](https://github.com/vburenin)'s [`ifacemaker`](https://github.com/vburenin/ifacemaker) tool.
188211
You can install it via `GO111MODULE=off go get github.com/vburenin/ifacemaker`.
189212

190-
After adding it to your `PATH` you can use `go generate` to update interfaces.
213+
After adding it to your `PATH` you can use `bin/generate-interfaces.sh` to update interfaces.
191214

192215
### Generating protobuf code
193216

bin/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
!coverage.sh

bin/build.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
# compile all packages + tests
6+
go build ./...
7+
go test -run ^$ ./...

bin/check-interfaces-generated.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
bin_dir=$(dirname "$0")
6+
7+
$bin_dir/generate-interfaces.sh
8+
diff_output=$(git diff --ignore-submodules)
9+
if [[ "$diff_output" != "" ]]; then
10+
# don't keep the changes used for the check
11+
git stash save --keep-index --quiet
12+
git stash drop --quiet
13+
14+
echo "ERROR: generated code is not up-to-date"
15+
echo "$diff_output"
16+
exit 1
17+
fi

bin/ci.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
# This script simulates a CI run.
3+
# Might not work on windows since race tests are broken.
4+
# Prerequisites must be installed manually:
5+
# - ifacemaker -> GO111MODULE=off go get github.com/vburenin/ifacemaker
6+
# - golangci-lint -> https://github.com/golangci/golangci-lint/releases
7+
# - test-data -> pushd test/cs-demos && git lfs pull -I '*' && popd
8+
9+
set -e
10+
11+
# get the commands straight form the travis config
12+
ci_commands=$(sed -n '/^script:/,/^\S/p' .travis.yml | grep -E '^ - ' | cut -c 5-)
13+
14+
while read -r cmd; do
15+
echo "running $cmd:"
16+
$cmd
17+
echo -e '\n'
18+
done <<<"$ci_commands"

bin/coverage.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
bin/download-test-data.sh
6+
7+
# don't cover mocks and generated protobuf code
8+
coverpkg_ignore='/(fake|msg)'
9+
coverpkg=$(go list ./... | grep -v -E ${coverpkg_ignore} | awk -vORS=, '{ print $1 }' | sed 's/,$/\n/')
10+
11+
# -timeout 30m because the CI is slow
12+
# output file must be called 'coverage.txt' for Codecov
13+
go test -v -timeout 30m -coverprofile=coverage.txt -coverpkg=$coverpkg ./...

bin/download-test-data.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
echo 'downloading/updating test data (~ 1 GB), this may take a while ...'
6+
7+
git submodule init
8+
git submodule update
9+
10+
pushd test/cs-demos >/dev/null
11+
git lfs pull -I '*'
12+
popd >/dev/null
13+
14+
echo 'download complete'

bin/generate-interfaces.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
go generate
6+
go generate ./sendtables

bin/git-hooks/link-git-hooks.sh

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/bin/bash
2+
3+
# Windows needs special treatment
4+
windows() { [[ -n "$WINDIR" ]]; }
5+
6+
# Cross-platform symlink function
7+
link() {
8+
if windows; then
9+
# We need elevated privileges and some windows stuff
10+
powershell "Start-Process cmd -ArgumentList '/c','cd',(Get-Location).path,'&& mklink','${1//\//\\}','${2//\//\\}','|| cd && pause' -v RunAs"
11+
else
12+
# You know what? I think ln's parameters are backwards.
13+
ln -s "$2" "$1"
14+
fi
15+
}
16+
17+
cd "$(dirname "$0")/../.."
18+
19+
if [ -f .git/hooks/pre-commit ]; then
20+
echo 'removing existing pre-commit hook'
21+
rm .git/hooks/pre-commit
22+
fi
23+
if [ -f .git/hooks/pre-push ]; then
24+
echo 'removing existing pre-push hook'
25+
rm .git/hooks/pre-push
26+
fi
27+
28+
link .git/hooks/pre-commit ../../bin/git-hooks/pre-commit.sh
29+
echo 'added pre-commit hook'
30+
31+
echo 'do you want to set up the pre-push hook for the regression suite?'
32+
echo -n 'this will download ~ 1 GB of test-data (if not already done) [y/N] '
33+
34+
read prePushYesNo
35+
if [[ "$prePushYesNo" == "y" || "$prePushYesNo" == "Y" ]]; then
36+
bin/download-test-data.sh
37+
link .git/hooks/pre-push ../../bin/git-hooks/pre-push.sh
38+
echo 'added pre-push hook'
39+
fi

0 commit comments

Comments
 (0)