-
Notifications
You must be signed in to change notification settings - Fork 8
/
utils_test.go
40 lines (37 loc) · 945 Bytes
/
utils_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
package main
import (
"bytes"
"os"
"os/exec"
"testing"
)
func copyFile(t *testing.T, src, dst string) {
t.Helper()
input, err := os.ReadFile(src)
if err != nil {
t.Fatalf("failed to read %s: %+v", src, err)
}
err = os.WriteFile(dst, input, 0777)
if err != nil {
t.Fatalf("failed to write %s: %+v", dst, err)
}
}
func compareFiles(t *testing.T, a, b string) {
contentA, err := os.ReadFile(a)
if err != nil {
t.Fatalf("failed to read file %s: %+v", a, err)
}
contentB, err := os.ReadFile(b)
if err != nil {
t.Fatalf("failed to read golden file %s: %+v", b, err)
}
if !bytes.Equal(contentA, contentB) {
if _, err := exec.LookPath("git"); err == nil {
diff := exec.Command("git", "diff", "--no-index", a, b)
stdoutStderr, _ := diff.CombinedOutput()
t.Fatalf("%s\n", stdoutStderr)
} else {
t.Fatalf("content of %s did not match %s.\nContent A:\n%s\n\nContent B:\n%s\n", a, b, contentA, contentB)
}
}
}