From a5652747fe3374d5b5372224d134c98e6129e3a3 Mon Sep 17 00:00:00 2001 From: Ilya Yachin Date: Sun, 25 Oct 2020 01:38:55 +1000 Subject: [PATCH] option to warn only on zero cov --- .xcccr.toml.example | 1 + README.md | 1 + RELEASENOTES.md | 4 ++-- configurator.go | 8 ++++++++ reporter.go | 23 +++++++++++++++++++---- types.go | 1 + 6 files changed, 32 insertions(+), 6 deletions(-) diff --git a/.xcccr.toml.example b/.xcccr.toml.example index e0b6b3b..f5fca3f 100644 --- a/.xcccr.toml.example +++ b/.xcccr.toml.example @@ -4,5 +4,6 @@ FilterPaths = ["Tests","UITests","Constants.swift"] # If a path contains one o FilterPattern = "[\\S\\s]*secret\\.json$" # Go flavored regex (non-PCRE!). Please check https://golang.org/pkg/regexp/ for more info. When used in config escape characters should be escaped twice for config parsing (unfortunately). Please note that if regex is used targets filtering has no effect. InvertFilter = false # Set `false` to disallow targets and paths in filters. Set `true` to allow only targets and paths in filters. Default `false`. IncludeMasked = false # Skip recalculating coverage, just read from xccov report regardless masked (filtered) targets and paths. Default `false`. +ZeroWarnOnly = false # Don't error on 0% coverage, produce warning only. MeterLOC = false # Set `false` for coverage percent and `true` for number of LOCs e.g. 42/13 (now/was). Default `false`. Tolerance = 5 # Percent. Default 0. \ No newline at end of file diff --git a/README.md b/README.md index 32c2bca..eeba389 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ This dead simple tool only works with Apple's `xccov` generated code coverage re `-i` | Invert filter. Applied to -rg and to targets/paths filter lists in config. If no filters supplied prevents reporting allowing nothing. `-m` | Include masked files coverage in total metrics. If true warnings would be produced only for unfiltered targets/paths, but total coverage would be reported for all the files as listed in original xccov json. `-loc` | Count covered LOCs diff instead of percent coverage. + `-z` | Don't error on 0% coverage, produce warning only. `-cfg` | Path to config. Parameters passed with the flags have precedence and overrides the values stored in config. Config passed with this flag overrides default config at `.xcccr.toml`. If there aren't any config default values are used. diff --git a/RELEASENOTES.md b/RELEASENOTES.md index 02684ce..81e4445 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -1,2 +1,2 @@ -- This release only tested manually. Help wanted to cover with tests. -- Some minor bugs are expected. \ No newline at end of file +- No breaking changes. New `ZeroWarnOnly` parameter added to config. Please check examples. +- This release still only tested manually. Help wanted to cover with tests. \ No newline at end of file diff --git a/configurator.go b/configurator.go index a709d30..070b7c0 100644 --- a/configurator.go +++ b/configurator.go @@ -65,6 +65,11 @@ func prepareRunConditions() (rnCnd *RunConditions, err error) { false, "Count covered LOCs diff instead of percent coverage.", ) + zeroWarn := flag.Bool( + "z", + false, + "Don't error on 0%% coverage, produce warning only.", + ) cfgPth := flag.String( "cfg", "", @@ -103,6 +108,9 @@ func prepareRunConditions() (rnCnd *RunConditions, err error) { if *meterLOC { cfg.MeterLOC = true } + if *zeroWarn { + cfg.ZeroWarnOnly = true + } cfg.ProjectPath = *projRoot if cfg.FilterPattern != "" { re, e := regexp.Compile(cfg.FilterPattern) diff --git a/reporter.go b/reporter.go index e549afc..95b6f1d 100644 --- a/reporter.go +++ b/reporter.go @@ -63,11 +63,26 @@ func produceVerdict(cfg *Config, tu *DiffUnit, last, current *XCCoverageReport) lstCovMsg = "last coverage is unavailable" } covMsg = fmt.Sprintf("%s: %s, %s.", covMsg, curCovMsg, lstCovMsg) - if lastPct > currentPct+cfg.Tolerance || currentPct == 0 { - fmt.Printf("::error::%s\n", covMsg) - os.Exit(1) + if lastPct > currentPct+cfg.Tolerance { + emmitError(covMsg) } - fmt.Println(covMsg) + currentPctZero := currentPct == 0 + if currentPctZero && cfg.ZeroWarnOnly == false { + emmitError(covMsg) + } + if currentPctZero { + covMsg = fmt.Sprintf("::warning::%s", covMsg) + } + exitMessage(covMsg) +} + +func emmitError(message string) { + fmt.Printf("::error::%s\n", message) + os.Exit(1) +} + +func exitMessage(message string) { + fmt.Println(message) os.Exit(0) } diff --git a/types.go b/types.go index e9774b8..1fda32e 100644 --- a/types.go +++ b/types.go @@ -42,6 +42,7 @@ type Config struct { FilterPattern string InvertFilter bool IncludeMasked bool + ZeroWarnOnly bool MeterLOC bool Tolerance int }