@@ -27,6 +27,8 @@ let functionList = {},
27
27
testsGenerated = [ ] ,
28
28
skippedFiles = [ ] ,
29
29
errors = [ ] ,
30
+ filesToProcess = [ ] ,
31
+ processedFiles = [ ] ,
30
32
ignoreFolders = [ 'node_modules' , 'pythagora_tests' , '__tests__' ] ,
31
33
ignoreFilesEndingWith = [ ".test.js" , ".test.ts" , ".test.tsx" ] ,
32
34
processExtensions = [ '.js' , '.ts' , '.tsx' ] ,
@@ -249,9 +251,16 @@ async function saveTests(filePath, name, testData) {
249
251
return testPath ;
250
252
}
251
253
252
- async function traverseDirectory ( file , onlyCollectFunctionData , prefix = '' , funcName , filesToProcess = [ file ] , processingFunction ) {
254
+ async function traverseDirectory ( file , onlyCollectFunctionData , prefix = '' , funcName , processingFunction ) {
255
+ if ( processedFiles . includes ( file ) ) {
256
+ return ;
257
+ }
258
+ processedFiles . push ( file ) ;
259
+
253
260
if ( await checkPathType ( file ) === 'file' && ! onlyCollectFunctionData ) {
254
- if ( ! processExtensions . includes ( path . extname ( file ) ) ) throw new Error ( 'File extension is not supported' ) ;
261
+ if ( ! processExtensions . includes ( path . extname ( file ) ) ) {
262
+ throw new Error ( 'File extension is not supported' ) ;
263
+ }
255
264
const newPrefix = `| ${ prefix } | ` ;
256
265
return await createTests ( file , newPrefix , funcName , processingFunction ) ;
257
266
}
@@ -264,18 +273,30 @@ async function traverseDirectory(file, onlyCollectFunctionData, prefix = '', fun
264
273
265
274
if ( stat . isDirectory ( ) ) {
266
275
if ( ignoreFolders . includes ( path . basename ( absolutePath ) ) || path . basename ( absolutePath ) . charAt ( 0 ) === '.' ) return ;
267
- console . log ( file )
268
276
269
277
if ( onlyCollectFunctionData && isPathInside ( path . dirname ( queriedPath ) , absolutePath ) ) {
270
278
updateFolderTree ( prefix , isLast , absolutePath ) ;
271
279
}
272
280
273
281
const newPrefix = isLast ? `${ prefix } ` : `${ prefix } | ` ;
274
- const directoryFiles = fs . readdirSync ( absolutePath ) ;
275
- filesToProcess . push ( ...directoryFiles . map ( f => path . join ( absolutePath , f ) ) ) ;
282
+ const directoryFiles = fs . readdirSync ( absolutePath )
283
+ . filter ( f => {
284
+ const absoluteFilePath = path . join ( absolutePath , f ) ;
285
+ const fileStat = fs . statSync ( absoluteFilePath ) ;
286
+ if ( fileStat . isDirectory ( ) ) {
287
+ const baseName = path . basename ( absoluteFilePath ) ;
288
+ return ! ignoreFolders . includes ( baseName ) && ! baseName . startsWith ( '.' ) ;
289
+ } else {
290
+ const ext = path . extname ( f ) ;
291
+ return processExtensions . includes ( ext ) && ! ignoreFilesEndingWith . some ( ending => f . endsWith ( ending ) ) ;
292
+ }
293
+ } )
294
+ . map ( f => path . join ( absolutePath , f ) ) ;
295
+ filesToProcess . push ( ...directoryFiles ) ;
296
+
297
+
276
298
} else {
277
299
if ( ! processExtensions . includes ( path . extname ( absolutePath ) ) ) return ;
278
- console . log ( file )
279
300
280
301
if ( onlyCollectFunctionData ) {
281
302
if ( isPathInside ( path . dirname ( queriedPath ) , absolutePath ) ) {
@@ -290,7 +311,10 @@ async function traverseDirectory(file, onlyCollectFunctionData, prefix = '', fun
290
311
291
312
while ( filesToProcess . length > 0 ) {
292
313
const nextFile = filesToProcess . shift ( ) ;
293
- await traverseDirectory ( nextFile , onlyCollectFunctionData , prefix , funcName , filesToProcess , processingFunction ) ;
314
+ if ( processedFiles . includes ( nextFile ) ) {
315
+ continue ; // Skip processing if it has already been processed
316
+ }
317
+ await traverseDirectory ( nextFile , onlyCollectFunctionData , prefix , funcName , processingFunction ) ;
294
318
}
295
319
}
296
320
@@ -303,6 +327,7 @@ function updateFolderTree(prefix, isLast, absolutePath) {
303
327
async function getFunctionsForExport ( dirPath ) {
304
328
rootPath = dirPath ;
305
329
await traverseDirectory ( rootPath , true ) ;
330
+ processedFiles = [ ] ;
306
331
await traverseDirectory ( rootPath , true ) ;
307
332
return functionList ;
308
333
}
@@ -317,10 +342,11 @@ async function generateTestsForDirectory(args, processingFunction = 'getUnitTest
317
342
rootPath = process . cwd ( ) ;
318
343
( { screen, spinner, scrollableContent } = initScreenForUnitTests ( ) ) ;
319
344
320
- let filesToProcess = [ ] ;
321
- await traverseDirectory ( queriedPath , true , undefined , funcName , filesToProcess , processingFunction ) ;
322
- await traverseDirectory ( queriedPath , true , undefined , funcName , filesToProcess , processingFunction ) ;
323
- await traverseDirectory ( queriedPath , false , undefined , funcName , filesToProcess , processingFunction ) ;
345
+ await traverseDirectory ( queriedPath , true , undefined , funcName , processingFunction ) ;
346
+ processedFiles = [ ] ;
347
+ await traverseDirectory ( queriedPath , true , undefined , funcName , processingFunction ) ;
348
+ processedFiles = [ ] ;
349
+ await traverseDirectory ( queriedPath , false , undefined , funcName , processingFunction ) ;
324
350
325
351
screen . destroy ( ) ;
326
352
process . stdout . write ( '\x1B[2J\x1B[0f' ) ;
@@ -330,7 +356,7 @@ async function generateTestsForDirectory(args, processingFunction = 'getUnitTest
330
356
console . error ( 'There were errors encountered while trying to generate unit tests.\n' ) ;
331
357
console . error ( `You can find logs here: ${ errLogPath } ` ) ;
332
358
}
333
- if ( skippedFiles . length ) console . log ( `${ bold } ${ skippedFiles . length } files were skipped because tests already exist. If you want to override them add "--force" flag to command${ reset } ` ) ;
359
+ if ( skippedFiles . length ) console . log ( `${ bold } Generation of ${ skippedFiles . length } test suites were skipped because tests already exist. If you want to override them add "--force" flag to command${ reset } ` ) ;
334
360
if ( testsGenerated . length === 0 ) {
335
361
console . log ( `${ bold + red } No tests generated!${ reset } ` ) ;
336
362
} else {
0 commit comments