Skip to content
Merged
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
3 changes: 2 additions & 1 deletion .github/.testcoverage-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ override:
threshold: 66
exclude:
paths:
- main\.go$
- main\.go$
- main_config\.go$
3 changes: 2 additions & 1 deletion .github/.testcoverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ threshold:
total: 100
exclude:
paths:
- main\.go$
- main\.go$
- main_config\.go$
20 changes: 19 additions & 1 deletion .github/workflows/action-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,22 @@ jobs:
config: ./.github/workflows/testdata/total100.yml
threshold-file: 0
threshold-package: 0
threshold-total: 0
threshold-total: 0

## Test 6

- name: "test: debug output"
uses: ./
id: test-6
continue-on-error: true
with:
profile: unexistant-profile.out
debug: true
threshold-file: 0
threshold-package: 0
threshold-total: 100

- name: "check: test should have failed"
if: steps.test-4.outcome != 'failure'
shell: bash
run: echo "Previous step should have failed" && exit 1
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ test:
# Runs test coverage check
.PHONY: check-coverage
check-coverage: test
go run ./main.go --config=./.github/.testcoverage-local.yml
go run ./ --config=./.github/.testcoverage-local.yml

# View coverage profile
.PHONY: view-coverage
Expand Down
6 changes: 6 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ inputs:
required: false
default: ""
type: string
debug:
description: Prints additional debugging output when running action.
required: false
default: false
type: boolean

# Individual properties
profile:
Expand Down Expand Up @@ -138,6 +143,7 @@ runs:
- --config=${{ inputs.config || '''''' }}
- --profile=${{ inputs.profile || '''''' }}
- --source-dir=${{ inputs.source-dir || '''''' }}
- --debug=${{ inputs.debug }}
- --github-action-output=true
- --threshold-file=${{ inputs.threshold-file }}
- --threshold-package=${{ inputs.threshold-package }}
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/google/go-github/v56 v56.0.0
github.com/johannesboyne/gofakes3 v0.0.0-20230914150226-f005f5cc03aa
github.com/narqo/go-badge v0.0.0-20230821190521-c9a75c019a59
github.com/rs/zerolog v1.34.0
github.com/stretchr/testify v1.10.0
golang.org/x/tools v0.26.0
gopkg.in/yaml.v3 v3.0.1
Expand All @@ -22,11 +23,14 @@ require (
github.com/google/go-querystring v1.1.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/ryszard/goskiplist v0.0.0-20150312221310-2dfbae5fcf46 // indirect
github.com/shabbyrobe/gocovmerge v0.0.0-20190829150210-3e036491d500 // indirect
golang.org/x/image v0.18.0 // indirect
golang.org/x/sys v0.26.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
15 changes: 15 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ github.com/alexflint/go-scalar v1.1.0/go.mod h1:LoFvNMqS1CPrMVltza4LvnGKhaSpc3oy
github.com/aws/aws-sdk-go v1.44.256/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI=
github.com/aws/aws-sdk-go v1.49.4 h1:qiXsqEeLLhdLgUIyfr5ot+N/dGPWALmtM1SetRmbUlY=
github.com/aws/aws-sdk-go v1.49.4/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
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/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
Expand All @@ -31,6 +33,11 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/narqo/go-badge v0.0.0-20230821190521-c9a75c019a59 h1:kbREB9muGo4sHLoZJD/E/IV8yK3Y15eEA9mYi/ztRsk=
github.com/narqo/go-badge v0.0.0-20230821190521-c9a75c019a59/go.mod h1:m9BzkaxwU4IfPQi9ko23cmuFltayFe8iS0dlRlnEWiM=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
Expand All @@ -40,6 +47,9 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
github.com/rs/zerolog v1.34.0 h1:k43nTLIwcTVQAncfCw4KZ2VY6ukYoZaBPNOE8txlOeY=
github.com/rs/zerolog v1.34.0/go.mod h1:bJsvje4Z08ROH4Nhs5iH600c3IkWhwp44iRc54W6wYQ=
github.com/ryszard/goskiplist v0.0.0-20150312221310-2dfbae5fcf46 h1:GHRpF1pTW19a8tTFrMLUcfWwyC0pnifVo2ClaLq+hP8=
github.com/ryszard/goskiplist v0.0.0-20150312221310-2dfbae5fcf46/go.mod h1:uAQ5PCi+MFsC7HjREoAz1BU+Mq60+05gifQSsHSDG/8=
github.com/shabbyrobe/gocovmerge v0.0.0-20190829150210-3e036491d500 h1:WnNuhiq+FOY3jNj6JXFT+eLN3CQ/oPIsDPRanvwsmbI=
Expand Down Expand Up @@ -77,9 +87,14 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
Expand Down
203 changes: 12 additions & 191 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,217 +1,38 @@
package main

import (
"errors"
"fmt"
"os"
"strings"

"github.com/alexflint/go-arg"

"github.com/vladopajic/go-test-coverage/v2/pkg/testcoverage"
"github.com/vladopajic/go-test-coverage/v2/pkg/testcoverage/logger"
)

const (
Version = "v2.13.2" // VERSION: when changing version update version in other places
Name = "go-test-coverage"
)

const (
// default value of string variables passed by CI
ciDefaultString = `''`
// default value of int variables passed by CI
ciDefaultInt = -1
)

type args struct {
ConfigPath string `arg:"-c,--config"`
Profile string `arg:"-p,--profile" help:"path to coverage profile"`
LocalPrefix string `arg:"-l,--local-prefix"` // deprecated
SourceDir string `arg:"-s,--source-dir"`
GithubActionOutput bool `arg:"-o,--github-action-output"`
ThresholdFile int `arg:"-f,--threshold-file"`
ThresholdPackage int `arg:"-k,--threshold-package"`
ThresholdTotal int `arg:"-t,--threshold-total"`

BreakdownFileName string `arg:"--breakdown-file-name"`
DiffBaseBreakdownFileName string `arg:"--diff-base-breakdown-file-name"`

BadgeFileName string `arg:"-b,--badge-file-name"`

CDNKey string `arg:"--cdn-key"`
CDNSecret string `arg:"--cdn-secret"`
CDNRegion string `arg:"--cdn-region"`
CDNEndpoint string `arg:"--cdn-endpoint"`
CDNFileName string `arg:"--cdn-file-name"`
CDNBucketName string `arg:"--cdn-bucket-name"`
CDNForcePathStyle bool `arg:"--cdn-force-path-style"`

GitToken string `arg:"--git-token"`
GitRepository string `arg:"--git-repository"`
GitBranch string `arg:"--git-branch"`
GitFileName string `arg:"--git-file-name"`
}

func newArgs() args {
return args{
ConfigPath: ciDefaultString,
Profile: ciDefaultString,
LocalPrefix: ciDefaultString,
SourceDir: ciDefaultString,
GithubActionOutput: false,
ThresholdFile: ciDefaultInt,
ThresholdPackage: ciDefaultInt,
ThresholdTotal: ciDefaultInt,

BreakdownFileName: ciDefaultString,
DiffBaseBreakdownFileName: ciDefaultString,

// Badge
BadgeFileName: ciDefaultString,

// CDN
CDNKey: ciDefaultString,
CDNSecret: ciDefaultString,
CDNRegion: ciDefaultString,
CDNEndpoint: ciDefaultString,
CDNFileName: ciDefaultString,
CDNBucketName: ciDefaultString,
CDNForcePathStyle: false,

// Git
GitToken: ciDefaultString,
GitRepository: ciDefaultString,
GitBranch: ciDefaultString,
GitFileName: ciDefaultString,
}
}

func (*args) Version() string {
return Name + " " + Version
}

//nolint:cyclop,maintidx,mnd,funlen // relax
func (a *args) overrideConfig(cfg testcoverage.Config) (testcoverage.Config, error) {
if !isCIDefaultString(a.Profile) {
cfg.Profile = a.Profile
}

if a.GithubActionOutput {
cfg.GithubActionOutput = true
}

if !isCIDefaultString(a.LocalPrefix) {
cfg.LocalPrefixDeprecated = a.LocalPrefix
}

if !isCIDefaultString(a.SourceDir) {
cfg.SourceDir = a.SourceDir
}

if !isCIDefaultInt(a.ThresholdFile) {
cfg.Threshold.File = a.ThresholdFile
}

if !isCIDefaultInt(a.ThresholdPackage) {
cfg.Threshold.Package = a.ThresholdPackage
}

if !isCIDefaultInt(a.ThresholdTotal) {
cfg.Threshold.Total = a.ThresholdTotal
}

if !isCIDefaultString(a.BreakdownFileName) {
cfg.BreakdownFileName = a.BreakdownFileName
}

if !isCIDefaultString(a.DiffBaseBreakdownFileName) {
cfg.Diff.BaseBreakdownFileName = a.DiffBaseBreakdownFileName
}

if !isCIDefaultString(a.BadgeFileName) {
cfg.Badge.FileName = a.BadgeFileName
}

if !isCIDefaultString(a.CDNSecret) {
cfg.Badge.CDN.Secret = a.CDNSecret
cfg.Badge.CDN.Key = escapeCiDefaultString(a.CDNKey)
cfg.Badge.CDN.Region = escapeCiDefaultString(a.CDNRegion)
cfg.Badge.CDN.FileName = escapeCiDefaultString(a.CDNFileName)
cfg.Badge.CDN.BucketName = escapeCiDefaultString(a.CDNBucketName)
cfg.Badge.CDN.ForcePathStyle = a.CDNForcePathStyle

if !isCIDefaultString(a.CDNEndpoint) {
cfg.Badge.CDN.Endpoint = a.CDNEndpoint
}
}

if !isCIDefaultString(a.GitToken) {
cfg.Badge.Git.Token = a.GitToken
cfg.Badge.Git.Branch = escapeCiDefaultString(a.GitBranch)
cfg.Badge.Git.FileName = escapeCiDefaultString(a.GitFileName)

parts := strings.Split(escapeCiDefaultString(a.GitRepository), "/")
if len(parts) != 2 {
return cfg, errors.New("--git-repository flag should have format {owner}/{repository}")
}

cfg.Badge.Git.Owner = parts[0]
cfg.Badge.Git.Repository = parts[1]
}

return cfg, nil
}

//nolint:forbidigo // relax
//nolint:forbidigo,wsl // relax
func main() {
cfg, err := readConfig()
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}

pass := testcoverage.Check(os.Stdout, cfg)
if !pass {
os.Exit(1)
}
}

func readConfig() (testcoverage.Config, error) {
cmdArgs := newArgs()
arg.MustParse(&cmdArgs)
logger.Init()

cfg := testcoverage.Config{}

// Load config from file
if !isCIDefaultString(cmdArgs.ConfigPath) {
err := testcoverage.ConfigFromFile(&cfg, cmdArgs.ConfigPath)
if err != nil {
return testcoverage.Config{}, fmt.Errorf("failed loading config from file: %w", err)
}
}

// Override config with values from args
cfg, err := cmdArgs.overrideConfig(cfg)
pass, err := testcoverage.Check(os.Stdout, cfg)
if err != nil {
return testcoverage.Config{}, fmt.Errorf("argument is not valid: %w", err)
}

// Validate config
if err := cfg.Validate(); err != nil {
return testcoverage.Config{}, fmt.Errorf("config file is not valid: %w", err)
fmt.Println("Running coverage check failed.")
if cfg.GithubActionOutput {
fmt.Printf("Please set `debug: true` input to see detailed output.")
} else {
fmt.Println("Please use `--debug=true` flag to see detailed output.")
}
}

return cfg, nil
}

func isCIDefaultString(v string) bool { return v == ciDefaultString }

func isCIDefaultInt(v int) bool { return v == ciDefaultInt }

func escapeCiDefaultString(v string) string {
if v == ciDefaultString {
return ""
if !pass || err != nil {
os.Exit(1)
}

return v
}
Loading
Loading