diff --git a/Gopkg.lock b/Gopkg.lock deleted file mode 100644 index d78a74b..0000000 --- a/Gopkg.lock +++ /dev/null @@ -1,80 +0,0 @@ -# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. - - -[[projects]] - digest = "1:4bb94bb2d837b5c7489d9e5e1fcffbc81fa1cb43024cbb4fe827787378f01e3b" - name = "github.com/fatih/color" - packages = ["."] - pruneopts = "UT" - revision = "507f6050b8568533fb3f5504de8e5205fa62a114" - version = "v1.6.0" - -[[projects]] - digest = "1:d45d0ae9aa93470f49280bcf0953419e867f8f26a6ec3218aafc31fb427d5bd0" - name = "github.com/fogleman/gg" - packages = ["."] - pruneopts = "UT" - revision = "6166aa3c1afaee416f384645a81636267aee6d25" - version = "v1.0.0" - -[[projects]] - branch = "master" - digest = "1:62c57507df491b657e9ae2645f30958d9964c8eeb380600469eedc951ebb3a0e" - name = "github.com/golang/freetype" - packages = [ - "raster", - "truetype", - ] - pruneopts = "UT" - revision = "e2365dfdc4a05e4b8299a783240d4a7d5a65d4e4" - -[[projects]] - digest = "1:c658e84ad3916da105a761660dcaeb01e63416c8ec7bc62256a9b411a05fcd67" - name = "github.com/mattn/go-colorable" - packages = ["."] - pruneopts = "UT" - revision = "167de6bfdfba052fa6b2d3664c8f5272e23c9072" - version = "v0.0.9" - -[[projects]] - digest = "1:d4d17353dbd05cb52a2a52b7fe1771883b682806f68db442b436294926bbfafb" - name = "github.com/mattn/go-isatty" - packages = ["."] - pruneopts = "UT" - revision = "0360b2af4f38e8d38c7fce2a9f4e702702d73a39" - version = "v0.0.3" - -[[projects]] - branch = "master" - digest = "1:53e6f54885d63f2c9afc898f10481d28295471dee95bb7faa74cee8a1df85e56" - name = "golang.org/x/image" - packages = [ - "font", - "font/basicfont", - "font/gofont/goregular", - "font/plan9font", - "math/fixed", - ] - pruneopts = "UT" - revision = "f3a9b89b59def9194717c1d0bd4c0d08fa1afa7b" - -[[projects]] - branch = "master" - digest = "1:bcdbc1c6f88196580500afc96255fb81b89e996ea43917c4973675c567f825ae" - name = "golang.org/x/sys" - packages = ["unix"] - pruneopts = "UT" - revision = "2f1e207ee39ff70f3433e49c6eb52677a515e3b5" - -[solve-meta] - analyzer-name = "dep" - analyzer-version = 1 - input-imports = [ - "github.com/fatih/color", - "github.com/fogleman/gg", - "github.com/golang/freetype/truetype", - "golang.org/x/image/font", - "golang.org/x/image/font/gofont/goregular", - ] - solver-name = "gps-cdcl" - solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml deleted file mode 100644 index cd2648c..0000000 --- a/Gopkg.toml +++ /dev/null @@ -1,38 +0,0 @@ -# Gopkg.toml example -# -# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md -# for detailed Gopkg.toml documentation. -# -# required = ["github.com/user/thing/cmd/thing"] -# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"] -# -# [[constraint]] -# name = "github.com/user/project" -# version = "1.0.0" -# -# [[constraint]] -# name = "github.com/user/project2" -# branch = "dev" -# source = "github.com/myfork/project2" -# -# [[override]] -# name = "github.com/x/y" -# version = "2.4.0" -# -# [prune] -# non-go = false -# go-tests = true -# unused-packages = true - - -[[constraint]] - name = "github.com/fatih/color" - version = "1.6.0" - -[[constraint]] - name = "github.com/fogleman/gg" - version = "1.0.0" - -[prune] - go-tests = true - unused-packages = true diff --git a/Makefile b/Makefile index 6118732..6b4d2d3 100644 --- a/Makefile +++ b/Makefile @@ -1,16 +1,29 @@ +.PHONY: build run test cover coverage configure dep + +all: fmt configure build cover run test + build: go build -run: - make build && ./gopherbadger -md="README.md" + +run: build + ./gopherbadger -md="README.md" + test: go test -v + cover: - go test ./... -coverprofile=coverage.out && go tool cover -html=coverage.out -o=coverage.html -coverage: - make cover + go test ./... -coverprofile=coverage.out + go tool cover -html=coverage.out -o=coverage.html + +coverage: cover + configure: - make dep -dep: - if ! [ -x "$(command -v dep)" ]; then\ - go get github.com/golang/dep/cmd/dep;\ - fi && dep ensure; + go mod download -x + go mod verify + go mod tidy -v + +dep: configure + +fmt: + go fmt ./... + go mod edit -fmt diff --git a/README.md b/README.md index bda5df5..cd4bfb8 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # GopherBadger ### Generate coverage badge images using Go! -![gopherbadger-tag-do-not-edit](https://img.shields.io/badge/Go%20Coverage-72%25-brightgreen.svg?longCache=true&style=flat) +![gopherbadger-tag-do-not-edit](https://img.shields.io/badge/Go%20Coverage-70%25-brightgreen.svg?longCache=true&style=flat) One day, I noticed that there was no easy way to generate coverage badges for my Golang projects. So I made one (see above)! diff --git a/coverage_badge.png b/coverage_badge.png index a7d2fcd..5e3661a 100644 Binary files a/coverage_badge.png and b/coverage_badge.png differ diff --git a/coverbadge/coverbadge.go b/coverbadge/coverbadge.go index bad8255..10d9661 100644 --- a/coverbadge/coverbadge.go +++ b/coverbadge/coverbadge.go @@ -31,7 +31,11 @@ func (badge Badge) DownloadBadge(filepath string, coverageFloat float64) { logging.Fatal("Creating file", err) return } - defer out.Close() + defer func() { + if cerr := out.Close(); cerr != nil { + logging.Error("Closing output file", cerr) + } + }() // Get the data resp, err := http.Get(badge.generateBadgeBadgeURL(coverageFloat)) @@ -39,7 +43,11 @@ func (badge Badge) DownloadBadge(filepath string, coverageFloat float64) { logging.Fatal("Fetching badge image", err) return } - defer resp.Body.Close() + defer func() { + if bcerr := resp.Body.Close(); bcerr != nil { + logging.Error("closing response body", bcerr) + } + }() // Write the body to file _, err = io.Copy(out, resp.Body) @@ -47,8 +55,6 @@ func (badge Badge) DownloadBadge(filepath string, coverageFloat float64) { logging.Fatal("Writing file to disk", err) return } - - return } func (badge Badge) WriteBadgeToMd(filepath string, coverageFloat float64, isSilent bool) { diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..d6b82cc --- /dev/null +++ b/go.mod @@ -0,0 +1,16 @@ +module github.com/jpoles1/gopherbadger + +go 1.17 + +require ( + github.com/fatih/color v1.6.0 + github.com/fogleman/gg v1.0.0 + github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 + golang.org/x/image v0.0.0-20180314180248-f3a9b89b59de +) + +require ( + github.com/mattn/go-colorable v0.0.9 // indirect + github.com/mattn/go-isatty v0.0.3 // indirect + golang.org/x/sys v0.0.0-20180316202216-2f1e207ee39f // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..160cb15 --- /dev/null +++ b/go.sum @@ -0,0 +1,14 @@ +github.com/fatih/color v1.6.0 h1:66qjqZk8kalYAvDRtM1AdAJQI0tj4Wrue3Eq3B3pmFU= +github.com/fatih/color v1.6.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= +github.com/fogleman/gg v1.0.0 h1:O2ToZn8ijCP2gXhVY701P1b1jrxKoVPh6CkaX2/PACE= +github.com/fogleman/gg v1.0.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k= +github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g= +github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k= +github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4= +github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= +github.com/mattn/go-isatty v0.0.3 h1:ns/ykhmWi7G9O+8a448SecJU3nSMBXJfqQkl0upE1jI= +github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= +golang.org/x/image v0.0.0-20180314180248-f3a9b89b59de h1:moc8EjTGZXlnKJcoDZDWCDV1Vn3Zt/MZDpIRmIs7qt0= +golang.org/x/image v0.0.0-20180314180248-f3a9b89b59de/go.mod h1:ux5Hcp/YLpHSI86hEcLt0YII63i6oz57MZXIpbrjZUs= +golang.org/x/sys v0.0.0-20180316202216-2f1e207ee39f h1:KjX81lL92yfj4utcAnxT/sgJt8XTuOkDN7Rr3HJmmZ8= +golang.org/x/sys v0.0.0-20180316202216-2f1e207ee39f/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= diff --git a/main.go b/main.go index 370755d..4727712 100644 --- a/main.go +++ b/main.go @@ -31,7 +31,7 @@ func getCommandOutput(commandString string, isSilent bool) chan float64 { reader := bufio.NewReader(stdout) coverageFloatChannel := make(chan float64) go func(reader io.Reader) { - re := regexp.MustCompile("total:\\s*\\(statements\\)?\\s*(\\d+\\.?\\d*)\\s*\\%") + re := regexp.MustCompile(`total:\s*\(statements\)?\s*(\d+\.?\d*)\s*%`) scanner := bufio.NewScanner(reader) for scanner.Scan() { lineText := scanner.Text() diff --git a/main_test.go b/main_test.go index d94d7a8..3fe52aa 100644 --- a/main_test.go +++ b/main_test.go @@ -25,13 +25,22 @@ func TestBadger(t *testing.T) { } func TestDrawBadge(t *testing.T) { - drawBadge(22.7, "test_badge.png") - drawBadge(88, "test_badge.png") - drawBadge(66, "test_badge.png") - if drawBadge(66, "bad_folder/test_badge.png") == nil { - t.Error("Should respond with error when saving to invalid folder") + // Test success conditions + if err := drawBadge(22.7, "test_badge.png"); err != nil { + t.Errorf("error drawing 22.7%% coverage: %s", err.Error()) } - if drawBadge(-34, "test_badge.png") == nil { - t.Error("Should respond with error when coverage is less than 0") + if err := drawBadge(88, "test_badge.png"); err != nil { + t.Errorf("error drawing 88%% coverage: %s", err.Error()) + } + if err := drawBadge(66, "test_badge.png"); err != nil { + t.Errorf("error drawing 66%% coverage: %s", err.Error()) + } + + // Test failure conditions + if err := drawBadge(66, "bad_folder/test_badge.png"); err == nil { + t.Errorf("should respond with error when saving to invalid folder") + } + if err := drawBadge(-34, "test_badge.png"); err == nil { + t.Errorf("should respond with error when coverage is less than 0") } }