File tree Expand file tree Collapse file tree 2 files changed +23
-2
lines changed
Expand file tree Collapse file tree 2 files changed +23
-2
lines changed Original file line number Diff line number Diff line change @@ -58,10 +58,16 @@ type CommandExecutor interface {
5858type CmdExecutor struct {}
5959
6060func (e * CmdExecutor ) Execute (command string ) ([]byte , error ) {
61+ if command == "" {
62+ return nil , fmt .Errorf ("failed to execute: empty command" )
63+ }
6164 args , err := shlex .Split (command )
6265 if err != nil {
6366 return nil , fmt .Errorf ("failed to split command: %w" , err )
6467 }
68+ if len (args ) == 0 {
69+ return nil , fmt .Errorf ("failed to execute: no command after splitting" )
70+ }
6571 return exec .Command (args [0 ], args [1 :]... ).Output ()
6672}
6773
Original file line number Diff line number Diff line change @@ -616,17 +616,31 @@ func TestCmdExecutor_Execute(t *testing.T) {
616616 name string
617617 command string
618618 wantOutput string
619+ wantErr string
619620 expectError bool
620621 }{
621622 {
622- name : "Echo Command " ,
623+ name : "Valid command " ,
623624 command : "echo test" ,
624625 wantOutput : "test\n " ,
625626 expectError : false ,
626627 },
627628 {
628- name : "Invalid Command" ,
629+ name : "Empty command" ,
630+ command : "" ,
631+ wantErr : "failed to execute: empty command" ,
632+ expectError : true ,
633+ },
634+ {
635+ name : "Command with unclosed quote" ,
636+ command : "echo \" unclosed quote" ,
637+ wantErr : "failed to split command:" ,
638+ expectError : true ,
639+ },
640+ {
641+ name : "Invalid command" ,
629642 command : "invalidcommand" ,
643+ wantErr : "exec: \" invalidcommand\" : executable file not found in $PATH" ,
630644 expectError : true ,
631645 },
632646 }
@@ -639,6 +653,7 @@ func TestCmdExecutor_Execute(t *testing.T) {
639653
640654 if tt .expectError {
641655 assert .Error (t , err )
656+ assert .Contains (t , err .Error (), tt .wantErr )
642657 } else {
643658 assert .NoError (t , err )
644659 assert .Equal (t , tt .wantOutput , string (output ))
You can’t perform that action at this time.
0 commit comments