virsh: Add testing for vshStringToArray

Add a '--split' switch for the 'virsh echo' command and add few test
cases to the virshtest.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Martin Kletzander <mkletzan@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2021-08-11 15:22:59 +02:00
parent 31fa241b46
commit 4db8ffeb2e
2 changed files with 26 additions and 0 deletions

View File

@ -487,6 +487,17 @@ mymain(void)
DO_TEST(46, "a\n", "#unbalanced; 'quotes\"\necho a # b");
DO_TEST(47, "a\n", "\\# ignored;echo a\n'#also' ignored");
/* test of splitting in vshStringToArray */
DO_TEST(48, "a\nb,c,\nd,,e,,\nf,,,e\n",
"-q", "echo", "--split", "a,b,,c,,,d,,,,e,,,,,f,,,,,,e");
DO_TEST(49, "\na\nb,c,\nd,,e,,\nf,,,e\n\n",
"-q", "echo", "--split", ",a,b,,c,,,d,,,,e,,,,,f,,,,,,e,");
DO_TEST(50, ",a\nb,c,\nd,,e,,\nf,,,e,\n",
"-q", "echo", "--split", ",,a,b,,c,,,d,,,,e,,,,,f,,,,,,e,,");
DO_TEST(51, ",\na\nb,c,\nd,,e,,\nf,,,e,\n\n",
"-q", "echo", "--split", ",,,a,b,,c,,,d,,,,e,,,,,f,,,,,,e,,,");
DO_TEST(52, ",,a\nb,c,\nd,,e,,\nf,,,e,,\n",
"-q", "echo", "--split", ",,,,a,b,,c,,,d,,,,e,,,,,f,,,,,,e,,,,");
# undef DO_TEST
VIR_FREE(custom_uri);

View File

@ -3115,6 +3115,10 @@ const vshCmdOptDef opts_echo[] = {
.type = VSH_OT_BOOL,
.help = N_("escape for XML use")
},
{.name = "split",
.type = VSH_OT_BOOL,
.help = N_("split each argument on ','; ',,' is an escape sequence")
},
{.name = "err",
.type = VSH_OT_BOOL,
.help = N_("output to stderr"),
@ -3153,11 +3157,14 @@ cmdEcho(vshControl *ctl, const vshCmd *cmd)
bool shell = vshCommandOptBool(cmd, "shell");
bool xml = vshCommandOptBool(cmd, "xml");
bool err = vshCommandOptBool(cmd, "err");
bool split = vshCommandOptBool(cmd, "split");
const vshCmdOpt *opt = NULL;
g_autofree char *arg = NULL;
g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
VSH_EXCLUSIVE_OPTIONS_VAR(shell, xml);
VSH_EXCLUSIVE_OPTIONS_VAR(shell, split);
VSH_EXCLUSIVE_OPTIONS_VAR(xml, split);
while ((opt = vshCommandOptArgv(ctl, cmd, opt))) {
const char *curr = opt->data;
@ -3166,6 +3173,14 @@ cmdEcho(vshControl *ctl, const vshCmd *cmd)
virBufferEscapeString(&buf, "%s", curr);
} else if (shell) {
virBufferEscapeShell(&buf, curr);
} else if (split) {
g_auto(GStrv) spl = NULL;
GStrv n;
vshStringToArray(curr, &spl);
for (n = spl; *n; n++)
virBufferAsprintf(&buf, "%s\n", *n);
} else {
virBufferAdd(&buf, curr, -1);
}