forked from linkerd/linkerd2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_helper.go
49 lines (40 loc) · 1.17 KB
/
test_helper.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
package cmd
import (
"fmt"
"io/ioutil"
"os"
"testing"
"github.com/sergi/go-diff/diffmatchpatch"
)
func diffCompare(t *testing.T, actual string, expected string) {
if actual != expected {
dmp := diffmatchpatch.New()
diffs := dmp.DiffMain(expected, actual, true)
// colorized output for local testing
// t.Fatalf("Unexpected output:\n%+v", dmp.DiffPrettyText(diffs))
diffs = dmp.DiffCleanupSemantic(diffs)
patches := dmp.PatchMake(diffs)
patchText := dmp.PatchToText(patches)
t.Fatalf("Unexpected output:\n%+v", patchText)
}
}
/**
Attempts to read a file and return the contents of that file as a string.
readOptionalTestFile returns an empty string if the file name parameter being passed
in is an empty string.
**/
func readOptionalTestFile(t *testing.T, fileName string) string {
var fileData string
if fileName != "" {
file, err := os.Open(fmt.Sprintf("%s/%s", "testdata", fileName))
if err != nil {
t.Fatalf("Failed to open expected output file: %v", err)
}
goldenStdOutFile, err := ioutil.ReadAll(file)
if err != nil {
t.Fatalf("Failed to read expected output file: %v", err)
}
fileData = string(goldenStdOutFile)
}
return fileData
}