github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/syft/pkg/cataloger/golang/config_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_Config(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 CatalogerConfig 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: CatalogerConfig{ 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 MainModuleVersion: DefaultMainModuleVersionConfig(), 55 }, 56 }, 57 { 58 name: "set via configuration", 59 env: map[string]string{ 60 "GOPATH": "/go", 61 "GOPROXY": "https://my.proxy", 62 "GOPRIVATE": "my.private", 63 "GONOPROXY": "no.proxy", 64 }, 65 opts: opts{ 66 local: true, 67 cacheDir: "/go-cache", 68 remote: true, 69 proxy: "https://alt.proxy,direct", 70 noProxy: "alt.no.proxy", 71 }, 72 expected: CatalogerConfig{ 73 SearchLocalModCacheLicenses: true, 74 LocalModCacheDir: "/go-cache", 75 SearchRemoteLicenses: true, 76 Proxies: []string{"https://alt.proxy", "direct"}, 77 NoProxy: []string{"alt.no.proxy"}, 78 MainModuleVersion: DefaultMainModuleVersionConfig(), 79 }, 80 }, 81 } 82 83 for _, test := range tests { 84 t.Run(test.name, func(t *testing.T) { 85 for k, v := range allEnv { 86 t.Setenv(k, v) 87 } 88 for k, v := range test.env { 89 t.Setenv(k, v) 90 } 91 got := DefaultCatalogerConfig(). 92 WithSearchLocalModCacheLicenses(test.opts.local). 93 WithLocalModCacheDir(test.opts.cacheDir). 94 WithSearchRemoteLicenses(test.opts.remote). 95 WithProxy(test.opts.proxy). 96 WithNoProxy(test.opts.noProxy) 97 98 assert.Equal(t, test.expected, got) 99 }) 100 } 101 }