github.com/noqcks/syft@v0.0.0-20230920222752-a9e2c4e288e5/syft/pkg/cataloger/golang/options_test.go (about)

     1  package golang
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/mitchellh/go-homedir"
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func Test_Options(t *testing.T) {
    11  	type opts struct {
    12  		local    bool
    13  		cacheDir string
    14  		remote   bool
    15  		proxy    string
    16  		noProxy  string
    17  	}
    18  
    19  	homedirCacheDisabled := homedir.DisableCache
    20  	homedir.DisableCache = true
    21  	t.Cleanup(func() {
    22  		homedir.DisableCache = homedirCacheDisabled
    23  	})
    24  
    25  	allEnv := map[string]string{
    26  		"HOME":      "/usr/home",
    27  		"GOPATH":    "",
    28  		"GOPROXY":   "",
    29  		"GOPRIVATE": "",
    30  		"GONOPROXY": "",
    31  	}
    32  
    33  	tests := []struct {
    34  		name     string
    35  		env      map[string]string
    36  		opts     opts
    37  		expected GoCatalogerOpts
    38  	}{
    39  		{
    40  			name: "set via env defaults",
    41  			env: map[string]string{
    42  				"GOPATH":    "/go",
    43  				"GOPROXY":   "https://my.proxy",
    44  				"GOPRIVATE": "my.private",
    45  				"GONOPROXY": "no.proxy",
    46  			},
    47  			opts: opts{},
    48  			expected: GoCatalogerOpts{
    49  				searchLocalModCacheLicenses: false,
    50  				localModCacheDir:            "/go/pkg/mod",
    51  				searchRemoteLicenses:        false,
    52  				proxies:                     []string{"https://my.proxy"},
    53  				noProxy:                     []string{"my.private", "no.proxy"},
    54  			},
    55  		},
    56  		{
    57  			name: "set via configuration",
    58  			env: map[string]string{
    59  				"GOPATH":    "/go",
    60  				"GOPROXY":   "https://my.proxy",
    61  				"GOPRIVATE": "my.private",
    62  				"GONOPROXY": "no.proxy",
    63  			},
    64  			opts: opts{
    65  				local:    true,
    66  				cacheDir: "/go-cache",
    67  				remote:   true,
    68  				proxy:    "https://alt.proxy,direct",
    69  				noProxy:  "alt.no.proxy",
    70  			},
    71  			expected: GoCatalogerOpts{
    72  				searchLocalModCacheLicenses: true,
    73  				localModCacheDir:            "/go-cache",
    74  				searchRemoteLicenses:        true,
    75  				proxies:                     []string{"https://alt.proxy", "direct"},
    76  				noProxy:                     []string{"alt.no.proxy"},
    77  			},
    78  		},
    79  	}
    80  
    81  	for _, test := range tests {
    82  		t.Run(test.name, func(t *testing.T) {
    83  			for k, v := range allEnv {
    84  				t.Setenv(k, v)
    85  			}
    86  			for k, v := range test.env {
    87  				t.Setenv(k, v)
    88  			}
    89  			got := NewGoCatalogerOpts().
    90  				WithSearchLocalModCacheLicenses(test.opts.local).
    91  				WithLocalModCacheDir(test.opts.cacheDir).
    92  				WithSearchRemoteLicenses(test.opts.remote).
    93  				WithProxy(test.opts.proxy).
    94  				WithNoProxy(test.opts.noProxy)
    95  
    96  			assert.Equal(t, test.expected, got)
    97  		})
    98  	}
    99  }