@@ -13,7 +13,7 @@ const {
1313 getRelatedFunctions,
1414 getModuleTypeFromFilePath
1515} = require ( "../utils/code" ) ;
16- const { getRelativePath, getFolderTreeItem, getTestFolderPath, checkPathType, isPathInside} = require ( "../utils/files" ) ;
16+ const { getRelativePath, getFolderTreeItem, getTestFolderPath, checkPathType, isPathInside, calculateDepth } = require ( "../utils/files" ) ;
1717const { initScreenForUnitTests} = require ( "./cmdGUI" ) ;
1818const { green, red, blue, bold, reset} = require ( '../utils/cmdPrint' ) . colors ;
1919
@@ -150,7 +150,7 @@ async function reformatDataForPythagoraAPI(funcData, filePath, testFilePath) {
150150 return funcData ;
151151}
152152
153- async function createTests ( filePath , prefix , funcToTest , processingFunction = 'getUnitTests' ) {
153+ async function createTests ( filePath , funcToTest , processingFunction = 'getUnitTests' ) {
154154 try {
155155 let extension = path . extname ( filePath ) ;
156156 let ast = await getAstFromFilePath ( filePath ) ;
@@ -183,18 +183,18 @@ async function createTests(filePath, prefix, funcToTest, processingFunction = 'g
183183 ) )
184184 ) ;
185185
186+ sortFolderTree ( ) ;
187+
186188 for ( const [ i , funcData ] of uniqueFoundFunctions . entries ( ) ) {
187- let isLast = uniqueFoundFunctions . indexOf ( funcData ) === uniqueFoundFunctions . length - 1 ;
188189 let indexToPush = fileIndex + 1 + i ;
190+ let prefix = folderStructureTree [ fileIndex ] . line . split ( path . basename ( folderStructureTree [ fileIndex ] . absolutePath ) ) [ 0 ] ;
189191 folderStructureTree . splice (
190192 indexToPush ,
191193 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+ }
198198 ) ;
199199 spinner . start ( folderStructureTree , indexToPush ) ;
200200
@@ -234,7 +234,7 @@ async function createTests(filePath, prefix, funcToTest, processingFunction = 'g
234234 }
235235
236236 } catch ( e ) {
237- if ( ! ignoreErrors . includes ( e . code ) ) errors . push ( e ) ;
237+ if ( ! ignoreErrors . includes ( e . code ) ) errors . push ( e . stack ) ;
238238 }
239239}
240240
@@ -251,7 +251,7 @@ async function saveTests(filePath, name, testData) {
251251 return testPath ;
252252}
253253
254- async function traverseDirectory ( file , onlyCollectFunctionData , prefix = '' , funcName , processingFunction ) {
254+ async function traverseDirectory ( file , onlyCollectFunctionData , funcName , processingFunction ) {
255255 if ( processedFiles . includes ( file ) ) {
256256 return ;
257257 }
@@ -261,24 +261,21 @@ async function traverseDirectory(file, onlyCollectFunctionData, prefix = '', fun
261261 if ( ! processExtensions . includes ( path . extname ( file ) ) ) {
262262 throw new Error ( 'File extension is not supported' ) ;
263263 }
264- const newPrefix = `| ${ prefix } | ` ;
265- return await createTests ( file , newPrefix , funcName , processingFunction ) ;
264+ return await createTests ( file , funcName , processingFunction ) ;
266265 }
267266
268267 const absolutePath = path . resolve ( file ) ;
269268 const stat = fs . statSync ( absolutePath ) ;
270- const isLast = filesToProcess . length === 0 ;
271269
272270 if ( ignoreFilesEndingWith . some ( ending => file . endsWith ( ending ) ) ) return ;
273271
274272 if ( stat . isDirectory ( ) ) {
275273 if ( ignoreFolders . includes ( path . basename ( absolutePath ) ) || path . basename ( absolutePath ) . charAt ( 0 ) === '.' ) return ;
276274
277275 if ( onlyCollectFunctionData && isPathInside ( path . dirname ( queriedPath ) , absolutePath ) ) {
278- updateFolderTree ( prefix , isLast , absolutePath ) ;
276+ updateFolderTree ( absolutePath ) ;
279277 }
280278
281- const newPrefix = isLast ? `${ prefix } ` : `${ prefix } | ` ;
282279 const directoryFiles = fs . readdirSync ( absolutePath )
283280 . filter ( f => {
284281 const absoluteFilePath = path . join ( absolutePath , f ) ;
@@ -300,12 +297,11 @@ async function traverseDirectory(file, onlyCollectFunctionData, prefix = '', fun
300297
301298 if ( onlyCollectFunctionData ) {
302299 if ( isPathInside ( path . dirname ( queriedPath ) , absolutePath ) ) {
303- updateFolderTree ( prefix , isLast , absolutePath ) ;
300+ updateFolderTree ( absolutePath ) ;
304301 }
305302 await processFile ( absolutePath , filesToProcess ) ;
306303 } else {
307- const newPrefix = isLast ? `| ${ prefix } ` : `| ${ prefix } | ` ;
308- await createTests ( absolutePath , newPrefix , funcName , processingFunction ) ;
304+ await createTests ( absolutePath , funcName , processingFunction ) ;
309305 }
310306 }
311307
@@ -314,13 +310,42 @@ async function traverseDirectory(file, onlyCollectFunctionData, prefix = '', fun
314310 if ( processedFiles . includes ( nextFile ) ) {
315311 continue ; // Skip processing if it has already been processed
316312 }
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 ) ) ;
318325 }
319326}
320327
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+ }
324349 }
325350}
326351
@@ -339,14 +364,14 @@ async function generateTestsForDirectory(args, processingFunction = 'getUnitTest
339364
340365 API . checkForAPIKey ( ) ;
341366 queriedPath = path . resolve ( pathToProcess ) ;
342- rootPath = process . cwd ( ) ;
367+ rootPath = args . pythagora_root ;
343368 ( { screen, spinner, scrollableContent } = initScreenForUnitTests ( ) ) ;
344369
345- await traverseDirectory ( queriedPath , true , undefined , funcName , processingFunction ) ;
370+ await traverseDirectory ( queriedPath , true , funcName , processingFunction ) ;
346371 processedFiles = [ ] ;
347- await traverseDirectory ( queriedPath , true , undefined , funcName , processingFunction ) ;
372+ await traverseDirectory ( queriedPath , true , funcName , processingFunction ) ;
348373 processedFiles = [ ] ;
349- await traverseDirectory ( queriedPath , false , undefined , funcName , processingFunction ) ;
374+ await traverseDirectory ( queriedPath , false , funcName , processingFunction ) ;
350375
351376 screen . destroy ( ) ;
352377 process . stdout . write ( '\x1B[2J\x1B[0f' ) ;
0 commit comments