Add few tests for builder and runner parts

This commit is contained in:
Max Claus Nunes 2018-06-20 11:54:10 -03:00
parent 04b1615274
commit 8e2f1573d6
8 changed files with 110 additions and 21 deletions

20
Gopkg.lock generated
View File

@ -1,6 +1,12 @@
# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
[[projects]]
name = "github.com/davecgh/go-spew"
packages = ["spew"]
revision = "346938d642f2ec3594ed81d874461961cd0faa76"
version = "v1.1.0"
[[projects]]
name = "github.com/fatih/color"
packages = ["."]
@ -34,6 +40,18 @@
]
revision = "49693fbb3fe3c3a75fc4e4d6fb1d7cedcbdeb385"
[[projects]]
name = "github.com/pmezard/go-difflib"
packages = ["difflib"]
revision = "792786c7400a136282c1664665ae0a8db921c6c2"
version = "v1.0.0"
[[projects]]
name = "github.com/stretchr/testify"
packages = ["assert"]
revision = "f35b8ab0b5a2cef36673838d662e249dd9c94686"
version = "v1.2.2"
[[projects]]
name = "github.com/urfave/cli"
packages = ["."]
@ -49,6 +67,6 @@
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
inputs-digest = "01046dcc133438d0c6e244499b12395a2ef02264beec4752652b8c78c571a08e"
inputs-digest = "501c303fff1c8cdb5806d0ebb0c92b671095cbc2049f5b8d2286df401f5efce5"
solver-name = "gps-cdcl"
solver-version = 1

View File

@ -17,3 +17,7 @@
[[constraint]]
branch = "master"
name = "github.com/mattn/go-zglob"
[[constraint]]
name = "github.com/stretchr/testify"
version = "1.2.2"

29
builder_test.go Normal file
View File

@ -0,0 +1,29 @@
package main
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
)
func TestBuilderSuccessBuild(t *testing.T) {
bArgs := []string{}
bin := "srv"
dir := filepath.Join("testdata", "server")
wd, err := os.Getwd()
if err != nil {
t.Fatalf("couldn't get current working directory: %v", err)
}
b := NewBuilder(dir, bin, wd, bArgs)
err = b.Build()
assert.Nil(t, err, "build error")
file, err := os.Open(filepath.Join(wd, bin))
if err != nil {
t.Fatalf("couldn't open open built binary: %v", err)
}
assert.NotNil(t, file, "binary not written properly")
}

View File

@ -157,7 +157,7 @@ func runGaper(cfg *Config) error {
logger.Debug(" | working directory: ", wd)
builder := NewBuilder(cfg.BuildPath, cfg.BinName, wd, cfg.BuildArgs)
runner := NewRunner(os.Stdout, filepath.Join(wd, builder.Binary()), cfg.ProgramArgs)
runner := NewRunner(os.Stdout, os.Stderr, filepath.Join(wd, builder.Binary()), cfg.ProgramArgs)
if err = builder.Build(); err != nil {
return fmt.Errorf("build error: %v", err)

View File

@ -21,22 +21,24 @@ type Runner interface {
}
type runner struct {
bin string
args []string
writer io.Writer
command *exec.Cmd
starttime time.Time
errors chan error
bin string
args []string
writerStdout io.Writer
writerStderr io.Writer
command *exec.Cmd
starttime time.Time
errors chan error
}
// NewRunner ...
func NewRunner(writer io.Writer, bin string, args []string) Runner {
func NewRunner(wStdout io.Writer, wStderr io.Writer, bin string, args []string) Runner {
return &runner{
bin: bin,
args: args,
writer: writer,
starttime: time.Now(),
errors: make(chan error),
bin: bin,
args: args,
writerStdout: wStdout,
writerStderr: wStderr,
starttime: time.Now(),
errors: make(chan error),
}
}
@ -112,6 +114,10 @@ func (r *runner) runBin() error {
return err
}
// TODO: handle or log errors
go io.Copy(r.writerStdout, stdout) // nolint errcheck
go io.Copy(r.writerStderr, stderr) // nolint errcheck
err = r.command.Start()
if err != nil {
return err
@ -119,15 +125,9 @@ func (r *runner) runBin() error {
r.starttime = time.Now()
// TODO: handle or log errors
go io.Copy(r.writer, stdout) // nolint errcheck
go io.Copy(r.writer, stderr) // nolint errcheck
// wait for exit errors
go func() {
if err := r.command.Wait(); err != nil {
r.errors <- err
}
r.errors <- r.command.Wait()
}()
return nil

35
runner_test.go Normal file
View File

@ -0,0 +1,35 @@
package main
import (
"bytes"
"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")
if runtime.GOOS == OSWindows {
assert.Equal(t, "Gaper\r\n", stdout.String())
} else {
assert.Equal(t, "Gaper\n", stdout.String())
}
}

2
testdata/print-gaper vendored Executable file
View File

@ -0,0 +1,2 @@
#!/usr/bin/env bash
echo "Gaper"

1
testdata/print-gaper.bat vendored Normal file
View File

@ -0,0 +1 @@
@echo Gaper