-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add license text as evidence (#1243)
fixes #256 This PR is based on #427, and uses the same implementation as it was implemented in yarn (See CycloneDX/cyclonedx-node-yarn#193) closes #427 --------- Signed-off-by: Matthias Schiebel <msl@compart.com> Signed-off-by: Christoph Uhland <42832096+cuhland@users.noreply.github.com> Signed-off-by: Christoph Uhland <christoph.uhland@bdr.de> Co-authored-by: Matthias Schiebel <msl@compart.com>
- Loading branch information
Showing
6 changed files
with
215 additions
and
4 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
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
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/*! | ||
This file is part of CycloneDX generator for NPM projects. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
SPDX-License-Identifier: Apache-2.0 | ||
Copyright (c) OWASP Foundation. All Rights Reserved. | ||
*/ | ||
|
||
const { getMimeForLicenseFile, LICENSE_FILENAME_PATTERN } = require('../../dist/_helpers') | ||
const { describe, expect, test } = require('@jest/globals') | ||
|
||
describe('LICENSE_FILENAME_PATTERN', () => { | ||
test.each([ | ||
'LICENCE', | ||
'licence', | ||
'LICENSE', | ||
'license', | ||
'NOTICE', | ||
'UNLICENCE', | ||
'UNLICENSE'])('valid name: %s', (fileName) => { | ||
const value = LICENSE_FILENAME_PATTERN.test(fileName) | ||
expect(value).toBeTruthy() | ||
}) | ||
test.each([ | ||
'my-license', | ||
'my_license', | ||
'myNotice', | ||
'the-LICENSE'])('invalid name: %s', (fileName) => { | ||
const value = LICENSE_FILENAME_PATTERN.test(fileName) | ||
expect(value).toBeFalsy() | ||
}) | ||
}) | ||
|
||
describe('getMimeForLicenseFile', () => { | ||
test.each([ | ||
['LICENCE', 'text/plain'], | ||
['site.html', 'text/html'], | ||
['license.md', 'text/markdown'], | ||
['info.xml', 'text/xml'], | ||
['UNKNOWN', 'text/plain'] | ||
])('check %s', (fileName, expected) => { | ||
const value = getMimeForLicenseFile(fileName) | ||
expect(value).toBe(expected) | ||
}) | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/*! | ||
This file is part of CycloneDX generator for NPM projects. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
SPDX-License-Identifier: Apache-2.0 | ||
Copyright (c) OWASP Foundation. All Rights Reserved. | ||
*/ | ||
|
||
const { Models, Builders, Factories } = require('@cyclonedx/cyclonedx-library') | ||
const fs = require('fs') | ||
const { BomBuilder, TreeBuilder } = require('../../dist/builders') | ||
const { jest, describe, expect, it } = require('@jest/globals') | ||
|
||
describe('BomBuilder', () => { | ||
const extRefFactory = new Factories.FromNodePackageJson.ExternalReferenceFactory() | ||
const bomBuilder = new BomBuilder( | ||
new Builders.FromNodePackageJson.ToolBuilder(extRefFactory), | ||
new Builders.FromNodePackageJson.ComponentBuilder( | ||
extRefFactory, | ||
new Factories.LicenseFactory() | ||
), | ||
new TreeBuilder(), | ||
new Factories.FromNodePackageJson.PackageUrlFactory('npm'), | ||
{ | ||
ignoreNpmErrors: true, | ||
metaComponentType: true, | ||
packageLockOnly: true, | ||
omitDependencyTypes: [], | ||
reproducible: true, | ||
flattenComponents: true, | ||
shortPURLs: true, | ||
gatherLicenseTexts: true | ||
}, | ||
null | ||
) | ||
describe('License fetching', () => { | ||
it('fetches existing license from directory', async () => { | ||
const fsMock = jest.spyOn(fs, 'readdirSync') | ||
const fileMock = jest.spyOn(fs, 'readFileSync') | ||
fsMock.mockReturnValue(['license.txt']) | ||
fileMock.mockReturnValue('license file content') | ||
const licenses = bomBuilder.fetchLicenseEvidence('test_module') | ||
const license = licenses.next().value | ||
expect(license).toBeInstanceOf(Models.NamedLicense) | ||
expect(license.name).toBe('file: license.txt') | ||
expect(license.text.contentType).toBe('text/plain') | ||
expect(license.text.encoding).toBe('base64') | ||
expect(license.text.content).toBe('license file content') | ||
}) | ||
it('fetches nothing from directory', async () => { | ||
const fsMock = jest.spyOn(fs, 'readdirSync') | ||
fsMock.mockReturnValue(['nothing.txt']) | ||
const licenses = bomBuilder.fetchLicenseEvidence('test_module') | ||
const license = licenses.next().value | ||
expect(license).toBeFalsy() | ||
}) | ||
}) | ||
}) |