-
Notifications
You must be signed in to change notification settings - Fork 70
/
compatible_test.go
135 lines (129 loc) · 2.9 KB
/
compatible_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
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
package graphviz_test
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"image"
"os"
"os/exec"
"path/filepath"
"testing"
"github.com/corona10/goimagehash"
"github.com/goccy/go-graphviz"
)
var (
testPaths = []string{
filepath.Join("testdata", "directed"),
filepath.Join("testdata", "undirected"),
}
imageHashJSON = filepath.Join("testdata", "imagehash.json")
)
const (
imageThreshold = 40
)
func generateTestData() error {
pathToHashDump := map[string]string{}
for _, path := range testPaths {
if err := filepath.Walk(path, func(p string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
tmpfile, err := os.CreateTemp("", "graphviz")
if err != nil {
return err
}
defer os.Remove(tmpfile.Name())
if err := exec.Command("dot", "-Tpng", fmt.Sprintf("-o%s", tmpfile.Name()), p).Run(); err != nil {
return err
}
img, _, err := image.Decode(tmpfile)
if err != nil {
return err
}
hash, err := goimagehash.DifferenceHash(img)
if err != nil {
return err
}
var b bytes.Buffer
if err := hash.Dump(&b); err != nil {
return err
}
pathToHashDump[p] = base64.StdEncoding.EncodeToString(b.Bytes())
return nil
}); err != nil {
return err
}
}
content, err := json.Marshal(pathToHashDump)
if err != nil {
return err
}
if err := os.WriteFile(imageHashJSON, content, 0644); err != nil {
return err
}
return nil
}
func TestGraphviz_Compatible(t *testing.T) {
// generate testdata/imagehash.json
//if err := generateTestData(); err != nil {
// t.Fatal(err)
//}
var pathToHashDump map[string]string
file, err := os.ReadFile(imageHashJSON)
if err != nil {
t.Fatal(err)
}
if err := json.Unmarshal(file, &pathToHashDump); err != nil {
t.Fatal(err)
}
for _, path := range testPaths {
filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
t.Run(path, func(t *testing.T) {
file, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
graph, err := graphviz.ParseBytes(file)
if err != nil {
t.Fatal(err)
}
defer graph.Close()
ctx := context.Background()
g, err := graphviz.New(ctx)
if err != nil {
t.Fatal(err)
}
defer g.Close()
image, err := g.RenderImage(ctx, graph)
if err != nil {
t.Fatal(err)
}
hash, err := goimagehash.DifferenceHash(image)
if err != nil {
t.Fatal(err)
}
dump, err := base64.StdEncoding.DecodeString(pathToHashDump[path])
if err != nil {
t.Fatal(err)
}
targetHash, err := goimagehash.LoadImageHash(bytes.NewBuffer(dump))
if err != nil {
t.Fatal(err)
}
distance, err := hash.Distance(targetHash)
if err != nil {
t.Fatal(err)
}
if distance > imageThreshold {
t.Fatalf("doesn't compatible image with dot. %s distance = %d", path, distance)
}
})
return nil
})
}
}