Skip to content

Commit

Permalink
Additional tests in TestProcessHooks
Browse files Browse the repository at this point in the history
  • Loading branch information
rfay committed Feb 15, 2022
1 parent 651a750 commit 85eca92
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
2 changes: 1 addition & 1 deletion docs/users/extending-commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ Advanced usages may require running commands directly with explicit arguments. T
hooks:
post-start:
- exec:
raw_exec: [ls, -lR, /var/www/html]
exec_raw: [ls, -lR, /var/www/html]
```

### `exec-host`: Execute a shell command on the host system
Expand Down
6 changes: 5 additions & 1 deletion pkg/ddevapp/ddevapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2555,7 +2555,7 @@ func TestDdevExec(t *testing.T) {
assert.NoError(err)
assert.Contains(out, "/usr/local")

// Try out a rawExec example
// Try out a execRaw example
out, _, err = app.Exec(&ddevapp.ExecOpts{
RawCmd: []string{"ls", "/usr/local"},
})
Expand Down Expand Up @@ -2721,6 +2721,7 @@ func TestProcessHooks(t *testing.T) {
{"exec": "echo MYSQL_USER=${MYSQL_USER}", "service": "db"},
{"exec": "echo TestProcessHooks > /var/www/html/TestProcessHooks${DDEV_ROUTER_HTTPS_PORT}.txt"},
{"exec": "touch /var/tmp/TestProcessHooks && touch /var/www/html/touch_works_after_and.txt"},
{"exec": "", "exec_raw": []string{"ls", "/usr/local"}},
},
}

Expand All @@ -2742,8 +2743,11 @@ func TestProcessHooks(t *testing.T) {
assert.Contains(userOut, "Exec command 'echo something' on the host")
assert.Contains(userOut, "Exec command 'echo MYSQL_USER=${MYSQL_USER}' in container/service 'db'")
assert.Contains(out, "MYSQL_USER=db")
assert.Contains(out, "bin\netc\ngames\ninclude\nlib\nman\nsbin\nshare\nsrc\n")
assert.Contains(userOut, "Exec command 'echo TestProcessHooks > /var/www/html/TestProcessHooks${DDEV_ROUTER_HTTPS_PORT}.txt' in container/service 'web'")
assert.Contains(userOut, "Exec command 'touch /var/tmp/TestProcessHooks && touch /var/www/html/touch_works_after_and.txt' in container/service 'web',")
assert.Contains(userOut, "Exec command '[ls /usr/local] (raw)")
assert.NotContains(userOut, "Task failed")
assert.FileExists(filepath.Join(app.AppRoot, fmt.Sprintf("TestProcessHooks%s.txt", app.RouterHTTPSPort)))
assert.FileExists(filepath.Join(app.AppRoot, "touch_works_after_and.txt"))

Expand Down
27 changes: 16 additions & 11 deletions pkg/ddevapp/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type Task interface {
// to be run in containers.
type ExecTask struct {
service string // Name of service, defaults to web
rawExec []string // Use rawExec if configured instead of exec
execRaw []string // Use execRaw if configured instead of exec
exec string // Actual command to be executed.
app *DdevApp
}
Expand All @@ -48,7 +48,7 @@ func (c ExecTask) Execute() error {
opts := &ExecOpts{
Service: c.service,
Cmd: c.exec,
RawCmd: c.rawExec,
RawCmd: c.execRaw,
Tty: isatty.IsTerminal(os.Stdin.Fd()),
NoCapture: true,
}
Expand All @@ -59,7 +59,12 @@ func (c ExecTask) Execute() error {

// GetDescription returns a human-readable description of the task
func (c ExecTask) GetDescription() string {
return fmt.Sprintf("Exec command '%s' in container/service '%s'", c.exec, c.service)
s := c.exec
if c.execRaw != nil {
s = fmt.Sprintf("%v (raw)", c.execRaw)
}

return fmt.Sprintf("Exec command '%s' in container/service '%s'", s, c.service)
}

// GetDescription returns a human-readable description of the task
Expand Down Expand Up @@ -118,26 +123,26 @@ func NewTask(app *DdevApp, ytask YAMLTask) Task {
}
util.Warning("Invalid exec-host value, not executing it: %v", e)
} else if e, ok = ytask["composer"]; ok {
if v, ok := ytask["raw_exec"]; ok {
if v, ok := ytask["exec_raw"]; ok {
raw, err := util.InterfaceSliceToStringSlice(v.([]interface{}))
if err != nil {
util.Warning("Invalid raw_exec value, not executing it: %v", e)
util.Warning("Invalid exec_raw value, not executing it: %v", e)
return nil
}

t := ComposerTask{app: app, rawExec: raw}
return t
}
util.Warning("Invalid composer value, not executing it: %v", e)
} else if e, ok = ytask["raw_exec"]; ok {
if v, ok := e.([]interface{}); ok {
raw, err := util.InterfaceSliceToStringSlice(v)
if err != nil {
util.Warning("Invalid raw_exec value, not executing it: %v", e)
} else if e, ok = ytask["exec_raw"]; ok {
if v, ok := ytask["exec_raw"]; ok {
raw, ok := v.([]string)
if !ok {
util.Warning("Invalid exec_raw value, not executing it: %v", e)
return nil
}

t := ExecTask{app: app, rawExec: raw}
t := ExecTask{app: app, execRaw: raw}
if t.service, ok = ytask["service"].(string); !ok {
t.service = nodeps.WebContainer
}
Expand Down

0 comments on commit 85eca92

Please sign in to comment.