-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbash.go
More file actions
294 lines (261 loc) · 8.03 KB
/
bash.go
File metadata and controls
294 lines (261 loc) · 8.03 KB
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package golden
import (
"bytes"
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
"github.com/nextmv-io/sdk/flatmap"
"github.com/sergi/go-diff/diffmatchpatch"
)
// BashTest executes a golden file test for a bash command. It walks over
// the goldenDir to gather all .sh scripts present in the dir. It then executes
// each of the scripts and compares expected vs. actual outputs. If
// displayStdout or displayStderr are true, the output of each script will be
// composed of the resulting stderr + stdout.
func BashTest(
t *testing.T,
goldenDir string,
bashConfig BashConfig,
) {
// Fail immediately, if dir does not exist
if stat, err := os.Stat(goldenDir); err != nil || !stat.IsDir() {
t.Fatalf("dir %s does not exist", goldenDir)
}
// Collect bash scripts.
var scripts []string
fn := func(path string, _ os.FileInfo, _ error) error {
// Only consider .sh files
if strings.HasSuffix(path, ".sh") {
scripts = append(scripts, path)
}
return nil
}
if err := filepath.Walk(goldenDir, fn); err != nil {
t.Fatal("error walking over files: ", err)
}
cwd, err := os.Getwd()
if err != nil {
t.Fatal("error getting current working directory: ", err)
}
// Execute a golden file test for each script. Make the script path
// absolute to avoid issues with custom working directories.
for _, script := range scripts {
BashTestFile(t, filepath.Join(cwd, script), bashConfig)
}
// Post-process files containing volatile data.
postProcessVolatileData(t, bashConfig)
}
// BashTestFile executes a golden file test for a single bash script. The
// script is executed and the expected output is compared with the actual
// output.
func BashTestFile(
t *testing.T,
script string,
bashConfig BashConfig,
) {
ext := goldenExtension
if bashConfig.GoldenExtension != "" {
ext = bashConfig.GoldenExtension
}
goldenFilePath := script + ext
// Function run by the test.
f := func(t *testing.T) {
// Make script path absolute to avoid issues with custom working
// directories.
var err error
script, err = filepath.Abs(script)
if err != nil {
t.Fatal("error getting absolute path for script: ", script, ": ", err)
}
if _, err := os.Stat(script); err != nil {
t.Fatalf("script %s does not exist", script)
}
// Execute a bash command which consists of executing a .sh file.
cmd := exec.Command("bash", script)
// Pass environment and add custom environment variables
cmd.Env = os.Environ()
for _, e := range bashConfig.Envs {
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", e[0], e[1]))
}
// Set custom working directory if provided.
if bashConfig.WorkingDir != "" {
cmd.Dir = bashConfig.WorkingDir
}
// Run the command and gather the output bytes.
out, err := runCmd(cmd, bashConfig.DisplayStdout, bashConfig.DisplayStderr)
if err != nil {
t.Fatal(err)
}
// Process the output data before comparison.
got := processOutput(t, out, goldenFilePath, bashConfig.OutputProcessConfig)
// Write the output bytes to a .golden file, if the test is being
// updated
if *update || bashConfig.OutputProcessConfig.AlwaysUpdate {
if err := os.WriteFile(goldenFilePath, []byte(got), 0o644); err != nil {
t.Fatal("error writing bash output to file: ", err)
}
}
// Read the .golden file.
outGolden, err := os.ReadFile(goldenFilePath)
if err != nil {
t.Fatal("error reading file: ", goldenFilePath, ": ", err)
}
// Perform the golden file comparison.
expected := string(outGolden)
if got != expected {
dmp := diffmatchpatch.New()
diffs := dmp.DiffMain(got, expected, true)
t.Errorf(
"\ngot:\n%s\nexpected:\n%s\ndiffs (look for the colors):\n%s",
got,
expected,
dmp.DiffPrettyText(diffs),
)
}
}
// Delay the execution of the test to adhere for rate limits.
if bashConfig.WaitBefore > 0 {
t.Logf("delaying test execution for %v", bashConfig.WaitBefore)
<-time.After(bashConfig.WaitBefore)
}
// Test is executed.
t.Run(script, f)
// Run post-process functions.
for _, f := range bashConfig.PostProcessFunctions {
err := f(goldenFilePath)
if err != nil {
t.Fatalf("error running post-process function: %v", err)
}
}
}
func processOutput(
t *testing.T,
out []byte,
goldenPath string,
config OutputProcessConfig,
) string {
// Check whether any JSON modifications are requested.
jsonModifications := false
for _, field := range config.TransientFields {
skip, err := skipFile(goldenPath, field.FileRegex, field.FileRegexFullPath)
if err != nil {
t.Fatalf("error checking file regex: %v", err)
}
if !skip {
jsonModifications = true
break
}
}
for _, rounding := range config.RoundingConfig {
skip, err := skipFile(goldenPath, rounding.FileRegex, rounding.FileRegexFullPath)
if err != nil {
t.Fatalf("error checking file regex: %v", err)
}
if !skip {
jsonModifications = true
break
}
}
// Apply JSON specific processing (if any were found).
if jsonModifications {
// Convert the output to a map[string]any for processing.
var err error
output := map[string]any{}
if err = json.Unmarshal(out, &output); err != nil {
t.Fatalf("transient fields or rounding config provided, but output is not valid JSON: %v", err)
}
// Flatten the map and apply the configured replacements /
// modifications.
flattenedOutput := flatmap.Do(output)
flattenedOutput, err = replaceTransient(goldenPath, flattenedOutput, config.TransientFields...)
if err != nil {
t.Fatal(err)
}
flattenedOutput, err = roundFields(goldenPath, flattenedOutput, config.RoundingConfig...)
if err != nil {
t.Fatal(err)
}
nestedOutput, err := flatmap.Undo(flattenedOutput)
if err != nil {
t.Fatal(err)
}
// Marshal the processed output back to JSON.
out, err = json.MarshalIndent(nestedOutput, "", " ")
if err != nil {
t.Fatal("error marshaling output: ", err)
}
}
got := string(out)
// Apply regex replacements for volatile data.
if !config.KeepVolatileData {
// Replace default volatile content with a placeholder.
got = regexReplaceAllDefault(got)
}
// Apply custom regex replacements.
for _, r := range config.VolatileRegexReplacements {
got = regexReplaceCustom(got, r.Replacement, r.Regex)
}
return got
}
func postProcessVolatileData(
t *testing.T,
bashConfig BashConfig,
) {
// Post-process files containing volatile data.
for _, file := range bashConfig.OutputProcessConfig.VolatileDataFiles {
// Read the file.
out, err := os.ReadFile(file)
if err != nil {
t.Fatal("error reading file: ", file, ": ", err)
}
got := string(out)
// Replace default volatile content with a placeholder.
if !bashConfig.OutputProcessConfig.KeepVolatileData {
got = regexReplaceAllDefault(got)
}
// Apply custom volatile regex replacements.
for _, r := range bashConfig.OutputProcessConfig.VolatileRegexReplacements {
got = regexReplaceCustom(got, r.Replacement, r.Regex)
}
// Write the output back to the file.
if err := os.WriteFile(file, []byte(got), 0o644); err != nil {
t.Fatal("error writing stabilized output to file: ", err)
}
}
}
func runCmd(cmd *exec.Cmd, displayStdout, displayStderr bool) (output []byte, err error) {
var out []byte
if displayStderr && displayStdout {
// If we are interested in displaying both stderr and stdout, we
// need to get the combined output in order to get the correct
// order of the output.
if out, err = cmd.CombinedOutput(); err != nil {
return nil, fmt.Errorf("error running command: %v: %s", err, out)
}
return out, nil
}
// Initialize variables that will hold the output of stdout and stderr.
var stdout bytes.Buffer
var stderr bytes.Buffer
// Assign the pointers of stderr and stdout to populate when the
// command is run.
cmd.Stderr = &stderr
cmd.Stdout = &stdout
// Run the actual command.
if err := cmd.Run(); err != nil {
return nil, fmt.Errorf("error running command: %v: %s %s", err, stderr.String(), stdout.String())
}
// Gather the output bytes. Writing stderr and stdout is optional.
if displayStderr {
out = stderr.Bytes()
}
if displayStdout {
out = append(out, stdout.Bytes()...)
}
return out, nil
}