1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-04-25 12:24:47 +02:00

fixup! Introduce a yaml_utils.Walk function

This commit is contained in:
Stefan Haller 2024-03-30 12:59:44 +01:00
parent 8487bc397d
commit 84e82ea8b6

View File

@ -222,11 +222,21 @@ func TestWalk_paths(t *testing.T) {
document: "foo:\n x: 5", document: "foo:\n x: 5",
expectedPaths: []string{"", "foo", "foo.x"}, expectedPaths: []string{"", "foo", "foo.x"},
}, },
{
name: "deeply nested",
document: "foo:\n bar:\n baz: 5",
expectedPaths: []string{"", "foo", "foo.bar", "foo.bar.baz"},
},
{ {
name: "array", name: "array",
document: "foo:\n bar: [3, 7]", document: "foo:\n bar: [3, 7]",
expectedPaths: []string{"", "foo", "foo.bar", "foo.bar[0]", "foo.bar[1]"}, expectedPaths: []string{"", "foo", "foo.bar", "foo.bar[0]", "foo.bar[1]"},
}, },
{
name: "nested arrays",
document: "foo:\n bar: [[3, 7], [8, 9]]",
expectedPaths: []string{"", "foo", "foo.bar", "foo.bar[0]", "foo.bar[0][0]", "foo.bar[0][1]", "foo.bar[1]", "foo.bar[1][0]", "foo.bar[1][1]"},
},
} }
for _, test := range tests { for _, test := range tests {
@ -268,6 +278,32 @@ func TestWalk_inPlaceChanges(t *testing.T) {
}, },
expectedOut: "x: 7\ny: 3\n", expectedOut: "x: 7\ny: 3\n",
}, },
{
name: "change nested value",
in: "x:\n y: 5",
callback: func(node *yaml.Node, path string) bool {
if path == "x.y" {
node.Value = "7"
return true
}
return false
},
// indentation is not preserved. See https://github.com/go-yaml/yaml/issues/899
expectedOut: "x:\n y: 7\n",
},
{
name: "change array value",
in: "x:\n - y: 5",
callback: func(node *yaml.Node, path string) bool {
if path == "x[0].y" {
node.Value = "7"
return true
}
return false
},
// indentation is not preserved. See https://github.com/go-yaml/yaml/issues/899
expectedOut: "x:\n - y: 7\n",
},
} }
for _, test := range tests { for _, test := range tests {