Skip to content

Commit bd3ae12

Browse files
author
Pelle Wessman
committed
Improve tests for getPackageFiles()
1 parent 9e21791 commit bd3ae12

File tree

2 files changed

+93
-6
lines changed

2 files changed

+93
-6
lines changed

lib/utils/path-resolve.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,11 @@ export async function getPackageFiles (cwd, inputPaths, config, debugLog) {
5656

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

59-
const includedPackageFiles = config?.projectIgnorePaths
59+
const includedPackageFiles = config?.projectIgnorePaths?.length
6060
? ignore()
6161
.add(config.projectIgnorePaths)
62-
.filter(packageFiles)
62+
.filter(packageFiles.map(item => path.relative(cwd, item)))
63+
.map(item => path.resolve(cwd, item))
6364
: packageFiles
6465

6566
return includedPackageFiles

test/path-resolve.spec.js

Lines changed: 90 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,95 @@ describe('Path Resolve', () => {
257257
])
258258
})
259259

260-
it('should respect .gitignore')
261-
it('should always ignore some paths')
262-
it('should respect ignore in socket config')
263-
it('should ignore irrelevant matches')
260+
it('should respect ignores from socket config', async () => {
261+
mockFs({
262+
'/bar/package-lock.json': '{}',
263+
'/bar/package.json': '{}',
264+
'/foo/package-lock.json': '{}',
265+
'/foo/package.json': '{}',
266+
})
267+
268+
await getPackageFiles(
269+
'/',
270+
['**/*'],
271+
{
272+
version: 2,
273+
projectIgnorePaths: [
274+
'/bar/*',
275+
'!/bar/package.json',
276+
]
277+
},
278+
() => {}
279+
).should.eventually.become([
280+
'/bar/package.json',
281+
'/foo/package.json',
282+
'/foo/package-lock.json',
283+
])
284+
})
285+
286+
it('should respect .gitignore', async () => {
287+
mockFs({
288+
'/.gitignore': '/bar\n!/bar/package.json',
289+
'/bar/package-lock.json': '{}',
290+
'/bar/package.json': '{}',
291+
'/foo/package-lock.json': '{}',
292+
'/foo/package.json': '{}',
293+
})
294+
295+
await getPackageFiles(
296+
'/',
297+
['**/*'],
298+
undefined,
299+
() => {}
300+
).should.eventually.become([
301+
'/foo/package.json',
302+
'/foo/package-lock.json',
303+
])
304+
})
305+
306+
it('should always ignore some paths', async () => {
307+
mockFs({
308+
// Mirrors the used list form https://github.com/novemberborn/ignore-by-default
309+
'/.git/some/dir/package.json': {},
310+
'/.log/some/dir/package.json': {},
311+
'/.nyc_output/some/dir/package.json': {},
312+
'/.sass-cache/some/dir/package.json': {},
313+
'/.yarn/some/dir/package.json': {},
314+
'/bower_components/some/dir/package.json': {},
315+
'/coverage/some/dir/package.json': {},
316+
'/node_modules/@socketsecurity/cli/package.json': '{}',
317+
'/foo/package-lock.json': '{}',
318+
'/foo/package.json': '{}',
319+
})
320+
321+
await getPackageFiles(
322+
'/',
323+
['**/*'],
324+
undefined,
325+
() => {}
326+
).should.eventually.become([
327+
'/foo/package.json',
328+
'/foo/package-lock.json',
329+
])
330+
})
331+
332+
it('should ignore irrelevant matches', async () => {
333+
mockFs({
334+
'/foo/package-foo.json': '{}',
335+
'/foo/package-lock.json': '{}',
336+
'/foo/package.json': '{}',
337+
'/foo/random.json': '{}',
338+
})
339+
340+
await getPackageFiles(
341+
'/',
342+
['**/*'],
343+
undefined,
344+
() => {}
345+
).should.eventually.become([
346+
'/foo/package.json',
347+
'/foo/package-lock.json',
348+
])
349+
})
264350
})
265351
})

0 commit comments

Comments
 (0)