Skip to content

Commit 7e3f320

Browse files
Add pre-commit hooks and improve Makefile for #15 (#23)
* add pre-commit hook script * add Makefile targets and readme instructions * add badges to README * update action options * verify failing unit test fails action * fix test * document the makefile in README
1 parent aea4589 commit 7e3f320

File tree

7 files changed

+129
-8
lines changed

7 files changed

+129
-8
lines changed

.github/workflows/golang-ci-lint.yaml

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: Go Lint
2+
on:
3+
push:
4+
tags:
5+
- v*
6+
branches:
7+
- master
8+
- main
9+
pull_request:
10+
permissions:
11+
contents: read
12+
# Optional: allow read access to pull request. Use with `only-new-issues` option.
13+
# pull-requests: read
14+
jobs:
15+
golangci:
16+
name: lint
17+
runs-on: ubuntu-latest
18+
steps:
19+
- uses: actions/checkout@v2
20+
- name: golangci-lint
21+
uses: golangci/golangci-lint-action@v2
22+
with:
23+
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
24+
version: v1.29
25+
26+
# Optional: working directory, useful for monorepos
27+
# working-directory: somedir
28+
29+
# Optional: golangci-lint command line arguments.
30+
# args: --issues-exit-code=0
31+
32+
# Optional: show only new issues if it's a pull request. The default value is `false`.
33+
only-new-issues: true

.github/workflows/run-tests.yaml

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Build and Test
1+
name: Unit Test
22
on:
33
push:
44
branches:
@@ -22,9 +22,6 @@ jobs:
2222
- name: Check out code into the Go module directory
2323
uses: actions/checkout@v2
2424

25-
- name: Vet
26-
run: go vet ./...
27-
2825
- name: Test
2926
run: go test ./...
3027

Makefile

+33-1
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,43 @@
44
# Debian/Ubuntu including WSL2: sudo apt-get install build-essential
55
# Windows (non-WSL2): choco install make or http://gnuwin32.sourceforge.net/install.html
66

7+
all:
8+
@echo "runner Makefile targets"
9+
@echo ""
10+
@echo " gen-mocks: create/recreate mocks for unit testing"
11+
@echo ""
12+
@echo " run-api: run the API server"
13+
@echo ""
14+
@echo " run-api-bg: run the API server in the background"
15+
@echo ""
16+
@echo " kill-api: kill the API server"
17+
@echo ""
18+
@echo " test: run all unit tests in the repo"
19+
@echo ""
20+
@echo " fmt: run go fmt on the repository"
21+
@echo ""
22+
@echo " install-hooks: install git-hooks in the cloned repo .git directory"
23+
@echo ""
24+
725
gen-mocks:
826
mockgen -source ./engine/runtime/types.go -package=mocks -destination ./engine/runtime/mocks/Runtime.go Runtime
927

1028
run-api:
1129
go run api/main.go
1230

31+
run-api-bg:
32+
go run api/main.go &
33+
34+
kill-api:
35+
./hack/kill_server.sh
36+
1337
test:
14-
go test ./...
38+
go test ./...
39+
40+
fmt:
41+
go fmt ./...
42+
43+
install-hooks:
44+
@echo "installing git hooks"
45+
cp ./hack/hooks/* .git/hooks/
46+
@echo "done"

engine/runtime/runtime.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package runtime
22

33
import (
44
"bytes"
5+
"context"
56
"fmt"
67
"io"
78
"os/exec"
8-
"context"
99
"time"
1010

1111
"github.com/runner-x/runner-x/util/print"
@@ -31,9 +31,9 @@ func (r *RuntimeAgent) RunCmd(runprops *RunProps) (*RunOutput, error) {
3131
if numArgs < 1 {
3232
return nil, nil
3333
} else if numArgs == 1 {
34-
cmd = exec.CommandContext(ctx,runprops.RunArgs[0])
34+
cmd = exec.CommandContext(ctx, runprops.RunArgs[0])
3535
} else {
36-
cmd = exec.CommandContext(ctx,runprops.RunArgs[0], runprops.RunArgs[1:]...)
36+
cmd = exec.CommandContext(ctx, runprops.RunArgs[0], runprops.RunArgs[1:]...)
3737
}
3838

3939
stdoutPipe, stdoutErr := cmd.StdoutPipe()

hack/hooks/pre-commit

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
3+
# go to repository root, handle case where this is run in root directory
4+
cd $(git rev-parse --show-toplevel)
5+
6+
# check go is installed
7+
if ! which go; then
8+
echo "go not installed, cannot run precheck"
9+
exit
10+
fi
11+
12+
# format all go files in repository
13+
go fmt ./...
File renamed without changes.

readme.md

+46
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# 401X runner project
22

3+
[![Unit Tests](https://github.com/camerondurham/runner/workflows/Unit%20Test/badge.svg?branch=main)](https://github.com/camerondurham/runner/actions?query=workflow%3A%22Unit+Test%22)
4+
5+
[![Go Lint](https://github.com/camerondurham/runner/workflows/Go%20Lint/badge.svg?branch=main)](https://github.com/camerondurham/runner/actions?query=workflow%3A%22Go+Lint%22)
6+
37
## Intro
48

59
The runner project is to create an interface for users to run their code remotely without having
@@ -52,6 +56,48 @@ can install [Docker Desktop](https://www.docker.com/get-started) if you like.
5256

5357
## Development
5458

59+
This repository is primarily written in Go and the Makefile has a helper
60+
commands to make development easier and more consistent.
61+
62+
> Note: before you start development, please run `make install-hooks`
63+
> to install [Git Hooks](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks)
64+
> in your repository's `.git/hooks` directory. This will install a pre-commit
65+
> hook that automatically formats your code with [gofmt](https://go.dev/blog/gofmt).
66+
67+
### Using the Makefile
68+
69+
By now, you are probably familiar with Makefiles. If not, this
70+
wiki provides a great summary: [cs104/wiki/makefile](https://bytes.usc.edu/cs104/wiki/makefile/) (written by Leif Wesche).
71+
72+
Here's a quick summary of what the targets will do:
73+
74+
```bash
75+
# print out all the makefile targets
76+
make
77+
78+
# create or create mocks for unit testing, helpful if you have
79+
# modified any of the interfaces in a `types.go` file
80+
make gen-mocks
81+
82+
# run the API server (blocking, you can't use the terminal anymore)
83+
make run-api
84+
85+
# run the API server in the background (you'll have to shut it down later)
86+
make run-api-bg
87+
88+
# kill the server if it's running (this works by killing the process using port 8080, the API port)
89+
make kill-api
90+
91+
# run all tests in the repository
92+
make test
93+
94+
# run go fmt on the repository to format your code
95+
make fmt
96+
97+
# install git-hooks to automatically format your code before you commit
98+
make install-hooks
99+
```
100+
55101
### CLI Setup
56102

57103
CLI stands for command line interface.

0 commit comments

Comments
 (0)