Skip to content

Commit

Permalink
Use standardized check.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
ifosch committed Oct 15, 2023
1 parent bb3f958 commit e23b251
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 153 deletions.
157 changes: 4 additions & 153 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
on: [pull_request]

env:
GO_VERSION: '^1.21.2'
GO_VERSION: '^1.21'

jobs:
test:
Expand All @@ -12,163 +12,14 @@ jobs:
uses: actions/setup-go@v2
with:
go-version: ${{ env.GO_VERSION }}
-
name: Prepare coverage
run: |
go install github.com/jandelgado/gcov2lcov@latest
-
name: Checkout
uses: actions/checkout@v2
-
name: Prepare coverage report
env:
CC_TEST_REPORTER_ID: ${{ secrets.CODECLIMATE_TOKEN }}
CC_TR_URL: "https://codeclimate.com/downloads/test-reporter"
CC_TR_BIN: "test-reporter-latest-linux-amd64"
run: |
mkdir -p coverage && \
curl -L ${CC_TR_URL}/${CC_TR_BIN} > ./cc-test-reporter && \
chmod +x ./cc-test-reporter && \
./cc-test-reporter before-build
-
name: Run tests
run: |
go test -v ./... -race -coverprofile=coverage/gcov.out -covermode=atomic && \
gcov2lcov -infile=coverage/gcov.out -outfile=coverage/lcov.info
-
name: Push coverage
env:
CC_TEST_REPORTER_ID: ${{ secrets.CODECLIMATE_TOKEN }}
run: |
./cc-test-reporter after-build -t lcov
check-formatting:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v2
-
name: Check formatting
run: |
gofmt -s -e -d -l . | tee /tmp/gofmt.output && [ $(cat /tmp/gofmt.output | wc -l) -eq 0 ]
check-smells:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v2
-
name: Install Go
uses: actions/setup-go@v2
with:
go-version: ${{ env.GO_VERSION }}
-
name: Check code smells
run: |
go vet ./...
check-complexity:
runs-on: ubuntu-latest
steps:
-
name: Install Go
uses: actions/setup-go@v2
with:
go-version: ${{ env.GO_VERSION }}
-
name: Install gocyclo
run: |
go install github.com/fzipp/gocyclo/cmd/gocyclo@latest
-
name: Checkout
uses: actions/checkout@v2
-
name: Check cyclomatic complexity
run: |
gocyclo -over 15 .
check-style:
runs-on: ubuntu-latest
steps:
-
name: Install Go
uses: actions/setup-go@v2
with:
go-version: ${{ env.GO_VERSION }}
-
name: Install golint
run: |
go install golang.org/x/lint/golint@latest
-
name: Checkout
uses: actions/checkout@v2
-
name: Check Style
run: |
golint ./...
check-ineffectual-assignments:
runs-on: ubuntu-latest
steps:
-
name: Install Go
uses: actions/setup-go@v2
with:
go-version: ${{ env.GO_VERSION }}
-
name: Install ineffassign
run: |
go install github.com/gordonklaus/ineffassign@latest
-
name: Checkout
uses: actions/checkout@v2
-
name: Download dependencies to local
run : |
go mod download
-
name: Check ineffectual assignments
run: |
ineffassign ./...
check-spelling:
runs-on: ubuntu-latest
steps:
-
name: Install Go
uses: actions/setup-go@v2
with:
go-version: ${{ env.GO_VERSION }}
-
name: Install spellchecker
run: |
go install github.com/client9/misspell/cmd/misspell@latest
-
name: Checkout
uses: actions/checkout@v2
-
name: Check spelling
run: |
misspell -error .
staticcheck:
runs-on: ubuntu-latest
steps:
-
name: Install Go
uses: actions/setup-go@v2
with:
go-version: ${{ env.GO_VERSION }}
-
name: Install staticcheck
run: |
go install honnef.co/go/tools/cmd/staticcheck@latest
-
name: Checkout
uses: actions/checkout@v2
-
name: Download dependencies to local
run : |
go mod download
-
name: Run staticcheck
run: |
staticcheck ./...
ls -la
ls -la ./scripts
scripts/check.sh all
check-license:
runs-on: ubuntu-latest
steps:
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/coverage/
/cc-test-reporter
118 changes: 118 additions & 0 deletions scripts/check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#!/bin/env -S bash -l

set -eu

is_codeclimate_setup() {
set +u
if [ -z "${CC_TEST_REPORTER_ID}" ]; then
set -u
return 1
fi
set -u
return 0
}

codeclimate_precheck() {
# This function requires CC_TEST_REPORTER_ID env var, containing
# CodeClimate Token, to be present in the environment
if is_codeclimate_setup; then
echo "*** CodeClimate is setup"
CC_TEST_REPORTER_URL=https://codeclimate.com/downloads/test-reporter/
CC_TEST_REPORTER_BIN=test-reporter-latest-linux-amd64
curl -L ${CC_TEST_REPORTER_URL}/${CC_TEST_REPORTER_BIN} > ./cc-test-reporter
chmod +x ./cc-test-reporter
./cc-test-reporter before-build
else
echo "*** CodeClimate is not setup"
fi
}

convert_coverage_report_gcov2lcov() {
go install github.com/jandelgado/gcov2lcov@latest
gcov2lcov -infile=${1} -outfile=${2}
}

tests_check() {
echo "*** Run unit tests"
EXTRA_TEST_PARAMS=""
if is_codeclimate_setup; then
mkdir -p coverage
EXTRA_TEST_PARAMS="${EXTRA_TEST_PARAMS}-coverprofile=coverage/gcov.out "
fi
go test -v ./... -race -covermode=atomic ${EXTRA_TEST_PARAMS}
if is_codeclimate_setup; then
convert_coverage_report_gcov2lcov coverage/gcov.out coverage/lcov.info
./cc-test-reporter after-build -t lcov
fi
}

complexity_check() {
echo "*** Complexity check"
go install github.com/fzipp/gocyclo/cmd/gocyclo@latest
gocyclo -avg -top 5 -over 15 .
}

format_check() {
echo "*** Format check"
gofmt -s -e -d -l . | tee /tmp/gofmt.output && [ $(cat /tmp/gofmt.output | wc -l) -eq 0 ]
}

inefficiencies_check() {
echo "*** Inefficiencies check"
go install github.com/gordonklaus/ineffassign@latest
go mod tidy
ineffassign ./...
}

smells_check() {
echo "*** Smells check"
go mod tidy
go vet ./...
}

spelling_check() {
echo "*** Spelling check"
go install github.com/client9/misspell/cmd/misspell@latest
misspell -error .
}

static_check() {
echo "*** Static check"
go install honnef.co/go/tools/cmd/staticcheck@latest
go mod download
staticcheck ./...
}

style_check() {
echo "*** Style check"
go install golang.org/x/lint/golint@latest
golint ./...
}

find_functions() {
HOOK=${1:-check}

declare -F | awk '{print $3}' | grep -E "_${HOOK}$" | sed -e 's/_${HOOK}$//'
}

try() {
if find_functions "check" | grep -w ${1} >/dev/null; then
${1} && echo "=== OK!" || (echo "=== NOK!" && return -1)
else
echo No ${1} available
return 255
fi
}

if [ "${1}" == "all" ]; then
failure=0
set +e
codeclimate_precheck
for check in $(find_functions "check"); do
try ${check} || failure=1
done
set -e
exit ${failure}
else
try ${1}
fi

0 comments on commit e23b251

Please sign in to comment.