Skip to content

Commit 241194b

Browse files
Fix expand unit tests functionality
1 parent cc1806d commit 241194b

File tree

8 files changed

+320
-231
lines changed

8 files changed

+320
-231
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pythagora",
3-
"version": "0.0.73",
3+
"version": "0.0.76",
44
"author": {
55
"name": "Zvonimir Sabljic",
66
"email": "[email protected]"

src/bin/postinstall.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,28 @@ const hash = crypto.createHash('sha256');
1515
hash.update(installationDirectory);
1616
const pathId = hash.digest('hex');
1717

18-
// Try to read the config file and get the userId
19-
let userId;
18+
let config;
2019
try {
21-
const config = JSON.parse(fs.readFileSync(configPath));
22-
userId = config.userId;
20+
config = JSON.parse(fs.readFileSync(configPath));
2321
} catch (err) {
2422
// Config file doesn't exist or is not valid JSON
2523
}
2624

25+
if (!config) config = {};
26+
2727
// If there's no userId, generate one and save it to the config file
28-
if (!userId) {
29-
userId = uuidv4();
28+
if (!config.userId) {
29+
config.userId = uuidv4();
3030
const configDir = path.dirname(configPath);
3131
if (!fs.existsSync(configDir)) {
3232
fs.mkdirSync(configDir, { recursive: true });
3333
}
34-
fs.writeFileSync(configPath, JSON.stringify({ userId }, null, 2));
34+
fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
3535
}
3636

3737
// Send the request to the telemetry endpoint
3838
const telemetryData = {
39-
userId,
39+
userId: config.userId,
4040
pathId,
4141
event: 'install',
4242
pythagoraVersion

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

@@ -153,7 +153,7 @@ async function reformatDataForPythagoraAPI(funcData, filePath, testFilePath) {
153153
return funcData;
154154
}
155155

156-
async function createTests(filePath, prefix, funcToTest, processingFunction = 'getUnitTests') {
156+
async function createTests(filePath, funcToTest, processingFunction = 'getUnitTests') {
157157
try {
158158
let extension = path.extname(filePath);
159159
let ast = await getAstFromFilePath(filePath);
@@ -186,18 +186,18 @@ async function createTests(filePath, prefix, funcToTest, processingFunction = 'g
186186
))
187187
);
188188

189+
sortFolderTree();
190+
189191
for (const [i, funcData] of uniqueFoundFunctions.entries()) {
190-
let isLast = uniqueFoundFunctions.indexOf(funcData) === uniqueFoundFunctions.length - 1;
191192
let indexToPush = fileIndex + 1 + i;
193+
let prefix = folderStructureTree[fileIndex].line.split(path.basename(folderStructureTree[fileIndex].absolutePath))[0];
192194
folderStructureTree.splice(
193195
indexToPush,
194196
0,
195-
getFolderTreeItem(
196-
prefix,
197-
isLast,
198-
`${funcData.functionName}.test${extension}`,
199-
filePath + ':' + funcData.functionName
200-
)
197+
{
198+
line: " ".repeat(prefix.length) + "└───" + funcData.functionName,
199+
absolutePath: filePath + ':' + funcData.functionName
200+
}
201201
);
202202
spinner.start(folderStructureTree, indexToPush);
203203

@@ -237,7 +237,7 @@ async function createTests(filePath, prefix, funcToTest, processingFunction = 'g
237237
}
238238

239239
} catch (e) {
240-
if (!ignoreErrors.includes(e.code)) errors.push(e);
240+
if (!ignoreErrors.includes(e.code)) errors.push(e.stack);
241241
}
242242
}
243243

@@ -254,7 +254,7 @@ async function saveTests(filePath, name, testData) {
254254
return testPath;
255255
}
256256

257-
async function traverseDirectory(file, onlyCollectFunctionData, prefix = '', funcName, processingFunction) {
257+
async function traverseDirectory(file, onlyCollectFunctionData, funcName, processingFunction) {
258258
if (processedFiles.includes(file)) {
259259
return;
260260
}
@@ -264,24 +264,21 @@ async function traverseDirectory(file, onlyCollectFunctionData, prefix = '', fun
264264
if (!processExtensions.includes(path.extname(file))) {
265265
throw new Error('File extension is not supported');
266266
}
267-
const newPrefix = `| ${prefix}| `;
268-
return await createTests(file, newPrefix, funcName, processingFunction);
267+
return await createTests(file, funcName, processingFunction);
269268
}
270269

271270
const absolutePath = path.resolve(file);
272271
const stat = fs.statSync(absolutePath);
273-
const isLast = filesToProcess.length === 0;
274272

275273
if (!stat.isDirectory() && isFileToIgnore(file)) return;
276274

277275
if (stat.isDirectory()) {
278276
if (ignoreFolders.includes(path.basename(absolutePath)) || path.basename(absolutePath).charAt(0) === '.') return;
279277

280278
if (onlyCollectFunctionData && isPathInside(path.dirname(queriedPath), absolutePath)) {
281-
updateFolderTree(prefix, isLast, absolutePath);
279+
updateFolderTree(absolutePath);
282280
}
283281

284-
const newPrefix = isLast ? `${prefix} ` : `${prefix}| `;
285282
const directoryFiles = fs.readdirSync(absolutePath)
286283
.filter(f => {
287284
const absoluteFilePath = path.join(absolutePath, f);
@@ -303,12 +300,11 @@ async function traverseDirectory(file, onlyCollectFunctionData, prefix = '', fun
303300

304301
if (onlyCollectFunctionData) {
305302
if (isPathInside(path.dirname(queriedPath), absolutePath)) {
306-
updateFolderTree(prefix, isLast, absolutePath);
303+
updateFolderTree(absolutePath);
307304
}
308305
await processFile(absolutePath, filesToProcess);
309306
} else {
310-
const newPrefix = isLast ? `| ${prefix} ` : `| ${prefix}| `;
311-
await createTests(absolutePath, newPrefix, funcName, processingFunction);
307+
await createTests(absolutePath, funcName, processingFunction);
312308
}
313309
}
314310

@@ -317,13 +313,42 @@ async function traverseDirectory(file, onlyCollectFunctionData, prefix = '', fun
317313
if (processedFiles.includes(nextFile)) {
318314
continue; // Skip processing if it has already been processed
319315
}
320-
await traverseDirectory(nextFile, onlyCollectFunctionData, prefix, funcName, processingFunction);
316+
await traverseDirectory(nextFile, onlyCollectFunctionData, funcName, processingFunction);
317+
}
318+
}
319+
320+
function updateFolderTree(absolutePath) {
321+
if (isPathInside(queriedPath, absolutePath) && !folderStructureTree.find(fst => fst.absolutePath === absolutePath)) {
322+
let depth = calculateDepth(queriedPath, absolutePath);
323+
let prefix = '';
324+
for (let i = 1; i < depth; i++) {
325+
prefix += '| ';
326+
}
327+
folderStructureTree.push(getFolderTreeItem(prefix + "├───", absolutePath));
321328
}
322329
}
323330

324-
function updateFolderTree(prefix, isLast, absolutePath) {
325-
if (!folderStructureTree.find(fst => fst.absolutePath === absolutePath)) {
326-
folderStructureTree.push(getFolderTreeItem(prefix, isLast, path.basename(absolutePath), absolutePath));
331+
function sortFolderTree() {
332+
// 1. Sort the folderStructureTree
333+
folderStructureTree.sort((a, b) => {
334+
if (a.absolutePath < b.absolutePath) {
335+
return -1;
336+
}
337+
if (a.absolutePath > b.absolutePath) {
338+
return 1;
339+
}
340+
return 0;
341+
});
342+
343+
// 2. Set prefix according to the position in the directory
344+
for (let i = 0; i < folderStructureTree.length; i++) {
345+
// Get the current directory path
346+
const currentDirPath = path.dirname(folderStructureTree[i].absolutePath);
347+
// Check if it's the last file in the directory
348+
if (i === folderStructureTree.length - 1 || path.dirname(folderStructureTree[i + 1].absolutePath) !== currentDirPath) {
349+
// Update the prefix for the last file in the directory
350+
folderStructureTree[i].line = folderStructureTree[i].line.replace("├───", "└───");
351+
}
327352
}
328353
}
329354

@@ -345,14 +370,14 @@ async function generateTestsForDirectory(args, processingFunction = 'getUnitTest
345370

346371
API.checkForAPIKey();
347372
queriedPath = path.resolve(pathToProcess);
348-
rootPath = process.cwd();
373+
rootPath = args.pythagora_root;
349374
({ screen, spinner, scrollableContent } = initScreenForUnitTests());
350375

351-
await traverseDirectory(queriedPath, true, undefined, funcName, processingFunction);
376+
await traverseDirectory(queriedPath, true, funcName, processingFunction);
352377
processedFiles = [];
353-
await traverseDirectory(queriedPath, true, undefined, funcName, processingFunction);
378+
await traverseDirectory(queriedPath, true, funcName, processingFunction);
354379
processedFiles = [];
355-
await traverseDirectory(queriedPath, false, undefined, funcName, processingFunction);
380+
await traverseDirectory(queriedPath, false, funcName, processingFunction);
356381

357382
screen.destroy();
358383
process.stdout.write('\x1B[2J\x1B[0f');

0 commit comments

Comments
 (0)