Skip to content

Commit deb8924

Browse files
committed
fix tests
1 parent d38dff2 commit deb8924

File tree

3 files changed

+70
-74
lines changed

3 files changed

+70
-74
lines changed

lib/utils/api-helpers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { AuthError } from './errors.js'
88
* @param {T} _name
99
* @param {import('@socketsecurity/sdk').SocketSdkErrorType<T>} result
1010
* @param {import('ora').Ora} spinner
11-
* @returns {void}
11+
* @returns {never}
1212
*/
1313
export function handleUnsuccessfulApiResponse (_name, result, spinner) {
1414
const resultError = 'error' in result && result.error && typeof result.error === 'object' ? result.error : {}

lib/utils/path-resolve.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import micromatch from 'micromatch'
99
import { ErrorWithCause } from 'pony-cause'
1010

1111
import { InputError } from './errors.js'
12-
import { setupSdk } from './sdk.js'
1312
import { isErrnoException } from './type-helpers.js'
1413

1514
/**
@@ -37,11 +36,12 @@ const BASE_GLOBBY_OPTS = {
3736
* @param {string} cwd The working directory to use when resolving paths
3837
* @param {string[]} inputPaths A list of paths to folders, package.json files and/or recognized lockfiles. Supports globs.
3938
* @param {import('@socketsecurity/config').SocketYml|undefined} config
39+
* @param {import('@socketsecurity/sdk').SocketSdkReturnType<"getReportSupportedFiles">['data']} supportedFiles
4040
* @param {typeof console.error} debugLog
4141
* @returns {Promise<string[]>}
4242
* @throws {InputError}
4343
*/
44-
export async function getPackageFiles (cwd, inputPaths, config, debugLog) {
44+
export async function getPackageFiles (cwd, inputPaths, config, supportedFiles, debugLog) {
4545
debugLog(`Globbed resolving ${inputPaths.length} paths:`, inputPaths)
4646

4747
// TODO: Does not support `~/` paths
@@ -53,7 +53,7 @@ export async function getPackageFiles (cwd, inputPaths, config, debugLog) {
5353

5454
debugLog(`Globbed resolved ${inputPaths.length} paths to ${entries.length} paths:`, entries)
5555

56-
const packageFiles = await mapGlobResultToFiles(entries)
56+
const packageFiles = await mapGlobResultToFiles(entries, supportedFiles)
5757

5858
debugLog(`Mapped ${entries.length} entries to ${packageFiles.length} files:`, packageFiles)
5959

@@ -71,18 +71,13 @@ export async function getPackageFiles (cwd, inputPaths, config, debugLog) {
7171
* Takes paths to folders, package.json and/or recognized lock files and resolves them to package.json + lockfile pairs (where possible)
7272
*
7373
* @param {string[]} entries
74+
* @param {import('@socketsecurity/sdk').SocketSdkReturnType<"getReportSupportedFiles">['data']} supportedFiles
7475
* @returns {Promise<string[]>}
7576
* @throws {InputError}
7677
*/
77-
export async function mapGlobResultToFiles (entries) {
78-
// TODO: setupSdk(getDefaultKey() || FREE_API_KEY) after #46 merged
79-
const sdk = await setupSdk()
80-
const supportedFiles = await sdk.getReportSupportedFiles()
81-
if (!supportedFiles.success) {
82-
throw new TypeError('failed to find supported files')
83-
}
78+
export async function mapGlobResultToFiles (entries, supportedFiles) {
8479
const packageFiles = await Promise.all(
85-
entries.map(entry => mapGlobEntryToFiles(entry, supportedFiles.data))
80+
entries.map(entry => mapGlobEntryToFiles(entry, supportedFiles))
8681
)
8782

8883
const uniquePackageFiles = [...new Set(packageFiles.flat())]

test/path-resolve.spec.js

Lines changed: 63 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,7 @@ import {
1515
chai.use(chaiAsPromised)
1616
chai.should()
1717

18-
// TODO: after #46 merged, use:
19-
// const globPatternsPromise = setupSdk(FREE_API_KEY)
20-
// .then(sdk => sdk.getReportSupportedFiles())
21-
// .then(res => {
22-
// if (!res.success) throw new Error('failed to get API supported files')
23-
// return res.data
24-
// })
25-
26-
const globPatternsPromise = Promise.resolve({
18+
const globPatterns = {
2719
general: {
2820
readme: {
2921
pattern: '*readme*'
@@ -70,14 +62,25 @@ const globPatternsPromise = Promise.resolve({
7062
pattern: 'setup.py'
7163
}
7264
}
73-
})
65+
}
7466

7567
/**
76-
* @param {string} file
77-
* @returns {Promise<string[]>}
68+
* @template {any[]} A
69+
* @template R
70+
* @template {(...args: A) => Promise<R[]>} Fn
71+
* @param {Fn} fn
72+
* @returns {Fn}
7873
*/
79-
const mapGlobEntry = async (file) =>
80-
mapGlobEntryToFiles(file, await globPatternsPromise)
74+
const sortedPromise = (fn) => /** @type {Fn} */ (async (...args) => {
75+
const result = await fn(...args)
76+
return result.sort()
77+
})
78+
79+
const sortedMapGlobEntry = sortedPromise(mapGlobEntryToFiles)
80+
81+
const sortedMapGlobResult = sortedPromise(mapGlobResultToFiles)
82+
83+
const sortedGetPackageFiles = sortedPromise(getPackageFiles)
8184

8285
describe('Path Resolve', () => {
8386
beforeEach(() => {
@@ -120,14 +123,14 @@ describe('Path Resolve', () => {
120123
mockFs({
121124
'/foo.txt': 'some content',
122125
})
123-
await mapGlobEntry('/foo.txt').should.eventually.become([])
126+
await sortedMapGlobEntry('/foo.txt', globPatterns).should.eventually.become([])
124127
})
125128

126129
it('should throw on errors', async () => {
127130
mockFs({
128131
'/package.json': { /* Empty directory */ },
129132
})
130-
await mapGlobEntry('/')
133+
await sortedMapGlobEntry('/', globPatterns)
131134
.should.eventually.be.rejectedWith(InputError, 'Expected \'/package.json\' to be a file')
132135
})
133136
})
@@ -138,32 +141,32 @@ describe('Path Resolve', () => {
138141
'/package-lock.json': '{}',
139142
'/package.json': '{}',
140143
})
141-
await mapGlobEntry('/').should.eventually.become([
142-
'/package.json',
143-
'/package-lock.json'
144+
await sortedMapGlobEntry('/', globPatterns).should.eventually.become([
145+
'/package-lock.json',
146+
'/package.json'
144147
])
145148
})
146149

147150
it('should resolve package without lock file', async () => {
148151
mockFs({
149152
'/package.json': '{}',
150153
})
151-
await mapGlobEntry('/').should.eventually.become(['/package.json'])
154+
await sortedMapGlobEntry('/', globPatterns).should.eventually.become(['/package.json'])
152155
})
153156

154157
it('should not resolve lock file without package', async () => {
155158
mockFs({
156159
'/package-lock.json': '{}',
157160
})
158-
await mapGlobEntry('/').should.eventually.become([])
161+
await sortedMapGlobEntry('/', globPatterns).should.eventually.become([])
159162
})
160163

161164
it('should support alternative lock files', async () => {
162165
mockFs({
163166
'/yarn.lock': '{}',
164167
'/package.json': '{}',
165168
})
166-
await mapGlobEntry('/').should.eventually.become([
169+
await sortedMapGlobEntry('/', globPatterns).should.eventually.become([
167170
'/package.json',
168171
'/yarn.lock'
169172
])
@@ -176,31 +179,31 @@ describe('Path Resolve', () => {
176179
'/package-lock.json': '{}',
177180
'/package.json': '{}',
178181
})
179-
await mapGlobEntry('/package.json').should.eventually.become([
180-
'/package.json',
181-
'/package-lock.json'
182+
await sortedMapGlobEntry('/package.json', globPatterns).should.eventually.become([
183+
'/package-lock.json',
184+
'/package.json'
182185
])
183186
})
184187

185188
it('should resolve package without lock file', async () => {
186189
mockFs({
187190
'/package.json': '{}',
188191
})
189-
await mapGlobEntry('/package.json').should.eventually.become(['/package.json'])
192+
await sortedMapGlobEntry('/package.json', globPatterns).should.eventually.become(['/package.json'])
190193
})
191194

192195
it('should not validate the input file', async () => {
193196
mockFs({})
194-
await mapGlobEntry('/package.json').should.eventually.become(['/package.json'])
197+
await sortedMapGlobEntry('/package.json', globPatterns).should.eventually.become(['/package.json'])
195198
})
196199

197200
it('should not validate the input file, but still add a complementary lock file', async () => {
198201
mockFs({
199202
'/package-lock.json': '{}',
200203
})
201-
await mapGlobEntry('/package.json').should.eventually.become([
202-
'/package.json',
203-
'/package-lock.json'
204+
await sortedMapGlobEntry('/package.json', globPatterns).should.eventually.become([
205+
'/package-lock.json',
206+
'/package.json'
204207
])
205208
})
206209

@@ -209,7 +212,7 @@ describe('Path Resolve', () => {
209212
'/yarn.lock': '{}',
210213
'/package.json': '{}',
211214
})
212-
await mapGlobEntry('/package.json').should.eventually.become([
215+
await sortedMapGlobEntry('/package.json', globPatterns).should.eventually.become([
213216
'/package.json',
214217
'/yarn.lock'
215218
])
@@ -222,17 +225,9 @@ describe('Path Resolve', () => {
222225
'/package-lock.json': '{}',
223226
'/package.json': '{}',
224227
})
225-
await mapGlobEntry('/package-lock.json').should.eventually.become([
226-
'/package.json',
227-
'/package-lock.json'
228-
])
229-
})
230-
231-
it('should assume input is correct and paired with package file', async () => {
232-
mockFs({})
233-
await mapGlobEntry('/package-lock.json').should.eventually.become([
234-
'/package.json',
235-
'/package-lock.json'
228+
await sortedMapGlobEntry('/package-lock.json', globPatterns).should.eventually.become([
229+
'/package-lock.json',
230+
'/package.json'
236231
])
237232
})
238233

@@ -241,7 +236,7 @@ describe('Path Resolve', () => {
241236
'/yarn.lock': '{}',
242237
'/package.json': '{}',
243238
})
244-
await mapGlobEntry('/yarn.lock').should.eventually.become([
239+
await sortedMapGlobEntry('/yarn.lock', globPatterns).should.eventually.become([
245240
'/package.json',
246241
'/yarn.lock'
247242
])
@@ -261,20 +256,20 @@ describe('Path Resolve', () => {
261256
'/abc/package.json': '{}',
262257
})
263258

264-
await mapGlobResultToFiles([
259+
await sortedMapGlobResult([
265260
'/',
266261
'/foo/package-lock.json',
267262
'/bar/package.json',
268263
'/abc/',
269264
'/abc/package.json'
270-
]).should.eventually.become([
271-
'/package.json',
272-
'/package-lock.json',
273-
'/foo/package.json',
274-
'/foo/package-lock.json',
265+
], globPatterns).should.eventually.become([
266+
'/abc/package.json',
275267
'/bar/package.json',
276268
'/bar/yarn.lock',
277-
'/abc/package.json',
269+
'/foo/package-lock.json',
270+
'/foo/package.json',
271+
'/package-lock.json',
272+
'/package.json'
278273
])
279274
})
280275
})
@@ -291,19 +286,20 @@ describe('Path Resolve', () => {
291286
'/abc/package.json': '{}',
292287
})
293288

294-
await getPackageFiles(
289+
await sortedGetPackageFiles(
295290
'/',
296291
['**/*'],
297292
undefined,
293+
globPatterns,
298294
() => {}
299295
).should.eventually.become([
300296
'/abc/package.json',
301297
'/bar/package.json',
302298
'/bar/yarn.lock',
303-
'/foo/package.json',
304299
'/foo/package-lock.json',
305-
'/package.json',
300+
'/foo/package.json',
306301
'/package-lock.json',
302+
'/package.json',
307303
])
308304
})
309305

@@ -312,10 +308,11 @@ describe('Path Resolve', () => {
312308
'/package.json': '{}',
313309
})
314310

315-
await getPackageFiles(
311+
await sortedGetPackageFiles(
316312
'/',
317313
['.'],
318314
undefined,
315+
globPatterns,
319316
() => {}
320317
).should.eventually.become([
321318
'/package.json',
@@ -330,7 +327,7 @@ describe('Path Resolve', () => {
330327
'/foo/package.json': '{}',
331328
})
332329

333-
await getPackageFiles(
330+
await sortedGetPackageFiles(
334331
'/',
335332
['**/*'],
336333
{
@@ -342,11 +339,12 @@ describe('Path Resolve', () => {
342339
issueRules: {},
343340
githubApp: {}
344341
},
342+
globPatterns,
345343
() => {}
346344
).should.eventually.become([
347345
'/bar/package.json',
348-
'/foo/package.json',
349346
'/foo/package-lock.json',
347+
'/foo/package.json'
350348
])
351349
})
352350

@@ -359,14 +357,15 @@ describe('Path Resolve', () => {
359357
'/foo/package.json': '{}',
360358
})
361359

362-
await getPackageFiles(
360+
await sortedGetPackageFiles(
363361
'/',
364362
['**/*'],
365363
undefined,
364+
globPatterns,
366365
() => {}
367366
).should.eventually.become([
368-
'/foo/package.json',
369367
'/foo/package-lock.json',
368+
'/foo/package.json'
370369
])
371370
})
372371

@@ -385,14 +384,15 @@ describe('Path Resolve', () => {
385384
'/foo/package.json': '{}',
386385
})
387386

388-
await getPackageFiles(
387+
await sortedGetPackageFiles(
389388
'/',
390389
['**/*'],
391390
undefined,
391+
globPatterns,
392392
() => {}
393393
).should.eventually.become([
394-
'/foo/package.json',
395394
'/foo/package-lock.json',
395+
'/foo/package.json'
396396
])
397397
})
398398

@@ -404,14 +404,15 @@ describe('Path Resolve', () => {
404404
'/foo/random.json': '{}',
405405
})
406406

407-
await getPackageFiles(
407+
await sortedGetPackageFiles(
408408
'/',
409409
['**/*'],
410410
undefined,
411+
globPatterns,
411412
() => {}
412413
).should.eventually.become([
413-
'/foo/package.json',
414414
'/foo/package-lock.json',
415+
'/foo/package.json'
415416
])
416417
})
417418
})

0 commit comments

Comments
 (0)