1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-14 11:57:48 +00:00
kyverno/cmd/cli/kubectl-kyverno/command/docs_test.go
Charles-Edouard Brétéché 3b44af8dbd
refactor: move utils cobra to command package (#8291)
* refactor: introduce cli variables package

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* fix

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* fix

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* fix

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* fix

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* fix

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* fix

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* lint

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* refactor: move utils cobra to command package

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

---------

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
2023-09-06 14:44:50 +00:00

180 lines
5.6 KiB
Go

package command
import (
"testing"
)
func TestFormatDescription(t *testing.T) {
tests := []struct {
name string
short bool
url string
experimental bool
lines []string
want string
}{{
name: "empty (short)",
short: true,
url: "",
experimental: false,
lines: []string{},
want: "",
}, {
name: "empty (long)",
short: true,
url: "https://example.com",
experimental: true,
lines: []string{},
want: "",
}, {
name: "one line (short)",
short: true,
url: "",
experimental: false,
lines: []string{"this is one line"},
want: "this is one line",
}, {
name: "one line with url (short)",
short: true,
url: "https://example.com",
experimental: false,
lines: []string{"this is one line"},
want: "this is one line",
}, {
name: "one line with experimental (short)",
short: true,
url: "",
experimental: true,
lines: []string{"this is one line"},
want: "this is one line",
}, {
name: "multiple line (short)",
short: true,
url: "",
experimental: false,
lines: []string{"this is one line", "this is a second line"},
want: "this is one line",
}, {
name: "multiple line with url (short)",
short: true,
url: "https://example.com",
experimental: false,
lines: []string{"this is one line", "this is a second line"},
want: "this is one line",
}, {
name: "multiple line with experimental (short)",
short: true,
url: "",
experimental: true,
lines: []string{"this is one line", "this is a second line"},
want: "this is one line",
}, {
name: "one line (long)",
short: false,
url: "",
experimental: false,
lines: []string{"this is one line"},
want: "this is one line",
}, {
name: "one line with url (long)",
short: false,
url: "https://example.com",
experimental: false,
lines: []string{"this is one line"},
want: "this is one line\n\n For more information visit https://example.com",
}, {
name: "one line with experimental (long)",
short: false,
url: "",
experimental: true,
lines: []string{"this is one line"},
want: "this is one line\n\n NOTE: This is an experimental command, use `KYVERNO_EXPERIMENTAL=true` to enable it.",
}, {
name: "multiple line (long)",
short: false,
url: "",
experimental: false,
lines: []string{"this is one line", "this is a second line"},
want: "this is one line\n this is a second line",
}, {
name: "multiple line with url (long)",
short: false,
url: "https://example.com",
experimental: false,
lines: []string{"this is one line", "this is a second line"},
want: "this is one line\n this is a second line\n\n For more information visit https://example.com",
}, {
name: "multiple line with experimental (long)",
short: false,
url: "",
experimental: true,
lines: []string{"this is one line", "this is a second line"},
want: "this is one line\n this is a second line\n\n NOTE: This is an experimental command, use `KYVERNO_EXPERIMENTAL=true` to enable it.",
}, {
name: "multiple line with url and experimental (long)",
short: false,
url: "https://example.com",
experimental: true,
lines: []string{"this is one line", "this is a second line"},
want: "this is one line\n this is a second line\n\n NOTE: This is an experimental command, use `KYVERNO_EXPERIMENTAL=true` to enable it.\n\n For more information visit https://example.com",
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := FormatDescription(tt.short, tt.url, tt.experimental, tt.lines...); got != tt.want {
t.Errorf("FormatDescription() = %v, want %v", got, tt.want)
}
})
}
}
func TestFormatExamples(t *testing.T) {
tests := []struct {
name string
in [][]string
want string
}{{
name: "nil",
in: nil,
want: "",
}, {
name: "empty",
in: [][]string{},
want: "",
}, {
name: "one",
in: [][]string{{
`# Fix Kyverno test files`,
`KYVERNO_EXPERIMENTAL=true kyverno fix test . --save`,
}},
want: " # Fix Kyverno test files\n KYVERNO_EXPERIMENTAL=true kyverno fix test . --save",
}, {
name: "multiple",
in: [][]string{{
`# Test a git repository containing Kyverno test cases`,
`kyverno test https://github.com/kyverno/policies/pod-security --git-branch main`,
}, {
`# Test a local folder containing test cases`,
`kyverno test .`,
}, {
`# Test some specific test cases out of many test cases in a local folder`,
`kyverno test . --test-case-selector "policy=disallow-latest-tag, rule=require-image-tag, resource=test-require-image-tag-pass"`,
}},
want: ` # Test a git repository containing Kyverno test cases
kyverno test https://github.com/kyverno/policies/pod-security --git-branch main
# Test a local folder containing test cases
kyverno test .
# Test some specific test cases out of many test cases in a local folder
kyverno test . --test-case-selector "policy=disallow-latest-tag, rule=require-image-tag, resource=test-require-image-tag-pass"`,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := FormatExamples(tt.in...); got != tt.want {
t.Errorf("FormatExamples() = %v, want %v", got, tt.want)
}
})
}
}