Skip to content

Commit 788252c

Browse files
committed
fix folder structure tree for loging of unit tests
1 parent bf058ee commit 788252c

File tree

5 files changed

+71
-37
lines changed

5 files changed

+71
-37
lines changed

src/helpers/unitTests.js

Lines changed: 52 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -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");
1717
const {initScreenForUnitTests} = require("./cmdGUI");
1818
const {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');

src/scripts/deleteAllFailed.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ let args = require('../utils/getArgs.js');
88
let metadata = fs.readFileSync(path.resolve(args.pythagora_root, PYTHAGORA_METADATA_DIR, METADATA_FILENAME));
99
metadata = JSON.parse(metadata);
1010
if (!metadata || !metadata.runs || !metadata.runs.length ||
11-
!metadata.runs[metadata.runs.length - 1].failed.length) return logAndExit('Previous test run had no failed tests. Nothing to delete, exiting...', 'log');
11+
!metadata.runs[metadata.runs.length - 1].failed.length) logAndExit('Previous test run had no failed tests. Nothing to delete, exiting...', 'log');
1212

1313
let deleteTests = metadata.runs[metadata.runs.length - 1].failed;
1414
let files = fs.readdirSync(path.resolve(args.pythagora_root, PYTHAGORA_TESTS_DIR));

src/scripts/review.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ const _ = require('lodash');
55
let args = require('../utils/getArgs.js');
66

77
const { PYTHAGORA_TESTS_DIR, PYTHAGORA_METADATA_DIR, REVIEW_DATA_FILENAME } = require('../const/common.js');
8-
const { logChange } = require('../utils/cmdPrint.js');
8+
const { logChange, logAndExit } = require('../utils/cmdPrint.js');
99
const { compareJson, getMetadata } = require('../utils/common.js');
1010

1111
const reviewFilePath = path.resolve(args.pythagora_root, PYTHAGORA_METADATA_DIR, REVIEW_DATA_FILENAME);
1212
const metadata = getMetadata();
1313

14-
if (!fs.existsSync(reviewFilePath)) return console.log('There is no changes stored for review. Please run tests first.');
14+
if (!fs.existsSync(reviewFilePath)) logAndExit('There is no changes stored for review. Please run tests first.');
1515

1616
const data = fs.readFileSync(reviewFilePath);
1717
const changes = JSON.parse(data);

src/utils/code.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ function processAst(ast, cb) {
212212
const expression = path.node.expression;
213213
if (expression && expression.type === 'AssignmentExpression') {
214214
const left = expression.left;
215-
if (left.object.type === 'MemberExpression' &&
215+
if (left.object && left.object.type === 'MemberExpression' &&
216216
left.object.object.name === 'module' &&
217217
left.object.property.name === 'exports') {
218218
if (expression.right.type === 'Identifier') {

src/utils/files.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ function getRelativePath(filePath, referenceFolderPath) {
1717
}
1818

1919

20-
function getFolderTreeItem(prefix, isLast, name, absolutePath) {
20+
function getFolderTreeItem(prefix, absolutePath) {
2121
return {
22-
line: `${prefix}${isLast ? '└───' : '├───'}${name}`,
23-
absolutePath
24-
};
22+
line: `${prefix}${path.basename(absolutePath)}`,
23+
absolutePath: absolutePath
24+
}
2525
}
2626

2727
function isPathInside(basePath, targetPath) {
2828
const relativePath = path.relative(basePath, targetPath);
29-
return !relativePath.startsWith('..') && !path.isAbsolute(relativePath);
29+
return !relativePath || (!relativePath.startsWith('..') && !path.isAbsolute(relativePath));
3030
}
3131

3232
function getTestFolderPath(filePath, rootPath) {
@@ -37,10 +37,19 @@ function getTestFolderPath(filePath, rootPath) {
3737
);
3838
}
3939

40+
function calculateDepth(basePath, targetPath) {
41+
const baseComponents = basePath.split(path.sep);
42+
const targetComponents = targetPath.split(path.sep);
43+
44+
// The depth is the difference in the number of components
45+
return targetComponents.length - baseComponents.length + 1;
46+
}
47+
4048
module.exports = {
4149
checkPathType,
4250
getRelativePath,
4351
getFolderTreeItem,
4452
isPathInside,
45-
getTestFolderPath
53+
getTestFolderPath,
54+
calculateDepth
4655
}

0 commit comments

Comments
 (0)