Skip to content

Commit 2a68c54

Browse files
committed
fix(overlay): stabilize explicitly tagged folded scalars and strengthen tests
1 parent ea403f4 commit 2a68c54

2 files changed

Lines changed: 65 additions & 11 deletions

File tree

overlay/foldedscalar.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ func stabilizeFoldedScalars(node *yaml.Node) {
2626
return
2727
}
2828

29-
if node.Kind == yaml.ScalarNode && node.Style == yaml.FoldedStyle && hasMoreIndentedLine(node.Value) {
30-
node.Style = yaml.LiteralStyle
29+
if node.Kind == yaml.ScalarNode && node.Style&yaml.FoldedStyle != 0 && hasMoreIndentedLine(node.Value) {
30+
node.Style = node.Style&^yaml.FoldedStyle | yaml.LiteralStyle
3131
}
3232

3333
for _, child := range node.Content {

overlay/foldedscalar_test.go

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,50 @@ func TestApplyToSurvivesRepeatedApplies(t *testing.T) {
7979
}
8080
}
8181

82+
func TestApplyToStrictSurvivesRepeatedApplies(t *testing.T) {
83+
t.Parallel()
84+
85+
o := &Overlay{
86+
Version: "1.0.0",
87+
Info: Info{Title: "Test", Version: "1.0.0"},
88+
Actions: []Action{
89+
{
90+
Target: "$.title",
91+
Update: yaml.Node{Kind: yaml.ScalarNode, Tag: "!!str", Value: "Updated"},
92+
},
93+
},
94+
}
95+
96+
doc := "title: Original\n" + foldedWithMoreIndentedLine
97+
want := decodeDescription(t, doc)
98+
99+
for i := range 30 {
100+
var node yaml.Node
101+
require.NoError(t, yaml.Unmarshal([]byte(doc), &node))
102+
_, err := o.ApplyToStrict(&node)
103+
require.NoError(t, err)
104+
105+
out, err := yaml.Marshal(&node)
106+
require.NoError(t, err)
107+
108+
doc = string(out)
109+
assert.Equal(t, want, decodeDescription(t, doc), "value changed after %d applies", i+1)
110+
}
111+
}
112+
113+
func TestStabilizeFoldedScalarsHandlesExplicitTags(t *testing.T) {
114+
t.Parallel()
115+
116+
doc := "description: !!str >-\n a\n b\n"
117+
want := decodeDescription(t, doc)
118+
119+
for i := range 5 {
120+
doc = roundTrip(t, doc, true)
121+
assert.Equal(t, want, decodeDescription(t, doc), "value changed after %d round trips", i+1)
122+
}
123+
assert.Contains(t, doc, "!!str", "explicit tag should be preserved")
124+
}
125+
82126
func TestStabilizeFoldedScalarsSurvivesRepeatedRoundTrips(t *testing.T) {
83127
t.Parallel()
84128

@@ -105,16 +149,19 @@ func TestStabilizeFoldedScalarsLeavesStableStylesAlone(t *testing.T) {
105149
t.Parallel()
106150

107151
tests := []struct {
108-
name string
109-
doc string
152+
name string
153+
doc string
154+
style string
110155
}{
111156
{
112-
name: "folded scalar with no more-indented line",
113-
doc: "description: >-\n one line\n another line\n",
157+
name: "folded scalar with no more-indented line",
158+
doc: "description: >-\n one line\n another line\n",
159+
style: ">-",
114160
},
115161
{
116-
name: "literal scalar with more-indented line",
117-
doc: "description: |-\n one line\n more indented\n",
162+
name: "literal scalar with more-indented line",
163+
doc: "description: |-\n one line\n more indented\n",
164+
style: "|-",
118165
},
119166
{
120167
name: "plain scalar",
@@ -128,10 +175,17 @@ func TestStabilizeFoldedScalarsLeavesStableStylesAlone(t *testing.T) {
128175

129176
want := decodeDescription(t, tt.doc)
130177

131-
doc := tt.doc
178+
doc := roundTrip(t, tt.doc, true)
179+
assert.Equal(t, want, decodeDescription(t, doc))
180+
if tt.style != "" {
181+
assert.Contains(t, doc, tt.style, "original style should be preserved")
182+
}
183+
132184
for range 5 {
133-
doc = roundTrip(t, doc, true)
134-
assert.Equal(t, want, decodeDescription(t, doc))
185+
next := roundTrip(t, doc, true)
186+
assert.Equal(t, doc, next, "representation should be a fixed point")
187+
assert.Equal(t, want, decodeDescription(t, next))
188+
doc = next
135189
}
136190
})
137191
}

0 commit comments

Comments
 (0)