Fix tests and respond to review feedback
This commit is contained in:
@@ -83,7 +83,6 @@ describe('satisfiesAll', () => {
|
|||||||
licenses: ['MIT'],
|
licenses: ['MIT'],
|
||||||
expected: true
|
expected: true
|
||||||
},
|
},
|
||||||
// TODO(dangoor): this does not seem correct to me: the only license is Apache-2.0 and it's on the list
|
|
||||||
{
|
{
|
||||||
candidate: 'Apache-2.0',
|
candidate: 'Apache-2.0',
|
||||||
licenses: ['MIT', 'ISC', 'Apache-2.0'],
|
licenses: ['MIT', 'ISC', 'Apache-2.0'],
|
||||||
@@ -234,7 +233,12 @@ describe('satisfies', () => {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
candidate: 'MIT OR OTHER',
|
candidate: 'MIT OR OTHER',
|
||||||
constraint: 'MIT OR LicenseRef-clearlydefined-OTHER',
|
allowList: ['MIT', 'LicenseRef-clearlydefined-OTHER'],
|
||||||
|
expected: true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
candidate: '(Apache-2.0 AND OTHER) OR (MIT AND OTHER)',
|
||||||
|
allowList: ['Apache-2.0', 'LicenseRef-clearlydefined-OTHER'],
|
||||||
expected: true
|
expected: true
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@@ -286,7 +290,7 @@ describe('isValid', () => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('removeInvalidSPDX', () => {
|
describe('cleanInvalidSPDX', () => {
|
||||||
const units = [
|
const units = [
|
||||||
{
|
{
|
||||||
candidate: 'MIT',
|
candidate: 'MIT',
|
||||||
@@ -314,7 +318,7 @@ describe('removeInvalidSPDX', () => {
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
for (const unit of units) {
|
for (const unit of units) {
|
||||||
const got: string = spdx.removeInvalidSPDX(unit.candidate)
|
const got: string = spdx.cleanInvalidSPDX(unit.candidate)
|
||||||
test(`should return ${unit.expected} for ("${unit.candidate}")`, () => {
|
test(`should return ${unit.expected} for ("${unit.candidate}")`, () => {
|
||||||
expect(got).toBe(unit.expected)
|
expect(got).toBe(unit.expected)
|
||||||
})
|
})
|
||||||
|
|||||||
+6
-6
@@ -12,7 +12,7 @@ import parse from 'spdx-expression-parse'
|
|||||||
// accepts a pair of well-formed SPDX expressions. the
|
// accepts a pair of well-formed SPDX expressions. the
|
||||||
// candidate is tested against the constraint
|
// candidate is tested against the constraint
|
||||||
export function satisfies(candidateExpr: string, allowList: string[]): boolean {
|
export function satisfies(candidateExpr: string, allowList: string[]): boolean {
|
||||||
candidateExpr = removeInvalidSPDX(candidateExpr)
|
candidateExpr = cleanInvalidSPDX(candidateExpr)
|
||||||
try {
|
try {
|
||||||
return spdxSatisfies(candidateExpr, allowList)
|
return spdxSatisfies(candidateExpr, allowList)
|
||||||
} catch (_) {
|
} catch (_) {
|
||||||
@@ -25,7 +25,7 @@ export function satisfiesAny(
|
|||||||
candidateExpr: string,
|
candidateExpr: string,
|
||||||
licenses: string[]
|
licenses: string[]
|
||||||
): boolean {
|
): boolean {
|
||||||
candidateExpr = removeInvalidSPDX(candidateExpr)
|
candidateExpr = cleanInvalidSPDX(candidateExpr)
|
||||||
try {
|
try {
|
||||||
return spdxlib.satisfiesAny(candidateExpr, licenses)
|
return spdxlib.satisfiesAny(candidateExpr, licenses)
|
||||||
} catch (_) {
|
} catch (_) {
|
||||||
@@ -38,7 +38,7 @@ export function satisfiesAll(
|
|||||||
candidateExpr: string,
|
candidateExpr: string,
|
||||||
licenses: string[]
|
licenses: string[]
|
||||||
): boolean {
|
): boolean {
|
||||||
candidateExpr = removeInvalidSPDX(candidateExpr)
|
candidateExpr = cleanInvalidSPDX(candidateExpr)
|
||||||
try {
|
try {
|
||||||
return spdxlib.satisfiesAll(candidateExpr, licenses)
|
return spdxlib.satisfiesAll(candidateExpr, licenses)
|
||||||
} catch (_) {
|
} catch (_) {
|
||||||
@@ -48,7 +48,7 @@ export function satisfiesAll(
|
|||||||
|
|
||||||
// accepts any SPDX expression
|
// accepts any SPDX expression
|
||||||
export function isValid(spdxExpr: string): boolean {
|
export function isValid(spdxExpr: string): boolean {
|
||||||
spdxExpr = removeInvalidSPDX(spdxExpr)
|
spdxExpr = cleanInvalidSPDX(spdxExpr)
|
||||||
try {
|
try {
|
||||||
parse(spdxExpr)
|
parse(spdxExpr)
|
||||||
return true
|
return true
|
||||||
@@ -57,10 +57,10 @@ export function isValid(spdxExpr: string): boolean {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const replaceOtherRegex = /(?<![\w-])OTHER(?![\w-])/
|
const replaceOtherRegex = /(?<![\w-])OTHER(?![\w-])/g
|
||||||
|
|
||||||
// adjusts license expressions to not include the invalid `OTHER`
|
// adjusts license expressions to not include the invalid `OTHER`
|
||||||
// which ClearlyDefined adds to license strings
|
// which ClearlyDefined adds to license strings
|
||||||
export function removeInvalidSPDX(spdxExpr: string): string {
|
export function cleanInvalidSPDX(spdxExpr: string): string {
|
||||||
return spdxExpr.replace(replaceOtherRegex, 'LicenseRef-clearlydefined-OTHER')
|
return spdxExpr.replace(replaceOtherRegex, 'LicenseRef-clearlydefined-OTHER')
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user