Skip to content

Commit 28743f8

Browse files
authored
Merge pull request #719 from actions/change-spdx-parser
Update SPDX Expression Parsing
2 parents d6f34c3 + b4ae47c commit 28743f8

17 files changed

+871
-298
lines changed

__tests__/config.test.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
import {expect, test, beforeEach} from '@jest/globals'
22
import {readConfig} from '../src/config'
33
import {getRefs} from '../src/git-refs'
4-
import * as Utils from '../src/utils'
4+
import * as spdx from '../src/spdx'
55
import {setInput, clearInputs} from './test-helpers'
66

7-
beforeAll(() => {
8-
jest.spyOn(Utils, 'isSPDXValid').mockReturnValue(true)
9-
})
10-
117
beforeEach(() => {
128
clearInputs()
139
})
@@ -19,11 +15,11 @@ test('it defaults to low severity', async () => {
1915

2016
test('it reads custom configs', async () => {
2117
setInput('fail-on-severity', 'critical')
22-
setInput('allow-licenses', ' BSD, GPL 2')
18+
setInput('allow-licenses', 'ISC, GPL-2.0')
2319

2420
const config = await readConfig()
2521
expect(config.fail_on_severity).toEqual('critical')
26-
expect(config.allow_licenses).toEqual(['BSD', 'GPL 2'])
22+
expect(config.allow_licenses).toEqual(['ISC', 'GPL-2.0'])
2723
})
2824

2925
test('it defaults to false for warn-only', async () => {
@@ -40,7 +36,7 @@ test('it defaults to empty allow/deny lists ', async () => {
4036

4137
test('it raises an error if both an allow and denylist are specified', async () => {
4238
setInput('allow-licenses', 'MIT')
43-
setInput('deny-licenses', 'BSD')
39+
setInput('deny-licenses', 'BSD-3-Clause')
4440

4541
await expect(readConfig()).rejects.toThrow(
4642
'You cannot specify both allow-licenses and deny-licenses'
@@ -204,21 +200,17 @@ test('it is not possible to disable both checks', async () => {
204200
})
205201

206202
describe('licenses that are not valid SPDX licenses', () => {
207-
beforeAll(() => {
208-
jest.spyOn(Utils, 'isSPDXValid').mockReturnValue(false)
209-
})
210-
211203
test('it raises an error for invalid licenses in allow-licenses', async () => {
212-
setInput('allow-licenses', ' BSD, GPL 2')
204+
setInput('allow-licenses', ' BSD-YOLO, GPL-2.0')
213205
await expect(readConfig()).rejects.toThrow(
214-
'Invalid license(s) in allow-licenses: BSD,GPL 2'
206+
'Invalid license(s) in allow-licenses: BSD-YOLO'
215207
)
216208
})
217209

218210
test('it raises an error for invalid licenses in deny-licenses', async () => {
219-
setInput('deny-licenses', ' BSD, GPL 2')
211+
setInput('deny-licenses', ' GPL-2.0, BSD-YOLO, Apache-2.0, ToIll')
220212
await expect(readConfig()).rejects.toThrow(
221-
'Invalid license(s) in deny-licenses: BSD,GPL 2'
213+
'Invalid license(s) in deny-licenses: BSD-YOLO, ToIll'
222214
)
223215
})
224216
})

__tests__/deny.test.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@ jest.mock('octokit', () => {
3333

3434
beforeEach(async () => {
3535
jest.resetModules()
36-
jest.doMock('spdx-satisfies', () => {
37-
// mock spdx-satisfies return value
38-
// true for BSD, false for all others
39-
return jest.fn((license: string, _: string): boolean => license === 'BSD')
40-
})
4136

4237
npmChange = createTestChange({ecosystem: 'npm'})
4338
rubyChange = createTestChange({ecosystem: 'rubygems'})

__tests__/external-config.test.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {expect, test, beforeEach} from '@jest/globals'
22
import {readConfig} from '../src/config'
3-
import * as Utils from '../src/utils'
3+
import * as spdx from '../src/spdx'
44
import {setInput, clearInputs} from './test-helpers'
55

66
const externalConfig = `fail_on_severity: 'high'
@@ -25,10 +25,6 @@ jest.mock('octokit', () => {
2525
}
2626
})
2727

28-
beforeAll(() => {
29-
jest.spyOn(Utils, 'isSPDXValid').mockReturnValue(true)
30-
})
31-
3228
beforeEach(() => {
3329
clearInputs()
3430
})
@@ -38,7 +34,7 @@ test('it reads an external config file', async () => {
3834

3935
const config = await readConfig()
4036
expect(config.fail_on_severity).toEqual('critical')
41-
expect(config.allow_licenses).toEqual(['BSD', 'GPL 2'])
37+
expect(config.allow_licenses).toEqual(['BSD-3-Clause', 'GPL-2.0'])
4238
})
4339

4440
test('raises an error when the config file was not found', async () => {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
fail_on_severity: critical
22
allow_licenses:
3-
- 'BSD'
4-
- 'GPL 2'
3+
- 'BSD-3-Clause'
4+
- 'GPL-2.0'

__tests__/licenses.test.ts

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import {expect, jest, test} from '@jest/globals'
22
import {Change, Changes} from '../src/schemas'
3-
4-
let getInvalidLicenseChanges: Function
3+
import {getInvalidLicenseChanges} from '../src/licenses'
54

65
const npmChange: Change = {
76
manifest: 'package.json',
@@ -30,7 +29,7 @@ const rubyChange: Change = {
3029
name: 'actionsomething',
3130
version: '3.2.0',
3231
package_url: 'pkg:gem/[email protected]',
33-
license: 'BSD',
32+
license: 'BSD-3-Clause',
3433
source_repository_url: 'github.com/some-repo',
3534
scope: 'runtime',
3635
vulnerabilities: [
@@ -100,29 +99,32 @@ jest.mock('octokit', () => {
10099

101100
beforeEach(async () => {
102101
jest.resetModules()
103-
jest.doMock('spdx-satisfies', () => {
104-
// mock spdx-satisfies return value
105-
// true for BSD, false for all others
106-
return jest.fn((license: string, _: string): boolean => license === 'BSD')
107-
})
108-
// eslint-disable-next-line @typescript-eslint/no-require-imports
109-
;({getInvalidLicenseChanges} = require('../src/licenses'))
110102
})
111103

112104
test('it adds license outside the allow list to forbidden changes', async () => {
113-
const changes: Changes = [npmChange, rubyChange]
105+
const changes: Changes = [
106+
npmChange, // MIT license
107+
rubyChange // BSD license
108+
]
109+
114110
const {forbidden} = await getInvalidLicenseChanges(changes, {
115-
allow: ['BSD']
111+
allow: ['BSD-3-Clause']
116112
})
113+
117114
expect(forbidden[0]).toBe(npmChange)
118115
expect(forbidden.length).toEqual(1)
119116
})
120117

121118
test('it adds license inside the deny list to forbidden changes', async () => {
122-
const changes: Changes = [npmChange, rubyChange]
119+
const changes: Changes = [
120+
npmChange, // MIT license
121+
rubyChange // BSD license
122+
]
123+
123124
const {forbidden} = await getInvalidLicenseChanges(changes, {
124-
deny: ['BSD']
125+
deny: ['BSD-3-Clause']
125126
})
127+
126128
expect(forbidden[0]).toBe(rubyChange)
127129
expect(forbidden.length).toEqual(1)
128130
})
@@ -133,7 +135,7 @@ test('it does not add license outside the allow list to forbidden changes if it
133135
{...rubyChange, change_type: 'removed'}
134136
]
135137
const {forbidden} = await getInvalidLicenseChanges(changes, {
136-
allow: ['BSD']
138+
allow: ['BSD-3-Clause']
137139
})
138140
expect(forbidden).toStrictEqual([])
139141
})
@@ -144,7 +146,7 @@ test('it does not add license inside the deny list to forbidden changes if it is
144146
{...rubyChange, change_type: 'removed'}
145147
]
146148
const {forbidden} = await getInvalidLicenseChanges(changes, {
147-
deny: ['BSD']
149+
deny: ['BSD-3-Clause']
148150
})
149151
expect(forbidden).toStrictEqual([])
150152
})
@@ -156,23 +158,18 @@ test('it adds license outside the allow list to forbidden changes if it is in bo
156158
{...rubyChange, change_type: 'removed'}
157159
]
158160
const {forbidden} = await getInvalidLicenseChanges(changes, {
159-
allow: ['BSD']
161+
allow: ['BSD-3-Clause']
160162
})
161163
expect(forbidden).toStrictEqual([npmChange])
162164
})
163165

164166
test('it adds all licenses to unresolved if it is unable to determine the validity', async () => {
165-
jest.resetModules() // reset module set in before
166-
jest.doMock('spdx-satisfies', () => {
167-
return jest.fn((_first: string, _second: string) => {
168-
throw new Error('Some Error')
169-
})
170-
})
171-
// eslint-disable-next-line @typescript-eslint/no-require-imports
172-
;({getInvalidLicenseChanges} = require('../src/licenses'))
173-
const changes: Changes = [npmChange, rubyChange]
167+
const changes: Changes = [
168+
{...npmChange, license: 'Foo'},
169+
{...rubyChange, license: 'Bar'}
170+
]
174171
const invalidLicenses = await getInvalidLicenseChanges(changes, {
175-
allow: ['BSD']
172+
allow: ['Apache-2.0']
176173
})
177174
expect(invalidLicenses.forbidden.length).toEqual(0)
178175
expect(invalidLicenses.unlicensed.length).toEqual(0)
@@ -182,7 +179,7 @@ test('it adds all licenses to unresolved if it is unable to determine the validi
182179
test('it does not filter out changes that are on the exclusions list', async () => {
183180
const changes: Changes = [pipChange, npmChange, rubyChange]
184181
const licensesConfig = {
185-
allow: ['BSD'],
182+
allow: ['BSD-3-Clause'],
186183
licenseExclusions: ['pkg:pypi/[email protected]', 'pkg:npm/[email protected]']
187184
}
188185
const invalidLicenses = await getInvalidLicenseChanges(
@@ -198,7 +195,7 @@ test('it does not fail when the packages dont have a valid PURL', async () => {
198195

199196
const changes: Changes = [emptyPurlChange, npmChange, rubyChange]
200197
const licensesConfig = {
201-
allow: ['BSD'],
198+
allow: ['BSD-3-Clause'],
202199
licenseExclusions: ['pkg:pypi/[email protected]', 'pkg:npm/[email protected]']
203200
}
204201

@@ -212,16 +209,18 @@ test('it does not fail when the packages dont have a valid PURL', async () => {
212209
test('it does filters out changes if they are not on the exclusions list', async () => {
213210
const changes: Changes = [pipChange, npmChange, rubyChange]
214211
const licensesConfig = {
215-
allow: ['BSD'],
212+
allow: ['BSD-3-Clause'],
216213
licenseExclusions: [
217214
'pkg:pypi/[email protected]',
218215
219216
]
220217
}
218+
221219
const invalidLicenses = await getInvalidLicenseChanges(
222220
changes,
223221
licensesConfig
224222
)
223+
225224
expect(invalidLicenses.forbidden.length).toEqual(2)
226225
expect(invalidLicenses.forbidden[0]).toBe(pipChange)
227226
expect(invalidLicenses.forbidden[1]).toBe(npmChange)

0 commit comments

Comments
 (0)