Skip to content

Commit

Permalink
Update main_test.go
Browse files Browse the repository at this point in the history
  • Loading branch information
sanmai authored Aug 24, 2024
1 parent 680d8e5 commit e454e55
Showing 1 changed file with 29 additions and 16 deletions.
45 changes: 29 additions & 16 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,45 +11,58 @@ func TestExecuteCommand(t *testing.T) {
expectedStatus int
expectedStdout string
expectedStderr string
expectError bool
}{
{
name: "Echo command",
args: []string{"echo", "Hello, World!"},
expectedStatus: 0,
expectedStdout: "Hello, World!\n",
expectedStderr: "",
expectError: false,
},
{
name: "Non-existent command",
args: []string{"non_existent_command"},
expectedStatus: 1,
expectedStatus: -1, // The status will not be set for a non-existent command
expectedStdout: "",
expectedStderr: "exec: \"non_existent_command\": executable file not found in $PATH\n",
expectedStderr: "",
expectError: true,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result, exitCode := executeCommand(tc.args)
result, err := executeCommand(tc.args)

if exitCode != tc.expectedStatus {
t.Errorf("Expected status %d, got %d", tc.expectedStatus, exitCode)
if tc.expectError && err == nil {
t.Errorf("Expected an error, but got none")
} else if !tc.expectError && err != nil {
t.Errorf("Unexpected error: %v", err)
}

if stdout, ok := result["stdout"].(string); !ok || stdout != tc.expectedStdout {
t.Errorf("Expected stdout %q, got %q", tc.expectedStdout, stdout)
}
if result != nil {
if status, ok := result["status"].(int); !ok || status != tc.expectedStatus {
t.Errorf("Expected status %d, got %v", tc.expectedStatus, result["status"])
}

if stderr, ok := result["stderr"].(string); !ok || stderr != tc.expectedStderr {
t.Errorf("Expected stderr %q, got %q", tc.expectedStderr, stderr)
}
if stdout, ok := result["stdout"].(string); !ok || stdout != tc.expectedStdout {
t.Errorf("Expected stdout %q, got %q", tc.expectedStdout, stdout)
}

if _, ok := result["took"].(float64); !ok {
t.Errorf("Expected 'took' to be a float64, got %T", result["took"])
}
if stderr, ok := result["stderr"].(string); !ok || stderr != tc.expectedStderr {
t.Errorf("Expected stderr %q, got %q", tc.expectedStderr, stderr)
}

if _, ok := result["took"].(float64); !ok {
t.Errorf("Expected 'took' to be a float64, got %T", result["took"])
}

if command, ok := result["command"].([]string); !ok || len(command) != len(tc.args) {
t.Errorf("Expected command %v, got %v", tc.args, command)
if command, ok := result["command"].([]string); !ok || len(command) != len(tc.args) {
t.Errorf("Expected command %v, got %v", tc.args, command)
}
} else if !tc.expectError {
t.Errorf("Expected a result, but got nil")
}
})
}
Expand Down

0 comments on commit e454e55

Please sign in to comment.