Skip to content

Instantly share code, notes, and snippets.

@confluentgist

confluentgist/main_test.go

Last active Apr 22, 2020
Embed
What would you like to do?
Bincover example main method test
package main
import (
"fmt"
"log"
"os"
"os/exec"
"regexp"
"testing"
"github.com/stretchr/testify/require"
"github.com/confluentinc/bincover"
)
func TestMainMethod(t *testing.T) {
const binPath = "./instr_bin"
// 1. Running a `go test` command to compile your instrumented binary.
// This could also be done outside of the test suite, in a Makefile, for example.
buildTestCmd := exec.Command("./build_instr_bin.sh")
output, err := buildTestCmd.CombinedOutput()
if err != nil {
log.Println(output)
panic(err)
}
// 2. Initializing a `CoverageCollector`
collector := bincover.NewCoverageCollector("echo_arg_coverage.out", true)
// 3. Calling `collector.Setup()` once before running all of your tests
err = collector.Setup()
require.NoError(t, err)
defer func() {
// 5. Calling `collector.TearDown()` after all the tests have finished
err := collector.TearDown()
if err != nil {
panic(err)
}
err = os.Remove(binPath)
if err != nil {
panic(err)
}
}()
tests := []struct {
name string
args []string
wantOutput string
outputPattern *regexp.Regexp
wantExitCode int
}{
{
name: "succeed running main with one arg",
args: []string{"hello"},
wantOutput: "Your argument is \"hello\"\n",
wantExitCode: 0,
},
{
name: "fail running main with two args",
args: []string{"hello", "world"},
wantOutput: "",
outputPattern: regexp.MustCompile(".*panic.*More than 2 arguments provided!"),
wantExitCode: 1,
},
{
name: "fail running main with no args",
args: []string{""},
wantOutput: "Please provide an argument\n",
wantExitCode: 1,
},
}
for _, tt := range tests {
fmt.Println(tt.name)
// 4. Running each test with the instrumented binary by calling `collector.RunBinary(binPath, mainTestName, env, args)`
output, exitCode, err := collector.RunBinary(binPath, "TestBincoverRunMain", []string{}, tt.args)
require.NoError(t, err)
if tt.outputPattern != nil {
require.Regexp(t, tt.outputPattern, output)
} else {
require.Equal(t, tt.wantOutput, output)
}
require.Equal(t, tt.wantExitCode, exitCode)
}
require.NoError(t, err)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment