Skip to content

Commit fd2be2a

Browse files
committed
Define has() helper function to check for undefined (close #93)
Previously, Licensee would create a license criteria configuration object at runtime when options like `--osi` and `--blueoak=` were set. Unfortunately, the code that did so used constructions like `option['blueOak'] || undefined`, while logic for checking licenses based on that configuration used the `hasown` package to check whether various kinds of license constraints had been set. This set us up for errors, like the one reported by @jayvdb in #93, where Licensee attempts to read constraints that haven't been provided, since they exist as own properties of the configuration object, but are set to `undefined`. This PR adds a test replicating #93 and redefines the `has()` helper function as a wrapper around `hasown` that _also_ checks to make sure the own property value is _not_ `undefined`. This avoids the error, and should for other invocations with similar flags.
1 parent cdf5ad1 commit fd2be2a

File tree

4 files changed

+18
-2
lines changed

4 files changed

+18
-2
lines changed

index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module.exports = licensee
33
var Arborist = require('@npmcli/arborist')
44
var blueOakList = require('@blueoak/list')
55
var correctLicenseMetadata = require('correct-license-metadata')
6-
var has = require('hasown')
6+
var hasOwn = require('hasown')
77
var npmLicenseCorrections = require('npm-license-corrections')
88
var osi = require('spdx-osi')
99
var parse = require('spdx-expression-parse')
@@ -281,3 +281,7 @@ function pushMissing (source, sink) {
281281
if (sink.indexOf(element) === -1) sink.push(element)
282282
})
283283
}
284+
285+
function has (object, key) {
286+
return hasOwn(object, key) && object[key] !== undefined
287+
}

tests/osi-flag-pass/package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "osi-flag-pass",
3+
"dependencies": {
4+
"gpl-2.0-licensed": "1.0.0"
5+
},
6+
"private": true
7+
}

tests/osi-flag-pass/test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var tap = require('tap')
2+
3+
var results = require('../run')(['--osi'], __dirname)
4+
5+
tap.equal(results.status, 0)

tests/osi-pass/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "blue-oak-fail",
2+
"name": "osi-pass",
33
"dependencies": {
44
"gpl-2.0-licensed": "1.0.0"
55
},

0 commit comments

Comments
 (0)