Skip to content

Commit aca12ec

Browse files
committed
tests for executor
1 parent 6370510 commit aca12ec

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

pkg/tagit/tagit.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,16 @@ type CommandExecutor interface {
5858
type CmdExecutor struct{}
5959

6060
func (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

pkg/tagit/tagit_test.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff 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))

0 commit comments

Comments
 (0)