This repository has been archived by the owner on Jun 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
edit_test.go
320 lines (288 loc) · 5.53 KB
/
edit_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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// Copyright 2020 VMware, Inc.
// SPDX-License-Identifier: BSD-2-Clause
package yamled_test
import (
"fmt"
"log"
"testing"
yamled "github.com/vmware-labs/go-yaml-edit"
yptr "github.com/vmware-labs/yaml-jsonpointer"
"golang.org/x/text/transform"
"gopkg.in/yaml.v3"
)
// ExampleT shows how to use the transformer to edit a YAML source in place.
// It also shows how the quoting style is preserved.
func ExampleT() {
src := `apiVersion: v1
kind: Service
metadata:
name: "foo" # some comment
namespace: myns
`
var root yaml.Node
if err := yaml.Unmarshal([]byte(src), &root); err != nil {
log.Fatal(err)
}
// Let's find some nodes in the YAML tree using the YAML JSONPointer library yptr.
nameNode, err := yptr.Find(&root, "/metadata/name")
if err != nil {
log.Fatal(err)
}
nsNode, err := yptr.Find(&root, "/metadata/namespace")
if err != nil {
log.Fatal(err)
}
out, _, err := transform.String(yamled.T(
yamled.Node(nameNode).With("bar"),
yamled.Node(nsNode).With("otherns"),
), src)
if err != nil {
log.Fatal(err)
}
fmt.Println(out)
// Output:
// apiVersion: v1
// kind: Service
// metadata:
// name: "bar" # some comment
// namespace: otherns
}
func TestEdit(t *testing.T) {
srcs := []struct {
src string
foo string
bar string
}{
{
src: `foo: abc
bar: xy
baz: end
`,
foo: "/foo",
bar: "/bar",
},
{
src: `foo: abc
data:
bar: xy
baz: end
`,
foo: "/foo",
bar: "/data/bar",
},
{
src: `bar: xy
data:
foo: abc
baz: end
`,
foo: "/data/foo",
bar: "/bar",
},
{
src: `bar: xy
data:
deeper:
foo: abc
baz: end
`,
foo: "/data/deeper/foo",
bar: "/bar",
},
}
testCases := []struct {
foo string
bar string
}{
{
foo: "AB",
bar: "xyz",
},
{
foo: "ABCD",
bar: "x",
},
{
foo: "ABCD",
bar: "",
},
{
foo: "",
bar: "x",
},
{
foo: "",
bar: "a#b",
},
{
foo: "",
bar: "a #b",
},
{
foo: "",
bar: " ",
},
{
foo: "a",
bar: "2",
},
{
foo: "a\nb\n",
bar: "ab",
},
{
foo: "\na\nb\n",
bar: "ab",
},
{
foo: "\na\nb\n\n\n",
bar: "ab",
},
{
foo: "a",
bar: "\n",
},
}
for i, tc := range testCases {
for j, cfg := range srcs {
t.Run(fmt.Sprintf("%d_%d", i, j), func(t *testing.T) {
var n yaml.Node
buf := []byte(cfg.src)
if err := yaml.Unmarshal(buf, &n); err != nil {
t.Fatal(err)
}
foo, err := yptr.Find(&n, cfg.foo)
if err != nil {
t.Fatal(err)
}
bar, err := yptr.Find(&n, cfg.bar)
if err != nil {
t.Fatal(err)
}
buf, _, err = transform.Bytes(yamled.T(
yamled.Node(foo).With(tc.foo),
yamled.Node(bar).With(tc.bar),
), []byte(cfg.src))
if err != nil {
t.Fatal(err)
}
t.Logf("after:\n%s", string(buf))
var ne yaml.Node
if err := yaml.Unmarshal(buf, &ne); err != nil {
t.Fatal(err)
}
check := func(path, want string) {
f, err := yptr.Find(&ne, path)
if err != nil {
t.Fatal(err)
}
if got := f.Value; got != want {
t.Errorf("got: %q, want: %q", got, want)
}
if tag := f.Tag; tag != "!!str" && tag != "!!null" {
t.Errorf("tag for %q must be either string or null, got %q", path, tag)
}
}
check(cfg.foo, tc.foo)
check(cfg.bar, tc.bar)
})
}
}
}
func TestIndent(t *testing.T) {
// the long strings here exercise ErrShortSrc and ErrShortDst codepaths in the transformer.
src := `out:
foo: abc
other:
bar: xy
baz: end
xxx: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
yyy: yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
www:
y: zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
`
testCases := []struct {
foo string
bar string
}{
{
foo: "edit test",
bar: "long\nzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz",
},
}
for i, tc := range testCases {
t.Run(fmt.Sprint(i), func(t *testing.T) {
var n yaml.Node
buf := []byte(src)
if err := yaml.Unmarshal(buf, &n); err != nil {
t.Fatal(err)
}
foo, err := yptr.Find(&n, "/out/foo")
if err != nil {
t.Fatal(err)
}
bar, err := yptr.Find(&n, "/out/other/bar")
if err != nil {
t.Fatal(err)
}
buf, _, err = transform.Bytes(yamled.T(
yamled.Node(foo).With(tc.foo),
yamled.Node(bar).With(tc.bar),
), []byte(src))
if err != nil {
t.Fatal(err)
}
t.Logf("after:\n%s", string(buf))
var ne yaml.Node
if err := yaml.Unmarshal(buf, &ne); err != nil {
t.Fatal(err)
}
check := func(path, want string) {
f, err := yptr.Find(&ne, path)
if err != nil {
t.Fatal(err)
}
if got := f.Value; got != want {
t.Errorf("got: %q, want: %q", got, want)
}
if tag := f.Tag; tag != "!!str" && tag != "!!null" {
t.Errorf("tag for %q must be either string or null, got %q", path, tag)
}
}
check("/out/foo", tc.foo)
check("/out/other/bar", tc.bar)
})
}
}
func TestBug1(t *testing.T) {
src := `data:
foo: |
bar: x
`
rep := `x: y
bar: y
`
var root yaml.Node
if err := yaml.Unmarshal([]byte(src), &root); err != nil {
t.Fatal(err)
}
foo, err := yptr.Find(&root, "/data/foo")
if err != nil {
t.Fatal(err)
}
out, _, err := transform.Bytes(yamled.T(
yamled.Node(foo).With(rep),
), []byte(src))
if err != nil {
t.Fatal(err)
}
t.Logf("got:\n%s", out)
want := `data:
foo: |
x: y
bar: y
`
if got := string(out); got != want {
t.Errorf("got: %q, want: %q", got, want)
}
}