Skip to content

Commit 24bd9a5

Browse files
committed
improve error handling in dispatch run tests
1 parent dd51616 commit 24bd9a5

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

cli/run_test.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ func TestRunCommand(t *testing.T) {
2121
t.Run("Run with non-existent env file", func(t *testing.T) {
2222
t.Parallel()
2323

24-
buff, msg, err := execRunCommand(&[]string{}, "run", "--env-file", "non-existent.env", "--", "echo", "hello")
24+
buff, err := execRunCommand(&[]string{}, "run", "--env-file", "non-existent.env", "--", "echo", "hello")
2525
if err != nil {
26-
t.Fatalf(msg, err)
26+
t.Fatal(err.Error())
2727
}
2828

2929
assert.Regexp(t, "Error: failed to load env file from .+/dispatch/cli/non-existent.env: open non-existent.env: no such file or directory\n", buff.String())
@@ -38,9 +38,9 @@ func TestRunCommand(t *testing.T) {
3838
t.Fatalf("Failed to write env file: %v", err)
3939
}
4040

41-
buff, msg, err := execRunCommand(&[]string{}, "run", "--env-file", envFile, "--", "printenv", "CHARACTER")
41+
buff, err := execRunCommand(&[]string{}, "run", "--env-file", envFile, "--", "printenv", "CHARACTER")
4242
if err != nil {
43-
t.Fatalf(msg, err)
43+
t.Fatal(err.Error())
4444
}
4545

4646
result, found := findEnvVariableInLogs(&buff)
@@ -56,9 +56,9 @@ func TestRunCommand(t *testing.T) {
5656
// Set environment variables
5757
envVars := []string{"CHARACTER=morty_smith"}
5858

59-
buff, msg, err := execRunCommand(&envVars, "run", "--", "printenv", "CHARACTER")
59+
buff, err := execRunCommand(&envVars, "run", "--", "printenv", "CHARACTER")
6060
if err != nil {
61-
t.Fatalf(msg, err)
61+
t.Fatal(err.Error())
6262
}
6363

6464
result, found := findEnvVariableInLogs(&buff)
@@ -79,9 +79,9 @@ func TestRunCommand(t *testing.T) {
7979

8080
// Set environment variables
8181
envVars := []string{"CHARACTER=morty_smith"}
82-
buff, msg, err := execRunCommand(&envVars, "run", "--env-file", envFile, "--", "printenv", "CHARACTER")
82+
buff, err := execRunCommand(&envVars, "run", "--env-file", envFile, "--", "printenv", "CHARACTER")
8383
if err != nil {
84-
t.Fatalf(msg, err)
84+
t.Fatal(err.Error())
8585
}
8686

8787
result, found := findEnvVariableInLogs(&buff)
@@ -100,13 +100,14 @@ func TestRunCommand(t *testing.T) {
100100

101101
envFile, err := createEnvFile(t.TempDir(), []byte("CHARACTER=rick_sanchez"))
102102
defer os.Remove(envFile)
103+
103104
if err != nil {
104105
t.Fatalf("Failed to write env file: %v", err)
105106
}
106107

107-
buff, msg, err := execRunCommand(&[]string{}, "run", "--env-file", envFile, "--", "printenv", "CHARACTER")
108+
buff, err := execRunCommand(&[]string{}, "run", "--env-file", envFile, "--", "printenv", "CHARACTER")
108109
if err != nil {
109-
t.Fatalf(msg, err)
110+
t.Fatal(err.Error())
110111
}
111112

112113
result, found := findEnvVariableInLogs(&buff)
@@ -117,7 +118,7 @@ func TestRunCommand(t *testing.T) {
117118
})
118119
}
119120

120-
func execRunCommand(envVars *[]string, arg ...string) (bytes.Buffer, string, error) {
121+
func execRunCommand(envVars *[]string, arg ...string) (bytes.Buffer, error) {
121122
// Create a context with a timeout to ensure the process doesn't run indefinitely
122123
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
123124
defer cancel()
@@ -139,18 +140,18 @@ func execRunCommand(envVars *[]string, arg ...string) (bytes.Buffer, string, err
139140

140141
// Start the command
141142
if err := cmd.Start(); err != nil {
142-
return errBuf, "Failed to start command: &v", err
143+
return errBuf, fmt.Errorf("Failed to start command: %w", err)
143144
}
144145

145146
// Wait for the command to finish or for the context to timeout
146147
if err := cmd.Wait(); err != nil {
147148
// Check if the error is due to context timeout (command running too long)
148149
if ctx.Err() == context.DeadlineExceeded {
149-
return errBuf, "Command timed out", err
150+
return errBuf, fmt.Errorf("Command timed out: %w", err)
150151
}
151152
}
152153

153-
return errBuf, "", nil
154+
return errBuf, nil
154155
}
155156

156157
func createEnvFile(path string, content []byte) (string, error) {

0 commit comments

Comments
 (0)