-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmagefile.go
266 lines (237 loc) · 9.06 KB
/
magefile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// Copyright (c) 2019-present Sven Greb <[email protected]>
// This source code is licensed under the MIT license found in the license file.
//go:build mage
// wand - a simple and powerful toolkit for Mage.
package main
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/fatih/color"
"github.com/magefile/mage/mg"
"github.com/svengreb/nib"
"github.com/svengreb/nib/inkpen"
"github.com/svengreb/nib/pencil"
osSupport "github.com/svengreb/wand/internal/support/os"
projectSupport "github.com/svengreb/wand/internal/support/project"
"github.com/svengreb/wand/pkg/elder"
wandProj "github.com/svengreb/wand/pkg/project"
wandProjVCS "github.com/svengreb/wand/pkg/project/vcs"
taskFSClean "github.com/svengreb/wand/pkg/task/fs/clean"
taskGofumpt "github.com/svengreb/wand/pkg/task/gofumpt"
taskGoimports "github.com/svengreb/wand/pkg/task/goimports"
taskGo "github.com/svengreb/wand/pkg/task/golang"
taskGoTest "github.com/svengreb/wand/pkg/task/golang/test"
taskGolangCI "github.com/svengreb/wand/pkg/task/golangcilint"
)
const (
// defaultIntegrationTestTag is the default tag name for integration tests.
defaultIntegrationTestTag = taskGoTest.DefaultIntegrationTestTag
// defaultTestOutputDirName is the default output directory name for test artifacts like profiles and reports.
defaultTestOutputDirName = taskGoTest.DefaultOutputDirName
)
const (
// wandVerbosity is the name of the environment variable for the wand verbosity level.
wandVerbosity env = iota
)
var (
// envPrefix is the full uppercase project name used as prefix for all project-specific environment variables.
envPrefix = strings.ToUpper(projectSupport.Name)
// ew is the project's Mage wand.Wand.
ew *elder.Elder
// optsTaskTest returns composed options for test task.
optsTaskTest = func(extraOpts ...taskGoTest.Option) []taskGoTest.Option {
opts := []taskGoTest.Option{
// Add the "./..." shortcut to recursively include tests of all sub-packages.
taskGoTest.WithPkgs(fmt.Sprintf("%s/...", ew.GetProjectMetadata().Options().GoModule.Path)),
taskGoTest.WithOutputDir(testOutputDir(ew.GetProjectMetadata().Options().BaseOutputDir)),
}
return append(opts, extraOpts...)
}
// testOutputDir returns the path to the subdirectory within the output directory that is used to store test profiles
// and reports.
testOutputDir = func(baseOutputDir string) string {
return filepath.Join(baseOutputDir, defaultTestOutputDirName)
}
)
// env is a project-specific environment variable.
type env int
// init initializes and populates the project and CLI message printer configurations.
func init() {
// Initialize and configure the project-wide CLI message printer.
verbLvl := nib.InfoVerbosity
wandVerbLvl, isVerbLvlSet := os.LookupEnv(wandVerbosity.String())
if isVerbLvlSet {
lvl, parseErr := nib.ParseVerbosity(wandVerbLvl)
if parseErr != nil {
color.Yellow("Invalid wand verbosity level %q, using default %q level", wandVerbLvl, verbLvl)
}
verbLvl = lvl
}
ink := inkpen.New(inkpen.WithPencilOptions(pencil.WithVerbosity(verbLvl)))
elderWand, ewErr := elder.New(
elder.WithProjectOptions(
wandProj.WithName(projectSupport.Name),
wandProj.WithDisplayName(projectSupport.DisplayName),
wandProj.WithVCSKind(wandProjVCS.KindGit),
wandProj.WithDefaultVersion(projectSupport.ReleaseVersion),
),
elder.WithGoRunnerOptions(
taskGo.WithRunnerEnv(osSupport.EnvSliceToMap(os.Environ())),
),
elder.WithNib(ink),
)
if ewErr != nil {
color.Red("Initialization failed: %v\n", ewErr)
os.Exit(1)
}
ew = elderWand
}
func (e env) String() string {
envVars := []string{
"VERBOSITY",
}
return fmt.Sprintf("%s_%s", envPrefix, envVars[e])
}
// Clean removes artifacts from previous task executions.
func Clean() {
ew.Infof("Removing previous test artifacts")
cleaned, err := ew.Clean(
ew.GetProjectMetadata().Options().Name,
// TODO: Required?
taskFSClean.WithLimitToAppOutputDir(true),
taskFSClean.WithPaths(testOutputDir(ew.GetProjectMetadata().Options().BaseOutputDir)),
)
if err != nil {
ew.ExitPrintf(1, nib.ErrorVerbosity, "%s\n ↳ %v", color.RedString("Clean failed:"), err)
}
ew.Successf("Clean completed")
for _, cp := range cleaned {
printRawf(" ↳ %s\n", cp)
}
}
// Format formats all Go source files with import optimizations and additional rules according to the Go standard code
// style.
func Format() {
mg.SerialDeps(
func() {
ew.Infof("Formatting all Go source files with import optimizations according to the Go standard code style")
opts := []taskGoimports.Option{
taskGoimports.WithListNonCompliantFiles(true),
taskGoimports.WithLocalPkgs(ew.GetProjectMetadata().Options().GoModule.Path),
taskGoimports.WithPersistedChanges(true),
taskGoimports.WithReportAllErrors(true),
}
if err := ew.Goimports(opts...); err != nil {
ew.ExitPrintf(1, nib.ErrorVerbosity, "Formatting failed:\n ↳ %v", err)
}
},
func() {
ew.Infof("Formatting all Go source files with additional rules according to the Go standard code style")
opts := []taskGofumpt.Option{
taskGofumpt.WithExtraRules(true),
taskGofumpt.WithListNonCompliantFiles(true),
taskGofumpt.WithPersistedChanges(true),
taskGofumpt.WithReportAllErrors(true),
}
if err := ew.Gofumpt(opts...); err != nil {
ew.ExitPrintf(1, nib.ErrorVerbosity, "Formatting failed:\n ↳ %v", err)
}
},
)
ew.Successf("Formatting completed")
}
// Lint runs all configured "golangci-lint" linters.
// See the ".golangci.yml" configuration file and official GolangCI documentations at https://golangci-lint.run
// and https://github.com/golangci/golangci-lint for more details.
func Lint() {
mg.SerialDeps(
func() {
ew.Infof(`Running configured "golangci-lint" linters`)
err := ew.GolangCILint(
taskGolangCI.WithVerboseOutput(true),
)
if err != nil {
ew.ExitPrintf(1, nib.ErrorVerbosity, "Linting failed:\n ↳ %v", err)
}
},
)
ew.Successf("Linting completed")
}
// Test runs all unit tests.
func Test() {
Clean()
ew.Infof("Running %s tests", color.CyanString(ew.GetProjectMetadata().Options().GoModule.Path))
runTest()
ew.Successf("Test(s) completed:\n ↳ %s", color.GreenString(ew.GetProjectMetadata().Options().GoModule.Path))
}
// TestCover runs all unit tests with coverage reports.
func TestCover() {
Clean()
ew.Infof("Running %s tests with coverage", color.CyanString(ew.GetProjectMetadata().Options().GoModule.Path))
runTest(taskGoTest.WithCoverageProfile(true))
ew.Successf("Test(s) completed:\n ↳ %s", color.GreenString(ew.GetProjectMetadata().Options().GoModule.Path))
}
// TestIntegration runs all integration tests.
func TestIntegration() {
Clean()
ew.Infof("Running %s integration tests", color.CyanString(ew.GetProjectMetadata().Options().GoModule.Path))
runTest(taskGoTest.WithGoOptions(
taskGo.WithTags(defaultIntegrationTestTag),
))
}
// TestRace runs all unit tests with enabled race detection.
// Please note that race detection will fail when using a Go executable that has been build in PIE mode!
// See "go help buildmode" and https://github.com/golang/go/issues/33514 for more details and make sure to run this
// task with a Go executable that was build without "-buildmode=pie" flag.
// Also see https://golang.org/doc/articles/race_detector.html for more details about the race detector.
func TestRace() {
Clean()
ew.Infof(
"Running %s tests with enabled race detection",
color.CyanString(ew.GetProjectMetadata().Options().GoModule.Path),
)
runTest(taskGoTest.WithGoOptions(
taskGo.WithRaceDetector(true),
))
}
// UpgradeMods updates outdated Go module dependencies interactively.
func UpgradeMods() {
ew.Infof("Updating outdated Go dependencies")
if err := ew.GoModUpgrade(); err != nil {
ew.ExitPrintf(1, nib.ErrorVerbosity, "%s\n ↳ %v", color.RedString("Updating outdated dependencies failed:"), err)
}
}
// Validate ensures that everything is properly initialized and operational.
func Validate(mageCtx context.Context) {
ew.Infof("Validating that the elder wand is properly initialized and operational")
errs := ew.Validate()
if len(errs) != 0 {
for _, err := range errs {
ew.Errorf(err.Error())
}
ew.ExitPrintf(1, nib.FatalVerbosity, "Validation incomplete")
}
ew.Successf("Validation completed")
}
// printRawf writes a message to the underlying io.Writer of ew without any specific formatting.
// If an error occurs while writing to the underlying io.Writer the message is printed to os.Stdout instead.
// When this also returns an error the error is written to os.Stderr instead.
func printRawf(format string, args ...interface{}) {
msg := fmt.Sprintf(format, args...)
if _, err := fmt.Fprint(ew.Writer(), msg); err != nil {
if _, err = fmt.Fprint(os.Stdout, msg); err != nil {
_, _ = fmt.Fprint(os.Stderr, err)
}
}
}
func runTest(opts ...taskGoTest.Option) {
if err := ew.GoTest(ew.GetProjectMetadata().Options().Name, optsTaskTest(opts...)...); err != nil {
ew.Warnf(color.YellowString(
"Please note that race detection will fail when using a Go executable that has been build in PIE mode!",
))
ew.ExitPrintf(1, nib.ErrorVerbosity, "%s\n ↳ %v", color.RedString("Test(s) failed:"), err)
}
}