File: main_test.go

package info (click to toggle)
golang-github-confluentinc-bincover 0.2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 184 kB
  • sloc: makefile: 42; sh: 33
file content (76 lines) | stat: -rw-r--r-- 1,752 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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"
	buildTestCmd := exec.Command("./build_instr_bin.sh")
	output, err := buildTestCmd.CombinedOutput()
	if err != nil {
		log.Println(output)
		panic(err)
	}
	collector := bincover.NewCoverageCollector("echo_arg_coverage.out", true)
	err = collector.Setup()
	require.NoError(t, err)
	defer func() {
		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)
		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)
}