-
Notifications
You must be signed in to change notification settings - Fork 0
/
snapshot.go
131 lines (111 loc) · 3.4 KB
/
snapshot.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
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package snapshotx
import (
"encoding/json"
"fmt"
"strings"
"testing"
"github.com/clinia/x/stringslice"
"github.com/tidwall/gjson"
"github.com/bradleyjkemp/cupaloy/v2"
"github.com/stretchr/testify/require"
"github.com/tidwall/sjson"
)
type (
ExceptOpt interface {
apply(t *testing.T, raw []byte) []byte
}
exceptPaths []string
exceptNestedKeys []string
)
func (e exceptPaths) apply(t *testing.T, raw []byte) []byte {
for _, ee := range e {
var err error
raw, err = sjson.DeleteBytes(raw, ee)
require.NoError(t, err)
}
return raw
}
func (e exceptNestedKeys) apply(t *testing.T, raw []byte) []byte {
parsed := gjson.ParseBytes(raw)
require.True(t, parsed.IsObject() || parsed.IsArray())
return deleteMatches(t, "", parsed, e, []string{}, raw)
}
func ExceptPaths(keys ...string) ExceptOpt {
return exceptPaths(keys)
}
func ExceptNestedKeys(nestedKeys ...string) ExceptOpt {
return exceptNestedKeys(nestedKeys)
}
func SnapshotT(t *testing.T, actual interface{}, except ...ExceptOpt) {
compare, err := json.MarshalIndent(actual, "", " ")
require.NoError(t, err, "%+v", actual)
for _, e := range except {
compare = e.apply(t, compare)
}
cupaloy.New(
cupaloy.CreateNewAutomatically(true),
cupaloy.FailOnUpdate(true),
cupaloy.SnapshotFileExtension(".json"),
).SnapshotT(t, compare)
}
// SnapshotTExcept
//
// DEPRECATED: please use SnapshotT instead
func SnapshotTExcept(t *testing.T, actual interface{}, except []string) {
compare, err := json.MarshalIndent(actual, "", " ")
require.NoError(t, err, "%+v", actual)
for _, e := range except {
compare, err = sjson.DeleteBytes(compare, e)
require.NoError(t, err, "%s", e)
}
cupaloy.New(
cupaloy.CreateNewAutomatically(true),
cupaloy.FailOnUpdate(true),
cupaloy.SnapshotFileExtension(".json"),
).SnapshotT(t, compare)
}
func deleteMatches(t *testing.T, key string, result gjson.Result, matches []string, parents []string, content []byte) []byte {
path := parents
if key != "" {
path = append(parents, key)
}
if result.IsObject() {
result.ForEach(func(key, value gjson.Result) bool {
content = deleteMatches(t, key.String(), value, matches, path, content)
return true
})
} else if result.IsArray() {
var i int
result.ForEach(func(_, value gjson.Result) bool {
content = deleteMatches(t, fmt.Sprintf("%d", i), value, matches, path, content)
i++
return true
})
}
if stringslice.Has(matches, key) {
content, err := sjson.DeleteBytes(content, strings.Join(path, "."))
require.NoError(t, err)
return content
}
return content
}
// SnapshotTExceptMatchingKeys works like SnapshotTExcept but deletes keys that match the given matches recursively.
//
// So instead of having deeply nested keys like `foo.bar.baz.0.key_to_delete` you can have `key_to_delete` and
// all occurences of `key_to_delete` will be removed.
//
// DEPRECATED: please use SnapshotT instead
func SnapshotTExceptMatchingKeys(t *testing.T, actual interface{}, matches []string) {
compare, err := json.MarshalIndent(actual, "", " ")
require.NoError(t, err, "%+v", actual)
parsed := gjson.ParseBytes(compare)
require.True(t, parsed.IsObject() || parsed.IsArray())
compare = deleteMatches(t, "", parsed, matches, []string{}, compare)
cupaloy.New(
cupaloy.CreateNewAutomatically(true),
cupaloy.FailOnUpdate(true),
cupaloy.SnapshotFileExtension(".json"),
).SnapshotT(t, compare)
}