-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathpath-resolve.test.mts
More file actions
341 lines (307 loc) · 9.79 KB
/
path-resolve.test.mts
File metadata and controls
341 lines (307 loc) · 9.79 KB
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import { existsSync, readdirSync, rmSync } from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import mockFs from 'mock-fs'
import { afterEach, describe, expect, it } from 'vitest'
import { normalizePath } from '@socketsecurity/registry/lib/path'
import {
NODE_MODULES,
PACKAGE_JSON,
PACKAGE_LOCK_JSON,
PNPM_LOCK_YAML,
YARN_LOCK,
} from '../constants.mjs'
import { getPackageFilesForScan } from './path-resolve.mts'
import type FileSystem from 'mock-fs/lib/filesystem'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const rootNmPath = path.join(__dirname, '../..', NODE_MODULES)
const mockFixturePath = normalizePath(path.join(__dirname, 'mock'))
const mockNmPath = normalizePath(rootNmPath)
// Remove broken symlinks in node_modules before loading to prevent mock-fs errors.
function cleanupBrokenSymlinks(dirPath: string): void {
try {
if (!existsSync(dirPath)) {
return
}
const entries = readdirSync(dirPath, { withFileTypes: true })
for (const entry of entries) {
const fullPath = path.join(dirPath, entry.name)
try {
if (entry.isSymbolicLink() && !existsSync(fullPath)) {
// Symlink exists but target does not, remove it.
rmSync(fullPath, { force: true })
} else if (entry.isDirectory()) {
// Recursively check subdirectories.
cleanupBrokenSymlinks(fullPath)
}
} catch {
// Ignore errors for individual entries.
}
}
} catch {
// If we cannot read the directory, skip cleanup.
}
}
// Clean up broken symlinks before loading node_modules.
cleanupBrokenSymlinks(rootNmPath)
// Load node_modules with error handling for any remaining issues.
const mockedNmCallback = (() => {
try {
return mockFs.load(rootNmPath)
} catch (e) {
// If loading fails due to broken symlinks or missing files, return empty mock.
console.warn(
`Warning: Failed to load node_modules for mock-fs: ${e instanceof Error ? e.message : String(e)}`,
)
return {}
}
})()
function mockTestFs(config: FileSystem.DirectoryItems) {
return mockFs({
...config,
[mockNmPath]: mockedNmCallback,
})
}
const globPatterns = {
general: {
readme: {
pattern: '*readme*',
},
notice: {
pattern: '*notice*',
},
license: {
pattern: '{licen{s,c}e{,-*},copying}',
},
},
npm: {
packagejson: {
pattern: PACKAGE_JSON,
},
packagelockjson: {
pattern: PACKAGE_LOCK_JSON,
},
npmshrinkwrap: {
pattern: 'npm-shrinkwrap.json',
},
yarnlock: {
pattern: YARN_LOCK,
},
pnpmlock: {
pattern: PNPM_LOCK_YAML,
},
pnpmworkspace: {
pattern: 'pnpm-workspace.yaml',
},
},
pypi: {
pipfile: {
pattern: 'pipfile',
},
pyproject: {
pattern: 'pyproject.toml',
},
requirements: {
pattern:
'{*requirements.txt,requirements/*.txt,requirements-*.txt,requirements.frozen}',
},
setuppy: {
pattern: 'setup.py',
},
},
}
type Fn = (...args: any[]) => Promise<any[]>
const sortedPromise =
(fn: Fn) =>
async (...args: any[]) => {
const result = await fn(...args)
return result.sort()
}
const sortedGetPackageFilesFullScans = sortedPromise(getPackageFilesForScan)
describe('Path Resolve', () => {
afterEach(() => {
mockFs.restore()
})
describe('getPackageFilesForScan()', () => {
it('should handle a "." inputPath', async () => {
mockTestFs({
[`${mockFixturePath}/package.json`]: '{}',
})
const actual = await sortedGetPackageFilesFullScans(['.'], globPatterns, {
cwd: mockFixturePath,
})
expect(actual.map(normalizePath)).toEqual([
`${mockFixturePath}/package.json`,
])
})
it('should respect ignores from socket config', async () => {
mockTestFs({
[`${mockFixturePath}/bar/package-lock.json`]: '{}',
[`${mockFixturePath}/bar/package.json`]: '{}',
[`${mockFixturePath}/foo/package-lock.json`]: '{}',
[`${mockFixturePath}/foo/package.json`]: '{}',
})
const actual = await sortedGetPackageFilesFullScans(
['**/*'],
globPatterns,
{
cwd: mockFixturePath,
config: {
version: 2,
projectIgnorePaths: ['bar/*', '!bar/package.json'],
issueRules: {},
githubApp: {},
},
},
)
expect(actual.map(normalizePath)).toEqual([
`${mockFixturePath}/bar/package.json`,
`${mockFixturePath}/foo/package-lock.json`,
`${mockFixturePath}/foo/package.json`,
])
})
it('should respect .gitignore', async () => {
mockTestFs({
[`${mockFixturePath}/.gitignore`]: 'bar/*\n!bar/package.json',
[`${mockFixturePath}/bar/package-lock.json`]: '{}',
[`${mockFixturePath}/bar/package.json`]: '{}',
[`${mockFixturePath}/foo/package-lock.json`]: '{}',
[`${mockFixturePath}/foo/package.json`]: '{}',
})
const actual = await sortedGetPackageFilesFullScans(
['**/*'],
globPatterns,
{ cwd: mockFixturePath },
)
expect(actual.map(normalizePath)).toEqual([
`${mockFixturePath}/bar/package.json`,
`${mockFixturePath}/foo/package-lock.json`,
`${mockFixturePath}/foo/package.json`,
])
})
it('should always ignore some paths', async () => {
mockTestFs({
// Mirrors the list from
// https://github.com/novemberborn/ignore-by-default/blob/v2.1.0/index.js
[`${mockFixturePath}/.git/some/dir/package.json`]: '{}',
[`${mockFixturePath}/.log/some/dir/package.json`]: '{}',
[`${mockFixturePath}/.nyc_output/some/dir/package.json`]: '{}',
[`${mockFixturePath}/.sass-cache/some/dir/package.json`]: '{}',
[`${mockFixturePath}/.yarn/some/dir/package.json`]: '{}',
[`${mockFixturePath}/bower_components/some/dir/package.json`]: '{}',
[`${mockFixturePath}/coverage/some/dir/package.json`]: '{}',
[`${mockFixturePath}/node_modules/socket/package.json`]: '{}',
[`${mockFixturePath}/foo/package-lock.json`]: '{}',
[`${mockFixturePath}/foo/package.json`]: '{}',
})
const actual = await sortedGetPackageFilesFullScans(
['**/*'],
globPatterns,
{ cwd: mockFixturePath },
)
expect(actual.map(normalizePath)).toEqual([
`${mockFixturePath}/foo/package-lock.json`,
`${mockFixturePath}/foo/package.json`,
])
})
it('should ignore irrelevant matches', async () => {
mockTestFs({
[`${mockFixturePath}/foo/package-foo.json`]: '{}',
[`${mockFixturePath}/foo/package-lock.json`]: '{}',
[`${mockFixturePath}/foo/package.json`]: '{}',
[`${mockFixturePath}/foo/random.json`]: '{}',
})
const actual = await sortedGetPackageFilesFullScans(
['**/*'],
globPatterns,
{ cwd: mockFixturePath },
)
expect(actual.map(normalizePath)).toEqual([
`${mockFixturePath}/foo/package-lock.json`,
`${mockFixturePath}/foo/package.json`,
])
})
it('should be lenient on oddities', async () => {
mockTestFs({
[`${mockFixturePath}/package.json`]: {
/* Empty directory */
},
})
const actual = await sortedGetPackageFilesFullScans(
['**/*'],
globPatterns,
{ cwd: mockFixturePath },
)
expect(actual.map(normalizePath)).toEqual([])
})
it('should resolve package and lockfile', async () => {
mockTestFs({
[`${mockFixturePath}/package-lock.json`]: '{}',
[`${mockFixturePath}/package.json`]: '{}',
})
const actual = await sortedGetPackageFilesFullScans(
['**/*'],
globPatterns,
{ cwd: mockFixturePath },
)
expect(actual.map(normalizePath)).toEqual([
`${mockFixturePath}/package-lock.json`,
`${mockFixturePath}/package.json`,
])
})
it('should resolve package without lockfile', async () => {
mockTestFs({
[`${mockFixturePath}/package.json`]: '{}',
})
const actual = await sortedGetPackageFilesFullScans(
['**/*'],
globPatterns,
{ cwd: mockFixturePath },
)
expect(actual.map(normalizePath)).toEqual([
`${mockFixturePath}/package.json`,
])
})
it('should support alternative lockfiles', async () => {
mockTestFs({
[`${mockFixturePath}/yarn.lock`]: '{}',
[`${mockFixturePath}/package.json`]: '{}',
})
const actual = await sortedGetPackageFilesFullScans(
['**/*'],
globPatterns,
{ cwd: mockFixturePath },
)
expect(actual.map(normalizePath)).toEqual([
`${mockFixturePath}/package.json`,
`${mockFixturePath}/yarn.lock`,
])
})
it('should handle all variations', async () => {
mockTestFs({
[`${mockFixturePath}/package-lock.json`]: '{}',
[`${mockFixturePath}/package.json`]: '{}',
[`${mockFixturePath}/foo/package-lock.json`]: '{}',
[`${mockFixturePath}/foo/package.json`]: '{}',
[`${mockFixturePath}/bar/yarn.lock`]: '{}',
[`${mockFixturePath}/bar/package.json`]: '{}',
[`${mockFixturePath}/abc/package.json`]: '{}',
})
const actual = await sortedGetPackageFilesFullScans(
['**/*'],
globPatterns,
{ cwd: mockFixturePath },
)
expect(actual.map(normalizePath)).toEqual([
`${mockFixturePath}/abc/package.json`,
`${mockFixturePath}/bar/package.json`,
`${mockFixturePath}/bar/yarn.lock`,
`${mockFixturePath}/foo/package-lock.json`,
`${mockFixturePath}/foo/package.json`,
`${mockFixturePath}/package-lock.json`,
`${mockFixturePath}/package.json`,
])
})
})
})