diff --git a/__tests__/config.test.ts b/__tests__/config.test.ts index 315a752..3f26da1 100644 --- a/__tests__/config.test.ts +++ b/__tests__/config.test.ts @@ -1,13 +1,9 @@ import {expect, test, beforeEach} from '@jest/globals' import {readConfig} from '../src/config' import {getRefs} from '../src/git-refs' -import * as Utils from '../src/utils' +import * as spdx from '../src/spdx' import {setInput, clearInputs} from './test-helpers' -beforeAll(() => { - jest.spyOn(Utils, 'isSPDXValid').mockReturnValue(true) -}) - beforeEach(() => { clearInputs() }) @@ -19,11 +15,11 @@ test('it defaults to low severity', async () => { test('it reads custom configs', async () => { setInput('fail-on-severity', 'critical') - setInput('allow-licenses', ' BSD, GPL 2') + setInput('allow-licenses', 'ISC, GPL-2.0') const config = await readConfig() expect(config.fail_on_severity).toEqual('critical') - expect(config.allow_licenses).toEqual(['BSD', 'GPL 2']) + expect(config.allow_licenses).toEqual(['ISC', 'GPL-2.0']) }) test('it defaults to false for warn-only', async () => { @@ -40,7 +36,7 @@ test('it defaults to empty allow/deny lists ', async () => { test('it raises an error if both an allow and denylist are specified', async () => { setInput('allow-licenses', 'MIT') - setInput('deny-licenses', 'BSD') + setInput('deny-licenses', 'BSD-3-Clause') await expect(readConfig()).rejects.toThrow( 'You cannot specify both allow-licenses and deny-licenses' @@ -204,21 +200,17 @@ test('it is not possible to disable both checks', async () => { }) describe('licenses that are not valid SPDX licenses', () => { - beforeAll(() => { - jest.spyOn(Utils, 'isSPDXValid').mockReturnValue(false) - }) - test('it raises an error for invalid licenses in allow-licenses', async () => { - setInput('allow-licenses', ' BSD, GPL 2') + setInput('allow-licenses', ' BSD-YOLO, GPL-2.0') await expect(readConfig()).rejects.toThrow( - 'Invalid license(s) in allow-licenses: BSD,GPL 2' + 'Invalid license(s) in allow-licenses: BSD-YOLO' ) }) test('it raises an error for invalid licenses in deny-licenses', async () => { - setInput('deny-licenses', ' BSD, GPL 2') + setInput('deny-licenses', ' GPL-2.0, BSD-YOLO, Apache-2.0, ToIll') await expect(readConfig()).rejects.toThrow( - 'Invalid license(s) in deny-licenses: BSD,GPL 2' + 'Invalid license(s) in deny-licenses: BSD-YOLO, ToIll' ) }) }) diff --git a/__tests__/deny.test.ts b/__tests__/deny.test.ts index 24e6be8..ad4fec6 100644 --- a/__tests__/deny.test.ts +++ b/__tests__/deny.test.ts @@ -33,11 +33,6 @@ jest.mock('octokit', () => { beforeEach(async () => { jest.resetModules() - jest.doMock('spdx-satisfies', () => { - // mock spdx-satisfies return value - // true for BSD, false for all others - return jest.fn((license: string, _: string): boolean => license === 'BSD') - }) npmChange = createTestChange({ecosystem: 'npm'}) rubyChange = createTestChange({ecosystem: 'rubygems'}) diff --git a/__tests__/external-config.test.ts b/__tests__/external-config.test.ts index a1a0e7c..e525b71 100644 --- a/__tests__/external-config.test.ts +++ b/__tests__/external-config.test.ts @@ -1,6 +1,6 @@ import {expect, test, beforeEach} from '@jest/globals' import {readConfig} from '../src/config' -import * as Utils from '../src/utils' +import * as spdx from '../src/spdx' import {setInput, clearInputs} from './test-helpers' const externalConfig = `fail_on_severity: 'high' @@ -25,10 +25,6 @@ jest.mock('octokit', () => { } }) -beforeAll(() => { - jest.spyOn(Utils, 'isSPDXValid').mockReturnValue(true) -}) - beforeEach(() => { clearInputs() }) diff --git a/__tests__/spdx.test.ts b/__tests__/spdx.test.ts index 460f4f1..95eb50c 100644 --- a/__tests__/spdx.test.ts +++ b/__tests__/spdx.test.ts @@ -1,7 +1,7 @@ import {expect, jest, test} from '@jest/globals' import * as spdx from '../src/spdx' -test('hello', () => { +test('satisfies', () => { expect(spdx.satisfies('MIT', 'MIT')).toBe(true) }) diff --git a/src/config.ts b/src/config.ts index 2a14a48..b2198ff 100644 --- a/src/config.ts +++ b/src/config.ts @@ -5,7 +5,7 @@ import * as core from '@actions/core' import * as z from 'zod' import {ConfigurationOptions, ConfigurationOptionsSchema} from './schemas' import {octokitClient} from './utils' -import {isValidSPDX} from './spdx' +import {isValid} from './spdx' type ConfigurationOptionsPartial = Partial @@ -114,10 +114,12 @@ function validateLicenses( return } - const invalid_licenses = licenses.filter(license => !isValidSPDX(license)) + const invalid_licenses = licenses.filter(license => !isValid(license)) if (invalid_licenses.length > 0) { - throw new Error(`Invalid license(s) in ${key}: ${invalid_licenses}`) + throw new Error( + `Invalid license(s) in ${key}: ${invalid_licenses.join(', ')}` + ) } } diff --git a/src/licenses.ts b/src/licenses.ts index 76008bc..8c88899 100644 --- a/src/licenses.ts +++ b/src/licenses.ts @@ -2,7 +2,7 @@ import spdxSatisfies from 'spdx-satisfies' import {Change, Changes} from './schemas' import {octokitClient} from './utils' import {parsePURL} from './purl' -import {isValidSPDX} from './spdx' +import {isValid} from './spdx' /** * Loops through a list of changes, filtering and returning the @@ -166,7 +166,7 @@ const setGHLicenses = async (changes: Change[]): Promise => { // Currently Dependency Graph licenses are truncated to 255 characters // This possibly makes them invalid spdx ids const truncatedDGLicense = (license: string): boolean => - license.length === 255 && !isValidSPDX(license) + license.length === 255 && !isValid(license) async function groupChanges( changes: Changes diff --git a/src/spdx.ts b/src/spdx.ts index 85c1f89..0ba6d7f 100644 --- a/src/spdx.ts +++ b/src/spdx.ts @@ -9,7 +9,7 @@ export function satisfies( } // can be a single license or an SPDX expression -export function isValidSPDX(spdxExpr: string): boolean { +export function isValid(spdxExpr: string): boolean { try { parse(spdxExpr) return true