@@ -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
@@ -150,7 +150,7 @@ async function reformatDataForPythagoraAPI(funcData, filePath, testFilePath) {
150
150
return funcData ;
151
151
}
152
152
153
- async function createTests ( filePath , prefix , funcToTest , processingFunction = 'getUnitTests' ) {
153
+ async function createTests ( filePath , funcToTest , processingFunction = 'getUnitTests' ) {
154
154
try {
155
155
let extension = path . extname ( filePath ) ;
156
156
let ast = await getAstFromFilePath ( filePath ) ;
@@ -183,18 +183,18 @@ async function createTests(filePath, prefix, funcToTest, processingFunction = 'g
183
183
) )
184
184
) ;
185
185
186
+ sortFolderTree ( ) ;
187
+
186
188
for ( const [ i , funcData ] of uniqueFoundFunctions . entries ( ) ) {
187
- let isLast = uniqueFoundFunctions . indexOf ( funcData ) === uniqueFoundFunctions . length - 1 ;
188
189
let indexToPush = fileIndex + 1 + i ;
190
+ let prefix = folderStructureTree [ fileIndex ] . line . split ( path . basename ( folderStructureTree [ fileIndex ] . absolutePath ) ) [ 0 ] ;
189
191
folderStructureTree . splice (
190
192
indexToPush ,
191
193
0 ,
192
- getFolderTreeItem (
193
- prefix ,
194
- isLast ,
195
- `${ funcData . functionName } .test${ extension } ` ,
196
- filePath + ':' + funcData . functionName
197
- )
194
+ {
195
+ line : " " . repeat ( prefix . length ) + "└───" + funcData . functionName ,
196
+ absolutePath : filePath + ':' + funcData . functionName
197
+ }
198
198
) ;
199
199
spinner . start ( folderStructureTree , indexToPush ) ;
200
200
@@ -234,7 +234,7 @@ async function createTests(filePath, prefix, funcToTest, processingFunction = 'g
234
234
}
235
235
236
236
} catch ( e ) {
237
- if ( ! ignoreErrors . includes ( e . code ) ) errors . push ( e ) ;
237
+ if ( ! ignoreErrors . includes ( e . code ) ) errors . push ( e . stack ) ;
238
238
}
239
239
}
240
240
@@ -251,7 +251,7 @@ async function saveTests(filePath, name, testData) {
251
251
return testPath ;
252
252
}
253
253
254
- async function traverseDirectory ( file , onlyCollectFunctionData , prefix = '' , funcName , processingFunction ) {
254
+ async function traverseDirectory ( file , onlyCollectFunctionData , funcName , processingFunction ) {
255
255
if ( processedFiles . includes ( file ) ) {
256
256
return ;
257
257
}
@@ -261,24 +261,21 @@ async function traverseDirectory(file, onlyCollectFunctionData, prefix = '', fun
261
261
if ( ! processExtensions . includes ( path . extname ( file ) ) ) {
262
262
throw new Error ( 'File extension is not supported' ) ;
263
263
}
264
- const newPrefix = `| ${ prefix } | ` ;
265
- return await createTests ( file , newPrefix , funcName , processingFunction ) ;
264
+ return await createTests ( file , funcName , processingFunction ) ;
266
265
}
267
266
268
267
const absolutePath = path . resolve ( file ) ;
269
268
const stat = fs . statSync ( absolutePath ) ;
270
- const isLast = filesToProcess . length === 0 ;
271
269
272
270
if ( ignoreFilesEndingWith . some ( ending => file . endsWith ( ending ) ) ) return ;
273
271
274
272
if ( stat . isDirectory ( ) ) {
275
273
if ( ignoreFolders . includes ( path . basename ( absolutePath ) ) || path . basename ( absolutePath ) . charAt ( 0 ) === '.' ) return ;
276
274
277
275
if ( onlyCollectFunctionData && isPathInside ( path . dirname ( queriedPath ) , absolutePath ) ) {
278
- updateFolderTree ( prefix , isLast , absolutePath ) ;
276
+ updateFolderTree ( absolutePath ) ;
279
277
}
280
278
281
- const newPrefix = isLast ? `${ prefix } ` : `${ prefix } | ` ;
282
279
const directoryFiles = fs . readdirSync ( absolutePath )
283
280
. filter ( f => {
284
281
const absoluteFilePath = path . join ( absolutePath , f ) ;
@@ -300,12 +297,11 @@ async function traverseDirectory(file, onlyCollectFunctionData, prefix = '', fun
300
297
301
298
if ( onlyCollectFunctionData ) {
302
299
if ( isPathInside ( path . dirname ( queriedPath ) , absolutePath ) ) {
303
- updateFolderTree ( prefix , isLast , absolutePath ) ;
300
+ updateFolderTree ( absolutePath ) ;
304
301
}
305
302
await processFile ( absolutePath , filesToProcess ) ;
306
303
} else {
307
- const newPrefix = isLast ? `| ${ prefix } ` : `| ${ prefix } | ` ;
308
- await createTests ( absolutePath , newPrefix , funcName , processingFunction ) ;
304
+ await createTests ( absolutePath , funcName , processingFunction ) ;
309
305
}
310
306
}
311
307
@@ -314,13 +310,42 @@ async function traverseDirectory(file, onlyCollectFunctionData, prefix = '', fun
314
310
if ( processedFiles . includes ( nextFile ) ) {
315
311
continue ; // Skip processing if it has already been processed
316
312
}
317
- await traverseDirectory ( nextFile , onlyCollectFunctionData , prefix , funcName , processingFunction ) ;
313
+ await traverseDirectory ( nextFile , onlyCollectFunctionData , funcName , processingFunction ) ;
314
+ }
315
+ }
316
+
317
+ function updateFolderTree ( absolutePath ) {
318
+ if ( isPathInside ( queriedPath , absolutePath ) && ! folderStructureTree . find ( fst => fst . absolutePath === absolutePath ) ) {
319
+ let depth = calculateDepth ( queriedPath , absolutePath ) ;
320
+ let prefix = '' ;
321
+ for ( let i = 1 ; i < depth ; i ++ ) {
322
+ prefix += '| ' ;
323
+ }
324
+ folderStructureTree . push ( getFolderTreeItem ( prefix + "├───" , absolutePath ) ) ;
318
325
}
319
326
}
320
327
321
- function updateFolderTree ( prefix , isLast , absolutePath ) {
322
- if ( ! folderStructureTree . find ( fst => fst . absolutePath === absolutePath ) ) {
323
- folderStructureTree . push ( getFolderTreeItem ( prefix , isLast , path . basename ( absolutePath ) , absolutePath ) ) ;
328
+ function sortFolderTree ( ) {
329
+ // 1. Sort the folderStructureTree
330
+ folderStructureTree . sort ( ( a , b ) => {
331
+ if ( a . absolutePath < b . absolutePath ) {
332
+ return - 1 ;
333
+ }
334
+ if ( a . absolutePath > b . absolutePath ) {
335
+ return 1 ;
336
+ }
337
+ return 0 ;
338
+ } ) ;
339
+
340
+ // 2. Set prefix according to the position in the directory
341
+ for ( let i = 0 ; i < folderStructureTree . length ; i ++ ) {
342
+ // Get the current directory path
343
+ const currentDirPath = path . dirname ( folderStructureTree [ i ] . absolutePath ) ;
344
+ // Check if it's the last file in the directory
345
+ if ( i === folderStructureTree . length - 1 || path . dirname ( folderStructureTree [ i + 1 ] . absolutePath ) !== currentDirPath ) {
346
+ // Update the prefix for the last file in the directory
347
+ folderStructureTree [ i ] . line = folderStructureTree [ i ] . line . replace ( "├───" , "└───" ) ;
348
+ }
324
349
}
325
350
}
326
351
@@ -339,14 +364,14 @@ async function generateTestsForDirectory(args, processingFunction = 'getUnitTest
339
364
340
365
API . checkForAPIKey ( ) ;
341
366
queriedPath = path . resolve ( pathToProcess ) ;
342
- rootPath = process . cwd ( ) ;
367
+ rootPath = args . pythagora_root ;
343
368
( { screen, spinner, scrollableContent } = initScreenForUnitTests ( ) ) ;
344
369
345
- await traverseDirectory ( queriedPath , true , undefined , funcName , processingFunction ) ;
370
+ await traverseDirectory ( queriedPath , true , funcName , processingFunction ) ;
346
371
processedFiles = [ ] ;
347
- await traverseDirectory ( queriedPath , true , undefined , funcName , processingFunction ) ;
372
+ await traverseDirectory ( queriedPath , true , funcName , processingFunction ) ;
348
373
processedFiles = [ ] ;
349
- await traverseDirectory ( queriedPath , false , undefined , funcName , processingFunction ) ;
374
+ await traverseDirectory ( queriedPath , false , funcName , processingFunction ) ;
350
375
351
376
screen . destroy ( ) ;
352
377
process . stdout . write ( '\x1B[2J\x1B[0f' ) ;
0 commit comments