|
1 | 1 | package main
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "errors" |
5 | 4 | "fmt"
|
6 | 5 | "os"
|
7 |
| - "strings" |
8 |
| - |
9 |
| - "github.com/alexflint/go-arg" |
10 | 6 |
|
11 | 7 | "github.com/vladopajic/go-test-coverage/v2/pkg/testcoverage"
|
| 8 | + "github.com/vladopajic/go-test-coverage/v2/pkg/testcoverage/logger" |
12 | 9 | )
|
13 | 10 |
|
14 | 11 | const (
|
15 | 12 | Version = "v2.13.2" // VERSION: when changing version update version in other places
|
16 | 13 | Name = "go-test-coverage"
|
17 | 14 | )
|
18 | 15 |
|
19 |
| -const ( |
20 |
| - // default value of string variables passed by CI |
21 |
| - ciDefaultString = `''` |
22 |
| - // default value of int variables passed by CI |
23 |
| - ciDefaultInt = -1 |
24 |
| -) |
25 |
| - |
26 |
| -type args struct { |
27 |
| - ConfigPath string `arg:"-c,--config"` |
28 |
| - Profile string `arg:"-p,--profile" help:"path to coverage profile"` |
29 |
| - LocalPrefix string `arg:"-l,--local-prefix"` // deprecated |
30 |
| - SourceDir string `arg:"-s,--source-dir"` |
31 |
| - GithubActionOutput bool `arg:"-o,--github-action-output"` |
32 |
| - ThresholdFile int `arg:"-f,--threshold-file"` |
33 |
| - ThresholdPackage int `arg:"-k,--threshold-package"` |
34 |
| - ThresholdTotal int `arg:"-t,--threshold-total"` |
35 |
| - |
36 |
| - BreakdownFileName string `arg:"--breakdown-file-name"` |
37 |
| - DiffBaseBreakdownFileName string `arg:"--diff-base-breakdown-file-name"` |
38 |
| - |
39 |
| - BadgeFileName string `arg:"-b,--badge-file-name"` |
40 |
| - |
41 |
| - CDNKey string `arg:"--cdn-key"` |
42 |
| - CDNSecret string `arg:"--cdn-secret"` |
43 |
| - CDNRegion string `arg:"--cdn-region"` |
44 |
| - CDNEndpoint string `arg:"--cdn-endpoint"` |
45 |
| - CDNFileName string `arg:"--cdn-file-name"` |
46 |
| - CDNBucketName string `arg:"--cdn-bucket-name"` |
47 |
| - CDNForcePathStyle bool `arg:"--cdn-force-path-style"` |
48 |
| - |
49 |
| - GitToken string `arg:"--git-token"` |
50 |
| - GitRepository string `arg:"--git-repository"` |
51 |
| - GitBranch string `arg:"--git-branch"` |
52 |
| - GitFileName string `arg:"--git-file-name"` |
53 |
| -} |
54 |
| - |
55 |
| -func newArgs() args { |
56 |
| - return args{ |
57 |
| - ConfigPath: ciDefaultString, |
58 |
| - Profile: ciDefaultString, |
59 |
| - LocalPrefix: ciDefaultString, |
60 |
| - SourceDir: ciDefaultString, |
61 |
| - GithubActionOutput: false, |
62 |
| - ThresholdFile: ciDefaultInt, |
63 |
| - ThresholdPackage: ciDefaultInt, |
64 |
| - ThresholdTotal: ciDefaultInt, |
65 |
| - |
66 |
| - BreakdownFileName: ciDefaultString, |
67 |
| - DiffBaseBreakdownFileName: ciDefaultString, |
68 |
| - |
69 |
| - // Badge |
70 |
| - BadgeFileName: ciDefaultString, |
71 |
| - |
72 |
| - // CDN |
73 |
| - CDNKey: ciDefaultString, |
74 |
| - CDNSecret: ciDefaultString, |
75 |
| - CDNRegion: ciDefaultString, |
76 |
| - CDNEndpoint: ciDefaultString, |
77 |
| - CDNFileName: ciDefaultString, |
78 |
| - CDNBucketName: ciDefaultString, |
79 |
| - CDNForcePathStyle: false, |
80 |
| - |
81 |
| - // Git |
82 |
| - GitToken: ciDefaultString, |
83 |
| - GitRepository: ciDefaultString, |
84 |
| - GitBranch: ciDefaultString, |
85 |
| - GitFileName: ciDefaultString, |
86 |
| - } |
87 |
| -} |
88 |
| - |
89 |
| -func (*args) Version() string { |
90 |
| - return Name + " " + Version |
91 |
| -} |
92 |
| - |
93 |
| -//nolint:cyclop,maintidx,mnd,funlen // relax |
94 |
| -func (a *args) overrideConfig(cfg testcoverage.Config) (testcoverage.Config, error) { |
95 |
| - if !isCIDefaultString(a.Profile) { |
96 |
| - cfg.Profile = a.Profile |
97 |
| - } |
98 |
| - |
99 |
| - if a.GithubActionOutput { |
100 |
| - cfg.GithubActionOutput = true |
101 |
| - } |
102 |
| - |
103 |
| - if !isCIDefaultString(a.LocalPrefix) { |
104 |
| - cfg.LocalPrefixDeprecated = a.LocalPrefix |
105 |
| - } |
106 |
| - |
107 |
| - if !isCIDefaultString(a.SourceDir) { |
108 |
| - cfg.SourceDir = a.SourceDir |
109 |
| - } |
110 |
| - |
111 |
| - if !isCIDefaultInt(a.ThresholdFile) { |
112 |
| - cfg.Threshold.File = a.ThresholdFile |
113 |
| - } |
114 |
| - |
115 |
| - if !isCIDefaultInt(a.ThresholdPackage) { |
116 |
| - cfg.Threshold.Package = a.ThresholdPackage |
117 |
| - } |
118 |
| - |
119 |
| - if !isCIDefaultInt(a.ThresholdTotal) { |
120 |
| - cfg.Threshold.Total = a.ThresholdTotal |
121 |
| - } |
122 |
| - |
123 |
| - if !isCIDefaultString(a.BreakdownFileName) { |
124 |
| - cfg.BreakdownFileName = a.BreakdownFileName |
125 |
| - } |
126 |
| - |
127 |
| - if !isCIDefaultString(a.DiffBaseBreakdownFileName) { |
128 |
| - cfg.Diff.BaseBreakdownFileName = a.DiffBaseBreakdownFileName |
129 |
| - } |
130 |
| - |
131 |
| - if !isCIDefaultString(a.BadgeFileName) { |
132 |
| - cfg.Badge.FileName = a.BadgeFileName |
133 |
| - } |
134 |
| - |
135 |
| - if !isCIDefaultString(a.CDNSecret) { |
136 |
| - cfg.Badge.CDN.Secret = a.CDNSecret |
137 |
| - cfg.Badge.CDN.Key = escapeCiDefaultString(a.CDNKey) |
138 |
| - cfg.Badge.CDN.Region = escapeCiDefaultString(a.CDNRegion) |
139 |
| - cfg.Badge.CDN.FileName = escapeCiDefaultString(a.CDNFileName) |
140 |
| - cfg.Badge.CDN.BucketName = escapeCiDefaultString(a.CDNBucketName) |
141 |
| - cfg.Badge.CDN.ForcePathStyle = a.CDNForcePathStyle |
142 |
| - |
143 |
| - if !isCIDefaultString(a.CDNEndpoint) { |
144 |
| - cfg.Badge.CDN.Endpoint = a.CDNEndpoint |
145 |
| - } |
146 |
| - } |
147 |
| - |
148 |
| - if !isCIDefaultString(a.GitToken) { |
149 |
| - cfg.Badge.Git.Token = a.GitToken |
150 |
| - cfg.Badge.Git.Branch = escapeCiDefaultString(a.GitBranch) |
151 |
| - cfg.Badge.Git.FileName = escapeCiDefaultString(a.GitFileName) |
152 |
| - |
153 |
| - parts := strings.Split(escapeCiDefaultString(a.GitRepository), "/") |
154 |
| - if len(parts) != 2 { |
155 |
| - return cfg, errors.New("--git-repository flag should have format {owner}/{repository}") |
156 |
| - } |
157 |
| - |
158 |
| - cfg.Badge.Git.Owner = parts[0] |
159 |
| - cfg.Badge.Git.Repository = parts[1] |
160 |
| - } |
161 |
| - |
162 |
| - return cfg, nil |
163 |
| -} |
164 |
| - |
165 |
| -//nolint:forbidigo // relax |
| 16 | +//nolint:forbidigo,wsl // relax |
166 | 17 | func main() {
|
167 | 18 | cfg, err := readConfig()
|
168 | 19 | if err != nil {
|
169 | 20 | fmt.Println(err.Error())
|
170 | 21 | os.Exit(1)
|
171 | 22 | }
|
172 | 23 |
|
173 |
| - pass := testcoverage.Check(os.Stdout, cfg) |
174 |
| - if !pass { |
175 |
| - os.Exit(1) |
176 |
| - } |
177 |
| -} |
178 |
| - |
179 |
| -func readConfig() (testcoverage.Config, error) { |
180 |
| - cmdArgs := newArgs() |
181 |
| - arg.MustParse(&cmdArgs) |
| 24 | + logger.Init() |
182 | 25 |
|
183 |
| - cfg := testcoverage.Config{} |
184 |
| - |
185 |
| - // Load config from file |
186 |
| - if !isCIDefaultString(cmdArgs.ConfigPath) { |
187 |
| - err := testcoverage.ConfigFromFile(&cfg, cmdArgs.ConfigPath) |
188 |
| - if err != nil { |
189 |
| - return testcoverage.Config{}, fmt.Errorf("failed loading config from file: %w", err) |
190 |
| - } |
191 |
| - } |
192 |
| - |
193 |
| - // Override config with values from args |
194 |
| - cfg, err := cmdArgs.overrideConfig(cfg) |
| 26 | + pass, err := testcoverage.Check(os.Stdout, cfg) |
195 | 27 | if err != nil {
|
196 |
| - return testcoverage.Config{}, fmt.Errorf("argument is not valid: %w", err) |
197 |
| - } |
198 |
| - |
199 |
| - // Validate config |
200 |
| - if err := cfg.Validate(); err != nil { |
201 |
| - return testcoverage.Config{}, fmt.Errorf("config file is not valid: %w", err) |
| 28 | + fmt.Println("Running coverage check failed.") |
| 29 | + if cfg.GithubActionOutput { |
| 30 | + fmt.Printf("Please set `debug: true` input to see detailed output.") |
| 31 | + } else { |
| 32 | + fmt.Println("Please use `--debug=true` flag to see detailed output.") |
| 33 | + } |
202 | 34 | }
|
203 |
| - |
204 |
| - return cfg, nil |
205 |
| -} |
206 |
| - |
207 |
| -func isCIDefaultString(v string) bool { return v == ciDefaultString } |
208 |
| - |
209 |
| -func isCIDefaultInt(v int) bool { return v == ciDefaultInt } |
210 |
| - |
211 |
| -func escapeCiDefaultString(v string) string { |
212 |
| - if v == ciDefaultString { |
213 |
| - return "" |
| 35 | + if !pass || err != nil { |
| 36 | + os.Exit(1) |
214 | 37 | }
|
215 |
| - |
216 |
| - return v |
217 | 38 | }
|
0 commit comments