Skip to content

Commit

Permalink
root component gets a fallback name
Browse files Browse the repository at this point in the history
fix #252

Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
  • Loading branch information
jkowalleck committed Feb 11, 2022
1 parent 26056b0 commit 76bb7a7
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ jobs:
- name: install testing-project "with-dev-dependencies"
run: npm ci
working-directory: tests/with-dev-dependencies
- name: install testing-project "with-dev-dependencies"
run: npm ci
working-directory: tests/no-name
- name: run tests
run: >
npm run test:unit --
Expand Down
6 changes: 5 additions & 1 deletion model/Bom.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ class Bom extends CycloneDXObject {
if (includeSerialNumber) {
this._serialNumber = 'urn:uuid:' + uuid.v4()
}
if (pkg) {
if (pkg && typeof pkg === 'object') {
if (!pkg.name) {
pkg = Object.assign({}, pkg) // work with a modified/fixed clone
pkg.name = 'NO-NAME-PACKAGE'
}
this._metadata = this.createMetadata(pkg, componentType)
this._components = this.listComponents(pkg, lockfile)
} else {
Expand Down
1 change: 1 addition & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ npm ci
## install testing-projects
npm ci --prefix 'tests/with-packages'
npm ci --prefix 'tests/with-dev-dependencies'
npm ci --prefix 'tests/no-name'

## run tests
npm test
Expand Down
102 changes: 102 additions & 0 deletions tests/__snapshots__/index.test.js.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,15 @@ test('createbom produces a BOM when all dependencies are dev-dependencies that s
done()
})
})

test('createbom produces a BOM when there is no name in the root package', done => {
// test for https://github.com/CycloneDX/cyclonedx-node-module/issues/252
bomHelpers.createbom('library', false, true, './tests/no-name', { dev: true }, (err, bom) => {
expect(err).toBeFalsy()

bom.metadata.timestamp = timestamp
bom.metadata.tools[0].version = programVersion
expect(bom.toJSON()).toMatchSnapshot()
done()
})
})
2 changes: 2 additions & 0 deletions tests/no-name/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
this test case is related to
https://github.com/CycloneDX/cyclonedx-node-module/issues/252
40 changes: 40 additions & 0 deletions tests/no-name/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions tests/no-name/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"description": "an package without a name. see https://github.com/CycloneDX/cyclonedx-node-module/issues/252",
"private": true,
"dependencies": {
"packageurl-js": "^0.0.1"
}
}

0 comments on commit 76bb7a7

Please sign in to comment.