11import { expect , jest , test } from '@jest/globals'
22import { Change , Changes } from '../src/schemas'
3-
4- let getInvalidLicenseChanges : Function
3+ import { getInvalidLicenseChanges } from '../src/licenses'
54
65const 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
101100beforeEach ( 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
112104test ( '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
121118test ( '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
164166test ( '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
182179test ( '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 () => {
212209test ( '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 : [
217214218215219216 ]
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