@@ -9,7 +9,18 @@ import { normalizePath } from '@socketsecurity/registry/lib/path'
9
9
import { getPackageFilesFullScans } from './dist/path-resolve'
10
10
11
11
const testPath = __dirname
12
- const mockPath = normalizePath ( path . join ( testPath , 'mock' ) )
12
+ const rootNmPath = path . join ( testPath , '../node_modules' )
13
+
14
+ const mockFixturePath = normalizePath ( path . join ( testPath , 'mock' ) )
15
+ const mockNmPath = normalizePath ( rootNmPath )
16
+ const mockedNmCallback = mockFs . load ( rootNmPath )
17
+
18
+ function mockTestFs ( config ) {
19
+ return mockFs ( {
20
+ ...config ,
21
+ [ mockNmPath ] : mockedNmCallback
22
+ } )
23
+ }
13
24
14
25
const globPatterns = {
15
26
general : {
@@ -68,7 +79,7 @@ const sortedPromise =
68
79
const result = await fn ( ...args )
69
80
return result . sort ( )
70
81
}
71
- const sortedGetPackageFiles = sortedPromise ( getPackageFilesFullScans )
82
+ const sortedGetPackageFilesFullScans = sortedPromise ( getPackageFilesFullScans )
72
83
73
84
describe ( 'Path Resolve' , ( ) => {
74
85
beforeEach ( ( ) => {
@@ -83,31 +94,33 @@ describe('Path Resolve', () => {
83
94
}
84
95
} )
85
96
86
- describe ( 'getPackageFiles ()' , ( ) => {
97
+ describe ( 'getPackageFilesFullScans ()' , ( ) => {
87
98
it ( 'should handle a "." inputPath' , async ( ) => {
88
- mockFs ( {
89
- [ `${ mockPath } /package.json` ] : '{}'
99
+ mockTestFs ( {
100
+ [ `${ mockFixturePath } /package.json` ] : '{}'
90
101
} )
91
102
92
- const actual = await sortedGetPackageFiles (
93
- mockPath ,
103
+ const actual = await sortedGetPackageFilesFullScans (
104
+ mockFixturePath ,
94
105
[ '.' ] ,
95
106
globPatterns ,
96
107
undefined
97
108
)
98
- expect ( actual . map ( normalizePath ) ) . toEqual ( [ `${ mockPath } /package.json` ] )
109
+ expect ( actual . map ( normalizePath ) ) . toEqual ( [
110
+ `${ mockFixturePath } /package.json`
111
+ ] )
99
112
} )
100
113
101
114
it ( 'should respect ignores from socket config' , async ( ) => {
102
- mockFs ( {
103
- [ `${ mockPath } /bar/package-lock.json` ] : '{}' ,
104
- [ `${ mockPath } /bar/package.json` ] : '{}' ,
105
- [ `${ mockPath } /foo/package-lock.json` ] : '{}' ,
106
- [ `${ mockPath } /foo/package.json` ] : '{}'
115
+ mockTestFs ( {
116
+ [ `${ mockFixturePath } /bar/package-lock.json` ] : '{}' ,
117
+ [ `${ mockFixturePath } /bar/package.json` ] : '{}' ,
118
+ [ `${ mockFixturePath } /foo/package-lock.json` ] : '{}' ,
119
+ [ `${ mockFixturePath } /foo/package.json` ] : '{}'
107
120
} )
108
121
109
- const actual = await sortedGetPackageFiles (
110
- mockPath ,
122
+ const actual = await sortedGetPackageFilesFullScans (
123
+ mockFixturePath ,
111
124
[ '**/*' ] ,
112
125
globPatterns ,
113
126
{
@@ -118,91 +131,91 @@ describe('Path Resolve', () => {
118
131
}
119
132
)
120
133
expect ( actual . map ( normalizePath ) ) . toEqual ( [
121
- `${ mockPath } /bar/package.json` ,
122
- `${ mockPath } /foo/package-lock.json` ,
123
- `${ mockPath } /foo/package.json`
134
+ `${ mockFixturePath } /bar/package.json` ,
135
+ `${ mockFixturePath } /foo/package-lock.json` ,
136
+ `${ mockFixturePath } /foo/package.json`
124
137
] )
125
138
} )
126
139
127
140
it ( 'should respect .gitignore' , async ( ) => {
128
- mockFs ( {
129
- [ `${ mockPath } /.gitignore` ] : 'bar/*\n!bar/package.json' ,
130
- [ `${ mockPath } /bar/package-lock.json` ] : '{}' ,
131
- [ `${ mockPath } /bar/package.json` ] : '{}' ,
132
- [ `${ mockPath } /foo/package-lock.json` ] : '{}' ,
133
- [ `${ mockPath } /foo/package.json` ] : '{}'
141
+ mockTestFs ( {
142
+ [ `${ mockFixturePath } /.gitignore` ] : 'bar/*\n!bar/package.json' ,
143
+ [ `${ mockFixturePath } /bar/package-lock.json` ] : '{}' ,
144
+ [ `${ mockFixturePath } /bar/package.json` ] : '{}' ,
145
+ [ `${ mockFixturePath } /foo/package-lock.json` ] : '{}' ,
146
+ [ `${ mockFixturePath } /foo/package.json` ] : '{}'
134
147
} )
135
148
136
- const actual = await sortedGetPackageFiles (
137
- mockPath ,
149
+ const actual = await sortedGetPackageFilesFullScans (
150
+ mockFixturePath ,
138
151
[ '**/*' ] ,
139
152
globPatterns ,
140
153
undefined
141
154
)
142
155
expect ( actual . map ( normalizePath ) ) . toEqual ( [
143
- `${ mockPath } /bar/package.json` ,
144
- `${ mockPath } /foo/package-lock.json` ,
145
- `${ mockPath } /foo/package.json`
156
+ `${ mockFixturePath } /bar/package.json` ,
157
+ `${ mockFixturePath } /foo/package-lock.json` ,
158
+ `${ mockFixturePath } /foo/package.json`
146
159
] )
147
160
} )
148
161
149
162
it ( 'should always ignore some paths' , async ( ) => {
150
- mockFs ( {
163
+ mockTestFs ( {
151
164
// Mirrors the list from
152
165
// https://github.com/novemberborn/ignore-by-default/blob/v2.1.0/index.js
153
- [ `${ mockPath } /.git/some/dir/package.json` ] : '{}' ,
154
- [ `${ mockPath } /.log/some/dir/package.json` ] : '{}' ,
155
- [ `${ mockPath } /.nyc_output/some/dir/package.json` ] : '{}' ,
156
- [ `${ mockPath } /.sass-cache/some/dir/package.json` ] : '{}' ,
157
- [ `${ mockPath } /.yarn/some/dir/package.json` ] : '{}' ,
158
- [ `${ mockPath } /bower_components/some/dir/package.json` ] : '{}' ,
159
- [ `${ mockPath } /coverage/some/dir/package.json` ] : '{}' ,
160
- [ `${ mockPath } /node_modules/socket/package.json` ] : '{}' ,
161
- [ `${ mockPath } /foo/package-lock.json` ] : '{}' ,
162
- [ `${ mockPath } /foo/package.json` ] : '{}'
166
+ [ `${ mockFixturePath } /.git/some/dir/package.json` ] : '{}' ,
167
+ [ `${ mockFixturePath } /.log/some/dir/package.json` ] : '{}' ,
168
+ [ `${ mockFixturePath } /.nyc_output/some/dir/package.json` ] : '{}' ,
169
+ [ `${ mockFixturePath } /.sass-cache/some/dir/package.json` ] : '{}' ,
170
+ [ `${ mockFixturePath } /.yarn/some/dir/package.json` ] : '{}' ,
171
+ [ `${ mockFixturePath } /bower_components/some/dir/package.json` ] : '{}' ,
172
+ [ `${ mockFixturePath } /coverage/some/dir/package.json` ] : '{}' ,
173
+ [ `${ mockFixturePath } /node_modules/socket/package.json` ] : '{}' ,
174
+ [ `${ mockFixturePath } /foo/package-lock.json` ] : '{}' ,
175
+ [ `${ mockFixturePath } /foo/package.json` ] : '{}'
163
176
} )
164
177
165
- const actual = await sortedGetPackageFiles (
166
- mockPath ,
178
+ const actual = await sortedGetPackageFilesFullScans (
179
+ mockFixturePath ,
167
180
[ '**/*' ] ,
168
181
globPatterns ,
169
182
undefined
170
183
)
171
184
expect ( actual . map ( normalizePath ) ) . toEqual ( [
172
- `${ mockPath } /foo/package-lock.json` ,
173
- `${ mockPath } /foo/package.json`
185
+ `${ mockFixturePath } /foo/package-lock.json` ,
186
+ `${ mockFixturePath } /foo/package.json`
174
187
] )
175
188
} )
176
189
177
190
it ( 'should ignore irrelevant matches' , async ( ) => {
178
- mockFs ( {
179
- [ `${ mockPath } /foo/package-foo.json` ] : '{}' ,
180
- [ `${ mockPath } /foo/package-lock.json` ] : '{}' ,
181
- [ `${ mockPath } /foo/package.json` ] : '{}' ,
182
- [ `${ mockPath } /foo/random.json` ] : '{}'
191
+ mockTestFs ( {
192
+ [ `${ mockFixturePath } /foo/package-foo.json` ] : '{}' ,
193
+ [ `${ mockFixturePath } /foo/package-lock.json` ] : '{}' ,
194
+ [ `${ mockFixturePath } /foo/package.json` ] : '{}' ,
195
+ [ `${ mockFixturePath } /foo/random.json` ] : '{}'
183
196
} )
184
197
185
- const actual = await sortedGetPackageFiles (
186
- mockPath ,
198
+ const actual = await sortedGetPackageFilesFullScans (
199
+ mockFixturePath ,
187
200
[ '**/*' ] ,
188
201
globPatterns ,
189
202
undefined
190
203
)
191
204
expect ( actual . map ( normalizePath ) ) . toEqual ( [
192
- `${ mockPath } /foo/package-lock.json` ,
193
- `${ mockPath } /foo/package.json`
205
+ `${ mockFixturePath } /foo/package-lock.json` ,
206
+ `${ mockFixturePath } /foo/package.json`
194
207
] )
195
208
} )
196
209
197
210
it ( 'should be lenient on oddities' , async ( ) => {
198
- mockFs ( {
199
- [ `${ mockPath } /package.json` ] : {
211
+ mockTestFs ( {
212
+ [ `${ mockFixturePath } /package.json` ] : {
200
213
/* Empty directory */
201
214
}
202
215
} )
203
216
204
- const actual = await sortedGetPackageFiles (
205
- mockPath ,
217
+ const actual = await sortedGetPackageFilesFullScans (
218
+ mockFixturePath ,
206
219
[ '**/*' ] ,
207
220
globPatterns ,
208
221
undefined
@@ -211,80 +224,82 @@ describe('Path Resolve', () => {
211
224
} )
212
225
213
226
it ( 'should resolve package and lock file' , async ( ) => {
214
- mockFs ( {
215
- [ `${ mockPath } /package-lock.json` ] : '{}' ,
216
- [ `${ mockPath } /package.json` ] : '{}'
227
+ mockTestFs ( {
228
+ [ `${ mockFixturePath } /package-lock.json` ] : '{}' ,
229
+ [ `${ mockFixturePath } /package.json` ] : '{}'
217
230
} )
218
231
219
- const actual = await sortedGetPackageFiles (
220
- mockPath ,
232
+ const actual = await sortedGetPackageFilesFullScans (
233
+ mockFixturePath ,
221
234
[ '**/*' ] ,
222
235
globPatterns ,
223
236
undefined
224
237
)
225
238
expect ( actual . map ( normalizePath ) ) . toEqual ( [
226
- `${ mockPath } /package-lock.json` ,
227
- `${ mockPath } /package.json`
239
+ `${ mockFixturePath } /package-lock.json` ,
240
+ `${ mockFixturePath } /package.json`
228
241
] )
229
242
} )
230
243
231
244
it ( 'should resolve package without lock file' , async ( ) => {
232
- mockFs ( {
233
- [ `${ mockPath } /package.json` ] : '{}'
245
+ mockTestFs ( {
246
+ [ `${ mockFixturePath } /package.json` ] : '{}'
234
247
} )
235
248
236
- const actual = await sortedGetPackageFiles (
237
- mockPath ,
249
+ const actual = await sortedGetPackageFilesFullScans (
250
+ mockFixturePath ,
238
251
[ '**/*' ] ,
239
252
globPatterns ,
240
253
undefined
241
254
)
242
- expect ( actual . map ( normalizePath ) ) . toEqual ( [ `${ mockPath } /package.json` ] )
255
+ expect ( actual . map ( normalizePath ) ) . toEqual ( [
256
+ `${ mockFixturePath } /package.json`
257
+ ] )
243
258
} )
244
259
245
260
it ( 'should support alternative lock files' , async ( ) => {
246
- mockFs ( {
247
- [ `${ mockPath } /yarn.lock` ] : '{}' ,
248
- [ `${ mockPath } /package.json` ] : '{}'
261
+ mockTestFs ( {
262
+ [ `${ mockFixturePath } /yarn.lock` ] : '{}' ,
263
+ [ `${ mockFixturePath } /package.json` ] : '{}'
249
264
} )
250
265
251
- const actual = await sortedGetPackageFiles (
252
- mockPath ,
266
+ const actual = await sortedGetPackageFilesFullScans (
267
+ mockFixturePath ,
253
268
[ '**/*' ] ,
254
269
globPatterns ,
255
270
undefined
256
271
)
257
272
expect ( actual . map ( normalizePath ) ) . toEqual ( [
258
- `${ mockPath } /package.json` ,
259
- `${ mockPath } /yarn.lock`
273
+ `${ mockFixturePath } /package.json` ,
274
+ `${ mockFixturePath } /yarn.lock`
260
275
] )
261
276
} )
262
277
263
278
it ( 'should handle all variations' , async ( ) => {
264
- mockFs ( {
265
- [ `${ mockPath } /package-lock.json` ] : '{}' ,
266
- [ `${ mockPath } /package.json` ] : '{}' ,
267
- [ `${ mockPath } /foo/package-lock.json` ] : '{}' ,
268
- [ `${ mockPath } /foo/package.json` ] : '{}' ,
269
- [ `${ mockPath } /bar/yarn.lock` ] : '{}' ,
270
- [ `${ mockPath } /bar/package.json` ] : '{}' ,
271
- [ `${ mockPath } /abc/package.json` ] : '{}'
279
+ mockTestFs ( {
280
+ [ `${ mockFixturePath } /package-lock.json` ] : '{}' ,
281
+ [ `${ mockFixturePath } /package.json` ] : '{}' ,
282
+ [ `${ mockFixturePath } /foo/package-lock.json` ] : '{}' ,
283
+ [ `${ mockFixturePath } /foo/package.json` ] : '{}' ,
284
+ [ `${ mockFixturePath } /bar/yarn.lock` ] : '{}' ,
285
+ [ `${ mockFixturePath } /bar/package.json` ] : '{}' ,
286
+ [ `${ mockFixturePath } /abc/package.json` ] : '{}'
272
287
} )
273
288
274
- const actual = await sortedGetPackageFiles (
275
- mockPath ,
289
+ const actual = await sortedGetPackageFilesFullScans (
290
+ mockFixturePath ,
276
291
[ '**/*' ] ,
277
292
globPatterns ,
278
293
undefined
279
294
)
280
295
expect ( actual . map ( normalizePath ) ) . toEqual ( [
281
- `${ mockPath } /abc/package.json` ,
282
- `${ mockPath } /bar/package.json` ,
283
- `${ mockPath } /bar/yarn.lock` ,
284
- `${ mockPath } /foo/package-lock.json` ,
285
- `${ mockPath } /foo/package.json` ,
286
- `${ mockPath } /package-lock.json` ,
287
- `${ mockPath } /package.json`
296
+ `${ mockFixturePath } /abc/package.json` ,
297
+ `${ mockFixturePath } /bar/package.json` ,
298
+ `${ mockFixturePath } /bar/yarn.lock` ,
299
+ `${ mockFixturePath } /foo/package-lock.json` ,
300
+ `${ mockFixturePath } /foo/package.json` ,
301
+ `${ mockFixturePath } /package-lock.json` ,
302
+ `${ mockFixturePath } /package.json`
288
303
] )
289
304
} )
290
305
} )
0 commit comments