Skip to content

Commit

Permalink
Add linting to pre-commit hook and CI
Browse files Browse the repository at this point in the history
  • Loading branch information
deanishe committed May 21, 2020
1 parent 07e25c9 commit 2bc7a39
Show file tree
Hide file tree
Showing 11 changed files with 484 additions and 73 deletions.
7 changes: 2 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,9 @@ jobs:
- name: Get dependencies
run: |
go get -v -t -d ./...
go get -v -d github.com/mfridman/tparse
- name: Lint Go source code
run: |
go install golang.org/x/lint/golint
golint -set_exit_status ./...
run: zsh run-tests.sh -vl

- name: Run unit tests
run: zsh run-tests.sh -ic ./...
Expand All @@ -56,4 +53,4 @@ jobs:
COVERALLS_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }}
run: |
go install github.com/mattn/goveralls
goveralls -coverprofile coverage.out -reponame deanishe/awgo
goveralls -coverprofile coverage.out -service github
37 changes: 37 additions & 0 deletions .golangci.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[run]
deadline = "5m"

[linters]
disable-all = true
enable = [
"deadcode",
"goconst",
"gocritic",
"gofmt",
"goimports",
"gosimple",
"ineffassign",
"scopelint",
"staticcheck",
"stylecheck",
"unconvert",
"unused",
"whitespace",
]

[linter-settings]
[linter-settings.errcheck]
check-blank = true
check-type-assertions = true

[linter-settings.goimports]
local-prefixes = "github.com/deanishe/awgo"

[issues]
max-same-issues = 50
max-issues-per-linter = 50
# exclude = ['ST1005']

[[issues.exclude-rules]]
linters = ['stylecheck']
text = "ST1005:"
3 changes: 1 addition & 2 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ steps:
displayName: Fetch Code

- script: |
go install golang.org/x/lint/golint
golint -set_exit_status ./...
./run-tests.sh -l
workingDirectory: '$(modulePath)'
displayName: Lint

Expand Down
10 changes: 8 additions & 2 deletions bin/hooks/pre-commit
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
#!/bin/zsh
#!/bin/zsh -e

find _examples -type f -name 'info.plist' -exec /usr/libexec/PlistBuddy -c 'Set :disabled false' {} \;
root="$( git rev-parse --show-toplevel )"

# make sure example workflows aren't disabled
find "${root}/_examples" -type f -name 'info.plist' -exec /usr/libexec/PlistBuddy -c 'Set :disabled false' {} \;

# lint
"${root}/run-tests.sh" -l
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ require (
github.com/bmatcuk/doublestar v1.3.0
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deanishe/go-env v0.4.0
github.com/golangci/golangci-lint v1.27.0 // indirect
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.5.1
golang.org/x/lint v0.0.0-20200302205851-738671d3881b // indirect
golang.org/x/text v0.3.2
gopkg.in/yaml.v2 v2.3.0 // indirect
howett.net/plist v0.0.0-20200419221736-3b63eb3a43b5
Expand Down
377 changes: 371 additions & 6 deletions go.sum

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion modd.conf
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ run-tests.sh
**/*.go
!_examples/** {
prep: "
# lint
GO111MODULE=on ./run-tests.sh -tvl
# run unit tests
GO111MODULE=on ./run-tests.sh -ic @dirmods
GO111MODULE=on ./run-tests.sh -tvic @dirmods
"
}
106 changes: 51 additions & 55 deletions run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ root="$( git rev-parse --show-toplevel )"
testdir="${root}/testenv"
iplist="${root}/info.plist"
covfile="${root}/coverage.out"
covjson="${root}/coverage.json"
covhtml="${root}/coverage.html"

verbose=false
Expand All @@ -14,9 +13,12 @@ opencover=false
usegocov=false
cover=false
mkip=false
colour=false
vopt=
gopts=()

test -t 1 && colour=true

# log <arg>... | Echo arguments to STDERR
log() {
echo "$@" >&2
Expand All @@ -28,32 +30,24 @@ installed() {
return $?
}

# info <arg>.. | Write args to STDERR if VERBOSE is true
info() {
$verbose && log $(print -P "%F{blue}.. %f") "$@"
return 0
}

# success <arg>.. | Write green "ok" and args to STDERR if VERBOSE is true
# success <arg>... | Write message in green to STDOUT
success() {
# $verbose && log $(print -P "%F{green}ok %f") "$@"
log $(print -P "%F{green}#####################################%f")
log $(print -P "%F{green}# $@ %f")
log $(print -P "%F{green}#####################################%f")
return 0
$verbose || return 0
$colour && {
print -P "%F{green}$@ %f"
} || echo "[OK] $@"
}

# error <arg>.. | Write red "error" and args to STDERR
# error <arg>... | Write message in red to STDERR
error() {
log $(print -P '%F{red}err%f') "$@"
$colour && {
print -P "%F{red}$@ %f" >&2
} || echo "[ERR] $@" >&2
}

# fail <arg>.. | Write red "error" and args to STDERR, then exit with status 1
# fail <arg>... | Write message in red to STDERR, then exit with status 1
fail() {
log $(print -P "%F{red}#####################################%f")
log $(print -P "%F{red}# $@ %f")
log $(print -P "%F{red}#####################################%f")
# error "$@"
error "$@"
exit 1
}

Expand All @@ -64,25 +58,26 @@ run-tests.sh [options] [<module>...]
Run unit tests in a workflow-like environment.
Usage:
run-tests.sh [-v|-V] [-c] [-C] [-i] [-g]
run-tests.sh -l
run-tests.sh [-v|-V] [-t] [-c|-g] [-C] [-i]
run-tests.sh [-t] -l
run-tests.sh [-g] -r
run-tests.sh -h
Options:
-c Write coverage report
-C Open HTML coverage report
-l Lint project
-r Just open coverage report
-g Use gocov for coverage report (implies -c)
-i Create a dummy info.plist
-h Show this help message and exit
-v Be verbose
-V Be even more verbose
-c write coverage report
-C open HTML coverage report
-l lint project
-r just open coverage report
-g use gocov for coverage report (implies -c)
-i create a dummy info.plist
-t force terminal (coloured) output
-h show this help message and exit
-v be verbose
-V be even more verbose
EOF
}

while getopts ":CcghilrvV" opt; do
while getopts ":CcghilrtvV" opt; do
case $opt in
c)
cover=true
Expand All @@ -106,6 +101,9 @@ while getopts ":CcghilrvV" opt; do
opencover=true
runtests=false
;;
t)
colour=true
;;
V)
gopts+=(-v)
verbose=true
Expand All @@ -126,56 +124,54 @@ done
shift $((OPTIND-1))

$runlint && {
golangci-lint run -c .golangci.toml
st=$?
[[ $st -ne 0 ]] && {
fail "linting failed"
diff=($(gofmt -s -l **/*.go))
test -z "$diff" || {
for s in $diff; do error "bad formatting: $s"; done
fail "gofmt -s found incorrectly formatted files"
}
success "linting passed"
success "all files formatted correctly"

go run golang.org/x/lint/golint -set_exit_status ./...
[[ $? -eq 0 ]] || fail "linting with golint failed"
success "golint found no issues"

go run github.com/golangci/golangci-lint/cmd/golangci-lint run -c .golangci.toml
[[ $? -eq 0 ]] || fail "linting with golangci-lint failed"
success "golangci-lint found no issues"
exit 0
}

$cover && gopts+=(-coverprofile="$covfile")

command mkdir $vopt -p "${testdir}"/{data,cache}
$mkip && touch $vopt "$iplist"
$mkip touch $vopt "$iplist"
trap "test -f \"$iplist\" && rm -f \"$iplist\"; test -d \"$testdir\" && rm -rf \"$testdir\";" EXIT INT TERM

cd "$root"
source "env.sh"
export alfred_version=
export alfred_workflow_data="${testdir}/data"
export alfred_workflow_cache="${testdir}/cache"

[[ $#@ -eq 0 ]] && {
pkgs=(./...)
} || {
pkgs=($@)
}
pkgs=(./...)
[[ $#@ -eq 0 ]] || pkgs=($@)

st=0
$runtests && {
go test -cover -json $gopts $pkgs | go run github.com/mfridman/tparse
# gotestsum -- $gopts $pkgs
st=$?

[[ $st -eq 0 ]] && {
success "passed"
}
command rm $vopt -rf "$testdir"/*
[[ $st -eq 0 ]] && success "unit tests passed"
}

test -f "$iplist" && command rm $vopt -f "$iplist"

cd -

[[ $st -ne 0 ]] && {
fail "failed"
}
[[ $st -ne 0 ]] && fail "unit tests failed"

$opencover && {
$usegocov && {
gocov convert "$covfile" > "$covjson"
gocov-html > "$covhtml" < "$covjson"
go run github.com/axw/gocov/gocov convert "$covfile" \
| go run github.com/matm/gocov-html > "$covhtml"
open "$covhtml"
} || {
go tool cover -html="$covfile"
Expand Down
1 change: 1 addition & 0 deletions update/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ func testUpdater(name string, u *Updater, t *testing.T) {
}

for _, td := range tests {
td := td // pin variable
t.Run(name+" "+td.name, func(t *testing.T) {
u.CurrentVersion = mustVersion(td.currentVersion)
u.AlfredVersion = mustVersion(td.alfredVersion)
Expand Down
5 changes: 3 additions & 2 deletions util/build/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func TestAlfredVersion(t *testing.T) {
}

for _, td := range tests {
td := td
td := td // pin variable
t.Run(fmt.Sprintf("dir=%q, env=%q", td.dir, td.envvar), func(t *testing.T) {
withEnv(map[string]string{
"alfred_version": td.envvar,
Expand Down Expand Up @@ -152,6 +152,7 @@ func TestDirs(t *testing.T) {
}

for _, td := range tests {
td := td // pin variable
t.Run(td.name, func(t *testing.T) {
withEnv(map[string]string{
"alfred_version": td.version,
Expand Down Expand Up @@ -206,7 +207,7 @@ func TestEnv(t *testing.T) {
}
env := info.Env()
for _, td := range tests {
td := td
td := td // pin variable
t.Run(td.key, func(t *testing.T) {
t.Parallel()
assert.Equal(t, td.x, env[td.key], "unexpected value")
Expand Down
5 changes: 5 additions & 0 deletions util/scripts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func TestExecutableRunner(t *testing.T) {

r := ExecRunner{}
for _, td := range data {
td := td // pin variable
t.Run(fmt.Sprintf("CanRunExecutable(%s)", td.in), func(t *testing.T) {
assert.Equal(t, td.valid, r.CanRun(td.in), "unexpected validity")

Expand Down Expand Up @@ -70,6 +71,7 @@ func TestScriptRunner(t *testing.T) {

r := ScriptRunner{DefaultInterpreters}
for _, td := range tests {
td := td // pin variable
t.Run(fmt.Sprintf("CanRunScript(%s)", td.in), func(t *testing.T) {
assert.Equal(t, td.valid, r.CanRun(td.in), "unexpected validity")
})
Expand All @@ -88,6 +90,7 @@ func TestRun(t *testing.T) {
}

for _, script := range scripts {
script := script // pin variable
t.Run(fmt.Sprintf("Run(%s)", script), func(t *testing.T) {
x := filepath.Base(script)

Expand Down Expand Up @@ -118,6 +121,7 @@ func TestNoRun(t *testing.T) {
}

for _, td := range tests {
td := td // pin variable
t.Run(fmt.Sprintf("NoRun(%s)", td.in), func(t *testing.T) {
_, err := Run(td.in, "blah")
assert.NotNil(t, err, "ran invalid script %q", td.in)
Expand Down Expand Up @@ -169,6 +173,7 @@ func TestNewScriptRunner(t *testing.T) {
}

for i, td := range data {
td := td // pin variable
t.Run(fmt.Sprintf("ScriptRunner(%d)", i), func(t *testing.T) {
r := NewScriptRunner(td.m)
var good, bad int
Expand Down

0 comments on commit 2bc7a39

Please sign in to comment.