-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathapply_test.go
More file actions
54 lines (41 loc) · 1.42 KB
/
apply_test.go
File metadata and controls
54 lines (41 loc) · 1.42 KB
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
package overlay_test
import (
"bytes"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
)
// NodeMatchesFile is a test that marshals the YAML file from the given node,
// then compares those bytes to those found in the expected file.
func NodeMatchesFile(
t *testing.T,
actual *yaml.Node,
expectedFile string,
msgAndArgs ...any,
) {
variadoc := func(pre ...any) []any { return append(msgAndArgs, pre...) }
var actualBuf bytes.Buffer
enc := yaml.NewEncoder(&actualBuf)
enc.SetIndent(2)
err := enc.Encode(actual)
require.NoError(t, err, variadoc("failed to marshal node: ")...)
expectedBytes, err := os.ReadFile(expectedFile)
require.NoError(t, err, variadoc("failed to read expected file: ")...)
// lazy redo snapshot
//os.WriteFile(expectedFile, actualBuf.Bytes(), 0644)
//t.Log("### EXPECT START ###\n" + string(expectedBytes) + "\n### EXPECT END ###\n")
//t.Log("### ACTUAL START ###\n" + actualBuf.string() + "\n### ACTUAL END ###\n")
assert.Equal(t, string(expectedBytes), actualBuf.String(), variadoc("node does not match expected file: ")...)
}
func TestApplyTo(t *testing.T) {
t.Parallel()
node, err := LoadSpecification("testdata/openapi.yaml")
require.NoError(t, err)
o, err := LoadOverlay("testdata/overlay.yaml")
require.NoError(t, err)
err = o.ApplyTo(node)
assert.NoError(t, err)
NodeMatchesFile(t, node, "testdata/openapi-overlayed.yaml")
}