Skip to content

Commit

Permalink
bash flag tests
Browse files Browse the repository at this point in the history
Signed-off-by: Gerd Oberlechner <[email protected]>
  • Loading branch information
geoberle committed Dec 4, 2024
1 parent c5a0952 commit c9bb894
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 12 deletions.
2 changes: 1 addition & 1 deletion tooling/templatize/pkg/pipeline/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func TestResourceGroupError(t *testing.T) {
},
}
err := rg.run(context.Background(), &PipelineRunOptions{}, &executionTargetImpl{})
assert.Error(t, err, "failed to execute shell command: /bin/bash: line 3: faaaaafffaa: command not found\n exit status 127")
assert.ErrorContains(t, err, "faaaaafffaa: command not found\n exit status 127")
// Test processing ends after first error
assert.Equal(t, len(tmpVals), 1)
}
Expand Down
5 changes: 1 addition & 4 deletions tooling/templatize/pkg/pipeline/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ func (s *Step) createCommand(ctx context.Context, dryRun bool, envVars map[strin
}

func buildBashScript(command string) string {
return "set -o errexit\n" +
"set -o nounset\n" +
"set -o pipefail\n" +
command
return fmt.Sprintf("set -o errexit -o nounset -o pipefail\n%s", command)
}

func (s *Step) runShellStep(ctx context.Context, kubeconfigFile string, options *PipelineRunOptions) error {
Expand Down
53 changes: 46 additions & 7 deletions tooling/templatize/pkg/pipeline/shell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: "DOES_NOT_EXIST: unbound variable\n exit status 1",
},
{
name: "test errexit",
vars: config.Variables{},
step: &Step{
Command: "false ; echo hello",
},
err: "failed to execute shell command: exit status 1",
},
{
name: "test pipefail",
vars: config.Variables{},
step: &Step{
Command: "false | echo",
},
err: "failed to execute shell command: \n exit status 1",
},
}
err := s.runShellStep(context.Background(), "", &PipelineRunOptions{})
assert.NilError(t, err)
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := tc.step.runShellStep(context.Background(), "", &PipelineRunOptions{})
if tc.err != "" {
assert.ErrorContains(t, err, tc.err)
} else {
assert.NilError(t, err)
}
})
}
}

0 comments on commit c9bb894

Please sign in to comment.