-
Notifications
You must be signed in to change notification settings - Fork 4
/
magefile.go
218 lines (174 loc) · 5.35 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
//go:build mage
// +build mage
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"os/exec"
"sort"
"strings"
"github.com/blang/semver/v4"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
// Default is the default build target.
var Default = Build
// All cleans output, builds, tests, and lints.
func All(ctx context.Context) error {
type target func(context.Context) error
targets := []target{
Clean,
Build,
Test,
UnitTest,
IntegrationTest,
Lint,
LintFix,
LegacyTestSuiteTags,
}
for _, t := range targets {
if err := t(ctx); err != nil {
return err
}
}
return nil
}
// Build builds the Captain CLI
func Build(ctx context.Context) error {
args := []string{"./cmd/captain"}
ldflags, err := getLdflags()
if err != nil {
return err
}
args = append([]string{"-ldflags", ldflags}, args...)
if cgo_enabled := os.Getenv("CGO_ENABLED"); cgo_enabled == "0" {
args = append([]string{"-a"}, args...)
}
return sh.RunV("go", append([]string{"build"}, args...)...)
}
// Clean removes any generated artifacts from the repository.
func Clean(ctx context.Context) error {
return sh.Rm("./captain")
}
// Lint runs the linter & performs static-analysis checks.
func Lint(ctx context.Context) error {
return sh.RunV("golangci-lint", "run", "./...")
}
// Applies lint checks and fixes any issues.
func LintFix(ctx context.Context) error {
if err := sh.RunV("golangci-lint", "run", "--fix", "./..."); err != nil {
return err
}
if err := sh.RunV("go", "mod", "tidy"); err != nil {
return err
}
return nil
}
func Test(ctx context.Context) error {
mg.Deps(Build)
return (makeTestTask("-tags", "integration", "./..."))(ctx)
}
// Test executes the test-suite for the Captain-CLI.
func UnitTest(ctx context.Context) error {
// `ginkgo ./...` or `go test ./...` work out of the box
// but `ginkgo ./...` includes ~ confusing empty test output for integration tests
// so `mage test` explicitly doesn't call ginkgo against the `/test/` directory
return (makeTestTask("./internal/...", "./cmd/..."))(ctx)
}
// Test executes the test-suite for the Captain-CLI.
func IntegrationTest(ctx context.Context) error {
if os.Getenv("LEGACY_VERSION_TO_TEST") == "" {
// only build captain if we're running tests against the latest captain version
// so that we can build against the active, branch, then run integration tests from a previous commit against that
// binary
mg.Deps(Build)
}
return (makeTestTask("-tags", "integration", "./test/"))(ctx)
}
func makeTestTask(args ...string) func(ctx context.Context) error {
return func(ctx context.Context) error {
ldflags, err := getLdflags()
if err != nil {
return err
}
args = append([]string{"-ldflags", ldflags}, args...)
if report := os.Getenv("REPORT"); report != "" {
return sh.RunV("ginkgo", append([]string{"-p", "--junit-report=report.xml"}, args...)...)
}
cmd := exec.Command("command", "-v", "ginkgo")
if err := cmd.Run(); err != nil {
return sh.RunV("go", append([]string{"test"}, args...)...)
}
return sh.RunV("ginkgo", append([]string{"-p"}, args...)...)
}
}
// this prints out a json-formatted list of tags where the ./test directory has changed
func LegacyTestSuiteTags(ctx context.Context) error {
tagsWithChanges, err := findTagsWithChangesInTest()
if err != nil {
return err
}
output, err := json.Marshal(tagsWithChanges)
if err != nil {
return err
}
fmt.Println(string(output))
return nil
}
// this prints out a json-formatted list of tags where the ./test directory has changed
func findTagsWithChangesInTest() ([]string, error) {
out, err := exec.Command("git", "tag", "--sort=version:refname").Output()
if err != nil {
return nil, err
}
var versionTags []semver.Version
for _, tag := range strings.Split(string(out), "\n") {
tagVersion, err := semver.ParseTolerant(tag)
if err == nil && // only include the tag if it parses...
len(tagVersion.Pre) == 0 && // and is not pre-release...
tagVersion.GTE(semver.Version{Major: 1, Minor: 10, Patch: 1}) { // and is >= 1.10.1 (backwards compatibility tests start after 1.10.2. We include 1.10.1 for a baseline but never include it in the output)
versionTags = append(versionTags, tagVersion)
}
}
// sort by version
sort.Sort(semver.Versions(versionTags))
// keep tags where there has been a change in ./test
tagsWithChanges := []string{}
for i := 0; i < len(versionTags)-1; i++ {
prevVersion := versionTags[i]
version := versionTags[i+1]
hasChanges, err := hasChangesIn(prevVersion, version, "./test")
if err != nil {
return nil, err
}
if hasChanges {
tagsWithChanges = append(tagsWithChanges, toTag(version))
}
}
return tagsWithChanges, nil
}
func toTag(tag semver.Version) string {
return fmt.Sprintf("v%s", tag.String())
}
func hasChangesIn(prevVersion semver.Version, version semver.Version, folder string) (bool, error) {
err := exec.Command("git", "diff", "--quiet", toTag(prevVersion), toTag(version), "--", folder).Run()
if err == nil {
return false, nil
}
if _, ok := err.(*exec.ExitError); ok {
return true, nil
}
return false, err
}
func getLdflags() (string, error) {
if ldflags := os.Getenv("LDFLAGS"); ldflags != "" {
return ldflags, nil
}
sha, err := exec.Command("git", "rev-parse", "HEAD").Output()
if err != nil {
return "", err
}
return fmt.Sprintf("-X github.com/rwx-research/captain-cli.Version=testing-%v", string(sha)), nil
}