Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to parse misconfigured license declaration #2

Merged
merged 1 commit into from
Aug 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
)
})

})