diff --git a/Makefile b/Makefile index 9a61cd1..d205cf6 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ quality: test: go test -coverprofile=coverage - go run main.go -coverage $$(go tool cover -func=coverage | grep total | cut -d$$'\t' -f6 | cut -d'.' -f1) > coverage.svg + go run main.go $$(go tool cover -func=coverage | grep total | cut -d$$'\t' -f5 | cut -d'.' -f1) > coverage.svg build: GOOS=darwin GOARCH=386 go build -o covbadger-darwin diff --git a/README.md b/README.md index 0f13df9..8d568ae 100644 --- a/README.md +++ b/README.md @@ -2,27 +2,14 @@ ![Coverage](https://rawgit.com/imsky/covbadger/master/coverage.svg) -`covbadger` generates code coverage badges from Cobertura-compatible XML coverage reports. +`covbadger` generates code coverage badges. -## Compatibility +## Usage -Several "standard" coverage libraries for various languages have Cobertura output built in: - -* JavaScript supports Cobertura with [istanbul](https://istanbul.js.org/) -* Python supports Cobertura with [nosetest](http://nose.readthedocs.io/en/latest/) and [pytest](https://docs.pytest.org/en/latest/) -* Scala supports Cobertura with [scoverage](https://github.com/scoverage/scalac-scoverage-plugin) - -There are also several coverage format conversion tools: - -* Go has [gocov-xml](https://github.com/AlekSi/gocov-xml) -* Erlang has [covertool](https://github.com/idubrov/covertool) -* .NET has [OpenCoverToCoberturaConverter](https://github.com/danielpalme/OpenCoverToCoberturaConverter) -* JaCoCo has [cover2cover](https://github.com/rix0rrr/cover2cover) -* Finally, LCOV reports can be converted using [lcov-to-cobertura-xml](https://github.com/eriwen/lcov-to-cobertura-xml) - -## Roadmap - -* Support for diffing an existing badge to get coverage delta +``` +# generate a badge that shows 97% coverage +covbadger 97 +``` ## License diff --git a/main.go b/main.go index 9b2ced7..5bdff2d 100644 --- a/main.go +++ b/main.go @@ -2,20 +2,13 @@ package main import ( "bytes" - "encoding/xml" "errors" "flag" "fmt" - "io/ioutil" - "math" + "strconv" "text/template" ) -//CoverageReport represents an individual coverage report -type CoverageReport struct { - LineRate float64 `xml:"line-rate,attr"` -} - //Badge represents a coverage badge type Badge struct { Coverage int @@ -51,7 +44,7 @@ var _badgeTemplate string = ` 100 { - return "", errors.New("Invalid coverage: " + string(coverage)) + return "", errors.New("Invalid coverage: " + strconv.Itoa(coverage)) } var buffer bytes.Buffer @@ -73,51 +66,27 @@ func RenderBadge(coverage int) (string, error) { return buffer.String(), nil } -func GetCoverageFromReports(files []string) int { - var sum float64 = 0 - reports := make([]CoverageReport, 0, len(files)) - - for _, fileName := range files { - var report CoverageReport - - in, err := ioutil.ReadFile(fileName) - - if err != nil { - panic(err) - } - - xml.Unmarshal(in, &report) - sum += report.LineRate - reports = append(reports, report) - } - - return int(math.Floor(sum / float64(len(reports)) * 100)) -} - -func Run(files []string, coverage int) { - var badge string - - if len(files) == 0 && coverage == 0 { +func Run(args []string) { + if len(args) != 1 { flag.Usage() return - } else if len(files) > 0 { - badge, _ = RenderBadge(GetCoverageFromReports(files)) - } else if coverage > 0 { - badge, _ = RenderBadge(coverage) } - fmt.Println(badge) + coverage, _ := strconv.Atoi(args[0]) + badge, err := RenderBadge(coverage) + + if err != nil { + panic(err) + } else { + fmt.Println(badge) + } } func main() { - var coverageFlag int - - flag.IntVar(&coverageFlag, "coverage", 0, "custom coverage value") - flag.Parse() flag.Usage = func() { - fmt.Println(`Usage: covbadger [files]`) - flag.PrintDefaults() + fmt.Println(`Usage: covbadger [coverage]`) } - Run(flag.Args(), coverageFlag) + flag.Parse() + Run(flag.Args()) } diff --git a/main_test.go b/main_test.go index 76cc26d..1aeedb1 100644 --- a/main_test.go +++ b/main_test.go @@ -24,23 +24,6 @@ var expected string = ` ` -func TestGetCoverageFromReports(t *testing.T) { - coverage := GetCoverageFromReports([]string{"test-report.xml"}) - - if coverage != 90 { - t.Errorf("Coverage is %v, expected 90", coverage) - } - - defer func() { - if r := recover(); r == nil { - t.Errorf("Bad reports did not cause errors") - } - }() - - badFiles := []string{"xxx.go"} - GetCoverageFromReports(badFiles) -} - func TestRenderBadge(t *testing.T) { var err error badge, _ := RenderBadge(90) @@ -88,7 +71,13 @@ func TestRenderBadge(t *testing.T) { } func TestCovbadger(t *testing.T) { - Run([]string{"test-report.xml"}, 0) - Run([]string{}, 99) + defer func() { + if r := recover(); r == nil { + t.Errorf("Invalid coverage value did not throw an error") + } + }() + main() + Run([]string{"99"}) + Run([]string{"-99"}) }