Skip to content
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
11 changes: 0 additions & 11 deletions .drone.yml

This file was deleted.

80 changes: 0 additions & 80 deletions Gopkg.lock

This file was deleted.

38 changes: 0 additions & 38 deletions Gopkg.toml

This file was deleted.

10 changes: 4 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
build:
go build
@go build
run:
make build && ./gopherbadger -md="README.md"
test:
go test -v
@go test -v
cover:
go test ./... -coverprofile=coverage.out && go tool cover -html=coverage.out -o=coverage.html
@go test ./... -coverprofile=coverage.out && go tool cover -html=coverage.out -o=coverage.html
coverage:
make cover
configure:
make dep
dep:
if ! [ -x "$(command -v dep)" ]; then\
go get github.com/golang/dep/cmd/dep;\
fi && dep ensure;
@go mod tidy
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.
11 changes: 6 additions & 5 deletions coverbadge/coverbadge.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package coverbadge

import (
"fmt"
"github.com/jpoles1/gopherbadger/logging"
"io"
"io/ioutil"
"math"
"net/http"
"os"
"regexp"
"strconv"

"github.com/jpoles1/gopherbadger/logging"
)

type Badge struct {
Expand All @@ -20,8 +23,8 @@ func (badge Badge) generateBadgeBadgeURL(coverageFloat float64) string {
if badge.CoveragePrefix != "" {
badge.CoveragePrefix += "%20"
}
urlTemplate := "https://img.shields.io/badge/%sCoverage-%.f%%25-brightgreen%s?longCache=true&style=%s"
return fmt.Sprintf(urlTemplate, badge.CoveragePrefix, coverageFloat, badge.ImageExtension, badge.Style)
urlTemplate := "https://img.shields.io/badge/%sCoverage-%s%%25-brightgreen%s?longCache=true&style=%s"
return fmt.Sprintf(urlTemplate, badge.CoveragePrefix, strconv.FormatFloat(math.Ceil(coverageFloat*10)/10, 'f', -1, 64), badge.ImageExtension, badge.Style)
}

func (badge Badge) DownloadBadge(filepath string, coverageFloat float64) {
Expand All @@ -47,8 +50,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) {
Expand Down
7 changes: 4 additions & 3 deletions coverbadge/coverbadge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package coverbadge
import (
"testing"
)

var coverageBadge = Badge{
CoveragePrefix: "Go",
Style: "flat",
Expand All @@ -16,13 +17,13 @@ func TestWriteBadgeToMd(t *testing.T) {
coverageBadge.WriteBadgeToMd("coverage_test.md", 22)
}
func TestGenerateBadgeBadgeURL_1(t *testing.T) {
testUrl(t, 1.1, "https://img.shields.io/badge/Go%20Coverage-1%25-brightgreen.png?longCache=true&style=flat")
testUrl(t, 1.1, "https://img.shields.io/badge/Go%20Coverage-1.1%25-brightgreen.png?longCache=true&style=flat")
}
func TestGenerateBadgeBadgeURL_10(t *testing.T) {
testUrl(t, 10.1, "https://img.shields.io/badge/Go%20Coverage-10%25-brightgreen.png?longCache=true&style=flat")
testUrl(t, 10.1, "https://img.shields.io/badge/Go%20Coverage-10.1%25-brightgreen.png?longCache=true&style=flat")
}
func TestGenerateBadgeBadgeURL_100(t *testing.T) {
testUrl(t, 100.1, "https://img.shields.io/badge/Go%20Coverage-100%25-brightgreen.png?longCache=true&style=flat")
testUrl(t, 100.0, "https://img.shields.io/badge/Go%20Coverage-100%25-brightgreen.png?longCache=true&style=flat")
}
func testUrl(t *testing.T, coverageFloat float64, expected string) {
url := coverageBadge.generateBadgeBadgeURL(coverageFloat)
Expand Down
14 changes: 14 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module github.com/jpoles1/gopherbadger

go 1.15

require (
github.com/ex-preman/gopherbadger/v3 v3.0.0 // indirect
github.com/fatih/color v1.6.0
github.com/fogleman/gg v1.0.0
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
github.com/mattn/go-colorable v0.0.9 // indirect
github.com/mattn/go-isatty v0.0.3 // indirect
golang.org/x/image v0.0.0-20180314180248-f3a9b89b59de
golang.org/x/sys v0.0.0-20180316202216-2f1e207ee39f // indirect
)
18 changes: 18 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
github.com/ex-preman/gopherbadger v1.0.0 h1:w2iPAyJ0I/xEELQJIBDaZdjEVKFWbG3uqXsCUDyRRAs=
github.com/ex-preman/gopherbadger v2.4.0+incompatible h1:udvRB6XU/F15Wf34GPMMoSnBWYFqZGf3J7DW2Cjlkg8=
github.com/ex-preman/gopherbadger/v3 v3.0.0 h1:2d/gce392J+fB4b+YY1yHvwjnxk8wff04REbp/EVDb8=
github.com/ex-preman/gopherbadger/v3 v3.0.0/go.mod h1:FCEGvQZPUmoZGgEmcvq5jSD0RrF5cYUTF/JZ5b+6/UE=
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=
25 changes: 19 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,18 @@ func getCommandOutput(commandString string) chan float64 {
if nil != err {
log.Fatalf("Error obtaining stdout: %s", err.Error())
}

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()
match := re.FindStringSubmatch(lineText)
if len(match) == 2 {
color.Green(lineText)
//fmt.Printf("Found coverage = %s%\n", match[1])
// fmt.Printf("Found coverage = %s\n", match[1])
coverageValue, err := strconv.ParseFloat(match[1], 32)
errCheck("Parsing coverage to float", err)
if err == nil {
Expand All @@ -49,10 +50,16 @@ func getCommandOutput(commandString string) chan float64 {
fmt.Println(lineText)
}
}
cmd.Wait()
if err := cmd.Wait(); err != nil {
log.Fatal(err)
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
}(reader)
if err := cmd.Run(); nil != err {
log.Fatalf("Error running program: %s, %s", cmd.Path, err)

if err := cmd.Start(); err != nil {
log.Fatal(err)
}
return coverageFloatChannel
}
Expand Down Expand Up @@ -141,7 +148,13 @@ func badger(config gopherBadgerConfig) {
} else {
coverageFloat = config.manualCoverageFlag
}
if config.badgeOutputFlag == true {

// validate coverageFloat value
if coverageFloat < float64(0) || coverageFloat > float64(100) {
logging.Fatal("Invalid percentage value! Must be a value between 0-100", errors.New("Invalid coverage value"))
}

if config.badgeOutputFlag {
coverageBadge.DownloadBadge("coverage_badge.png", coverageFloat)
}
if config.updateMdFilesFlag != "" {
Expand Down
6 changes: 3 additions & 3 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ 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")
_ = 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")
}
Expand Down