-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds support for unit tests, golangci-lint and gosec
- Loading branch information
1 parent
0e96026
commit 0922fc4
Showing
3 changed files
with
83 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
run: | ||
skip-dirs: | ||
- vendor | ||
|
||
skip-files: | ||
- ".*\\_test.go$" | ||
|
||
linters: | ||
# refer https://golangci-lint.run/usage/linters/ | ||
disable-all: true | ||
enable: | ||
# errcheck is a program for checking for unchecked errors in Go code. These unchecked errors can be critical bugs in some cases | ||
- errcheck | ||
# checks whether code was gofmt-ed. By default this tool runs with -s option to check for code simplification | ||
- gofmt | ||
# Linter for Go source code that specializes in simplifying code | ||
- gosimple | ||
# Vet examines Go source code and reports suspicious constructs, such as Printf calls whose arguments do not align with the format string | ||
- govet | ||
# Detects when assignments to existing variables are not used | ||
- ineffassign | ||
# Checks for misuse of Sprintf to construct a host with port in a URL. | ||
- nosprintfhostport | ||
- staticcheck | ||
# Like the front-end of a Go compiler, parses and type-checks Go code | ||
- typecheck | ||
# Checks Go code for unused constants, variables, functions and types | ||
- unused |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,53 @@ | ||
.PHONY: fmt | ||
fmt: ## Run go fmt against code. | ||
go fmt ./... | ||
GO_VERSION ?= 1.20.2 | ||
GOLANG_CI_VER ?= v1.52 | ||
GOSEC_VER ?= 2.15.0 | ||
TEST_COVERAGE_FILE=lcov.info | ||
TEST_COVERAGE_HTML_FILE=coverage_unit.html | ||
TEST_COVERAGE_FUNC_FILE=func_coverage.out | ||
|
||
.PHONY: vet | ||
vet: ## Run go vet against code. | ||
go vet ./... | ||
# CONTAINER_RUNNABLE checks if tests and lint check can be run inside container. | ||
PODMAN ?= $(shell podman -v > /dev/null 2>&1; echo $$?) | ||
ifeq ($(PODMAN), 0) | ||
CONTAINER_RUNTIME=podman | ||
else | ||
CONTAINER_RUNTIME=docker | ||
endif | ||
CONTAINER_RUNNABLE ?= $(shell $(CONTAINER_RUNTIME) -v > /dev/null 2>&1; echo $$?) | ||
|
||
.PHONY: test | ||
test: fmt vet ## Run tests. | ||
go test ./... -coverprofile cover.out | ||
.PHONY: unit_clean | ||
unit_clean: ## clean up the unit test artifacts created | ||
ifeq ($(CONTAINER_RUNNABLE), 0) | ||
$(CONTAINER_RUNTIME) system prune -f | ||
endif | ||
rm ${TEST_COVERAGE_FILE} ${TEST_COVERAGE_HTML_FILE} ${TEST_COVERAGE_FUNC_FILE} > /dev/null 2>&1 | ||
|
||
.PHONY: unit | ||
unit: ## Run unit tests against code. | ||
ifeq ($(CONTAINER_RUNNABLE), 0) | ||
$(CONTAINER_RUNTIME) run -it -v ${PWD}:/go/src -w /go/src docker.io/library/golang:${GO_VERSION}-alpine3.17 \ | ||
/bin/sh -c "go test ./... -v -coverprofile ${TEST_COVERAGE_FILE}; \ | ||
go tool cover -html=${TEST_COVERAGE_FILE} -o ${TEST_COVERAGE_HTML_FILE}; \ | ||
go tool cover -func=${TEST_COVERAGE_FILE} -o ${TEST_COVERAGE_FUNC_FILE}" | ||
else | ||
go test ./... -v -coverprofile ${TEST_COVERAGE_FILE} | ||
go tool cover -html=${TEST_COVERAGE_FILE} -o ${TEST_COVERAGE_HTML_FILE} | ||
go tool cover -func=${TEST_COVERAGE_FILE} -o ${TEST_COVERAGE_FUNC_FILE} | ||
endif | ||
|
||
# Install link at https://golangci-lint.run/usage/install/ if not running inside a container | ||
.PHONY: lint | ||
lint: ## Run lint against code. | ||
ifeq ($(CONTAINER_RUNNABLE), 0) | ||
$(CONTAINER_RUNTIME) run -it -v ${PWD}:/go/src -w /go/src docker.io/golangci/golangci-lint:${GOLANG_CI_VER}-alpine golangci-lint run ./... -v | ||
else | ||
golangci-lint run ./... -v | ||
endif | ||
|
||
# Install link at https://github.com/securego/gosec#install if not running inside a container | ||
.PHONY: gosec | ||
gosec: ## inspects source code for security problem by scanning the Go Abstract Syntax Tree | ||
ifeq ($(CONTAINER_RUNNABLE), 0) | ||
$(CONTAINER_RUNTIME) run -it -v ${PWD}:/go/src -w /go/src docker.io/securego/gosec:${GOSEC_VER} ./... | ||
else | ||
gosec ./... | ||
endif |