@@ -8,7 +8,11 @@ import (
88 "github.com/spf13/cobra"
99)
1010
11- // TestExecuteCommandArgumentParsing tests the argument parsing logic for execute
11+ // TestExecuteCommandArgumentParsing tests the argument parsing logic for execute.
12+ // The logic uses LooksLikeVMTarget to decide if the first arg is a VM or a command:
13+ // - UUID → treated as VM target
14+ // - Known alias → treated as VM target
15+ // - Anything else → treated as command, uses HEAD
1216func TestExecuteCommandArgumentParsing (t * testing.T ) {
1317 tests := []struct {
1418 name string
@@ -19,26 +23,47 @@ func TestExecuteCommandArgumentParsing(t *testing.T) {
1923 errorMessage string
2024 }{
2125 {
22- name : "2+ args: first is target, rest is command" ,
23- args : []string {"my-vm " , "echo" , "hello" },
26+ name : "UUID target + command args " ,
27+ args : []string {"3bfea344-6bf2-4655-be27-64be7b5eb332 " , "echo" , "hello" },
2428 expectError : false ,
25- expectedTarget : "my-vm " ,
29+ expectedTarget : "3bfea344-6bf2-4655-be27-64be7b5eb332 " ,
2630 expectedCommand : []string {"echo" , "hello" },
2731 },
2832 {
29- name : "2 args: target and single command" ,
30- args : []string {"my-vm " , "ls" },
33+ name : "UUID target + single command" ,
34+ args : []string {"3bfea344-6bf2-4655-be27-64be7b5eb332 " , "ls" },
3135 expectError : false ,
32- expectedTarget : "my-vm " ,
36+ expectedTarget : "3bfea344-6bf2-4655-be27-64be7b5eb332 " ,
3337 expectedCommand : []string {"ls" },
3438 },
3539 {
36- name : "1 arg: command only, uses HEAD" ,
40+ name : "command only (not a UUID), uses HEAD" ,
41+ args : []string {"echo" , "hello" },
42+ expectError : false ,
43+ expectedTarget : "" ,
44+ expectedCommand : []string {"echo" , "hello" },
45+ },
46+ {
47+ name : "single command, uses HEAD" ,
3748 args : []string {"echo hello" },
3849 expectError : false ,
3950 expectedTarget : "" ,
4051 expectedCommand : []string {"echo hello" },
4152 },
53+ {
54+ name : "command with path, not a UUID" ,
55+ args : []string {"jq" , ".foo" },
56+ expectError : false ,
57+ expectedTarget : "" ,
58+ expectedCommand : []string {"jq" , ".foo" },
59+ },
60+ {
61+ name : "python command, not a UUID" ,
62+ args : []string {"python3" , "-c" , "print('hi')" },
63+ expectError : false ,
64+ expectedTarget : "" ,
65+ expectedCommand : []string {"python3" , "-c" , "print('hi')" },
66+ },
4267 {
4368 name : "0 args: error" ,
4469 args : []string {},
@@ -59,14 +84,18 @@ func TestExecuteCommandArgumentParsing(t *testing.T) {
5984 if len (args ) == 1 {
6085 capturedTarget = ""
6186 capturedCommand = args
62- } else {
87+ } else if utils . LooksLikeVMTarget ( args [ 0 ]) {
6388 capturedTarget = args [0 ]
6489 capturedCommand = args [1 :]
90+ } else {
91+ capturedTarget = ""
92+ capturedCommand = args
6593 }
6694 return nil
6795 },
6896 }
6997
98+ cmd .Flags ().SetInterspersed (false )
7099 cmd .SetArgs (tt .args )
71100 err := cmd .Execute ()
72101
@@ -139,9 +168,12 @@ func TestExecuteCommandWithHEAD(t *testing.T) {
139168 if len (args ) == 1 {
140169 target = ""
141170 command = args
142- } else {
171+ } else if utils . LooksLikeVMTarget ( args [ 0 ]) {
143172 target = args [0 ]
144173 command = args [1 :]
174+ } else {
175+ target = ""
176+ command = args
145177 }
146178
147179 // When target is empty, HEAD should be used
0 commit comments