Skip to content

Commit a707089

Browse files
committed
tests
Signed-off-by: Jan Kowalleck <[email protected]>
1 parent 0fedd41 commit a707089

File tree

1 file changed

+170
-4
lines changed

1 file changed

+170
-4
lines changed

tests/integration/Utils.LicenseUtility.LicenseEvidenceGatherer.node.test.js

Lines changed: 170 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,21 @@ const { memfs } = require('memfs')
2323
const { suite, test } = require('mocha')
2424

2525
const {
26+
Models: { Attachment },
27+
Enums: { AttachmentEncoding },
2628
Utils: { LicenseUtility: { LicenseEvidenceGatherer } }
2729
} = require('../../')
2830

2931
suite('integration: Utils.LicenseUtility.LicenseEvidenceGatherer', () => {
32+
3033
test('no path -> throws', () => {
3134
const { fs } = memfs({ '/': {} })
3235
const leg = new LicenseEvidenceGatherer({ fs })
3336
assert.throws(
34-
() => {
35-
Array.from(leg.getFileAttachments('/foo'))
36-
},
37+
() => { Array.from(leg.getFileAttachments('/foo')) },
3738
{
3839
code: 'ENOENT',
39-
message: "ENOENT: no such file or directory, scandir '/foo'",
40+
message: 'ENOENT: no such file or directory, scandir \'/foo\'',
4041
path: '/foo',
4142
}
4243
)
@@ -54,4 +55,169 @@ suite('integration: Utils.LicenseUtility.LicenseEvidenceGatherer', () => {
5455
assert.deepEqual(found, [])
5556
assert.deepEqual(errors, [])
5657
})
58+
59+
test('ignore LICENSE folder', () => {
60+
// see https://reuse-standard.org/
61+
const { fs } = memfs({
62+
'/LICENSE/MIT.txt': 'MIT License text here...',
63+
'/LICENSE/GPL-3.0-or-later.txt': 'GPL-3.0-or-later License text here...'
64+
})
65+
const leg = new LicenseEvidenceGatherer({ fs })
66+
const errors = []
67+
const found = Array.from(
68+
leg.getFileAttachments(
69+
'/',
70+
(e) => { errors.push(e) }
71+
))
72+
assert.deepEqual(found, [])
73+
assert.deepEqual(errors, [])
74+
})
75+
76+
test('ignore LICENSES folder', () => {
77+
// see https://reuse-standard.org/
78+
const { fs } = memfs({
79+
'/LICENSES/MIT.txt': 'MIT License text here...',
80+
'/LICENSES/GPL-3.0-or-later.txt': 'GPL-3.0-or-later License text here...'
81+
})
82+
const leg = new LicenseEvidenceGatherer({ fs })
83+
const errors = []
84+
const found = Array.from(
85+
leg.getFileAttachments(
86+
'/',
87+
(e) => { errors.push(e) }
88+
))
89+
assert.deepEqual(found, [])
90+
assert.deepEqual(errors, [])
91+
})
92+
93+
test('reports unreadable files', () => {
94+
// see https://reuse-standard.org/
95+
const { fs } = memfs({
96+
'/LICENSE': 'license text here...',
97+
})
98+
const expectedError = new Error(
99+
'skipped license file /LICENSE',
100+
{ cause: new Error('Custom read error: Access denied!') })
101+
fs.readFileSync = function () { throw expectedError.cause }
102+
const leg = new LicenseEvidenceGatherer({ fs })
103+
const errors = []
104+
const found = Array.from(
105+
leg.getFileAttachments(
106+
'/',
107+
(e) => { errors.push(e) }
108+
))
109+
assert.deepEqual(found, [])
110+
assert.deepEqual(errors, [expectedError])
111+
})
112+
113+
test('finds licenses as expected', () => {
114+
// see https://reuse-standard.org/
115+
const { fs } = memfs({
116+
'/LICENSE': 'LICENSE file expected',
117+
'/LICENCE': 'LICENCE file expected',
118+
'/UNLICENSE': 'UNLICENSE file expected',
119+
'/UNLICENCE': 'UNLICENCE file expected',
120+
'/NOTICE': 'NOTICE file expected',
121+
'/MIT.license': 'MIT.license file expected',
122+
'/MIT.licence': 'MIT.licence file expected',
123+
'/license.mit': 'license.mit file expected',
124+
'/license.txt': 'license.txt file expected',
125+
'/license.js': 'license.js file unexpected',
126+
})
127+
const leg = new LicenseEvidenceGatherer({ fs })
128+
const errors = []
129+
const found = Array.from(
130+
leg.getFileAttachments(
131+
'/',
132+
(e) => { errors.push(e) }
133+
))
134+
function orderByFilePath (a, b) {
135+
return a.filePath.localeCompare(b.filePath)
136+
}
137+
assert.deepEqual(found.sort(orderByFilePath), [
138+
{
139+
'filePath': '/LICENSE',
140+
'file': 'LICENSE',
141+
'text': new Attachment(
142+
'TElDRU5TRSBmaWxlIGV4cGVjdGVk', {
143+
'contentType': 'text/plain',
144+
'encoding': 'base64'
145+
})
146+
},
147+
{
148+
'filePath': '/LICENCE',
149+
'file': 'LICENCE',
150+
'text': new Attachment(
151+
'TElDRU5DRSBmaWxlIGV4cGVjdGVk', {
152+
'contentType': 'text/plain',
153+
'encoding': 'base64'
154+
})
155+
},
156+
{
157+
'filePath': '/UNLICENCE',
158+
'file': 'UNLICENCE',
159+
'text': new Attachment(
160+
'VU5MSUNFTkNFIGZpbGUgZXhwZWN0ZWQ=', {
161+
'contentType': 'text/plain',
162+
'encoding': 'base64'
163+
})
164+
},
165+
{
166+
'filePath': '/UNLICENSE',
167+
'file': 'UNLICENSE',
168+
'text': new Attachment(
169+
'VU5MSUNFTlNFIGZpbGUgZXhwZWN0ZWQ=', {
170+
'contentType': 'text/plain',
171+
'encoding': 'base64'
172+
})
173+
},
174+
{
175+
'filePath': '/NOTICE',
176+
'file': 'NOTICE',
177+
'text': new Attachment(
178+
'Tk9USUNFIGZpbGUgZXhwZWN0ZWQ=', {
179+
'contentType': 'text/plain',
180+
'encoding': 'base64'
181+
})
182+
},
183+
{
184+
'filePath': '/MIT.license',
185+
'file': 'MIT.license',
186+
'text': new Attachment(
187+
'TUlULmxpY2Vuc2UgZmlsZSBleHBlY3RlZA==',{
188+
'contentType': 'text/plain',
189+
'encoding': 'base64'
190+
})
191+
},
192+
{
193+
'filePath': '/MIT.licence',
194+
'file': 'MIT.licence',
195+
'text': new Attachment(
196+
'TUlULmxpY2VuY2UgZmlsZSBleHBlY3RlZA==',{
197+
'contentType': 'text/plain',
198+
'encoding': 'base64'
199+
})
200+
},
201+
{
202+
'filePath': '/license.mit',
203+
'file': 'license.mit',
204+
'text': new Attachment(
205+
'bGljZW5zZS5taXQgZmlsZSBleHBlY3RlZA==', {
206+
'contentType': 'text/plain',
207+
'encoding': 'base64'
208+
})
209+
},
210+
{
211+
'filePath': '/license.txt',
212+
'file': 'license.txt',
213+
'text': new Attachment(
214+
'bGljZW5zZS50eHQgZmlsZSBleHBlY3RlZA==', {
215+
'contentType': 'text/plain',
216+
'encoding': 'base64'
217+
})
218+
}
219+
].sort(orderByFilePath))
220+
assert.deepEqual(errors, [])
221+
})
222+
57223
})

0 commit comments

Comments
 (0)