@@ -202,6 +202,190 @@ suite("nearestParentDartFrogProject", () => {
202
202
} ) ;
203
203
} ) ;
204
204
205
+ suite ( "nearestChildDartFrogProjects" , ( ) => {
206
+ let fsStub : any ;
207
+ let nearestChildDartFrogProjects : any ;
208
+
209
+ beforeEach ( ( ) => {
210
+ fsStub = {
211
+ existsSync : sinon . stub ( ) ,
212
+ readFileSync : sinon . stub ( ) ,
213
+ statSync : sinon . stub ( ) ,
214
+ readdirSync : sinon . stub ( ) ,
215
+ } ;
216
+
217
+ nearestChildDartFrogProjects = proxyquire (
218
+ "../../../utils/dart-frog-structure" ,
219
+ {
220
+ fs : fsStub ,
221
+ }
222
+ ) . nearestChildDartFrogProjects ;
223
+ } ) ;
224
+
225
+ afterEach ( ( ) => {
226
+ sinon . restore ( ) ;
227
+ } ) ;
228
+
229
+ test ( "returns a single path when the file path is a Dart Frog project" , ( ) => {
230
+ const filePath = "/home/project" ;
231
+
232
+ const dartFrogPubspecRoutesPath = path . join ( filePath , "routes" ) ;
233
+ const dartFrogPubspecPath = path . join ( filePath , "pubspec.yaml" ) ;
234
+ fsStub . existsSync . withArgs ( filePath ) . returns ( true ) ;
235
+ fsStub . statSync . withArgs ( filePath ) . returns ( {
236
+ isDirectory : ( ) => true ,
237
+ } ) ;
238
+ fsStub . readdirSync . withArgs ( filePath ) . returns ( [ ] ) ;
239
+ fsStub . existsSync . withArgs ( dartFrogPubspecRoutesPath ) . returns ( true ) ;
240
+ fsStub . existsSync . withArgs ( dartFrogPubspecPath ) . returns ( true ) ;
241
+ fsStub . readFileSync
242
+ . withArgs ( dartFrogPubspecPath , "utf-8" )
243
+ . returns ( validPubspecYaml ) ;
244
+
245
+ const dartFrogProjects = nearestChildDartFrogProjects ( filePath ) ;
246
+
247
+ assert . deepEqual ( dartFrogProjects , [ filePath ] ) ;
248
+ } ) ;
249
+
250
+ test ( "returns the path to all the child Dart Frog projects" , ( ) => {
251
+ fsStub . existsSync . returns ( false ) ;
252
+
253
+ const filePath = "/home/project" ;
254
+ fsStub . existsSync . withArgs ( filePath ) . returns ( true ) ;
255
+ fsStub . statSync . withArgs ( filePath ) . returns ( {
256
+ isDirectory : ( ) => true ,
257
+ } ) ;
258
+ fsStub . readdirSync
259
+ . withArgs ( filePath )
260
+ . returns ( [ "file.dart" , "frog1" , "frog2" , "subdirectory" , "flutter" ] ) ;
261
+
262
+ const fileDartPath = "/home/project/file.dart" ;
263
+ fsStub . existsSync . withArgs ( fileDartPath ) . returns ( true ) ;
264
+ fsStub . statSync . withArgs ( fileDartPath ) . returns ( {
265
+ isDirectory : ( ) => false ,
266
+ } ) ;
267
+
268
+ const dartFrogPath1 = "/home/project/frog1" ;
269
+ const dartFrogPubspecRoutesPath1 = path . join ( dartFrogPath1 , "routes" ) ;
270
+ const dartFrogPubspecPath1 = path . join ( dartFrogPath1 , "pubspec.yaml" ) ;
271
+ fsStub . existsSync . withArgs ( dartFrogPath1 ) . returns ( true ) ;
272
+ fsStub . statSync . withArgs ( dartFrogPath1 ) . returns ( {
273
+ isDirectory : ( ) => true ,
274
+ } ) ;
275
+ fsStub . readdirSync . withArgs ( dartFrogPath1 ) . returns ( [ ] ) ;
276
+ fsStub . existsSync . withArgs ( dartFrogPubspecRoutesPath1 ) . returns ( true ) ;
277
+ fsStub . existsSync . withArgs ( dartFrogPubspecPath1 ) . returns ( true ) ;
278
+ fsStub . readFileSync
279
+ . withArgs ( dartFrogPubspecPath1 , "utf-8" )
280
+ . returns ( validPubspecYaml ) ;
281
+
282
+ const dartFrogPath2 = "/home/project/frog2" ;
283
+ const dartFrogPubspecRoutesPath2 = path . join ( dartFrogPath2 , "routes" ) ;
284
+ const dartFrogPubspecPath2 = path . join ( dartFrogPath2 , "pubspec.yaml" ) ;
285
+ fsStub . existsSync . withArgs ( dartFrogPath2 ) . returns ( true ) ;
286
+ fsStub . statSync . withArgs ( dartFrogPath2 ) . returns ( {
287
+ isDirectory : ( ) => true ,
288
+ } ) ;
289
+ fsStub . readdirSync . withArgs ( dartFrogPath2 ) . returns ( [ ] ) ;
290
+ fsStub . existsSync . withArgs ( dartFrogPubspecRoutesPath2 ) . returns ( true ) ;
291
+ fsStub . existsSync . withArgs ( dartFrogPubspecPath2 ) . returns ( true ) ;
292
+ fsStub . readFileSync
293
+ . withArgs ( dartFrogPubspecPath2 , "utf-8" )
294
+ . returns ( validPubspecYaml ) ;
295
+
296
+ const subdirectory = "/home/project/subdirectory" ;
297
+ fsStub . existsSync . withArgs ( subdirectory ) . returns ( true ) ;
298
+ fsStub . statSync . withArgs ( subdirectory ) . returns ( {
299
+ isDirectory : ( ) => true ,
300
+ } ) ;
301
+ fsStub . readdirSync . withArgs ( subdirectory ) . returns ( [ "frog3" ] ) ;
302
+
303
+ const dartFrogPath3 = "/home/project/subdirectory/frog3" ;
304
+ const dartFrogPubspecRoutesPath3 = path . join ( dartFrogPath3 , "routes" ) ;
305
+ const dartFrogPubspecPath3 = path . join ( dartFrogPath3 , "pubspec.yaml" ) ;
306
+ fsStub . existsSync . withArgs ( dartFrogPath3 ) . returns ( true ) ;
307
+ fsStub . statSync . withArgs ( dartFrogPath3 ) . returns ( {
308
+ isDirectory : ( ) => true ,
309
+ } ) ;
310
+ fsStub . readdirSync . withArgs ( dartFrogPath3 ) . returns ( [ ] ) ;
311
+ fsStub . existsSync . withArgs ( dartFrogPubspecRoutesPath3 ) . returns ( true ) ;
312
+ fsStub . existsSync . withArgs ( dartFrogPubspecPath3 ) . returns ( true ) ;
313
+ fsStub . readFileSync
314
+ . withArgs ( dartFrogPubspecPath3 , "utf-8" )
315
+ . returns ( validPubspecYaml ) ;
316
+
317
+ const flutterPath = "/home/project/flutter" ;
318
+ const flutterPubspecPath = path . join ( flutterPath , "pubspec.yaml" ) ;
319
+ fsStub . existsSync . withArgs ( flutterPath ) . returns ( true ) ;
320
+ fsStub . statSync . withArgs ( flutterPath ) . returns ( {
321
+ isDirectory : ( ) => true ,
322
+ } ) ;
323
+ fsStub . readdirSync . withArgs ( flutterPath ) . returns ( [ ] ) ;
324
+ fsStub . existsSync . withArgs ( flutterPubspecPath ) . returns ( true ) ;
325
+ fsStub . readFileSync
326
+ . withArgs ( flutterPubspecPath , "utf-8" )
327
+ . returns ( invalidPubspecYaml ) ;
328
+
329
+ const dartFrogProjects = nearestChildDartFrogProjects ( filePath ) ;
330
+
331
+ assert . deepEqual ( dartFrogProjects , [
332
+ dartFrogPath1 ,
333
+ dartFrogPath2 ,
334
+ dartFrogPath3 ,
335
+ ] ) ;
336
+ } ) ;
337
+
338
+ suite ( "returns undefined" , ( ) => {
339
+ test ( "when path does not exist" , ( ) => {
340
+ const filePath = "/home/project/routes/animals/frog.dart" ;
341
+ fsStub . existsSync . withArgs ( filePath ) . returns ( false ) ;
342
+
343
+ const result = nearestChildDartFrogProjects ( filePath ) ;
344
+
345
+ assert . equal ( result , undefined ) ;
346
+ } ) ;
347
+
348
+ test ( "when path is not a directory" , ( ) => {
349
+ const filePath = "/home/project/routes/animals/frog.dart" ;
350
+ fsStub . existsSync . withArgs ( filePath ) . returns ( true ) ;
351
+ fsStub . statSync . withArgs ( filePath ) . returns ( {
352
+ isDirectory : ( ) => false ,
353
+ } ) ;
354
+
355
+ const result = nearestChildDartFrogProjects ( filePath ) ;
356
+
357
+ assert . equal ( result , undefined ) ;
358
+ } ) ;
359
+
360
+ test ( "when subdirectory is not a Dart Frog project" , ( ) => {
361
+ const filePath = "/home/project" ;
362
+ fsStub . existsSync . withArgs ( filePath ) . returns ( true ) ;
363
+ fsStub . statSync . withArgs ( filePath ) . returns ( {
364
+ isDirectory : ( ) => true ,
365
+ } ) ;
366
+ fsStub . readdirSync . withArgs ( filePath ) . returns ( [ "frog" ] ) ;
367
+
368
+ const dartFrogPath = "/home/project/frog" ;
369
+ const dartFrogPubspecRoutesPath = path . join ( dartFrogPath , "routes" ) ;
370
+ const dartFrogPubspecPath = path . join ( dartFrogPath , "pubspec.yaml" ) ;
371
+ fsStub . existsSync . withArgs ( dartFrogPath ) . returns ( true ) ;
372
+ fsStub . statSync . withArgs ( dartFrogPath ) . returns ( {
373
+ isDirectory : ( ) => true ,
374
+ } ) ;
375
+ fsStub . readdirSync . withArgs ( dartFrogPath ) . returns ( [ ] ) ;
376
+ fsStub . existsSync . withArgs ( dartFrogPubspecRoutesPath ) . returns ( true ) ;
377
+ fsStub . existsSync . withArgs ( dartFrogPubspecPath ) . returns ( true ) ;
378
+ fsStub . readFileSync
379
+ . withArgs ( dartFrogPubspecPath , "utf-8" )
380
+ . returns ( invalidPubspecYaml ) ;
381
+
382
+ const result = nearestChildDartFrogProjects ( filePath ) ;
383
+
384
+ assert . equal ( result , undefined ) ;
385
+ } ) ;
386
+ } ) ;
387
+ } ) ;
388
+
205
389
suite ( "isDartFrogProject" , ( ) => {
206
390
let fsStub : any ;
207
391
let isDartFrogProject : any ;
0 commit comments