Skip to content

Commit 2470b8b

Browse files
authored
Merge pull request #1505 from afbjorklund/yaml3-validate
Make sure that yaml is valid with gopkg.in/yaml.v3 as well
2 parents 6b51e5d + 1d5cfe9 commit 2470b8b

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ require (
3737
golang.org/x/sync v0.1.0
3838
golang.org/x/sys v0.7.0
3939
gopkg.in/op/go-logging.v1 v1.0.0-20160211212156-b2cb9fa56473
40+
gopkg.in/yaml.v3 v3.0.1
4041
gotest.tools/v3 v3.4.0
4142
inet.af/tcpproxy v0.0.0-20220326234310-be3ee21c9fa0
4243
k8s.io/api v0.26.3
@@ -112,7 +113,6 @@ require (
112113
gopkg.in/inf.v0 v0.9.1 // indirect
113114
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
114115
gopkg.in/yaml.v2 v2.4.0 // indirect
115-
gopkg.in/yaml.v3 v3.0.1 // indirect
116116
gvisor.dev/gvisor v0.0.0-20221216231429-a78e892a26d2 // indirect
117117
k8s.io/klog/v2 v2.90.1 // indirect
118118
k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280 // indirect

pkg/limayaml/load.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,18 @@ import (
1010
"github.com/lima-vm/lima/pkg/store/dirnames"
1111
"github.com/lima-vm/lima/pkg/store/filenames"
1212
"github.com/sirupsen/logrus"
13+
yamlv3 "gopkg.in/yaml.v3"
1314
)
1415

1516
func unmarshalYAML(data []byte, v interface{}, comment string) error {
1617
if err := yaml.UnmarshalWithOptions(data, v, yaml.DisallowDuplicateKey()); err != nil {
1718
return fmt.Errorf("failed to unmarshal YAML (%s): %w", comment, err)
1819
}
20+
// the go-yaml library doesn't catch all markup errors, unfortunately
21+
// make sure to get a "second opinion", using the same library as "yq"
22+
if err := yamlv3.Unmarshal(data, v); err != nil {
23+
return fmt.Errorf("failed to unmarshal YAML (%s): %w", comment, err)
24+
}
1925
if err := yaml.UnmarshalWithOptions(data, v, yaml.Strict()); err != nil {
2026
logrus.WithField("comment", comment).WithError(err).Warn("Non-strict YAML is deprecated and will be unsupported in a future version of Lima")
2127
// Non-strict YAML is known to be used by Rancher Desktop:

pkg/limayaml/load_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package limayaml
2+
3+
import (
4+
"testing"
5+
6+
"gotest.tools/v3/assert"
7+
)
8+
9+
func TestLoadEmpty(t *testing.T) {
10+
_, err := Load([]byte{}, "empty.yaml")
11+
assert.NilError(t, err)
12+
}
13+
14+
func TestLoadError(t *testing.T) {
15+
// missing a "script:" line
16+
s := `
17+
provision:
18+
- mode: system
19+
script: |
20+
#!/bin/sh
21+
echo one
22+
- mode: system
23+
#!/bin/sh
24+
echo two
25+
- mode: system
26+
script: |
27+
#!/bin/sh
28+
echo three
29+
`
30+
_, err := Load([]byte(s), "error.yaml")
31+
assert.ErrorContains(t, err, "failed to unmarshal YAML")
32+
}

0 commit comments

Comments
 (0)