From 5b1a2ac736073342ece0968fa62b90e08675318d Mon Sep 17 00:00:00 2001 From: Gerd Oberlechner Date: Wed, 4 Dec 2024 12:58:45 +0100 Subject: [PATCH] bash flag tests Signed-off-by: Gerd Oberlechner --- tooling/templatize/pkg/pipeline/shell_test.go | 53 ++++++++++++++++--- 1 file changed, 46 insertions(+), 7 deletions(-) diff --git a/tooling/templatize/pkg/pipeline/shell_test.go b/tooling/templatize/pkg/pipeline/shell_test.go index b4c2b2c8f..35873fa74 100644 --- a/tooling/templatize/pkg/pipeline/shell_test.go +++ b/tooling/templatize/pkg/pipeline/shell_test.go @@ -152,13 +152,52 @@ func TestMapStepVariables(t *testing.T) { } func TestRunShellStep(t *testing.T) { - expectedOutput := "hello\n" - s := &Step{ - Command: "echo hello", - outputFunc: func(output string) { - assert.Equal(t, output, expectedOutput) + testCases := []struct { + name string + vars config.Variables + step *Step + err string + }{ + { + name: "basic", + vars: config.Variables{}, + step: &Step{ + Command: "echo hello", + }, + }, + { + name: "test nounset", + vars: config.Variables{}, + step: &Step{ + Command: "echo $DOES_NOT_EXIST", + }, + err: "failed to execute shell command: /bin/bash: line 3: DOES_NOT_EXIST: unbound variable\n exit status 1", }, + { + name: "test errexit", + vars: config.Variables{}, + step: &Step{ + Command: "ls /does-not-exist ; echo hello", + }, + err: "failed to execute shell command: ls: /does-not-exist: No such file or directory\n exit status 1", + }, + { + name: "test pipefail", + vars: config.Variables{}, + step: &Step{ + Command: "ls /does-not-exist | echo", + }, + err: "failed to execute shell command: \nls: /does-not-exist: No such file or directory\n exit status 1", + }, + } + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + err := tc.step.runShellStep(context.Background(), "", &PipelineRunOptions{}) + if tc.err != "" { + assert.Error(t, err, tc.err) + } else { + assert.NilError(t, err) + } + }) } - err := s.runShellStep(context.Background(), "", &PipelineRunOptions{}) - assert.NilError(t, err) }