Skip to content

Commit c7565d4

Browse files
committed
Fix tests and respond to review feedback
1 parent 82299c3 commit c7565d4

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

__tests__/spdx.test.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ describe('satisfiesAll', () => {
8383
licenses: ['MIT'],
8484
expected: true
8585
},
86-
// TODO(dangoor): this does not seem correct to me: the only license is Apache-2.0 and it's on the list
8786
{
8887
candidate: 'Apache-2.0',
8988
licenses: ['MIT', 'ISC', 'Apache-2.0'],
@@ -234,7 +233,12 @@ describe('satisfies', () => {
234233
},
235234
{
236235
candidate: 'MIT OR OTHER',
237-
constraint: 'MIT OR LicenseRef-clearlydefined-OTHER',
236+
allowList: ['MIT', 'LicenseRef-clearlydefined-OTHER'],
237+
expected: true
238+
},
239+
{
240+
candidate: '(Apache-2.0 AND OTHER) OR (MIT AND OTHER)',
241+
allowList: ['Apache-2.0', 'LicenseRef-clearlydefined-OTHER'],
238242
expected: true
239243
}
240244
]
@@ -286,7 +290,7 @@ describe('isValid', () => {
286290
}
287291
})
288292

289-
describe('removeInvalidSPDX', () => {
293+
describe('cleanInvalidSPDX', () => {
290294
const units = [
291295
{
292296
candidate: 'MIT',
@@ -314,7 +318,7 @@ describe('removeInvalidSPDX', () => {
314318
}
315319
]
316320
for (const unit of units) {
317-
const got: string = spdx.removeInvalidSPDX(unit.candidate)
321+
const got: string = spdx.cleanInvalidSPDX(unit.candidate)
318322
test(`should return ${unit.expected} for ("${unit.candidate}")`, () => {
319323
expect(got).toBe(unit.expected)
320324
})

src/spdx.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import parse from 'spdx-expression-parse'
1212
// accepts a pair of well-formed SPDX expressions. the
1313
// candidate is tested against the constraint
1414
export function satisfies(candidateExpr: string, allowList: string[]): boolean {
15-
candidateExpr = removeInvalidSPDX(candidateExpr)
15+
candidateExpr = cleanInvalidSPDX(candidateExpr)
1616
try {
1717
return spdxSatisfies(candidateExpr, allowList)
1818
} catch (_) {
@@ -25,7 +25,7 @@ export function satisfiesAny(
2525
candidateExpr: string,
2626
licenses: string[]
2727
): boolean {
28-
candidateExpr = removeInvalidSPDX(candidateExpr)
28+
candidateExpr = cleanInvalidSPDX(candidateExpr)
2929
try {
3030
return spdxlib.satisfiesAny(candidateExpr, licenses)
3131
} catch (_) {
@@ -38,7 +38,7 @@ export function satisfiesAll(
3838
candidateExpr: string,
3939
licenses: string[]
4040
): boolean {
41-
candidateExpr = removeInvalidSPDX(candidateExpr)
41+
candidateExpr = cleanInvalidSPDX(candidateExpr)
4242
try {
4343
return spdxlib.satisfiesAll(candidateExpr, licenses)
4444
} catch (_) {
@@ -48,7 +48,7 @@ export function satisfiesAll(
4848

4949
// accepts any SPDX expression
5050
export function isValid(spdxExpr: string): boolean {
51-
spdxExpr = removeInvalidSPDX(spdxExpr)
51+
spdxExpr = cleanInvalidSPDX(spdxExpr)
5252
try {
5353
parse(spdxExpr)
5454
return true
@@ -57,10 +57,10 @@ export function isValid(spdxExpr: string): boolean {
5757
}
5858
}
5959

60-
const replaceOtherRegex = /(?<![\w-])OTHER(?![\w-])/
60+
const replaceOtherRegex = /(?<![\w-])OTHER(?![\w-])/g
6161

6262
// adjusts license expressions to not include the invalid `OTHER`
6363
// which ClearlyDefined adds to license strings
64-
export function removeInvalidSPDX(spdxExpr: string): string {
64+
export function cleanInvalidSPDX(spdxExpr: string): string {
6565
return spdxExpr.replace(replaceOtherRegex, 'LicenseRef-clearlydefined-OTHER')
6666
}

0 commit comments

Comments
 (0)