Merge pull request #926 from dangoor/dangoor/replace-other

Replace OTHER with a LicenseRef
This commit is contained in:
Kevin Dangoor
2025-05-07 13:25:04 -04:00
committed by GitHub
4 changed files with 106 additions and 3 deletions
+69
View File
@@ -55,6 +55,16 @@ describe('satisfiesAny', () => {
candidate: 'MIT OR ISC',
licenses: ['MiT'],
expected: false
},
{
candidate: 'MIT AND OTHER',
licenses: ['MIT'],
expected: false
},
{
candidate: 'MIT OR OTHER',
licenses: ['MIT', 'LicenseRef-clearlydefined-OTHER'],
expected: true
}
]
@@ -130,6 +140,16 @@ describe('satisfiesAll', () => {
candidate: 'MIT OR ISC',
licenses: ['MiT'],
expected: false
},
{
candidate: 'MIT AND OTHER',
licenses: ['MIT'],
expected: false
},
{
candidate: 'MIT AND OTHER',
licenses: ['MIT', 'LicenseRef-clearlydefined-OTHER'],
expected: true
}
]
@@ -210,6 +230,16 @@ describe('satisfies', () => {
candidate: '',
allowList: ['BSD-3-Clause', 'ISC', 'MIT'],
expected: false
},
{
candidate: 'MIT OR 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
}
]
@@ -246,6 +276,10 @@ describe('isValid', () => {
{
candidate: '',
expected: false
},
{
candidate: 'MIT AND OTHER',
expected: true
}
]
for (const unit of units) {
@@ -255,3 +289,38 @@ describe('isValid', () => {
})
}
})
describe('cleanInvalidSPDX', () => {
const units = [
{
candidate: 'MIT',
expected: 'MIT'
},
{
candidate: 'OTHER',
expected: 'LicenseRef-clearlydefined-OTHER'
},
{
candidate: 'LicenseRef-clearlydefined-OTHER',
expected: 'LicenseRef-clearlydefined-OTHER'
},
{
candidate: 'OTHER AND MIT',
expected: 'LicenseRef-clearlydefined-OTHER AND MIT'
},
{
candidate: 'MIT AND OTHER',
expected: 'MIT AND LicenseRef-clearlydefined-OTHER'
},
{
candidate: 'MIT AND SomethingElse-OTHER',
expected: 'MIT AND SomethingElse-OTHER'
}
]
for (const unit of units) {
const got: string = spdx.cleanInvalidSPDX(unit.candidate)
test(`should return ${unit.expected} for ("${unit.candidate}")`, () => {
expect(got).toBe(unit.expected)
})
}
})
Generated Vendored
+24 -2
View File
@@ -1380,7 +1380,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.isValid = exports.satisfiesAll = exports.satisfiesAny = exports.satisfies = void 0;
exports.cleanInvalidSPDX = exports.isValid = exports.satisfiesAll = exports.satisfiesAny = exports.satisfies = void 0;
const spdxlib = __importStar(__nccwpck_require__(1452));
const spdx_satisfies_1 = __importDefault(__nccwpck_require__(5131));
const spdx_expression_parse_1 = __importDefault(__nccwpck_require__(3326));
@@ -1393,6 +1393,7 @@ const spdx_expression_parse_1 = __importDefault(__nccwpck_require__(3326));
// accepts a pair of well-formed SPDX expressions. the
// candidate is tested against the constraint
function satisfies(candidateExpr, allowList) {
candidateExpr = cleanInvalidSPDX(candidateExpr);
try {
return (0, spdx_satisfies_1.default)(candidateExpr, allowList);
}
@@ -1403,6 +1404,7 @@ function satisfies(candidateExpr, allowList) {
exports.satisfies = satisfies;
// accepts an SPDX expression and a non-empty list of licenses (not expressions)
function satisfiesAny(candidateExpr, licenses) {
candidateExpr = cleanInvalidSPDX(candidateExpr);
try {
return spdxlib.satisfiesAny(candidateExpr, licenses);
}
@@ -1413,6 +1415,7 @@ function satisfiesAny(candidateExpr, licenses) {
exports.satisfiesAny = satisfiesAny;
// accepts an SPDX expression and a non-empty list of licenses (not expressions)
function satisfiesAll(candidateExpr, licenses) {
candidateExpr = cleanInvalidSPDX(candidateExpr);
try {
return spdxlib.satisfiesAll(candidateExpr, licenses);
}
@@ -1423,6 +1426,7 @@ function satisfiesAll(candidateExpr, licenses) {
exports.satisfiesAll = satisfiesAll;
// accepts any SPDX expression
function isValid(spdxExpr) {
spdxExpr = cleanInvalidSPDX(spdxExpr);
try {
(0, spdx_expression_parse_1.default)(spdxExpr);
return true;
@@ -1432,6 +1436,13 @@ function isValid(spdxExpr) {
}
}
exports.isValid = isValid;
const replaceOtherRegex = /(?<![\w-])OTHER(?![\w-])/g;
// adjusts license expressions to not include the invalid `OTHER`
// which ClearlyDefined adds to license strings
function cleanInvalidSPDX(spdxExpr) {
return spdxExpr.replace(replaceOtherRegex, 'LicenseRef-clearlydefined-OTHER');
}
exports.cleanInvalidSPDX = cleanInvalidSPDX;
/***/ }),
@@ -50493,7 +50504,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.isValid = exports.satisfiesAll = exports.satisfiesAny = exports.satisfies = void 0;
exports.cleanInvalidSPDX = exports.isValid = exports.satisfiesAll = exports.satisfiesAny = exports.satisfies = void 0;
const spdxlib = __importStar(__nccwpck_require__(1452));
const spdx_satisfies_1 = __importDefault(__nccwpck_require__(5131));
const spdx_expression_parse_1 = __importDefault(__nccwpck_require__(3326));
@@ -50506,6 +50517,7 @@ const spdx_expression_parse_1 = __importDefault(__nccwpck_require__(3326));
// accepts a pair of well-formed SPDX expressions. the
// candidate is tested against the constraint
function satisfies(candidateExpr, allowList) {
candidateExpr = cleanInvalidSPDX(candidateExpr);
try {
return (0, spdx_satisfies_1.default)(candidateExpr, allowList);
}
@@ -50516,6 +50528,7 @@ function satisfies(candidateExpr, allowList) {
exports.satisfies = satisfies;
// accepts an SPDX expression and a non-empty list of licenses (not expressions)
function satisfiesAny(candidateExpr, licenses) {
candidateExpr = cleanInvalidSPDX(candidateExpr);
try {
return spdxlib.satisfiesAny(candidateExpr, licenses);
}
@@ -50526,6 +50539,7 @@ function satisfiesAny(candidateExpr, licenses) {
exports.satisfiesAny = satisfiesAny;
// accepts an SPDX expression and a non-empty list of licenses (not expressions)
function satisfiesAll(candidateExpr, licenses) {
candidateExpr = cleanInvalidSPDX(candidateExpr);
try {
return spdxlib.satisfiesAll(candidateExpr, licenses);
}
@@ -50536,6 +50550,7 @@ function satisfiesAll(candidateExpr, licenses) {
exports.satisfiesAll = satisfiesAll;
// accepts any SPDX expression
function isValid(spdxExpr) {
spdxExpr = cleanInvalidSPDX(spdxExpr);
try {
(0, spdx_expression_parse_1.default)(spdxExpr);
return true;
@@ -50545,6 +50560,13 @@ function isValid(spdxExpr) {
}
}
exports.isValid = isValid;
const replaceOtherRegex = /(?<![\w-])OTHER(?![\w-])/g;
// adjusts license expressions to not include the invalid `OTHER`
// which ClearlyDefined adds to license strings
function cleanInvalidSPDX(spdxExpr) {
return spdxExpr.replace(replaceOtherRegex, 'LicenseRef-clearlydefined-OTHER');
}
exports.cleanInvalidSPDX = cleanInvalidSPDX;
/***/ }),
Generated Vendored
+1 -1
View File
File diff suppressed because one or more lines are too long
+12
View File
@@ -12,6 +12,7 @@ import parse from 'spdx-expression-parse'
// accepts a pair of well-formed SPDX expressions. the
// candidate is tested against the constraint
export function satisfies(candidateExpr: string, allowList: string[]): boolean {
candidateExpr = cleanInvalidSPDX(candidateExpr)
try {
return spdxSatisfies(candidateExpr, allowList)
} catch (_) {
@@ -24,6 +25,7 @@ export function satisfiesAny(
candidateExpr: string,
licenses: string[]
): boolean {
candidateExpr = cleanInvalidSPDX(candidateExpr)
try {
return spdxlib.satisfiesAny(candidateExpr, licenses)
} catch (_) {
@@ -36,6 +38,7 @@ export function satisfiesAll(
candidateExpr: string,
licenses: string[]
): boolean {
candidateExpr = cleanInvalidSPDX(candidateExpr)
try {
return spdxlib.satisfiesAll(candidateExpr, licenses)
} catch (_) {
@@ -45,6 +48,7 @@ export function satisfiesAll(
// accepts any SPDX expression
export function isValid(spdxExpr: string): boolean {
spdxExpr = cleanInvalidSPDX(spdxExpr)
try {
parse(spdxExpr)
return true
@@ -52,3 +56,11 @@ export function isValid(spdxExpr: string): boolean {
return false
}
}
const replaceOtherRegex = /(?<![\w-])OTHER(?![\w-])/g
// adjusts license expressions to not include the invalid `OTHER`
// which ClearlyDefined adds to license strings
export function cleanInvalidSPDX(spdxExpr: string): string {
return spdxExpr.replace(replaceOtherRegex, 'LicenseRef-clearlydefined-OTHER')
}