Merge pull request #107682 from slaskawi/fix_kubectl_explain_description_printer

Fix printing control character in kubectl explain
This commit is contained in:
Kubernetes Prow Robot 2022-01-21 15:01:19 -08:00 committed by GitHub
commit 3e9eddb8fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 6 deletions

View File

@ -68,7 +68,7 @@ func (m *modelPrinter) PrintDescription(schema proto.Schema) error {
return err
}
}
if err := m.Writer.Indent(descriptionIndentLevel).WriteWrapped(desc); err != nil {
if err := m.Writer.Indent(descriptionIndentLevel).WriteWrapped("%s", desc); err != nil {
return err
}
}

View File

@ -24,19 +24,22 @@ import (
)
func TestModel(t *testing.T) {
gvk := schema.GroupVersionKind{
oneKind := schema.GroupVersionKind{
Group: "",
Version: "v1",
Kind: "OneKind",
}
schema := resources.LookupResource(gvk)
if schema == nil {
t.Fatal("Couldn't find schema v1.OneKind")
controlCharacterKind := schema.GroupVersionKind{
Group: "",
Version: "v1",
Kind: "ControlCharacterKind",
}
tests := []struct {
path []string
want string
gvk schema.GroupVersionKind
}{
{
want: `KIND: OneKind
@ -60,6 +63,7 @@ FIELDS:
`,
path: []string{},
gvk: oneKind,
},
{
want: `KIND: OneKind
@ -95,6 +99,7 @@ FIELDS:
`,
path: []string{"field1"},
gvk: oneKind,
},
{
want: `KIND: OneKind
@ -106,6 +111,7 @@ DESCRIPTION:
This string must be a string
`,
path: []string{"field1", "string"},
gvk: oneKind,
},
{
want: `KIND: OneKind
@ -119,12 +125,32 @@ DESCRIPTION:
This is an int in an array
`,
path: []string{"field1", "array"},
gvk: oneKind,
},
{
want: `KIND: ControlCharacterKind
VERSION: v1
DESCRIPTION:
Control character %
FIELDS:
field1 <>
Control character %
`,
path: []string{},
gvk: controlCharacterKind,
},
}
for _, test := range tests {
schema := resources.LookupResource(test.gvk)
if schema == nil {
t.Fatalf("Couldn't find schema %v", test.gvk)
}
buf := bytes.Buffer{}
if err := PrintModelDescription(test.path, &buf, schema, gvk, false); err != nil {
if err := PrintModelDescription(test.path, &buf, schema, test.gvk, false); err != nil {
t.Fatalf("Failed to PrintModelDescription for path %v: %v", test.path, err)
}
got := buf.String()

View File

@ -39,6 +39,21 @@
}
]
},
"ControlCharacterKind": {
"description": "Control character %",
"properties": {
"field1": {
"description": "Control character %",
}
},
"x-kubernetes-group-version-kind": [
{
"group": "",
"kind": "ControlCharacterKind",
"version": "v1"
}
]
},
"OtherKind": {
"description": "This is another kind of Kind",
"required": [