From 087f0991b580fe1643ef06985719a510bf7d4bf7 Mon Sep 17 00:00:00 2001 From: gkampitakis Date: Sun, 15 Jan 2023 15:36:54 +0000 Subject: [PATCH] Initial Commit --- .gitignore | 18 ++++++++++++++++++ LICENSE | 21 +++++++++++++++++++++ Makefile | 22 ++++++++++++++++++++++ README.md | 0 coverage.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ go.mod | 3 +++ golangci.yml | 21 +++++++++++++++++++++ 7 files changed, 130 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 README.md create mode 100644 coverage.go create mode 100644 go.mod create mode 100644 golangci.yml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1dd333b --- /dev/null +++ b/.gitignore @@ -0,0 +1,18 @@ +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +vendor/ + +**/.DS_Store +.vscode diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..8946f45 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022 Georgios Kampitakis + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..89774f8 --- /dev/null +++ b/Makefile @@ -0,0 +1,22 @@ +.PHONY: install-tools lint test test-verbose format help + +help: + @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' + +install-tools: ## Install linting tools + go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.49.0 + go install mvdan.cc/gofumpt@latest + go install github.com/segmentio/golines@latest + +lint: ## Run golangci linter + golangci-lint run -c ./golangci.yml ./... + +format: ## Format code + gofumpt -l -w -extra . + golines . -w + +test: ## Run tests + go test -race -count=1 -cover ./... + +test-verbose: ## Run tests with verbose output + go test -race -count=1 -v -cover ./... diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/coverage.go b/coverage.go new file mode 100644 index 0000000..35d28ba --- /dev/null +++ b/coverage.go @@ -0,0 +1,45 @@ +package coverage + +import ( + "fmt" + "os" + "runtime" + "strings" + "testing" +) + +func Run(t *testing.M, c float64, callbacks ...func(t *testing.M)) { + var code int + defer func() { + os.Exit(code) + }() + + code = t.Run() + + if testing.CoverMode() == "" { + fmt.Printf("\nFAIL coverage is not enabled. You can enable by using `-cover`\n\n") + code = 1 + } else { + coverage := testing.Coverage() + if coverage*100 < c { + code = 1 + fmt.Printf("\nFAIL Coverage threshold not met %.1f >= %.1f for %s\n\n", c, coverage*100, packageName()) + } + } + + for _, callback := range callbacks { + callback(t) + } +} + +func packageName() string { + pc, _, _, _ := runtime.Caller(2) + funcName := runtime.FuncForPC(pc).Name() + lastSlash := strings.LastIndexByte(funcName, '/') + if lastSlash < 0 { + lastSlash = 0 + } + lastDot := strings.LastIndexByte(funcName[lastSlash:], '.') + lastSlash + + return funcName[:lastDot] +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..2b6f724 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module coverage + +go 1.19 diff --git a/golangci.yml b/golangci.yml new file mode 100644 index 0000000..6ec406f --- /dev/null +++ b/golangci.yml @@ -0,0 +1,21 @@ +linters: + enable: + - unconvert + - stylecheck + - lll + - unparam + - goimports + - unparam + - gofumpt + - revive + - unconvert + - gocognit + + disable: + - errcheck + +linters-settings: + lll: + line-length: 130 + gofumpt: + extra-rules: true