Skip to content

Commit

Permalink
Define has() helper function to check for undefined (close #93)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kemitchell committed Sep 2, 2024
1 parent cdf5ad1 commit fd2be2a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module.exports = licensee
var Arborist = require('@npmcli/arborist')
var blueOakList = require('@blueoak/list')
var correctLicenseMetadata = require('correct-license-metadata')
var has = require('hasown')
var hasOwn = require('hasown')
var npmLicenseCorrections = require('npm-license-corrections')
var osi = require('spdx-osi')
var parse = require('spdx-expression-parse')
Expand Down Expand Up @@ -281,3 +281,7 @@ function pushMissing (source, sink) {
if (sink.indexOf(element) === -1) sink.push(element)
})
}

function has (object, key) {
return hasOwn(object, key) && object[key] !== undefined
}
7 changes: 7 additions & 0 deletions tests/osi-flag-pass/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "osi-flag-pass",
"dependencies": {
"gpl-2.0-licensed": "1.0.0"
},
"private": true
}
5 changes: 5 additions & 0 deletions tests/osi-flag-pass/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var tap = require('tap')

var results = require('../run')(['--osi'], __dirname)

tap.equal(results.status, 0)
2 changes: 1 addition & 1 deletion tests/osi-pass/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "blue-oak-fail",
"name": "osi-pass",
"dependencies": {
"gpl-2.0-licensed": "1.0.0"
},
Expand Down

0 comments on commit fd2be2a

Please sign in to comment.