@@ -13,7 +13,7 @@ const {
13
13
getRelatedFunctions,
14
14
getModuleTypeFromFilePath
15
15
} = require ( "../utils/code" ) ;
16
- const { getRelativePath, getFolderTreeItem, getTestFolderPath, checkPathType, isPathInside} = require ( "../utils/files" ) ;
16
+ const { getRelativePath, getFolderTreeItem, getTestFolderPath, checkPathType, isPathInside, calculateDepth } = require ( "../utils/files" ) ;
17
17
const { initScreenForUnitTests} = require ( "./cmdGUI" ) ;
18
18
const { green, red, blue, bold, reset} = require ( '../utils/cmdPrint' ) . colors ;
19
19
@@ -153,7 +153,7 @@ async function reformatDataForPythagoraAPI(funcData, filePath, testFilePath) {
153
153
return funcData ;
154
154
}
155
155
156
- async function createTests ( filePath , prefix , funcToTest , processingFunction = 'getUnitTests' ) {
156
+ async function createTests ( filePath , funcToTest , processingFunction = 'getUnitTests' ) {
157
157
try {
158
158
let extension = path . extname ( filePath ) ;
159
159
let ast = await getAstFromFilePath ( filePath ) ;
@@ -186,18 +186,18 @@ async function createTests(filePath, prefix, funcToTest, processingFunction = 'g
186
186
) )
187
187
) ;
188
188
189
+ sortFolderTree ( ) ;
190
+
189
191
for ( const [ i , funcData ] of uniqueFoundFunctions . entries ( ) ) {
190
- let isLast = uniqueFoundFunctions . indexOf ( funcData ) === uniqueFoundFunctions . length - 1 ;
191
192
let indexToPush = fileIndex + 1 + i ;
193
+ let prefix = folderStructureTree [ fileIndex ] . line . split ( path . basename ( folderStructureTree [ fileIndex ] . absolutePath ) ) [ 0 ] ;
192
194
folderStructureTree . splice (
193
195
indexToPush ,
194
196
0 ,
195
- getFolderTreeItem (
196
- prefix ,
197
- isLast ,
198
- `${ funcData . functionName } .test${ extension } ` ,
199
- filePath + ':' + funcData . functionName
200
- )
197
+ {
198
+ line : " " . repeat ( prefix . length ) + "└───" + funcData . functionName ,
199
+ absolutePath : filePath + ':' + funcData . functionName
200
+ }
201
201
) ;
202
202
spinner . start ( folderStructureTree , indexToPush ) ;
203
203
@@ -237,7 +237,7 @@ async function createTests(filePath, prefix, funcToTest, processingFunction = 'g
237
237
}
238
238
239
239
} catch ( e ) {
240
- if ( ! ignoreErrors . includes ( e . code ) ) errors . push ( e ) ;
240
+ if ( ! ignoreErrors . includes ( e . code ) ) errors . push ( e . stack ) ;
241
241
}
242
242
}
243
243
@@ -254,7 +254,7 @@ async function saveTests(filePath, name, testData) {
254
254
return testPath ;
255
255
}
256
256
257
- async function traverseDirectory ( file , onlyCollectFunctionData , prefix = '' , funcName , processingFunction ) {
257
+ async function traverseDirectory ( file , onlyCollectFunctionData , funcName , processingFunction ) {
258
258
if ( processedFiles . includes ( file ) ) {
259
259
return ;
260
260
}
@@ -264,24 +264,21 @@ async function traverseDirectory(file, onlyCollectFunctionData, prefix = '', fun
264
264
if ( ! processExtensions . includes ( path . extname ( file ) ) ) {
265
265
throw new Error ( 'File extension is not supported' ) ;
266
266
}
267
- const newPrefix = `| ${ prefix } | ` ;
268
- return await createTests ( file , newPrefix , funcName , processingFunction ) ;
267
+ return await createTests ( file , funcName , processingFunction ) ;
269
268
}
270
269
271
270
const absolutePath = path . resolve ( file ) ;
272
271
const stat = fs . statSync ( absolutePath ) ;
273
- const isLast = filesToProcess . length === 0 ;
274
272
275
273
if ( ! stat . isDirectory ( ) && isFileToIgnore ( file ) ) return ;
276
274
277
275
if ( stat . isDirectory ( ) ) {
278
276
if ( ignoreFolders . includes ( path . basename ( absolutePath ) ) || path . basename ( absolutePath ) . charAt ( 0 ) === '.' ) return ;
279
277
280
278
if ( onlyCollectFunctionData && isPathInside ( path . dirname ( queriedPath ) , absolutePath ) ) {
281
- updateFolderTree ( prefix , isLast , absolutePath ) ;
279
+ updateFolderTree ( absolutePath ) ;
282
280
}
283
281
284
- const newPrefix = isLast ? `${ prefix } ` : `${ prefix } | ` ;
285
282
const directoryFiles = fs . readdirSync ( absolutePath )
286
283
. filter ( f => {
287
284
const absoluteFilePath = path . join ( absolutePath , f ) ;
@@ -303,12 +300,11 @@ async function traverseDirectory(file, onlyCollectFunctionData, prefix = '', fun
303
300
304
301
if ( onlyCollectFunctionData ) {
305
302
if ( isPathInside ( path . dirname ( queriedPath ) , absolutePath ) ) {
306
- updateFolderTree ( prefix , isLast , absolutePath ) ;
303
+ updateFolderTree ( absolutePath ) ;
307
304
}
308
305
await processFile ( absolutePath , filesToProcess ) ;
309
306
} else {
310
- const newPrefix = isLast ? `| ${ prefix } ` : `| ${ prefix } | ` ;
311
- await createTests ( absolutePath , newPrefix , funcName , processingFunction ) ;
307
+ await createTests ( absolutePath , funcName , processingFunction ) ;
312
308
}
313
309
}
314
310
@@ -317,13 +313,42 @@ async function traverseDirectory(file, onlyCollectFunctionData, prefix = '', fun
317
313
if ( processedFiles . includes ( nextFile ) ) {
318
314
continue ; // Skip processing if it has already been processed
319
315
}
320
- await traverseDirectory ( nextFile , onlyCollectFunctionData , prefix , funcName , processingFunction ) ;
316
+ await traverseDirectory ( nextFile , onlyCollectFunctionData , funcName , processingFunction ) ;
317
+ }
318
+ }
319
+
320
+ function updateFolderTree ( absolutePath ) {
321
+ if ( isPathInside ( queriedPath , absolutePath ) && ! folderStructureTree . find ( fst => fst . absolutePath === absolutePath ) ) {
322
+ let depth = calculateDepth ( queriedPath , absolutePath ) ;
323
+ let prefix = '' ;
324
+ for ( let i = 1 ; i < depth ; i ++ ) {
325
+ prefix += '| ' ;
326
+ }
327
+ folderStructureTree . push ( getFolderTreeItem ( prefix + "├───" , absolutePath ) ) ;
321
328
}
322
329
}
323
330
324
- function updateFolderTree ( prefix , isLast , absolutePath ) {
325
- if ( ! folderStructureTree . find ( fst => fst . absolutePath === absolutePath ) ) {
326
- folderStructureTree . push ( getFolderTreeItem ( prefix , isLast , path . basename ( absolutePath ) , absolutePath ) ) ;
331
+ function sortFolderTree ( ) {
332
+ // 1. Sort the folderStructureTree
333
+ folderStructureTree . sort ( ( a , b ) => {
334
+ if ( a . absolutePath < b . absolutePath ) {
335
+ return - 1 ;
336
+ }
337
+ if ( a . absolutePath > b . absolutePath ) {
338
+ return 1 ;
339
+ }
340
+ return 0 ;
341
+ } ) ;
342
+
343
+ // 2. Set prefix according to the position in the directory
344
+ for ( let i = 0 ; i < folderStructureTree . length ; i ++ ) {
345
+ // Get the current directory path
346
+ const currentDirPath = path . dirname ( folderStructureTree [ i ] . absolutePath ) ;
347
+ // Check if it's the last file in the directory
348
+ if ( i === folderStructureTree . length - 1 || path . dirname ( folderStructureTree [ i + 1 ] . absolutePath ) !== currentDirPath ) {
349
+ // Update the prefix for the last file in the directory
350
+ folderStructureTree [ i ] . line = folderStructureTree [ i ] . line . replace ( "├───" , "└───" ) ;
351
+ }
327
352
}
328
353
}
329
354
@@ -345,14 +370,14 @@ async function generateTestsForDirectory(args, processingFunction = 'getUnitTest
345
370
346
371
API . checkForAPIKey ( ) ;
347
372
queriedPath = path . resolve ( pathToProcess ) ;
348
- rootPath = process . cwd ( ) ;
373
+ rootPath = args . pythagora_root ;
349
374
( { screen, spinner, scrollableContent } = initScreenForUnitTests ( ) ) ;
350
375
351
- await traverseDirectory ( queriedPath , true , undefined , funcName , processingFunction ) ;
376
+ await traverseDirectory ( queriedPath , true , funcName , processingFunction ) ;
352
377
processedFiles = [ ] ;
353
- await traverseDirectory ( queriedPath , true , undefined , funcName , processingFunction ) ;
378
+ await traverseDirectory ( queriedPath , true , funcName , processingFunction ) ;
354
379
processedFiles = [ ] ;
355
- await traverseDirectory ( queriedPath , false , undefined , funcName , processingFunction ) ;
380
+ await traverseDirectory ( queriedPath , false , funcName , processingFunction ) ;
356
381
357
382
screen . destroy ( ) ;
358
383
process . stdout . write ( '\x1B[2J\x1B[0f' ) ;
0 commit comments