Debian Codegraph @

internal/test/golden.go

raw
1 package test
2
3 import (
4 "bytes"
5 "flag"
6 "fmt"
7 "io/ioutil"
8 "os"
9 "path/filepath"
10 "strings"
11 "testing"
12 )
13
14 var update = flag.Bool("update", false, "Update .golden files")
15
16 type section struct {
17 ctype byte
18 s []string
19 }
20
21 // Inspired by https://github.com/paulgb/simplediff
22 func diff(before, after []string) []section {
23 beforeMap := make(map[string][]int)
24 for i, s := range before {
25 beforeMap[s] = append(beforeMap[s], i)
26 }
27 overlap := make([]int, len(before))
28 // Track start/len of largest overlapping match in old/new
29 var startBefore, startAfter, subLen int
30 for iafter, s := range after {
31 o := make([]int, len(before))
32 for _, ibefore := range beforeMap[s] {
33 idx := 1
34 if ibefore > 0 && overlap[ibefore-1] > 0 {
35 idx = overlap[ibefore-1] + 1
36 }
37 o[ibefore] = idx
38 if idx > subLen {
39 // largest substring so far, store indices
40 subLen = o[ibefore]
41 startBefore = ibefore - subLen + 1
42 startAfter = iafter - subLen + 1
43 }
44 }
45 overlap = o
46 }
47 if subLen == 0 {
48 // No common substring, issue - and +
49 r := make([]section, 0)
50 if len(before) > 0 {
51 r = append(r, section{'-', before})
52 }
53 if len(after) > 0 {
54 r = append(r, section{'+', after})
55 }
56 return r
57 }
58 // common substring unchanged, recurse on before/after substring
59 r := diff(before[0:startBefore], after[0:startAfter])
60 r = append(r, section{' ', after[startAfter : startAfter+subLen]})
61 r = append(r, diff(before[startBefore+subLen:], after[startAfter+subLen:])...)
62 return r
63 }
64
65 // DiffStrings does a entry by entry comparison of got and want.
66 func DiffStrings(t *testing.T, got []string, want []string) {
67 t.Helper()
68 if len(got) == 0 && len(want) == 0 {
69 return
70 }
71 sections := diff(want, got)
72 if len(sections) == 1 && sections[0].ctype == ' ' {
73 // Equal
74 return
75 }
76 t.Error("diff -want +got:")
77 for _, s := range sections {
78 if s.ctype == ' ' && len(s.s) > 5 {
79 // Omit excess unchanged lines
80 for i := 0; i < 2; i++ {
81 t.Logf("|%c%s", s.ctype, s.s[i])
82 }
83 t.Log("...")
84 for i := len(s.s) - 2; i < len(s.s); i++ {
85 t.Logf("|%c%s", s.ctype, s.s[i])
86 }
87 continue
88 }
89 for _, l := range s.s {
90 t.Logf("|%c%s", s.ctype, l)
91 }
92 }
93 }
94
95 // DiffLines does a line by line comparison of got and want.
96 func DiffLines(t *testing.T, got []byte, want []byte) {
97 t.Helper()
98 if !bytes.Equal(got, want) {
99 b := bytes.NewBufferString("diff -want +got:\n")
100 glines := strings.Split(string(got), "\n")
101 wlines := strings.Split(string(want), "\n")
102 sections := diff(wlines, glines)
103 for _, s := range sections {
104 if s.ctype == ' ' && len(s.s) > 5 {
105 // Omit excess unchanged lines
106 for i := 0; i < 2; i++ {
107 fmt.Fprintf(b, "|%c%s\n", s.ctype, s.s[i])
108 }
109 b.WriteString("...\n")
110 for i := len(s.s) - 2; i < len(s.s); i++ {
111 fmt.Fprintf(b, "|%c%s\n", s.ctype, s.s[i])
112 }
113 continue
114 }
115 for _, l := range s.s {
116 fmt.Fprintf(b, "|%c%s\n", s.ctype, l)
117 }
118 }
119 t.Error(b.String())
120 }
121 }
122
123 // DiffGolden does a line by comparison of got to the golden file specified by path. If the update
124 // flag is true, differing golden files will be updated with lines in got.
125 func DiffGolden(t *testing.T, got []byte, path ...string) {
126 t.Helper()
127 pathstr := filepath.Join(path...)
128 f, err := os.Open(pathstr)
129 if err != nil {
130 t.Fatal(err)
131 }
132 golden, err := ioutil.ReadAll(f)
133 if err != nil {
134 t.Fatal(err)
135 }
136 if !bytes.Equal(got, golden) {
137 if *update {
138 // Update golden file
139 if err := ioutil.WriteFile(pathstr, got, 0666); err != nil {
140 t.Fatal(err)
141 }
142 } else {
143 t.Errorf("Test output did not match %s\nTo update golden file, run: go test -update", pathstr)
144 // Fail test with differences
145 DiffLines(t, got, golden)
146 }
147 }
148 }