Skip to content

Commit

Permalink
Merge pull request #2 from GAntoine:parse-misconfigured-license-objects
Browse files Browse the repository at this point in the history
Attempt to parse misconfigured license declaration
  • Loading branch information
bnb authored Aug 5, 2019
2 parents e1b0fed + f222720 commit 2c7ef18
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/parseLicense.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,18 @@ function parse (file) {
return handleUndefinedAndNull(file.license.type)
}
}

if (file.licenses !== undefined) {
return handleUndefinedAndNull(file.licenses[0].type)
if (Array.isArray(file.licenses)) {
return handleUndefinedAndNull(file.licenses[0].type)
}

if (typeof file.licenses === 'object') {
return handleUndefinedAndNull(file.licenses.type)
}
}

return handleUndefinedAndNull(undefined)
}

function handleUndefinedAndNull (licenseString) {
Expand Down
3 changes: 3 additions & 0 deletions tests/dummy/package.misconfiguredObjectType.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"licenses": {"type": "LMAO"}
}
3 changes: 3 additions & 0 deletions tests/dummy/package.stringType.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"license": "BRB"
}
26 changes: 26 additions & 0 deletions tests/parseLicense.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ describe('test `license` export', () => {
)
})

test('test the output of an string that is identified by `license`', () => {
const pkg = loadPackagejson.sync(`${__dirname}/dummy/package.stringType.json`)
const actual = parseLicense(pkg)

expect(actual).toEqual(
'BRB'
)
})

test('test the output of an object that is identified by `license` which has a `type` property', () => {
const pkg = loadPackagejson.sync(`${__dirname}/dummy/package.objectType.json`)
const actual = parseLicense(pkg)
Expand All @@ -22,4 +31,21 @@ describe('test `license` export', () => {
)
})

test('test the output of an array that is identified by `licenses` but misconfigured as a single object', () => {
const pkg = loadPackagejson.sync(`${__dirname}/dummy/package.misconfiguredObjectType.json`)
const actual = parseLicense(pkg)

expect(actual).toEqual(
'LMAO'
)
})

test('test the output of a missing license object is informative placeholder text', () => {
const actual = parseLicense({})

expect(actual).toEqual(
'invalid license'
)
})

})

0 comments on commit 2c7ef18

Please sign in to comment.