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

Commit

Permalink
added run binary tests with cmd funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
mtodzo committed Nov 23, 2020
1 parent c825eed commit 5f81f0c
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 2 deletions.
80 changes: 78 additions & 2 deletions run_bin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bincover

import (
"bytes"
"errors"
"io/ioutil"
"log"
"os"
Expand Down Expand Up @@ -419,6 +420,7 @@ func TestCoverageCollector_RunBinary(t *testing.T) {
wantPanic bool
panicMessage string
skipSetup bool
cmdFuncs []CoverageCollectorOption
}{
{
name: "panic if Setup not called",
Expand Down Expand Up @@ -538,6 +540,57 @@ func TestCoverageCollector_RunBinary(t *testing.T) {
errMessage: "testing: warning: no tests to run\n",
wantExitCode: -1,
},
{
name: "succeed running binary with stdin pipe preCmdFunc",
args: args{
binPath: "./test_bins/read_stdin.sh",
mainTestName: "",
env: nil,
args: nil,
},
fields: fields{
MergedCoverageFilename: "temp_coverage.out",
CollectCoverage: false,
},
wantOutput: "Hello world\n",
wantExitCode: 1,
wantErr: false,
cmdFuncs: []CoverageCollectorOption{stdinPipePreFunc()},
},
{
name: "fail running binary with error in preCmdFunc",
args: args{
binPath: "./test_bins/read_stdin.sh",
mainTestName: "",
env: nil,
args: nil,
},
fields: fields{
MergedCoverageFilename: "temp_coverage.out",
CollectCoverage: false,
},
errMessage: "oh no!",
wantExitCode: -1,
wantErr: true,
cmdFuncs: []CoverageCollectorOption{errPreCmdFunc()},
},
{
name: "fail running binary with error in postCmdFunc",
args: args{
binPath: "./test_bins/read_stdin.sh",
mainTestName: "",
env: nil,
args: nil,
},
fields: fields{
MergedCoverageFilename: "temp_coverage.out",
CollectCoverage: false,
},
errMessage: "oh no!",
wantExitCode: -1,
wantErr: true,
cmdFuncs: []CoverageCollectorOption{errPostCmdFunc()},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -549,11 +602,11 @@ func TestCoverageCollector_RunBinary(t *testing.T) {
if tt.wantPanic {
require.PanicsWithValue(t,
tt.panicMessage,
func() { _, _, _ = c.RunBinary(tt.args.binPath, tt.args.mainTestName, tt.args.env, tt.args.args)},
func() { _, _, _ = c.RunBinary(tt.args.binPath, tt.args.mainTestName, tt.args.env, tt.args.args, tt.cmdFuncs...)},
)
return
}
gotOutput, gotExitCode, err := c.RunBinary(tt.args.binPath, tt.args.mainTestName, tt.args.env, tt.args.args)
gotOutput, gotExitCode, err := c.RunBinary(tt.args.binPath, tt.args.mainTestName, tt.args.env, tt.args.args, tt.cmdFuncs...)
if (err != nil) != tt.wantErr {
t.Errorf("RunBinary() error = %v, wantErr %v", err, tt.wantErr)
return
Expand All @@ -578,6 +631,29 @@ func TestCoverageCollector_RunBinary(t *testing.T) {
}
}

func stdinPipePreFunc() CoverageCollectorOption {
f := CmdFunc(func(cmd *exec.Cmd) error {
writer, _ := cmd.StdinPipe()
_, err := writer.Write([]byte("Hello world\n"))
return err
})
return PreExec(f)
}

func errPreCmdFunc() CoverageCollectorOption {
f := CmdFunc(func(cmd *exec.Cmd) error {
return errors.New("oh no!")
})
return PreExec(f)
}

func errPostCmdFunc() CoverageCollectorOption {
f := CmdFunc(func(cmd *exec.Cmd) error {
return errors.New("oh no!")
})
return PreExec(f)
}

func tempFile(t *testing.T) *os.File {
f, err := ioutil.TempFile("", "")
require.NoError(t, err)
Expand Down
5 changes: 5 additions & 0 deletions test_bins/read_stdin.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash
echo Hello world
echo START_BINCOVER_METADATA
echo "{\"cover_mode\":\"\",\"exit_code\":1}"
echo END_BINCOVER_METADATA

0 comments on commit 5f81f0c

Please sign in to comment.