-
Notifications
You must be signed in to change notification settings - Fork 595
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: golang remote license search not executing when error reading lo…
…cal mod dir (#3549) Signed-off-by: Keith Zantow <[email protected]>
- Loading branch information
Showing
4 changed files
with
103 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,81 +23,11 @@ import ( | |
"github.com/anchore/syft/syft/pkg" | ||
) | ||
|
||
func Test_LocalLicenseSearch(t *testing.T) { | ||
func Test_LicenseSearch(t *testing.T) { | ||
loc1 := file.NewLocation("github.com/someorg/[email protected]/LICENSE") | ||
loc2 := file.NewLocation("github.com/!cap!o!r!g/[email protected]/LICENSE.txt") | ||
loc3 := file.NewLocation("github.com/someorg/[email protected]/LiCeNsE.tXt") | ||
|
||
licenseScanner := licenses.TestingOnlyScanner() | ||
|
||
tests := []struct { | ||
name string | ||
version string | ||
expected pkg.License | ||
}{ | ||
{ | ||
name: "github.com/someorg/somename", | ||
version: "v0.3.2", | ||
expected: pkg.License{ | ||
Value: "Apache-2.0", | ||
SPDXExpression: "Apache-2.0", | ||
Type: license.Concluded, | ||
URLs: []string{"file://$GOPATH/pkg/mod/" + loc1.RealPath}, | ||
Locations: file.NewLocationSet(), | ||
}, | ||
}, | ||
{ | ||
name: "github.com/CapORG/CapProject", | ||
version: "v4.111.5", | ||
expected: pkg.License{ | ||
Value: "MIT", | ||
SPDXExpression: "MIT", | ||
Type: license.Concluded, | ||
URLs: []string{"file://$GOPATH/pkg/mod/" + loc2.RealPath}, | ||
Locations: file.NewLocationSet(), | ||
}, | ||
}, | ||
{ | ||
name: "github.com/someorg/strangelicense", | ||
version: "v1.2.3", | ||
expected: pkg.License{ | ||
Value: "Apache-2.0", | ||
SPDXExpression: "Apache-2.0", | ||
Type: license.Concluded, | ||
URLs: []string{"file://$GOPATH/pkg/mod/" + loc3.RealPath}, | ||
Locations: file.NewLocationSet(), | ||
}, | ||
}, | ||
} | ||
|
||
wd, err := os.Getwd() | ||
require.NoError(t, err) | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
l := newGoLicenseResolver( | ||
"", | ||
CatalogerConfig{ | ||
SearchLocalModCacheLicenses: true, | ||
LocalModCacheDir: filepath.Join(wd, "test-fixtures", "licenses", "pkg", "mod"), | ||
}, | ||
) | ||
lics, err := l.getLicenses(context.Background(), licenseScanner, fileresolver.Empty{}, test.name, test.version) | ||
require.NoError(t, err) | ||
|
||
require.Len(t, lics, 1) | ||
|
||
require.Equal(t, test.expected, lics[0]) | ||
}) | ||
} | ||
} | ||
|
||
func Test_RemoteProxyLicenseSearch(t *testing.T) { | ||
loc1 := file.NewLocation("github.com/someorg/[email protected]/LICENSE") | ||
loc2 := file.NewLocation("github.com/!cap!o!r!g/[email protected]/LICENSE.txt") | ||
|
||
licenseScanner := licenses.TestingOnlyScanner() | ||
|
||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
buf := &bytes.Buffer{} | ||
uri := strings.TrimPrefix(strings.TrimSuffix(r.RequestURI, ".zip"), "/") | ||
|
@@ -135,52 +65,116 @@ func Test_RemoteProxyLicenseSearch(t *testing.T) { | |
})) | ||
defer server.Close() | ||
|
||
wd, err := os.Getwd() | ||
require.NoError(t, err) | ||
|
||
licenseScanner := licenses.TestingOnlyScanner() | ||
|
||
tests := []struct { | ||
name string | ||
version string | ||
expected pkg.License | ||
config CatalogerConfig | ||
expected []pkg.License | ||
}{ | ||
{ | ||
name: "github.com/someorg/somename", | ||
version: "v0.3.2", | ||
expected: pkg.License{ | ||
config: CatalogerConfig{ | ||
SearchLocalModCacheLicenses: true, | ||
LocalModCacheDir: filepath.Join(wd, "test-fixtures", "licenses", "pkg", "mod"), | ||
}, | ||
expected: []pkg.License{{ | ||
Value: "Apache-2.0", | ||
SPDXExpression: "Apache-2.0", | ||
Type: license.Concluded, | ||
URLs: []string{server.URL + "/github.com/someorg/somename/@v/v0.3.2.zip#" + loc1.RealPath}, | ||
URLs: []string{"file://$GOPATH/pkg/mod/" + loc1.RealPath}, | ||
Locations: file.NewLocationSet(), | ||
}}, | ||
}, | ||
{ | ||
name: "github.com/CapORG/CapProject", | ||
version: "v4.111.5", | ||
config: CatalogerConfig{ | ||
SearchLocalModCacheLicenses: true, | ||
LocalModCacheDir: filepath.Join(wd, "test-fixtures", "licenses", "pkg", "mod"), | ||
}, | ||
expected: []pkg.License{{ | ||
Value: "MIT", | ||
SPDXExpression: "MIT", | ||
Type: license.Concluded, | ||
URLs: []string{"file://$GOPATH/pkg/mod/" + loc2.RealPath}, | ||
Locations: file.NewLocationSet(), | ||
}}, | ||
}, | ||
{ | ||
name: "github.com/someorg/strangelicense", | ||
version: "v1.2.3", | ||
config: CatalogerConfig{ | ||
SearchLocalModCacheLicenses: true, | ||
LocalModCacheDir: filepath.Join(wd, "test-fixtures", "licenses", "pkg", "mod"), | ||
}, | ||
expected: []pkg.License{{ | ||
Value: "Apache-2.0", | ||
SPDXExpression: "Apache-2.0", | ||
Type: license.Concluded, | ||
URLs: []string{"file://$GOPATH/pkg/mod/" + loc3.RealPath}, | ||
Locations: file.NewLocationSet(), | ||
}}, | ||
}, | ||
{ | ||
name: "github.com/someorg/somename", | ||
version: "v0.3.2", | ||
config: CatalogerConfig{ | ||
SearchRemoteLicenses: true, | ||
Proxies: []string{server.URL}, | ||
}, | ||
expected: []pkg.License{{ | ||
Value: "Apache-2.0", | ||
SPDXExpression: "Apache-2.0", | ||
Type: license.Concluded, | ||
URLs: []string{server.URL + "/github.com/someorg/somename/@v/v0.3.2.zip#" + loc1.RealPath}, | ||
Locations: file.NewLocationSet(), | ||
}}, | ||
}, | ||
{ | ||
name: "github.com/CapORG/CapProject", | ||
version: "v4.111.5", | ||
expected: pkg.License{ | ||
config: CatalogerConfig{ | ||
SearchRemoteLicenses: true, | ||
Proxies: []string{server.URL}, | ||
}, | ||
expected: []pkg.License{{ | ||
Value: "MIT", | ||
SPDXExpression: "MIT", | ||
Type: license.Concluded, | ||
URLs: []string{server.URL + "/github.com/CapORG/CapProject/@v/v4.111.5.zip#" + loc2.RealPath}, | ||
Locations: file.NewLocationSet(), | ||
}}, | ||
}, | ||
{ | ||
name: "github.com/CapORG/CapProject", | ||
version: "v4.111.5", | ||
config: CatalogerConfig{ | ||
SearchLocalModCacheLicenses: true, | ||
LocalModCacheDir: filepath.Join(wd, "test-fixtures"), // valid dir but does not find modules | ||
SearchRemoteLicenses: true, | ||
Proxies: []string{server.URL}, | ||
}, | ||
expected: []pkg.License{{ | ||
Value: "MIT", | ||
SPDXExpression: "MIT", | ||
Type: license.Concluded, | ||
URLs: []string{server.URL + "/github.com/CapORG/CapProject/@v/v4.111.5.zip#" + loc2.RealPath}, | ||
Locations: file.NewLocationSet(), | ||
}}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
|
||
l := newGoLicenseResolver( | ||
"", | ||
CatalogerConfig{ | ||
SearchRemoteLicenses: true, | ||
Proxies: []string{server.URL}, | ||
}, | ||
) | ||
|
||
lics, err := l.getLicenses(context.Background(), licenseScanner, fileresolver.Empty{}, test.name, test.version) | ||
require.NoError(t, err) | ||
|
||
require.Len(t, lics, 1) | ||
|
||
require.Equal(t, test.expected, lics[0]) | ||
l := newGoLicenseResolver("", test.config) | ||
lics := l.getLicenses(context.Background(), licenseScanner, fileresolver.Empty{}, test.name, test.version) | ||
require.EqualValues(t, test.expected, lics) | ||
}) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters