Skip to content

Commit

Permalink
Splitting out main to allow for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan Poles committed Jun 26, 2020
1 parent 3dd0959 commit a5f8f26
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 18 deletions.
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-68%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-72%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.
59 changes: 43 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,22 @@ func containsString(slice []string, item string) bool {
return ok
}

type gopherBadgerConfig struct {
badgeOutputFlag bool
badgeStyleFlag string
updateMdFilesFlag string
coveragePrefixFlag string
coverageCommandFlag string
manualCoverageFlag float64
rootFolderFlag string
tagsFlag string
shortFlag bool
}

var badgeStyles = []string{"plastic", "flat", "flat-square", "for-the-badge", "social"}

func main() {
badgeOutputFlag := flag.Bool("png", true, "Boolean to decide if .png will be generated by the software")
badgeStyles := []string{"plastic", "flat", "flat-square", "for-the-badge", "social"}
badgeStyleFlag := flag.String("style", "flat", "Badge style from list: ["+strings.Join(badgeStyles, ",")+"]")
updateMdFilesFlag := flag.String("md", "", "A list of markdown filepaths for badge updates.")
coveragePrefixFlag := flag.String("prefix", "Go", "A prefix to specify the coverage in your badge.")
Expand All @@ -79,46 +92,60 @@ func main() {
tagsFlag := flag.String("tags", "", "The build tests you'd like to include in your coverage")
shortFlag := flag.Bool("short", false, "It will skip tests marked as testing.Short()")
flag.Parse()
config := gopherBadgerConfig{
badgeOutputFlag: *badgeOutputFlag,
badgeStyleFlag: *badgeStyleFlag,
updateMdFilesFlag: *updateMdFilesFlag,
coveragePrefixFlag: *coveragePrefixFlag,
coverageCommandFlag: *coverageCommandFlag,
manualCoverageFlag: *manualCoverageFlag,
rootFolderFlag: *rootFolderFlag,
tagsFlag: *tagsFlag,
shortFlag: *shortFlag,
}
badger(config)
}

if !containsString(badgeStyles, *badgeStyleFlag) {
func badger(config gopherBadgerConfig) {
if !containsString(badgeStyles, config.badgeStyleFlag) {
logging.Fatal("Invalid style flag! Must be a member of list: ["+strings.Join(badgeStyles, ", ")+"]", errors.New("Invalid style flag"))
}

coverageBadge := coverbadge.Badge{
CoveragePrefix: *coveragePrefixFlag,
Style: *badgeStyleFlag,
CoveragePrefix: config.coveragePrefixFlag,
Style: config.badgeStyleFlag,
ImageExtension: ".png",
}

var coverageFloat float64
coverageCommand := ""

if *coverageCommandFlag != "" {
coverageCommand = *coverageCommandFlag
if *tagsFlag != "" || *shortFlag {
if config.coverageCommandFlag != "" {
coverageCommand = config.coverageCommandFlag
if config.tagsFlag != "" || config.shortFlag {
log.Println("Warning: When the covercmd flag is used the -tags and -short flags will be ignored.")
}
} else {
flagsCommands := ""
if *tagsFlag != "" {
flagsCommands = flagsCommands + " -tags \"" + *tagsFlag + "\""
if config.tagsFlag != "" {
flagsCommands = flagsCommands + " -tags \"" + config.tagsFlag + "\""
}
if *shortFlag {
if config.shortFlag {
flagsCommands = flagsCommands + " -short"
}
coverageCommand = fmt.Sprintf("go test %s/... -coverprofile=coverage.out %s && %s", *rootFolderFlag, flagsCommands, toolCoverCommand)
coverageCommand = fmt.Sprintf("go test %s/... -coverprofile=coverage.out %s && %s", config.rootFolderFlag, flagsCommands, toolCoverCommand)
}

if *manualCoverageFlag == -1 {
if config.manualCoverageFlag == -1 {
coverageFloat = <-getCommandOutput(coverageCommand)
} else {
coverageFloat = *manualCoverageFlag
coverageFloat = config.manualCoverageFlag
}
if *badgeOutputFlag == true {
if config.badgeOutputFlag == true {
coverageBadge.DownloadBadge("coverage_badge.png", coverageFloat)
}
if *updateMdFilesFlag != "" {
for _, filepath := range strings.Split(*updateMdFilesFlag, ",") {
if config.updateMdFilesFlag != "" {
for _, filepath := range strings.Split(config.updateMdFilesFlag, ",") {
coverageBadge.WriteBadgeToMd(filepath, coverageFloat)
}
}
Expand Down
17 changes: 16 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,22 @@ import (

func TestGetCommandOutput(t *testing.T) {
getCommandOutput("echo test")
//getCommandOutput("echo 'testline\ncoverage: 35%'")
getCommandOutput("echo 'testline\ncoverage: 35%'")
}

func TestBadger(t *testing.T) {
defaultConfig := gopherBadgerConfig{
badgeOutputFlag: true,
badgeStyleFlag: "flat",
updateMdFilesFlag: "",
coveragePrefixFlag: "Go",
coverageCommandFlag: "echo 'testline\ncoverage: 35%'",
manualCoverageFlag: 90,
rootFolderFlag: ".",
tagsFlag: "",
shortFlag: false,
}
badger(defaultConfig)
}

func TestDrawBadge(t *testing.T) {
Expand Down

0 comments on commit a5f8f26

Please sign in to comment.