Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Miscellaneous Improvements #21

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 0 additions & 80 deletions Gopkg.lock

This file was deleted.

38 changes: 0 additions & 38 deletions Gopkg.toml

This file was deleted.

33 changes: 23 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# GopherBadger
### Generate coverage badge images using Go!

<a href='https://github.com/jpoles1/gopherbadger' target='_blank'>![gopherbadger-tag-do-not-edit](https://img.shields.io/badge/Go%20Coverage-72%25-brightgreen.svg?longCache=true&style=flat)</a>
<a href='https://github.com/jpoles1/gopherbadger' target='_blank'>![gopherbadger-tag-do-not-edit](https://img.shields.io/badge/Go%20Coverage-70%25-brightgreen.svg?longCache=true&style=flat)</a>

One day, I noticed that there was no easy way to generate coverage badges for my Golang projects. So I made one (see above)!

Expand Down
Binary file modified coverage_badge.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 10 additions & 4 deletions coverbadge/coverbadge.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,30 @@ 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))
if err != nil {
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)
if err != nil {
logging.Fatal("Writing file to disk", err)
return
}

return
}

func (badge Badge) WriteBadgeToMd(filepath string, coverageFloat float64, isSilent bool) {
Expand Down
16 changes: 16 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
)
14 changes: 14 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
23 changes: 16 additions & 7 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}