@@ -8,113 +8,6 @@ import (
8
8
"gopkg.in/yaml.v3"
9
9
)
10
10
11
- func TestUpdateYamlValue (t * testing.T ) {
12
- tests := []struct {
13
- name string
14
- in string
15
- path []string
16
- value string
17
- expectedOut string
18
- expectedErr string
19
- }{
20
- {
21
- name : "update value" ,
22
- in : "foo: bar\n " ,
23
- path : []string {"foo" },
24
- value : "baz" ,
25
- expectedOut : "foo: baz\n " ,
26
- expectedErr : "" ,
27
- },
28
- {
29
- name : "add new key and value" ,
30
- in : "foo: bar\n " ,
31
- path : []string {"foo2" },
32
- value : "baz" ,
33
- expectedOut : "foo: bar\n foo2: baz\n " ,
34
- expectedErr : "" ,
35
- },
36
- {
37
- name : "add new key and value when document was empty" ,
38
- in : "" ,
39
- path : []string {"foo" },
40
- value : "bar" ,
41
- expectedOut : "foo: bar\n " ,
42
- expectedErr : "" ,
43
- },
44
- {
45
- name : "preserve inline comment" ,
46
- in : "foo: bar # my comment\n " ,
47
- path : []string {"foo2" },
48
- value : "baz" ,
49
- expectedOut : "foo: bar # my comment\n foo2: baz\n " ,
50
- expectedErr : "" ,
51
- },
52
- {
53
- name : "nested update" ,
54
- in : "foo:\n bar: baz\n " ,
55
- path : []string {"foo" , "bar" },
56
- value : "qux" ,
57
- expectedOut : "foo:\n bar: qux\n " ,
58
- expectedErr : "" ,
59
- },
60
- {
61
- name : "nested where parents doesn't exist yet" ,
62
- in : "" ,
63
- path : []string {"foo" , "bar" , "baz" },
64
- value : "qux" ,
65
- expectedOut : "foo:\n bar:\n baz: qux\n " ,
66
- expectedErr : "" ,
67
- },
68
- {
69
- name : "don't rewrite file if value didn't change" ,
70
- in : "foo:\n bar: baz\n " ,
71
- path : []string {"foo" , "bar" },
72
- value : "baz" ,
73
- expectedOut : "foo:\n bar: baz\n " ,
74
- expectedErr : "" ,
75
- },
76
-
77
- // Error cases
78
- {
79
- name : "existing document is not a dictionary" ,
80
- in : "42\n " ,
81
- path : []string {"foo" },
82
- value : "bar" ,
83
- expectedOut : "42\n " ,
84
- expectedErr : "yaml document is not a dictionary" ,
85
- },
86
- {
87
- name : "trying to update a note that is not a scalar" ,
88
- in : "foo: [1, 2, 3]\n " ,
89
- path : []string {"foo" },
90
- value : "bar" ,
91
- expectedOut : "foo: [1, 2, 3]\n " ,
92
- expectedErr : "yaml node is not a scalar" ,
93
- },
94
- {
95
- name : "not all path elements are dictionaries" ,
96
- in : "foo:\n bar: [1, 2, 3]\n " ,
97
- path : []string {"foo" , "bar" , "baz" },
98
- value : "qux" ,
99
- expectedOut : "foo:\n bar: [1, 2, 3]\n " ,
100
- expectedErr : "yaml node in path is not a dictionary" ,
101
- },
102
- }
103
-
104
- for _ , test := range tests {
105
- t .Run (test .name , func (t * testing.T ) {
106
- out , actualErr := UpdateYamlValue ([]byte (test .in ), test .path , test .value )
107
- if test .expectedErr == "" {
108
- assert .NoError (t , actualErr )
109
- } else {
110
- assert .EqualError (t , actualErr , test .expectedErr )
111
- }
112
-
113
- assert .Equal (t , test .expectedOut , string (out ))
114
- })
115
- }
116
- }
117
-
118
11
func TestRenameYamlKey (t * testing.T ) {
119
12
tests := []struct {
120
13
name string
0 commit comments