-
Notifications
You must be signed in to change notification settings - Fork 4
/
integration_test.go
150 lines (123 loc) · 3.32 KB
/
integration_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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package mermaid_test
import (
"bytes"
"os"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/yuin/goldmark"
"go.abhg.dev/goldmark/mermaid"
"go.abhg.dev/goldmark/mermaid/internal/svgtest"
"go.abhg.dev/goldmark/mermaid/mermaidcdp"
"gopkg.in/yaml.v3"
)
func TestIntegration_Client(t *testing.T) {
t.Parallel()
testdata, err := os.ReadFile("testdata/client.yaml")
require.NoError(t, err)
var tests []struct {
Desc string `yaml:"desc"`
NoScript bool `yaml:"noscript"`
Give string `yaml:"give"`
Want string `yaml:"want"`
ContainerTag string `yaml:"containerTag"`
}
require.NoError(t, yaml.Unmarshal(testdata, &tests))
for _, tt := range tests {
tt := tt
t.Run(tt.Desc, func(t *testing.T) {
t.Parallel()
ext := mermaid.Extender{
RenderMode: mermaid.RenderModeClient,
MermaidURL: "mermaid.js",
NoScript: tt.NoScript,
ContainerTag: tt.ContainerTag,
}
md := goldmark.New(goldmark.WithExtensions(&ext))
var got bytes.Buffer
require.NoError(t, md.Convert([]byte(tt.Give), &got))
assert.Equal(t,
strings.TrimSuffix(tt.Want, "\n"),
strings.TrimSuffix(got.String(), "\n"),
)
})
}
}
func TestIntegration_Server_CLI(t *testing.T) {
t.Parallel()
mmdcPath := filepath.Join("node_modules", ".bin", "mmdc")
if _, err := os.Stat(mmdcPath); err != nil {
// 'yarn install' must already have been run.
t.Fatalf("mmdc not found at %s", mmdcPath)
}
testdata, err := os.ReadFile("testdata/server_cli.yaml")
require.NoError(t, err)
var tests []struct {
Desc string `yaml:"desc"`
Give string `yaml:"give"`
Want string `yaml:"want"`
ContainerTag string `yaml:"containerTag"`
}
require.NoError(t, yaml.Unmarshal(testdata, &tests))
for _, tt := range tests {
tt := tt
t.Run(tt.Desc, func(t *testing.T) {
t.Parallel()
ext := mermaid.Extender{
CLI: mermaid.MMDC(mmdcPath),
RenderMode: mermaid.RenderModeServer,
ContainerTag: tt.ContainerTag,
}
md := goldmark.New(goldmark.WithExtensions(&ext))
var got bytes.Buffer
require.NoError(t, md.Convert([]byte(tt.Give), &got))
assert.Equal(t,
svgtest.Normalize(tt.Want),
svgtest.Normalize(got.String()),
)
})
}
}
func TestIntegration_Server_CDP(t *testing.T) {
cdpCompiler, err := mermaidcdp.New(&mermaidcdp.Config{
JSSource: loadMermaidJS(t),
})
require.NoError(t, err)
t.Cleanup(func() {
assert.NoError(t, cdpCompiler.Close(),
"unable to stop the CDP compiler")
})
testdata, err := os.ReadFile("testdata/server_cdp.yaml")
require.NoError(t, err)
var tests []struct {
Desc string `yaml:"desc"`
Give string `yaml:"give"`
Want string `yaml:"want"`
}
require.NoError(t, yaml.Unmarshal(testdata, &tests))
for _, tt := range tests {
tt := tt
t.Run(tt.Desc, func(t *testing.T) {
t.Parallel()
ext := mermaid.Extender{
Compiler: cdpCompiler,
RenderMode: mermaid.RenderModeServer,
}
md := goldmark.New(goldmark.WithExtensions(&ext))
var got bytes.Buffer
require.NoError(t, md.Convert([]byte(tt.Give), &got))
assert.Equal(t,
svgtest.Normalize(tt.Want),
svgtest.Normalize(got.String()),
)
})
}
}
func loadMermaidJS(t *testing.T) string {
t.Helper()
b, err := os.ReadFile("testdata/mermaid.js")
require.NoError(t, err)
return string(b)
}