From 8588275988e0b9d7dd11057710651f30e6f79903 Mon Sep 17 00:00:00 2001 From: Roland Schaer Date: Mon, 18 Apr 2022 05:34:18 -0500 Subject: [PATCH] Add Github Actions for linting (#13) --- .github/workflows/lint.yml | 53 ++++++++++++++++++++++++++++++++++++ .github/workflows/test.yml | 56 ++++++++++++++++++++++++++++++++++++++ .gitignore | 4 +++ Makefile | 55 +++++++++++++++++++++++++++++++++++++ output/tableoutput_test.go | 1 + 5 files changed, 169 insertions(+) create mode 100644 .github/workflows/lint.yml create mode 100644 .github/workflows/test.yml create mode 100644 Makefile diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..0570104 --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,53 @@ +name: Lint + +env: + GO_VERSION: 1.18 + +permissions: + contents: read + +on: + push: + branches: + - issue/gh-actions + # paths: + # - "**.go" + # - go.mod + # - go.sum + pull_request: + # paths: + # - "**.go" + # - go.mod + # - go.sum + +jobs: + + lint: + name: Lint + runs-on: macos-latest + steps: + - uses: actions/checkout@v3 + + - name: Install Go + uses: actions/setup-go@v3 + with: + go-version: ${{ env.GO_VERSION }} + + - name: Set Go Cache Paths + id: go-cache-paths + run: | + echo "::set-output name=go-mod::$(go env GOMODCACHE)" + + - name: Restore Go Modules Cache + uses: actions/cache@v3 + with: + path: ${{ steps.go-cache-paths.outputs.go-mod }} + key: ${{ runner.os }}-go-mod-${{ hashFiles('**/go.sum') }} + + - name: Verify modules + run: | + make verify + + - name: Lint code + run: | + make lint diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..ee53013 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,56 @@ +name: Test + +env: + GO_VERSION: 1.18 + +permissions: + contents: read + +on: + push: + branches: + - issue/gh-actions +# on: [push, pull_request] + +jobs: + + test: + name: Test + runs-on: macos-latest + steps: + - uses: actions/checkout@v3 + + - name: Install Go + uses: actions/setup-go@v3 + with: + go-version: ${{ env.GO_VERSION }} + + - name: Set Go Cache Paths + id: go-cache-paths + run: | + echo "::set-output name=go-build::$(go env GOCACHE)" + echo "::set-output name=go-mod::$(go env GOMODCACHE)" + + - name: Restore Go Cache + uses: actions/cache@v3 + with: + path: ${{ steps.go-cache-paths.outputs.go-build }} + key: ${{ runner.os }}-go-build-${{ hashFiles('**/go.sum') }} + + - name: Restore Go Modules Cache + uses: actions/cache@v3 + with: + path: ${{ steps.go-cache-paths.outputs.go-mod }} + key: ${{ runner.os }}-go-mod-${{ hashFiles('**/go.sum') }} + + - name: Verify modules + run: | + make verify + + - name: Run Tests + run: | + make test + + - name: Build binary + run: | + make build diff --git a/.gitignore b/.gitignore index 7b84f69..91a0941 100644 --- a/.gitignore +++ b/.gitignore @@ -15,6 +15,10 @@ vendor/ # Output of the go coverage tool, specifically when used with LiteIDE *.out +# Unit tests and coverage reports +profile.cov +report.xml + ### Intellij ### # User-specific stuff: .idea/ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..32c219d --- /dev/null +++ b/Makefile @@ -0,0 +1,55 @@ +GREEN := $(shell tput -Txterm setaf 2) +YELLOW := $(shell tput -Txterm setaf 3) +WHITE := $(shell tput -Txterm setaf 7) +CYAN := $(shell tput -Txterm setaf 6) +RESET := $(shell tput -Txterm sgr0) + +GOTESTSUM_VERSION := v1.7.0 +STATICCHECK_VERSION := 2022.1 + +# Suppress CGO compiler warnings +export CGO_CFLAGS=-w + +.PHONY: all build test + +all: help + +## Targets: + +build: ## Build your project and put the output binary in out/bin/ + @mkdir -p out/bin + @go build -o out/bin/ . + +clean: ## Clean up task related files + @rm -fr ./out + @rm -f ./report.xml ./profile.cov + +verify: ## Verify dependencies + @go mod verify + @go mod download + +lint: ## Run linters + @go install honnef.co/go/tools/cmd/staticcheck@${STATICCHECK_VERSION} + + @go mod tidy + @git diff --exit-code go.mod + + @staticcheck ./... + +test: ## Run tests + @go install gotest.tools/gotestsum@${GOTESTSUM_VERSION} + @gotestsum -- -coverprofile=profile.cov ./... + +coverage: test ## Generate coverage report + @go tool cover -html=profile.cov + +help: ## Show this help + @echo '' + @echo 'Usage:' + @echo ' ${YELLOW}make${RESET} ${GREEN}${RESET}' + @echo '' + @echo 'Targets:' + @awk 'BEGIN {FS = ":.*?## "} { \ + if (/^[a-zA-Z_-]+:.*?##.*$$/) {printf " ${YELLOW}%-20s${GREEN}%s${RESET}\n", $$1, $$2} \ + else if (/^## .*$$/) {printf " ${CYAN}%s${RESET}\n", substr($$1,4)} \ + }' $(MAKEFILE_LIST) diff --git a/output/tableoutput_test.go b/output/tableoutput_test.go index f538b72..15c4072 100644 --- a/output/tableoutput_test.go +++ b/output/tableoutput_test.go @@ -35,6 +35,7 @@ var asciiTpl = `+-------------------------------------+ ` +//lint:ignore ST1018 stick to unicode characters for test output var tableTpl = `%s  DESCRIPTION  KEY  VALUE  TYPE   sensor  key  value  type