gaper/runner_test.go

82 lines
1.8 KiB
Go
Raw Normal View History

package gaper
import (
"bytes"
2018-07-12 08:27:07 -05:00
"errors"
2018-06-20 20:40:09 -05:00
"os"
2018-07-12 08:27:07 -05:00
"os/exec"
"path/filepath"
"runtime"
"testing"
"github.com/stretchr/testify/assert"
)
func TestRunnerSuccessRun(t *testing.T) {
stdout := bytes.NewBufferString("")
stderr := bytes.NewBufferString("")
pArgs := []string{}
bin := filepath.Join("testdata", "print-gaper")
if runtime.GOOS == OSWindows {
bin += ".bat"
}
runner := NewRunner(stdout, stderr, bin, pArgs)
cmd, err := runner.Run()
assert.Nil(t, err, "error running binary")
assert.NotNil(t, cmd.Process, "process has not started")
errCmd := <-runner.Errors()
assert.Nil(t, errCmd, "async error running binary")
2018-06-20 21:15:58 -05:00
assert.Contains(t, stdout.String(), "Gaper Test Message")
assert.Equal(t, stderr.String(), "")
}
2018-06-20 20:40:09 -05:00
func TestRunnerSuccessKill(t *testing.T) {
bin := filepath.Join("testdata", "print-gaper")
if runtime.GOOS == OSWindows {
bin += ".bat"
}
runner := NewRunner(os.Stdout, os.Stderr, bin, nil)
_, err := runner.Run()
assert.Nil(t, err, "error running binary")
err = runner.Kill()
assert.Nil(t, err, "error killing program")
errCmd := <-runner.Errors()
assert.NotNil(t, errCmd, "kill program")
}
2018-07-12 08:27:07 -05:00
func TestRunnerExitedNotStarted(t *testing.T) {
runner := NewRunner(os.Stdout, os.Stderr, "", nil)
assert.Equal(t, runner.Exited(), false)
}
func TestRunnerExitStatusNonExitError(t *testing.T) {
runner := NewRunner(os.Stdout, os.Stderr, "", nil)
err := errors.New("non exec.ExitError")
assert.Equal(t, runner.ExitStatus(err), 0)
}
func testExit() {
os.Exit(1)
}
func TestRunnerExitStatusExitError(t *testing.T) {
if os.Getenv("TEST_EXIT") == "1" {
testExit()
return
}
cmd := exec.Command(os.Args[0], "-test.run=TestRunnerExitStatusExitError")
cmd.Env = append(os.Environ(), "TEST_EXIT=1")
err := cmd.Run()
runner := NewRunner(os.Stdout, os.Stderr, "", nil)
assert.Equal(t, runner.ExitStatus(err), 1)
}