/
test.go
132 lines (108 loc) · 3.27 KB
/
test.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
// Copyright (c) 2019-present Sven Greb <development@svengreb.de>
// This source code is licensed under the MIT license found in the license file.
// Package test provides a task for the Go toolchain "test" command.
package test
import (
"fmt"
"path/filepath"
"github.com/svengreb/wand/pkg/app"
"github.com/svengreb/wand/pkg/task"
taskGo "github.com/svengreb/wand/pkg/task/golang"
)
// Task is a task for the Go toolchain "test" command.
type Task struct {
ac app.Config
opts *Options
}
// BuildParams builds the parameters.
// Note that configured flags are applied after the "GOFLAGS" environment variable and could overwrite already defined
// flags. In addition, the output directory for test artifacts like profiles and reports must exist or must be be
// created before, otherwise the "test" Go toolchain command will fail to run.
//
// See `go help environment`, `go help env` and the `go` command documentations for more details:
// - https://golang.org/cmd/go/#hdr-Environment_variables
func (t *Task) BuildParams() []string {
params := []string{"test"}
params = append(params, taskGo.BuildGoOptions(t.opts.taskGoOpts...)...)
if t.opts.EnableVerboseOutput {
params = append(params, "-v")
}
if t.opts.DisableCache {
params = append(params, "-count=1")
}
if t.opts.EnableBlockProfile {
params = append(params,
fmt.Sprintf(
"-blockprofile=%s",
filepath.Join(t.opts.OutputDir, t.opts.BlockProfileOutputFileName),
),
)
}
if t.opts.EnableCoverageProfile {
params = append(params,
fmt.Sprintf(
"-coverprofile=%s",
filepath.Join(t.opts.OutputDir, t.opts.CoverageProfileOutputFileName),
),
)
}
if t.opts.EnableCPUProfile {
params = append(params,
fmt.Sprintf("-cpuprofile=%s",
filepath.Join(t.opts.OutputDir, t.opts.CPUProfileOutputFileName),
),
)
}
if t.opts.EnableMemoryProfile {
params = append(params,
fmt.Sprintf("-memprofile=%s",
filepath.Join(t.opts.OutputDir, t.opts.MemoryProfileOutputFileName),
),
)
}
if t.opts.EnableMutexProfile {
params = append(params,
fmt.Sprintf("-mutexprofile=%s",
filepath.Join(t.opts.OutputDir, t.opts.MutexProfileOutputFileName),
),
)
}
if t.opts.EnableTraceProfile {
params = append(params,
fmt.Sprintf("-trace=%s",
filepath.Join(t.opts.OutputDir, t.opts.TraceProfileOutputFileName),
),
)
}
if len(t.opts.Flags) > 0 {
params = append(params, t.opts.Flags...)
}
params = append(params, t.opts.Pkgs...)
return params
}
// Env returns the task specific environment.
func (t *Task) Env() map[string]string {
return t.opts.Env
}
// Kind returns the task kind.
func (t *Task) Kind() task.Kind {
return task.KindExec
}
// Name returns the unique task name.
func (t *Task) Name() string {
return t.opts.name
}
// Options returns the task options.
func (t *Task) Options() task.Options {
return *t.opts
}
// New creates a new task for the Go toolchain "test" command.
//nolint:gocritic // The app.Config struct is passed as value by design to ensure immutability.
func New(ac app.Config, opts ...Option) *Task {
opt := NewOptions(opts...)
// Store test profiles and reports within the application specific subdirectory.
if opt.OutputDir == "" {
opt.OutputDir = filepath.Join(ac.BaseOutputDir, DefaultOutputDirName)
}
return &Task{ac: ac, opts: opt}
}