diff --git a/pkg/blocks/executor.go b/pkg/blocks/executor.go index 698e0830..3b21754f 100644 --- a/pkg/blocks/executor.go +++ b/pkg/blocks/executor.go @@ -96,7 +96,7 @@ func (e *ScriptExecutor) Execute(ctx context.Context, execCtx TTPExecutionContex body := expandedInlines[0] if e.Name == ExecutorPowershellOnLinux || e.Name == ExecutorPowershell { // Wrap the PowerShell command in a script block - body = fmt.Sprintf("&{%s}\n\n", body) + body = fmt.Sprintf("$ErrorActionPreference = 'Stop' ; &{%s}\n\n", body) } // expand variables in environment diff --git a/pkg/blocks/executor_test.go b/pkg/blocks/executor_test.go new file mode 100644 index 00000000..fadbc627 --- /dev/null +++ b/pkg/blocks/executor_test.go @@ -0,0 +1,34 @@ +package blocks + +import ( + "context" + "testing" +) + +func TestBashExecutor_Echo_Success(t *testing.T) { + emptyEnvironment := map[string]string{} + execCtx := TTPExecutionContext{Vars: &TTPExecutionVars{}} + executor := NewExecutor("bash", "echo success", "", []string{}, emptyEnvironment) + + result, err := executor.Execute(context.Background(), execCtx) + if err != nil { + t.Fatalf("expected no error, got %v", err) + } + if result.Stdout != "success\n" { + t.Fatalf("expected output 'success\\n', got %#v", result.Stdout) + } +} + +func TestBashExecutor_False_FailFast(t *testing.T) { + emptyEnvironment := map[string]string{} + execCtx := TTPExecutionContext{Vars: &TTPExecutionVars{}} + executor := NewExecutor("bash", "false; echo success", "", []string{}, emptyEnvironment) + + result, err := executor.Execute(context.Background(), execCtx) + if err == nil { + t.Fatalf("expected error, got result %#v", result) + } + if err.Error() != "exit status 1" { + t.Fatalf("expected error 'exit status 1', got %#v", err.Error()) + } +}