-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
configuration.go
212 lines (186 loc) · 6.17 KB
/
configuration.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
package testza
import (
"flag"
"fmt"
"math/rand"
"os"
"runtime"
"strconv"
"strings"
"time"
"github.com/MarvinJWendt/testza/internal"
"github.com/klauspost/cpuid/v2"
"github.com/pterm/pterm"
)
var randomSeed int64
var showStartupMessage = true
func init() {
initSync.Lock()
defer initSync.Unlock()
// Defining flags to show up in the help message
flag.Bool("testza.disable-color", false, "disables colored output")
flag.Bool("testza.disable-line-numbers", false, "disables line numbers in output")
flag.Bool("testza.disable-startup-message", false, "disable the startup message")
flag.Int64("testza.seed", 0, "seed used for random operations")
flag.Int("testza.diff-context-lines", 2, "sets the context line count in difference output")
for i, arg := range os.Args {
// Check if the argument is a flag
if !strings.HasPrefix(arg, "--") {
continue
}
// Figure out if the flag has a value
var value string
if i != len(os.Args)-1 {
value = os.Args[i+1]
if strings.HasPrefix(value, "-") {
value = ""
}
}
// Check for set flags and run the appropriate function
switch strings.TrimPrefix(arg, "--testza.") {
case "disable-color":
SetColorsEnabled(false)
case "disable-line-numbers":
SetLineNumbersEnabled(false)
case "disable-startup-message":
SetShowStartupMessage(false)
case "seed":
seed, err := strconv.Atoi(value)
pterm.Fatal.PrintOnError(err)
SetRandomSeed(int64(seed))
case "diff-context-lines":
v, err := strconv.Atoi(value)
pterm.Fatal.PrintOnError(err)
SetDiffContextLines(v)
}
}
go func() {
initSync.Lock()
defer initSync.Unlock()
if randomSeed == 0 {
randomSeed = time.Now().UnixNano()
rand.Seed(randomSeed)
}
if showStartupMessage {
var startupMessage string
startupMessage += infoPrinter.WithLevel(1).Sprintln("Running tests with " + secondary("Testza"))
startupMessage += infoPrinter.Sprintfln(`Using seed "%s" for random operations`, secondary(randomSeed))
startupMessage += infoPrinter.Sprintfln(`System info: OS=%s | arch=%s | cpu=%s | go=%s`, secondary(runtime.GOOS), secondary(runtime.GOARCH), secondary(cpuid.CPU.BrandName), secondary(runtime.Version()))
startupMessage += fmt.Sprintln()
pterm.Println(startupMessage)
}
}()
}
// SetColorsEnabled controls if testza should print colored output.
// You should use this in the init() method of the package, which contains your tests.
//
// > This setting can also be set by the command line flag --testza.disable-color.
//
// Example:
//
// init() {
// testza.SetColorsEnabled(false) // Disable colored output
// testza.SetColorsEnabled(true) // Enable colored output
// }
func SetColorsEnabled(enabled bool) {
initSync.Lock()
defer initSync.Unlock()
if enabled {
pterm.EnableColor()
} else {
pterm.DisableColor()
}
}
// GetColorsEnabled returns current value of ColorsEnabled setting.
// ColorsEnabled controls if testza should print colored output.
func GetColorsEnabled() bool {
return pterm.PrintColor
}
// SetLineNumbersEnabled controls if line numbers should be printed in failing tests.
// You should use this in the init() method of the package, which contains your tests.
//
// > This setting can also be set by the command line flag --testza.disable-line-numbers.
//
// Example:
//
// init() {
// testza.SetLineNumbersEnabled(false) // Disable line numbers
// testza.SetLineNumbersEnabled(true) // Enable line numbers
// }
func SetLineNumbersEnabled(enabled bool) {
initSync.Lock()
defer initSync.Unlock()
internal.LineNumbersEnabled = enabled
}
// GetLineNumbersEnabled returns current value of LineNumbersEnabled setting.
// LineNumbersEnabled controls if line numbers should be printed in failing tests.
func GetLineNumbersEnabled() bool {
return internal.LineNumbersEnabled
}
// SetRandomSeed sets the seed for the random generator used in testza.
// Using the same seed will result in the same random sequences each time and guarantee a reproducible test run.
// Use this setting, if you want a 100% deterministic test.
// You should use this in the init() method of the package, which contains your tests.
//
// > This setting can also be set by the command line flag --testza.seed.
//
// Example:
//
// init() {
// testza.SetRandomSeed(1337) // Set the seed to 1337
// testza.SetRandomSeed(time.Now().UnixNano()) // Set the seed back to the current time (default | non-deterministic)
// }
func SetRandomSeed(seed int64) {
initSync.Lock()
defer initSync.Unlock()
randomSeed = seed
rand.Seed(seed)
}
// GetRandomSeed returns current value of the random seed setting.
func GetRandomSeed() int64 {
return randomSeed
}
// SetShowStartupMessage controls if the startup message should be printed.
// You should use this in the init() method of the package, which contains your tests.
//
// > This setting can also be set by the command line flag --testza.disable-startup-message.
//
// Example:
//
// init() {
// testza.SetShowStartupMessage(false) // Disable the startup message
// testza.SetShowStartupMessage(true) // Enable the startup message
// }
func SetShowStartupMessage(show bool) {
initSync.Lock()
defer initSync.Unlock()
showStartupMessage = show
}
// GetShowStartupMessage returns current value of showStartupMessage setting.
// showStartupMessage setting controls if the startup message should be printed.
func GetShowStartupMessage() bool {
return showStartupMessage
}
// SetDiffContextLines controls how many lines are shown around a changed diff line.
// If set to -1 it will show full diff.
// You should use this in the init() method of the package, which contains your tests.
//
// > This setting can also be set by the command line flag --testza.diff-context-lines.
//
// Example:
//
// init() {
// testza.SetDiffContextLines(-1) // Show all diff lines
// testza.SetDiffContextLines(3) // Show 3 lines around every changed line
// }
func SetDiffContextLines(lines int) {
initSync.Lock()
defer initSync.Unlock()
internal.DiffContextLines = lines
}
// GetDiffContextLines returns current value of DiffContextLines setting.
// DiffContextLines setting controls how many lines are shown around a changed diff line.
// If set to -1 it will show full diff.
func GetDiffContextLines() int {
return internal.DiffContextLines
}