Skip to content

Commit 9d00fa7

Browse files
committed
fix traverseDirectory() recursion when files are processed multiple times
1 parent a2bf4c6 commit 9d00fa7

File tree

1 file changed

+38
-12
lines changed

1 file changed

+38
-12
lines changed

src/helpers/unitTests.js

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ let functionList = {},
2727
testsGenerated = [],
2828
skippedFiles = [],
2929
errors = [],
30+
filesToProcess = [],
31+
processedFiles = [],
3032
ignoreFolders = ['node_modules', 'pythagora_tests', '__tests__'],
3133
ignoreFilesEndingWith = [".test.js", ".test.ts", ".test.tsx"],
3234
processExtensions = ['.js', '.ts', '.tsx'],
@@ -249,9 +251,16 @@ async function saveTests(filePath, name, testData) {
249251
return testPath;
250252
}
251253

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+
253260
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+
}
255264
const newPrefix = `| ${prefix}| `;
256265
return await createTests(file, newPrefix, funcName, processingFunction);
257266
}
@@ -264,18 +273,30 @@ async function traverseDirectory(file, onlyCollectFunctionData, prefix = '', fun
264273

265274
if (stat.isDirectory()) {
266275
if (ignoreFolders.includes(path.basename(absolutePath)) || path.basename(absolutePath).charAt(0) === '.') return;
267-
console.log(file)
268276

269277
if (onlyCollectFunctionData && isPathInside(path.dirname(queriedPath), absolutePath)) {
270278
updateFolderTree(prefix, isLast, absolutePath);
271279
}
272280

273281
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+
276298
} else {
277299
if (!processExtensions.includes(path.extname(absolutePath))) return;
278-
console.log(file)
279300

280301
if (onlyCollectFunctionData) {
281302
if (isPathInside(path.dirname(queriedPath), absolutePath)) {
@@ -290,7 +311,10 @@ async function traverseDirectory(file, onlyCollectFunctionData, prefix = '', fun
290311

291312
while (filesToProcess.length > 0) {
292313
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);
294318
}
295319
}
296320

@@ -303,6 +327,7 @@ function updateFolderTree(prefix, isLast, absolutePath) {
303327
async function getFunctionsForExport(dirPath) {
304328
rootPath = dirPath;
305329
await traverseDirectory(rootPath, true);
330+
processedFiles = [];
306331
await traverseDirectory(rootPath, true);
307332
return functionList;
308333
}
@@ -317,10 +342,11 @@ async function generateTestsForDirectory(args, processingFunction = 'getUnitTest
317342
rootPath = process.cwd();
318343
({ screen, spinner, scrollableContent } = initScreenForUnitTests());
319344

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);
324350

325351
screen.destroy();
326352
process.stdout.write('\x1B[2J\x1B[0f');
@@ -330,7 +356,7 @@ async function generateTestsForDirectory(args, processingFunction = 'getUnitTest
330356
console.error('There were errors encountered while trying to generate unit tests.\n');
331357
console.error(`You can find logs here: ${errLogPath}`);
332358
}
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}`);
334360
if (testsGenerated.length === 0) {
335361
console.log(`${bold+red}No tests generated!${reset}`);
336362
} else {

0 commit comments

Comments
 (0)