blob: 358876a317711391535dd8f146ff1a72cdf1aaef [file] [log] [blame]
// Copyright 2019 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package execution
import (
"bytes"
"context"
"os/exec"
"testing"
)
func TestExecAll(t *testing.T) {
echo, err := exec.LookPath("echo")
if err != nil {
t.Fatal(err)
}
type command struct {
cmd []string
expectedSuccess bool
expectedOutput string
}
helloCmd := command{cmd: []string{echo, "hello"}, expectedSuccess: true, expectedOutput: "hello"}
byeCmd := command{cmd: []string{echo, "bye"}, expectedSuccess: true, expectedOutput: "bye"}
unknownCmd := command{cmd: []string{"unknownCmd"}, expectedSuccess: false, expectedOutput: "failed to exec cmd"}
tests := []struct {
name string
cmds []command
}{
{"All pass", []command{helloCmd, byeCmd}},
{"Middle command fails", []command{helloCmd, unknownCmd, byeCmd}},
{"Last command fails", []command{helloCmd, byeCmd, unknownCmd}},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ctx := context.Background()
var stdout, stderr bytes.Buffer
executor := NewExecutor(&stdout, &stderr, "")
var cmds [][]string
var expectedErr bool
for _, cmd := range test.cmds {
cmds = append(cmds, cmd.cmd)
if !cmd.expectedSuccess {
expectedErr = true
}
}
err := executor.ExecAll(ctx, cmds)
if expectedErr && err == nil {
t.Fatal("expected an error but didn't get one.")
}
if !expectedErr && err != nil {
t.Fatalf("failed with error: %v", err)
}
})
}
}