Skip to content
This repository has been archived by the owner on Feb 23, 2023. It is now read-only.

Commit

Permalink
addressed PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mtodzo committed Dec 9, 2020
1 parent 1d2d631 commit b447991
Showing 1 changed file with 42 additions and 54 deletions.
96 changes: 42 additions & 54 deletions run_bin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package bincover
import (
"bytes"
"errors"
"io"
"io/ioutil"
"log"
"os"
Expand All @@ -13,6 +14,12 @@ import (
"github.com/stretchr/testify/require"
)

const (
ohNoErrMsg = "oh no!"
preFuncHelloMsg = "Hello from prefunc\n"
helloWorldOutput = "Hello world\n"
)

func TestMain(m *testing.M) {
// Build necessary binaries before executing unit tests.
buildTestCmd := exec.Command("go", []string{"test", "./test_bins", "-tags", "testrunmain", "-coverpkg=./...", "-c", "-o", "set_covermode"}...)
Expand Down Expand Up @@ -405,7 +412,7 @@ func TestPreExec(t *testing.T) {
args []string
}
var (
buffer1, buffer2 string
buffer = new(bytes.Buffer)
)

tests := []struct {
Expand All @@ -415,37 +422,27 @@ func TestPreExec(t *testing.T) {
args args
wantErr bool
wantErrMsg string
buffer *string
buffer *bytes.Buffer
}{
{
name: "test running one prefunc",
args: args{
binPath: "./test_bins/read_stdin.sh",
mainTestName: "TestRunMain",
},
buffer: &buffer1,
preCmdFuncs: []PreCmdFunc{printToBufferPreFunc(&buffer1)},
expected: "Hello from prefunc\n",
},
{
name: "test running two prefuncs",
args: args{
binPath: "./test_bins/read_stdin.sh",
mainTestName: "TestRunMain",
},
buffer: &buffer2,
preCmdFuncs: []PreCmdFunc{printToBufferPreFunc(&buffer2), printToBufferPreFunc(&buffer2)},
expected: "Hello from prefunc\nHello from prefunc\n",
buffer: buffer,
preCmdFuncs: []PreCmdFunc{printToBufferPreFunc(buffer, preFuncHelloMsg), printToBufferPreFunc(buffer, preFuncHelloMsg)},
expected: preFuncHelloMsg + preFuncHelloMsg,
},
{
name: "test error in prefuncs",
args: args{
binPath: "./test_bins/read_stdin.sh",
mainTestName: "TestRunMain",
},
preCmdFuncs: []PreCmdFunc{errPreCmdFunc()},
preCmdFuncs: []PreCmdFunc{errPreCmdFunc(ohNoErrMsg)},
wantErr: true,
wantErrMsg: "oh no!",
wantErrMsg: ohNoErrMsg,
},
}
for _, tt := range tests {
Expand All @@ -457,7 +454,7 @@ func TestPreExec(t *testing.T) {
require.Error(t, err)
require.Equal(t, tt.wantErrMsg, err.Error())
} else {
require.Equal(t, tt.expected, *tt.buffer)
require.Equal(t, tt.expected, tt.buffer.String())
require.NoError(t, err)
}
})
Expand All @@ -472,7 +469,7 @@ func TestPostExec(t *testing.T) {
args []string
}
var (
buffer1, buffer2 string
buffer = new(bytes.Buffer)
)

tests := []struct {
Expand All @@ -482,37 +479,27 @@ func TestPostExec(t *testing.T) {
args args
wantErr bool
wantErrMsg string
buffer *string
buffer *bytes.Buffer
}{
{
name: "test running one postfunc",
args: args{
binPath: "./set_covermode",
mainTestName: "TestRunMain",
},
buffer: &buffer1,
postCmdFuncs: []PostCmdFunc{printCommandOutputToBuffer(&buffer1)},
expected: "Hello world\n",
},
{
name: "test running two prefuncs",
args: args{
binPath: "./set_covermode",
mainTestName: "TestRunMain",
},
buffer: &buffer2,
postCmdFuncs: []PostCmdFunc{printCommandOutputToBuffer(&buffer2), printCommandOutputToBuffer(&buffer2)},
expected: "Hello world\nHello world\n",
buffer: buffer,
postCmdFuncs: []PostCmdFunc{printCommandOutputToBuffer(buffer), printCommandOutputToBuffer(buffer)},
expected: helloWorldOutput + helloWorldOutput,
},
{
name: "test error in prefuncs",
args: args{
binPath: "./set_covermode",
mainTestName: "TestRunMain",
},
postCmdFuncs: []PostCmdFunc{errPostCmdFunc()},
postCmdFuncs: []PostCmdFunc{errPostCmdFunc(ohNoErrMsg)},
wantErr: true,
wantErrMsg: "oh no!",
wantErrMsg: ohNoErrMsg,
},
}
for _, tt := range tests {
Expand All @@ -524,7 +511,7 @@ func TestPostExec(t *testing.T) {
require.Error(t, err)
require.Equal(t, tt.wantErrMsg, err.Error())
} else {
require.Equal(t, tt.expected, *tt.buffer)
require.Equal(t, tt.expected, tt.buffer.String())
require.NoError(t, err)
}
})
Expand Down Expand Up @@ -699,10 +686,10 @@ func TestCoverageCollector_RunBinary(t *testing.T) {
MergedCoverageFilename: "temp_coverage.out",
CollectCoverage: false,
},
errMessage: "oh no!",
errMessage: ohNoErrMsg,
wantExitCode: -1,
wantErr: true,
cmdFuncs: []CoverageCollectorOption{errPreCmdFuncCovCollectorOption()},
cmdFuncs: []CoverageCollectorOption{errPreCmdFuncCovCollectorOption(ohNoErrMsg)},
},
{
name: "fail running binary with error in postCmdFunc",
Expand All @@ -716,10 +703,10 @@ func TestCoverageCollector_RunBinary(t *testing.T) {
MergedCoverageFilename: "temp_coverage.out",
CollectCoverage: false,
},
errMessage: "oh no!",
errMessage: ohNoErrMsg,
wantExitCode: -1,
wantErr: true,
cmdFuncs: []CoverageCollectorOption{errPostCmdFuncCovCollectorOption()},
cmdFuncs: []CoverageCollectorOption{errPostCmdFuncCovCollectorOption(ohNoErrMsg)},
},
{
name: "succeed running binary with pre and post cmdFuncs",
Expand Down Expand Up @@ -790,12 +777,12 @@ func stdinPipePreFuncCovCollectorOption() CoverageCollectorOption {
return PreExec(f)
}

func errPreCmdFuncCovCollectorOption() CoverageCollectorOption {
return PreExec(errPreCmdFunc())
func errPreCmdFuncCovCollectorOption(errMsg string) CoverageCollectorOption {
return PreExec(errPreCmdFunc(errMsg))
}

func errPostCmdFuncCovCollectorOption() CoverageCollectorOption {
return PostExec(errPostCmdFunc())
func errPostCmdFuncCovCollectorOption(errMsg string) CoverageCollectorOption {
return PostExec(errPostCmdFunc(errMsg))
}

func nilPreCmdFunc() CoverageCollectorOption {
Expand All @@ -805,31 +792,32 @@ func nilPreCmdFunc() CoverageCollectorOption {
return PreExec(f)
}

func printToBufferPreFunc(buffer *string) PreCmdFunc {
func printToBufferPreFunc(w io.Writer, toWrite string) PreCmdFunc {
f := PreCmdFunc(func(cmd *exec.Cmd) error {
*buffer += "Hello from prefunc\n"
return nil
_, err := w.Write([]byte(toWrite))
return err
})
return f
}

func errPreCmdFunc() PreCmdFunc {
func errPreCmdFunc(errMsg string) PreCmdFunc {
f := PreCmdFunc(func(cmd *exec.Cmd) error {
return errors.New("oh no!")
return errors.New(errMsg)
})
return f
}

func printCommandOutputToBuffer(buffer *string) PostCmdFunc {
func printCommandOutputToBuffer(w io.Writer) PostCmdFunc {
f := PostCmdFunc(func(cmd *exec.Cmd, output string, err error) error {
*buffer += output
return nil
_, writeErr := w.Write([]byte(output))
return writeErr
})
return f
}
func errPostCmdFunc() PostCmdFunc {

func errPostCmdFunc(errMsg string) PostCmdFunc {
f := PostCmdFunc(func(cmd *exec.Cmd, output string, err error) error {
return errors.New("oh no!")
return errors.New(errMsg)
})
return f
}
Expand Down

0 comments on commit b447991

Please sign in to comment.