From 9f360cefc3889d5d5b18f49fe56a81cbd5018662 Mon Sep 17 00:00:00 2001 From: Christophe de Vienne Date: Mon, 19 Feb 2024 17:22:04 +0100 Subject: [PATCH] exec: add a 'stdin' attribute If set, the value is written to the script stdin. This avoid the need to use 'echo' in the script. Signed-off-by: Christophe de Vienne --- executors/exec/README.md | 12 ++++++++++++ executors/exec/exec.go | 6 +++++- tests/exec.yml | 9 +++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/executors/exec/README.md b/executors/exec/README.md index 7169d68c..790d8b80 100644 --- a/executors/exec/README.md +++ b/executors/exec/README.md @@ -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 diff --git a/executors/exec/exec.go b/executors/exec/exec.go index 55bea5cf..c97b5bbd 100644 --- a/executors/exec/exec.go +++ b/executors/exec/exec.go @@ -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"` } @@ -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) } @@ -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) diff --git a/tests/exec.yml b/tests/exec.yml index fda77633..1789dd70 100644 --- a/tests/exec.yml +++ b/tests/exec.yml @@ -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