Skip to content

Commit 23c836c

Browse files
committed
Bolster tests, mostly in error-handling
1 parent 2e3340b commit 23c836c

File tree

2 files changed

+234
-1
lines changed

2 files changed

+234
-1
lines changed

kyaml/kyaml.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ func (ky *Encoder) renderNode(node *yaml.Node, indent int, flags flagMask, out i
207207
case yaml.AliasNode:
208208
return ky.renderAlias(node, indent, flags, out)
209209
}
210-
return nil
210+
return fmt.Errorf("kyaml internal error: line %d: unknown node kind %v", node.Line, node.Kind)
211211
}
212212

213213
// renderDocument processes a YAML document node, rendering it to the output.

kyaml/kyaml_test.go

Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
"github.com/google/go-cmp/cmp"
2727
"github.com/google/go-cmp/cmp/cmpopts"
28+
yamlv3 "go.yaml.in/yaml/v3"
2829
"sigs.k8s.io/randfill"
2930
"sigs.k8s.io/yaml"
3031
)
@@ -2334,6 +2335,25 @@ func TestKYAMLFromYAML(t *testing.T) {
23342335
# It can also be multi-line.
23352336
`,
23362337
expectCompact: "---\n42\n",
2338+
}, {
2339+
name: "YAML alias",
2340+
input: `
2341+
list:
2342+
- &alias 42
2343+
- 43
2344+
- *alias
2345+
`,
2346+
expectRegular: `
2347+
---
2348+
{
2349+
list: [
2350+
42,
2351+
43,
2352+
42,
2353+
],
2354+
}
2355+
`,
2356+
expectCompact: "---\n{list: [42, 43, 42]}\n",
23372357
},
23382358
}
23392359

@@ -2574,3 +2594,216 @@ func TestRenderStringEscapes(t *testing.T) {
25742594
})
25752595
}
25762596
}
2597+
2598+
func TestUnexpectedErrors(t *testing.T) {
2599+
// This test is to ensure that we don't panic when we encounter unexpected
2600+
// errors, such as nil pointers or other runtime errors.
2601+
ky := &Encoder{}
2602+
2603+
t.Run("renderNode(nil)", func(t *testing.T) {
2604+
err := ky.renderNode(nil, 0, 0, nil)
2605+
if err != nil {
2606+
t.Errorf("unexpected error: %v", err)
2607+
}
2608+
})
2609+
2610+
t.Run("renderNode(<unknown kind>)", func(t *testing.T) {
2611+
n := yamlv3.Node{
2612+
Kind: 0,
2613+
}
2614+
err := ky.renderNode(&n, 0, 0, nil)
2615+
if err == nil {
2616+
t.Errorf("expected error")
2617+
}
2618+
})
2619+
2620+
t.Run("YAML Node without tag", func(t *testing.T) {
2621+
n := yamlv3.Node{
2622+
Kind: yamlv3.DocumentNode,
2623+
Content: []*yamlv3.Node{
2624+
&yamlv3.Node{Kind: yamlv3.ScalarNode, Value: "test", Tag: ""},
2625+
},
2626+
}
2627+
err := ky.renderDocument(&n, 0, 0, nil)
2628+
if err == nil {
2629+
t.Errorf("expected error")
2630+
}
2631+
})
2632+
2633+
t.Run("renderDocument", func(t *testing.T) {
2634+
buf := bytes.Buffer{}
2635+
t.Run("should pass", func(t *testing.T) {
2636+
n := yamlv3.Node{
2637+
Kind: yamlv3.DocumentNode,
2638+
Content: []*yamlv3.Node{
2639+
&yamlv3.Node{Kind: yamlv3.ScalarNode, Value: "test", Tag: "!!str"},
2640+
},
2641+
}
2642+
err := ky.renderDocument(&n, 0, 0, &buf)
2643+
if err != nil {
2644+
t.Errorf("unexpected error: %v", err)
2645+
}
2646+
})
2647+
t.Run("0 contents", func(t *testing.T) {
2648+
n := yamlv3.Node{
2649+
Kind: yamlv3.DocumentNode,
2650+
Content: nil,
2651+
}
2652+
err := ky.renderDocument(&n, 0, 0, &buf)
2653+
if err == nil {
2654+
t.Errorf("expected error")
2655+
}
2656+
})
2657+
t.Run("2 contents", func(t *testing.T) {
2658+
n := yamlv3.Node{
2659+
Kind: yamlv3.DocumentNode,
2660+
Content: []*yamlv3.Node{
2661+
&yamlv3.Node{Kind: yamlv3.ScalarNode, Value: "test1", Tag: "!!str"},
2662+
&yamlv3.Node{Kind: yamlv3.ScalarNode, Value: "test2", Tag: "!!str"},
2663+
},
2664+
}
2665+
err := ky.renderDocument(&n, 0, 0, &buf)
2666+
if err == nil {
2667+
t.Errorf("expected error")
2668+
}
2669+
})
2670+
t.Run("non-zero indent", func(t *testing.T) {
2671+
n := yamlv3.Node{
2672+
Kind: yamlv3.DocumentNode,
2673+
Content: []*yamlv3.Node{
2674+
&yamlv3.Node{Kind: yamlv3.ScalarNode, Value: "test", Tag: "!!str"},
2675+
},
2676+
}
2677+
err := ky.renderDocument(&n, 1, 0, &buf)
2678+
if err == nil {
2679+
t.Errorf("expected error")
2680+
}
2681+
})
2682+
})
2683+
2684+
t.Run("renderScalar", func(t *testing.T) {
2685+
buf := bytes.Buffer{}
2686+
t.Run("should pass", func(t *testing.T) {
2687+
n := yamlv3.Node{
2688+
Kind: yamlv3.ScalarNode,
2689+
Value: "test",
2690+
Tag: "!!str",
2691+
}
2692+
err := ky.renderScalar(&n, 0, 0, &buf)
2693+
if err != nil {
2694+
t.Errorf("unexpected error: %v", err)
2695+
}
2696+
})
2697+
t.Run("unknown tag", func(t *testing.T) {
2698+
n := yamlv3.Node{
2699+
Kind: yamlv3.ScalarNode,
2700+
Value: "test",
2701+
Tag: "",
2702+
}
2703+
err := ky.renderScalar(&n, 0, 0, &buf)
2704+
if err == nil {
2705+
t.Errorf("expected error")
2706+
}
2707+
})
2708+
})
2709+
2710+
t.Run("renderMapping", func(t *testing.T) {
2711+
buf := bytes.Buffer{}
2712+
t.Run("should pass 1", func(t *testing.T) {
2713+
n := yamlv3.Node{
2714+
Kind: yamlv3.MappingNode,
2715+
Content: []*yamlv3.Node{
2716+
&yamlv3.Node{Kind: yamlv3.ScalarNode, Value: "key", Tag: "!!str"},
2717+
&yamlv3.Node{Kind: yamlv3.ScalarNode, Value: "val", Tag: "!!str"},
2718+
},
2719+
}
2720+
err := ky.renderMapping(&n, 0, 0, &buf)
2721+
if err != nil {
2722+
t.Errorf("unexpected error: %v", err)
2723+
}
2724+
})
2725+
t.Run("should pass 2", func(t *testing.T) {
2726+
n := yamlv3.Node{
2727+
Kind: yamlv3.MappingNode,
2728+
Content: []*yamlv3.Node{
2729+
&yamlv3.Node{Kind: yamlv3.ScalarNode, Value: "key", Tag: "!!str", LineComment: "line comment"},
2730+
&yamlv3.Node{Kind: yamlv3.ScalarNode, Value: "val", Tag: "!!str"},
2731+
},
2732+
}
2733+
err := ky.renderMapping(&n, 0, 0, &buf)
2734+
if err != nil {
2735+
t.Errorf("unexpected error: %v", err)
2736+
}
2737+
})
2738+
t.Run("should pass 3", func(t *testing.T) {
2739+
n := yamlv3.Node{
2740+
Kind: yamlv3.MappingNode,
2741+
Content: []*yamlv3.Node{
2742+
&yamlv3.Node{Kind: yamlv3.ScalarNode, Value: "key", Tag: "!!str"},
2743+
&yamlv3.Node{Kind: yamlv3.ScalarNode, Value: "val", Tag: "!!str", LineComment: "line comment"},
2744+
},
2745+
}
2746+
err := ky.renderMapping(&n, 0, 0, &buf)
2747+
if err != nil {
2748+
t.Errorf("unexpected error: %v", err)
2749+
}
2750+
})
2751+
t.Run("line-comments on key and val", func(t *testing.T) {
2752+
n := yamlv3.Node{
2753+
Kind: yamlv3.MappingNode,
2754+
Content: []*yamlv3.Node{
2755+
&yamlv3.Node{Kind: yamlv3.ScalarNode, Value: "key", Tag: "!!str", LineComment: "line comment"},
2756+
&yamlv3.Node{Kind: yamlv3.ScalarNode, Value: "val", Tag: "!!str", LineComment: "line comment"},
2757+
},
2758+
}
2759+
err := ky.renderMapping(&n, 0, 0, &buf)
2760+
if err == nil {
2761+
t.Errorf("expected error")
2762+
}
2763+
})
2764+
})
2765+
}
2766+
2767+
func TestNodeKindString(t *testing.T) {
2768+
tests := []struct {
2769+
kind yamlv3.Kind
2770+
expected string
2771+
}{
2772+
{yamlv3.DocumentNode, "document"},
2773+
{yamlv3.ScalarNode, "scalar"},
2774+
{yamlv3.MappingNode, "mapping"},
2775+
{yamlv3.SequenceNode, "sequence"},
2776+
{yamlv3.AliasNode, "alias"},
2777+
{0, "unknown"},
2778+
}
2779+
2780+
ky := &Encoder{}
2781+
for _, tt := range tests {
2782+
t.Run(tt.expected, func(t *testing.T) {
2783+
if got := ky.nodeKindString(tt.kind); got != tt.expected {
2784+
t.Errorf("nodeKindString(%d) = %q, want %q", tt.kind, got, tt.expected)
2785+
}
2786+
})
2787+
}
2788+
}
2789+
2790+
func TestIsCuddledKind(t *testing.T) {
2791+
tests := []struct {
2792+
node yamlv3.Node
2793+
expected bool
2794+
}{
2795+
{yamlv3.Node{Kind: yamlv3.SequenceNode}, true},
2796+
{yamlv3.Node{Kind: yamlv3.MappingNode}, true},
2797+
{yamlv3.Node{Kind: yamlv3.DocumentNode}, false},
2798+
{yamlv3.Node{Kind: yamlv3.ScalarNode}, false},
2799+
{yamlv3.Node{Kind: yamlv3.AliasNode, Alias: &yamlv3.Node{Kind: yamlv3.SequenceNode}}, true},
2800+
{yamlv3.Node{Kind: yamlv3.AliasNode, Alias: &yamlv3.Node{Kind: yamlv3.ScalarNode}}, false},
2801+
{yamlv3.Node{Kind: 0}, false},
2802+
}
2803+
2804+
for _, tt := range tests {
2805+
if got := isCuddledKind(&tt.node); got != tt.expected {
2806+
t.Errorf("isCuddledKind(%v) = %v, want %v", tt.node, got, tt.expected)
2807+
}
2808+
}
2809+
}

0 commit comments

Comments
 (0)