Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exec: add a 'stdin' attribute #767

Merged
merged 1 commit into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions executors/exec/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ testcases:
echo "Bar"
```

Input:

```yaml
name: Title of TestSuite
testcases:
- name: with stdin
steps:
- type: exec
stdin: Foo
script: cat
```

## Output

```yaml
Expand Down
6 changes: 5 additions & 1 deletion executors/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func New() venom.Executor {

// Executor represents a Test Exec
type Executor struct {
Stdin *string `json:"stdin,omitempty" yaml:"stdin,omitempty"`
Script *string `json:"script,omitempty" yaml:"script,omitempty"`
}

Expand Down Expand Up @@ -120,7 +121,7 @@ func (Executor) Run(ctx context.Context, step venom.TestStep) (interface{}, erro
defer os.Remove(scriptPath)

// Chmod file
if err := os.Chmod(scriptPath, 0700); err != nil {
if err := os.Chmod(scriptPath, 0o700); err != nil {
return nil, fmt.Errorf("cannot chmod script %s: %s", scriptPath, err)
}

Expand All @@ -129,6 +130,9 @@ func (Executor) Run(ctx context.Context, step venom.TestStep) (interface{}, erro
cmd := exec.CommandContext(ctx, shell, opts...)
venom.Debug(ctx, "teststep exec '%s %s'", shell, strings.Join(opts, " "))
cmd.Dir = venom.StringVarFromCtx(ctx, "venom.testsuite.workdir")
if e.Stdin != nil {
cmd.Stdin = strings.NewReader(*e.Stdin)
}
stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, fmt.Errorf("runScriptAction: Cannot get stdout pipe: %s", err)
Expand Down
9 changes: 9 additions & 0 deletions tests/exec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,18 @@ testcases:
bar:
from: result.systemoutjson.notexisting
default: "test"
json:
from: result.systemoutjson

- name: verify default
steps:
- script: echo "{{.cat-json.foo}} {{.cat-json.bar}}"
assertions:
- result.systemout ShouldEqual "bar test"

- name: stdin
steps:
- stdin: "{{.cat-json.json}}"
script: cat
assertions:
- result.systemoutjson.foo ShouldContainSubstring bar