Skip to content

Commit

Permalink
Merge pull request #8 from mcubik/issue/5-configured-metric-ignored
Browse files Browse the repository at this point in the history
Fix issue #5: Configured metric ignored
  • Loading branch information
mcubik committed Nov 6, 2018
2 parents 8fd1d7b + fc6f568 commit ec083da
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 18 deletions.
36 changes: 25 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"errors"
"flag"
"fmt"
"github.com/mcubik/goverreport/report"
Expand All @@ -15,6 +14,7 @@ import (
type arguments struct {
coverprofile, metric, sortBy, order string
threshold float64
metricDefaulted bool
}

var args arguments
Expand All @@ -36,12 +36,22 @@ func init() {
flag.StringVar(&args.order, "order", "asc", "Sort order: asc, desc")
flag.Float64Var(&args.threshold, "threshold", 0, "Return an error if the coverage is below a threshold")
flag.StringVar(&args.metric, "metric", "block", "Use a specific metric for the threshold: block, stmt")
args.metricDefaulted = true
}

func parseArguments() {
flag.Parse()
flag.Visit(func(f *flag.Flag) {
if f.Name == "metric" {
args.metricDefaulted = false
}
})
}

func main() {

// Parse arguments
flag.Parse()
parseArguments()
config, err := loadConfig(configFile)
if err != nil {
fmt.Println(err)
Expand All @@ -61,19 +71,25 @@ func main() {
func run(config configuration, args arguments, writer io.Writer) (bool, error) {

// Use config values if arguments aren't set
if args.metric == "" {
args.metric = config.Metric
var metric string
var threshold float64
if args.metricDefaulted && config.Metric != "" {
metric = config.Metric
} else {
metric = args.metric
}
if args.threshold == 0 {
args.threshold = config.Threshold
threshold = config.Threshold
} else {
threshold = args.threshold
}

rep, err := report.GenerateReport(args.coverprofile, config.Root, config.Exclusions, args.sortBy, args.order)
if err != nil {
return false, err
}
report.PrintTable(rep, writer)
passed, err := checkThreshold(args.threshold, rep.Total, args.metric)
passed, err := checkThreshold(threshold, rep.Total, metric)
if err != nil {
return false, err
}
Expand All @@ -82,9 +98,7 @@ func run(config configuration, args arguments, writer io.Writer) (bool, error) {

// Loads the report configuration from a yml file
func loadConfig(filename string) (configuration, error) {
conf := configuration{
Exclusions: []string{},
Metric: "block"}
conf := configuration{Exclusions: []string{}}
data, err := ioutil.ReadFile(filename)
if err != nil {
if !os.IsNotExist(err) {
Expand All @@ -99,7 +113,7 @@ func loadConfig(filename string) (configuration, error) {
}

// Checks whether the coverage is above a threshold value.
// thresholdType states which value will be used to check the threshold,
// metric states which value will be used to check the threshold,
// block coverage (block) or statement coverage (stmt).
func checkThreshold(threshold float64, total report.Summary, metric string) (bool, error) {
if threshold > 0 {
Expand All @@ -113,7 +127,7 @@ func checkThreshold(threshold float64, total report.Summary, metric string) (boo
return false, nil
}
default:
return false, errors.New("Invalid threshold type, use 'block' or 'stmt'")
return false, fmt.Errorf("Invalid metric '%s', use 'block' or 'stmt'", metric)
}
}
return true, nil
Expand Down
46 changes: 39 additions & 7 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"github.com/mcubik/goverreport/report"
"github.com/stretchr/testify/assert"
"os"
"testing"
)

Expand All @@ -18,26 +19,26 @@ func TestLoadConfiguration(t *testing.T) {
Metric: "stmt"})
}

func TestDefaultConfig(t *testing.T) {
func TestEmptyConfig(t *testing.T) {
assert := assert.New(t)
conf, err := loadConfig("emptyconfig.yml")
assert.NoError(err)
assert.Equal(conf, configuration{
Root: "",
Exclusions: []string{},
Threshold: 0,
Metric: "block"})
Metric: ""})
}

func TestDefaultConfigWhenFileMissing(t *testing.T) {
func TestEmptyConfigWhenFileMissing(t *testing.T) {
assert := assert.New(t)
conf, err := loadConfig("xxxxxx.yml")
assert.NoError(err)
assert.Equal(conf, configuration{
Root: "",
Exclusions: []string{},
Threshold: 0,
Metric: "block"})
Metric: ""})
}

func TestThreshold(t *testing.T) {
Expand Down Expand Up @@ -117,17 +118,48 @@ func TestRunFailInvalidArugment(t *testing.T) {
assert.Error(err)
}

func TestTakesConfigurationIfNotOverriden(t *testing.T) {
func TestTakesConfigurationIfNotOverridenByCommandLineArgs(t *testing.T) {
assert := assert.New(t)
config := configuration{Threshold: 80, Metric: "stmt"}
args := arguments{
coverprofile: "sample_coverage.out",
threshold: 0,
metric: "block",
sortBy: "filename",
order: "asc",
metricDefaulted: true}
buf := bytes.Buffer{}
passed, err := run(config, args, &buf)
assert.NoError(err)
assert.True(passed) // Passes stmt coverage
}

func TestCommandLineArgsOverridesConfiguration(t *testing.T) {
assert := assert.New(t)
config := configuration{Threshold: 80, Metric: "block"}
args := arguments{
coverprofile: "sample_coverage.out",
threshold: 0,
metric: "",
metric: "stmt",
sortBy: "filename",
order: "asc"}
buf := bytes.Buffer{}
passed, err := run(config, args, &buf)
assert.NoError(err)
assert.False(passed)
assert.True(passed) // Passes stmt coverage
}

func TestMetricArgument(t *testing.T) {
os.Args[1] = ""
assert := assert.New(t)
// Metric default value
parseArguments()
assert.Equal("block", args.metric)
assert.True(args.metricDefaulted)

// Metric set
os.Args[1] = "-metric=stmt"
parseArguments()
assert.Equal("stmt", args.metric)
assert.False(args.metricDefaulted)
}

0 comments on commit ec083da

Please sign in to comment.