-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathscancode.js
244 lines (228 loc) · 11.1 KB
/
scancode.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// Copyright (c) Microsoft Corporation and others. Licensed under the MIT license.
// SPDX-License-Identifier: MIT
const assert = require('assert')
const summarizer = require('../../../providers/summary/scancode')()
summarizer.logger = { info: () => { } }
const fs = require('fs')
const path = require('path')
const { get, uniq, flatten } = require('lodash')
const { expect } = require('chai')
const scancodeVersions = ['2.2.1', '2.9.2', '2.9.8', '3.0.0', '3.0.2', '30.1.0']
describe('ScancodeSummarizer basic compatability', () => {
it('summarizes basic npm', () => {
const coordinates = { type: 'npm', provider: 'npmjs' }
for (let version of scancodeVersions) {
const harvestData = getHarvestData(version, 'npm-basic')
if (!harvestData) continue
const result = summarizer.summarize(coordinates, harvestData)
assert.equal(result.licensed.declared, 'ISC', `Declared license mismatch for version ${version}`)
assert.equal(result.described.releaseDate, '2017-05-19', `releaseDate mismatch for version ${version}`)
assert.deepEqual(uniq(flatten(result.files.map(x => x.attributions))).filter(x => x).length, 1)
assert.deepEqual(result.files.find(x => x.path === 'package/LICENSE').natures, ['license'])
assert.equal(flatten(result.files.map(x => x.natures)).filter(x => x).length, 1)
}
})
it('summarizes large npm', () => {
// let's drop in a 2.9.2 and a 3.0.0 version of https://github.com/RedisLabsModules/RediSearch/tree/v1.4.3
// this won't have a declared license, since it is common clause, but it will have natures and attributions, etc
//3.00 is the one that is pending, need to run it locally
// curl -d '{"type":"scancode", "url":"cd:/git/github/RedisLabsModules/RediSearch/v1.4.3"}' -H "Content-Type: application/json" -H "X-token: secret" -X POST http://localhost:5000/requests
const coordinates = { type: 'npm', provider: 'npmjs' }
const overrides = {
'2.9.2': 'BSD-3-Clause AND GPL-2.0-only'
}
for (let version of scancodeVersions) {
const harvestData = getHarvestData(version, 'npm-large')
if (!harvestData) continue
const result = summarizer.summarize(coordinates, harvestData)
assert.equal(
result.licensed.declared,
overrides[version] || 'BSD-3-Clause OR GPL-2.0',
`Declared license mismatch for version ${version}`
)
assert.equal(result.described.releaseDate, '2018-03-31', `releaseDate mismatch for version ${version}`)
assert.deepEqual(uniq(flatten(result.files.map(x => x.attributions))).filter(x => x).length, 33)
assert.deepEqual(result.files.find(x => x.path === 'package/LICENSE').natures, ['license'])
assert.equal(flatten(result.files.map(x => x.natures)).filter(x => x).length, 1)
}
})
it('summarizes maven with a complex declared license in later versions of ScanCode', () => {
const coordinates = { type: 'maven', provider: 'mavencentral' }
const harvestData = getHarvestData('30.1.0', 'maven-complex-declared-license')
const result = summarizer.summarize(coordinates, harvestData)
assert.equal(
result.licensed.declared,
'MIT'
)
})
it('throws an error on an invalid scancode version', () => {
const version = '0.0.0'
const coordinates = { type: 'npm', provider: 'npmjs' }
const harvestData = getHarvestData(version, 'npm-basic')
try {
summarizer.summarize(coordinates, harvestData)
throw new Error('Invalid version of ScanCode')
} catch (error) {
expect(error.message).to.eq('Invalid version of ScanCode')
}
})
it('summarizes ruby gems', () => {
const coordinates = { type: 'gem', provider: 'rubygems' }
const undefinedOverrides = ['2.9.2', '2.9.8']
for (let version of scancodeVersions) {
const harvestData = getHarvestData(version, 'gem')
if (!harvestData) continue
const result = summarizer.summarize(coordinates, harvestData)
assert.equal(
get(result.licensed, 'declared'),
undefinedOverrides.indexOf(version) > -1 ? undefined : 'MIT',
`Declared license mismatch for version ${version}`
)
assert.equal(result.files.find(x => x.path === 'MIT-LICENSE.md').license, 'MIT')
assert.equal(result.described.releaseDate, '2018-08-09', `releaseDate mismatch for version ${version}`)
assert.deepEqual(uniq(flatten(result.files.map(x => x.attributions))).filter(x => x).length, 3)
}
})
it('summarizes git repos', () => {
const coordinates = { type: 'git', provider: 'github' }
for (let version of scancodeVersions) {
const harvestData = getHarvestData(version, 'git')
if (!harvestData) continue
const result = summarizer.summarize(coordinates, harvestData)
assert.equal(result.licensed.declared, 'ISC', `Declared license mismatch for version ${version}`)
assert.equal(result.described.releaseDate, '2017-02-24', `releaseDate mismatch for version ${version}`)
assert.deepEqual(uniq(flatten(result.files.map(x => x.attributions))).filter(x => x).length, 1)
assert.deepEqual(result.files.find(x => x.path === 'LICENSE').natures, ['license'])
assert.equal(flatten(result.files.map(x => x.natures)).filter(x => x).length, 1)
}
})
it('summarizes pythons', () => {
const coordinates = { type: 'pypi', provider: 'pypi', name: 'redis', revision: '3.0.1' }
for (let version of scancodeVersions) {
const harvestData = getHarvestData(version, 'python')
if (!harvestData) continue
const result = summarizer.summarize(coordinates, harvestData)
assert.equal(result.licensed.declared, 'MIT', `Declared license mismatch for version ${version}`)
assert.equal(result.described.releaseDate, '2018-11-15', `releaseDate mismatch for version ${version}`)
assert.deepEqual(uniq(flatten(result.files.map(x => x.attributions))).filter(x => x).length, 1)
assert.deepEqual(result.files.find(x => x.path === 'redis-3.0.1/LICENSE').natures, ['license'])
assert.equal(flatten(result.files.map(x => x.natures)).filter(x => x).length, 1)
}
})
})
describe('ScancodeSummarizer fixtures', () => {
it('summarizes basic npm 3.0.2', () => {
const coordinates = { type: 'npm', provider: 'npmjs', name: 'glob', revision: '7.1.2' }
const harvestData = getHarvestData('3.0.2', 'npm-basic')
const result = summarizer.summarize(coordinates, harvestData)
assert.deepEqual(result, {
described: { releaseDate: '2017-05-19' },
licensed: { declared: 'ISC' },
files: [
{ path: 'package/changelog.md', hashes: { sha1: '97bfa68176e50777c07a7ba58f98ff7a1730ac00' } },
{ path: 'package/common.js', hashes: { sha1: '2f948b495467f2a7ac0afbb1008af557ab040143' } },
{ path: 'package/glob.js', hashes: { sha1: 'c2e95cdccba36eaca7b12e2bcf9b383438cee52d' } },
{
path: 'package/LICENSE',
license: 'ISC',
natures: ['license'],
attributions: ['Copyright (c) Isaac Z. Schlueter and Contributors'],
hashes: { sha1: 'bb408e929caeb1731945b2ba54bc337edb87cc66' }
},
{ path: 'package/package.json', hashes: { sha1: '844f90fa8a6fbf45d581593a333f69c5cb1f2d58' } },
{ path: 'package/README.md', hashes: { sha1: '449f1592c9cf2d32a0d74bead66d7267218f2c4f' } },
{ path: 'package/sync.js', hashes: { sha1: '7482bc56682b97175655976b07044afcb65b0cc9' } }
]
})
})
it('summarizes basic npm 30.1.0', () => {
const coordinates = { type: 'npm', provider: 'npmjs', name: 'glob', revision: '7.1.2' }
const harvestData = getHarvestData('30.1.0', 'npm-basic')
const result = summarizer.summarize(coordinates, harvestData)
//console.log(result)
assert.deepEqual(result, {
described: { releaseDate: '2017-05-19' },
licensed: { declared: 'ISC' },
files: [
{ path: 'package/changelog.md', license: 'ISC', hashes: { sha1: '97bfa68176e50777c07a7ba58f98ff7a1730ac00' } },
{ path: 'package/common.js', hashes: { sha1: '2f948b495467f2a7ac0afbb1008af557ab040143' } },
{ path: 'package/glob.js', hashes: { sha1: 'c2e95cdccba36eaca7b12e2bcf9b383438cee52d' } },
{
path: 'package/LICENSE',
license: 'ISC',
natures: ['license'],
attributions: ['Copyright (c) Isaac Z. Schlueter and Contributors'],
hashes: { sha1: 'bb408e929caeb1731945b2ba54bc337edb87cc66' }
},
{ path: 'package/package.json', license: 'ISC', hashes: { sha1: '844f90fa8a6fbf45d581593a333f69c5cb1f2d58' } },
{ path: 'package/README.md', hashes: { sha1: '449f1592c9cf2d32a0d74bead66d7267218f2c4f' } },
{ path: 'package/sync.js', hashes: { sha1: '7482bc56682b97175655976b07044afcb65b0cc9' } }
]
})
})
it('summarizes commons-clause as NOASSERTION in 3.0.2', () => {
const coordinates = {
type: 'git',
provider: 'github',
namespace: 'RedisLabsModules',
name: 'RediSearch',
revision: '7f1082687d4779918be0d8109a134d79e6fbcb41'
}
const harvestData = getHarvestData('3.0.2', 'commons-clause')
const result = summarizer.summarize(coordinates, harvestData)
assert.deepEqual(result.described, { releaseDate: '2019-01-31' })
assert.deepEqual(result.licensed, { declared: 'NOASSERTION' })
assert.deepEqual(result.files.length, 576)
assert.deepEqual(uniq(flatten(result.files.map(x => x.attributions))).filter(x => x).length, 21)
assert.deepEqual(result.files.filter(x => x.natures), [
{
path: 'LICENSE',
license: 'NOASSERTION',
natures: ['license'],
attributions: ['Copyright 2018-2019 Redis Labs Ltd. and Contributors.'],
hashes: { sha1: '6c9da49858267f91fa10f08ad556f02fdc689e63' }
},
{
path: 'src/dep/friso/LICENSE.md',
license: 'Apache-2.0 AND MIT',
natures: ['license'],
attributions: ['Copyright (c) 2010'],
hashes: { sha1: 'aeb9db6237c570c389886e0540e01a0ec78134bb' }
},
{
path: 'src/dep/hll/LICENSE',
license: 'MIT',
natures: ['license'],
attributions: ['Copyright (c) 2015 Artem Zaytsev <[email protected]>'],
hashes: { sha1: 'c0e8a4bbdcbc9c81f7ea72b0631f67973aa0f244' }
},
{
path: 'src/dep/libnu/LICENSE',
license: 'MIT',
natures: ['license'],
attributions: ['Copyright (c) 2013 Aleksey Tulinov <[email protected]>'],
hashes: { sha1: 'ea1ed91b37e5c99835b9ebf0861f96dfda2524cd' }
},
{
path: 'src/dep/snowball/COPYING',
license: 'BSD-3-Clause',
natures: ['license'],
attributions: ['Copyright (c) 2001, Dr Martin Porter', 'Copyright (c) 2004,2005, Richard Boulton'],
hashes: { sha1: '3938505906e841002141cb01bbda1e971614e34a' }
},
{
path: 'src/dep/triemap/LICENSE',
license: 'BSD-2-Clause',
natures: ['license'],
attributions: ['Copyright (c) 2017, Redis Labs'],
hashes: { sha1: 'f35ea366f34f63097146d8b77417d393aab877d3' }
}
])
})
})
function getHarvestData(version, test) {
const fileName = path.join(__dirname, `../../fixtures/scancode/${version}/${test}.json`)
if (fs.existsSync(fileName)) {
return JSON.parse(fs.readFileSync(fileName))
}
}