refactor javascript cataloger to use configuration options when creating packages (#2438)

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
This commit is contained in:
Alex Goodman 2023-12-15 17:11:02 -05:00 committed by GitHub
parent 05660da8d7
commit 4eace4b141
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 10 deletions

View File

@ -3,25 +3,25 @@ package javascript
const npmBaseURL = "https://registry.npmjs.org" const npmBaseURL = "https://registry.npmjs.org"
type CatalogerConfig struct { type CatalogerConfig struct {
searchRemoteLicenses bool SearchRemoteLicenses bool `json:"search-remote-licenses" yaml:"search-remote-licenses" mapstructure:"search-remote-licenses"`
npmBaseURL string NPMBaseURL string `json:"npm-base-url" yaml:"npm-base-url" mapstructure:"npm-base-url"`
} }
func DefaultCatalogerConfig() CatalogerConfig { func DefaultCatalogerConfig() CatalogerConfig {
return CatalogerConfig{ return CatalogerConfig{
searchRemoteLicenses: false, SearchRemoteLicenses: false,
npmBaseURL: npmBaseURL, NPMBaseURL: npmBaseURL,
} }
} }
func (j CatalogerConfig) WithSearchRemoteLicenses(input bool) CatalogerConfig { func (j CatalogerConfig) WithSearchRemoteLicenses(input bool) CatalogerConfig {
j.searchRemoteLicenses = input j.SearchRemoteLicenses = input
return j return j
} }
func (j CatalogerConfig) WithNpmBaseURL(input string) CatalogerConfig { func (j CatalogerConfig) WithNpmBaseURL(input string) CatalogerConfig {
if input != "" { if input != "" {
j.npmBaseURL = input j.NPMBaseURL = input
} }
return j return j
} }

View File

@ -113,8 +113,8 @@ func newPnpmPackage(resolver file.Resolver, location file.Location, name, versio
func newYarnLockPackage(cfg CatalogerConfig, resolver file.Resolver, location file.Location, name, version string) pkg.Package { func newYarnLockPackage(cfg CatalogerConfig, resolver file.Resolver, location file.Location, name, version string) pkg.Package {
var licenseSet pkg.LicenseSet var licenseSet pkg.LicenseSet
if cfg.searchRemoteLicenses { if cfg.SearchRemoteLicenses {
license, err := getLicenseFromNpmRegistry(cfg.npmBaseURL, name, version) license, err := getLicenseFromNpmRegistry(cfg.NPMBaseURL, name, version)
if err == nil && license != "" { if err == nil && license != "" {
licenses := pkg.NewLicensesFromValues(license) licenses := pkg.NewLicensesFromValues(license)
licenseSet = pkg.NewLicenseSet(licenses...) licenseSet = pkg.NewLicenseSet(licenses...)

View File

@ -204,7 +204,7 @@ func TestSearchYarnForLicenses(t *testing.T) {
}{ }{
{ {
name: "search remote licenses returns the expected licenses when search is set to true", name: "search remote licenses returns the expected licenses when search is set to true",
config: CatalogerConfig{searchRemoteLicenses: true}, config: CatalogerConfig{SearchRemoteLicenses: true},
requestHandlers: []handlerPath{ requestHandlers: []handlerPath{
{ {
// https://registry.yarnpkg.com/@babel/code-frame/7.10.4 // https://registry.yarnpkg.com/@babel/code-frame/7.10.4
@ -232,7 +232,7 @@ func TestSearchYarnForLicenses(t *testing.T) {
for _, handler := range tc.requestHandlers { for _, handler := range tc.requestHandlers {
mux.HandleFunc(handler.path, handler.handler) mux.HandleFunc(handler.path, handler.handler)
} }
tc.config.npmBaseURL = url tc.config.NPMBaseURL = url
adapter := newGenericYarnLockAdapter(tc.config) adapter := newGenericYarnLockAdapter(tc.config)
pkgtest.TestFileParser(t, fixture, adapter.parseYarnLock, tc.expectedPackages, nil) pkgtest.TestFileParser(t, fixture, adapter.parseYarnLock, tc.expectedPackages, nil)
}) })