MCPcopy Index your code
hub / github.com/ddev/ddev / TestRunHostCommandWithOptions

Function TestRunHostCommandWithOptions

pkg/exec/exec_test.go:14–106  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

12)
13
14func TestRunHostCommandWithOptions(t *testing.T) {
15 bashPath := util.FindBashPath()
16
17 // Test basic command execution without options
18 t.Run("no options", func(t *testing.T) {
19 output, err := exec.RunHostCommandWithOptions(bashPath, []exec.CmdOption{}, "-c", "echo hello")
20 require.NoError(t, err)
21 assert.Equal(t, "hello", strings.TrimSpace(output))
22 })
23
24 // Test WithStdin option
25 t.Run("with stdin", func(t *testing.T) {
26 stdin := strings.NewReader("test input")
27
28 output, err := exec.RunHostCommandWithOptions(bashPath, []exec.CmdOption{
29 exec.WithStdin(stdin),
30 }, "-c", "cat")
31 require.NoError(t, err)
32 assert.Equal(t, "test input", strings.TrimSpace(output))
33 })
34
35 // Test WithEnv option
36 t.Run("with env", func(t *testing.T) {
37 output, err := exec.RunHostCommandWithOptions(bashPath, []exec.CmdOption{
38 exec.WithEnv([]string{"TEST_VAR=test_value"}),
39 }, "-c", "echo $TEST_VAR")
40 require.NoError(t, err)
41 assert.Equal(t, "test_value", strings.TrimSpace(output))
42 })
43
44 // Test multiple options combined
45 t.Run("with stdin and env", func(t *testing.T) {
46 stdin := strings.NewReader("hello world")
47 output, err := exec.RunHostCommandWithOptions(bashPath, []exec.CmdOption{
48 exec.WithStdin(stdin),
49 exec.WithEnv(append(os.Environ(), "TEST_PREFIX=prefix:")),
50 }, "-c", "echo $TEST_PREFIX$(cat)")
51 require.NoError(t, err)
52 assert.Equal(t, "prefix:hello world", strings.TrimSpace(output))
53 })
54
55 // Test empty options slice
56 t.Run("empty options", func(t *testing.T) {
57 output, err := exec.RunHostCommandWithOptions(bashPath, []exec.CmdOption{}, "-c", "echo test")
58 require.NoError(t, err)
59 assert.Equal(t, "test", strings.TrimSpace(output))
60 })
61
62 // Test nil options slice
63 t.Run("nil options", func(t *testing.T) {
64 output, err := exec.RunHostCommandWithOptions(bashPath, nil, "-c", "echo test")
65 require.NoError(t, err)
66 assert.Equal(t, "test", strings.TrimSpace(output))
67 })
68
69 // Test command with error
70 t.Run("command error", func(t *testing.T) {
71 _, err := exec.RunHostCommandWithOptions("nonexistent-ddev-command", []exec.CmdOption{})

Callers

nothing calls this directly

Calls 5

FindBashPathFunction · 0.92
WithStdinFunction · 0.92
WithEnvFunction · 0.92
ErrorMethod · 0.80

Tested by

no test coverage detected