-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdiff3_test.go
107 lines (97 loc) · 2.19 KB
/
diff3_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
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
package diff3_test
import (
"bytes"
"fmt"
"io"
"log"
"os"
"testing"
"github.com/epiclabs-io/diff3"
)
func compareReader(t *testing.T, a, b io.Reader) bool {
abytes, err := io.ReadAll(a)
if err != nil {
t.Fatal(err)
}
bbytes, err := io.ReadAll(b)
if err != nil {
t.Fatal(err)
}
return bytes.Equal(abytes, bbytes)
}
func TestDiff3(t *testing.T) {
const testDir = "./testdata"
const generate = false // set to true to genereate the expected files *-m.txt and *-error.txt
files, err := os.ReadDir(testDir)
if err != nil {
log.Fatal(err)
}
tests := make(map[string]bool)
for _, f := range files {
if len(f.Name()) > 3 {
tests[f.Name()[:3]] = true
}
}
for k := range tests {
func() {
a, err := os.Open(fmt.Sprintf("%s/%s-a.txt", testDir, k))
if err != nil {
log.Fatal(err)
}
defer a.Close()
b, err := os.Open(fmt.Sprintf("%s/%s-b.txt", testDir, k))
if err != nil {
log.Fatal(err)
}
defer b.Close()
o, err := os.Open(fmt.Sprintf("%s/%s-o.txt", testDir, k))
if err != nil {
log.Fatal(err)
}
defer o.Close()
var m io.ReadCloser
if !generate {
m, err = os.Open(fmt.Sprintf("%s/%s-m.txt", testDir, k))
if err != nil {
log.Fatal(err)
}
defer m.Close()
}
mr, mergeError := diff3.Merge(a, o, b, true, "A", "B")
if generate {
m, err := os.OpenFile(
fmt.Sprintf("%s/%s-m.txt", testDir, k),
os.O_WRONLY|os.O_TRUNC|os.O_CREATE,
0666,
)
if err != nil {
t.Fatal(err)
}
defer m.Close()
if mergeError == nil {
_, err = io.Copy(m, mr.Result)
if err != nil {
t.Fatal(err)
}
}
if mergeError != nil {
err = os.WriteFile(fmt.Sprintf("%s/%s-error.txt", testDir, k), []byte(mergeError.Error()), 0666)
if err != nil {
t.Fatal(err)
}
}
} else {
var expectError = false
if _, err := os.Stat(fmt.Sprintf("%s/%s-error.txt", testDir, k)); !os.IsNotExist(err) {
expectError = true
}
if mergeError != nil && !expectError {
t.Fatalf("Did not expect merge error: %s", err)
}
if !expectError && !compareReader(t, mr.Result, m) {
t.Fatalf("Test #%s does not match expected result", k)
}
}
}()
}
}