github.com/nextlinux/gosbom@v0.81.1-0.20230627115839-1ff50c281391/gosbom/pkg/cataloger/golang/scan_binary_test.go (about)

     1  package golang
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"runtime/debug"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func Test_getBuildInfo(t *testing.T) {
    13  	type args struct {
    14  		r io.ReaderAt
    15  	}
    16  	tests := []struct {
    17  		name    string
    18  		args    args
    19  		wantBi  *debug.BuildInfo
    20  		wantErr assert.ErrorAssertionFunc
    21  	}{
    22  		{
    23  			name: "recover from panic",
    24  			args: args{
    25  				r: nil, // trying to use a nil reader will cause a panic
    26  			},
    27  			wantBi:  nil, // we should not return anything useful
    28  			wantErr: assert.Error,
    29  		},
    30  	}
    31  	for _, tt := range tests {
    32  		t.Run(tt.name, func(t *testing.T) {
    33  			gotBi, err := getBuildInfo(tt.args.r)
    34  			if !tt.wantErr(t, err, fmt.Sprintf("getBuildInfo(%v)", tt.args.r)) {
    35  				return
    36  			}
    37  			assert.Equalf(t, tt.wantBi, gotBi, "getBuildInfo(%v)", tt.args.r)
    38  		})
    39  	}
    40  }