Skip to content

Commit b6a5565

Browse files
committed
refactor makeRequest(), add support for unit tests when function is defined inside module.exports
1 parent 33b3c90 commit b6a5565

File tree

7 files changed

+27
-22
lines changed

7 files changed

+27
-22
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.63",
3+
"version": "0.0.65",
44
"author": {
55
"name": "Zvonimir Sabljic",
66
"email": "[email protected]"

src/helpers/api.js

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -51,32 +51,23 @@ async function makeRequest(data, options, customLogFunction) {
5151
let stringified = chunk.toString();
5252
try {
5353
let json = JSON.parse(stringified);
54-
if (json.error || json.message) gptResponse = json;
55-
return;
54+
if (json.error || json.message) {
55+
gptResponse = json;
56+
return;
57+
}
5658
} catch (e) {}
5759

58-
if (!stringified.includes('pythagora_end:')) {
59-
let receivedMessages = extractGPTMessageFromStreamData(stringified);
60-
receivedMessages.forEach(rm => {
61-
let content = _.get(rm, 'choices.0.delta.content');
62-
if (content) {
63-
gptResponse += content;
64-
if (customLogFunction) customLogFunction(gptResponse);
65-
else process.stdout.write(content);
66-
}
67-
});
68-
} else {
69-
gptResponse += stringified;
70-
if (customLogFunction) customLogFunction(gptResponse);
71-
else process.stdout.write(stringified);
72-
}
60+
gptResponse += stringified;
61+
if (customLogFunction) customLogFunction(gptResponse);
62+
else process.stdout.write(stringified);
7363
} catch (e) {}
7464
});
7565
res.on('end', async function () {
7666
process.stdout.write('\n');
7767
if (res.statusCode >= 400) throw new Error(`Response status code: ${res.statusCode}. Error message: ${gptResponse}`);
68+
if (gptResponse.error) throw new Error(`Error: ${gptResponse.error.message}. Code: ${gptResponse.error.code}`);
7869
if (gptResponse.message) throw new Error(`Error: ${gptResponse.message}. Code: ${gptResponse.code}`);
79-
gptResponse = cleanupGPTResponse(gptResponse.split('pythagora_end:').pop());
70+
gptResponse = cleanupGPTResponse(gptResponse);
8071
resolve(gptResponse);
8172
});
8273
});

src/helpers/unitTests.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ async function processFile(filePath) {
3838
let ast = await getAstFromFilePath(filePath);
3939
let syntaxType = await getModuleTypeFromFilePath(ast);
4040
processAst(ast, (funcName, path, type) => {
41+
if (type === 'exportFnDef') exportsFn.push(funcName);
4142
if (type === 'exportFn') {
4243
exportsFn.push(funcName);
4344
} else if (type === 'exportObj') {

src/scripts/testsEligibleForExport.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const {getAllGeneratedTests} = require("../utils/common");
44
const {convertOldTestForGPT} = require("../utils/legacy");
55
const {testEligibleForExportLog} = require("../utils/cmdPrint");
66
const {isEligibleForExport} = require("../helpers/api");
7-
const args = require("../utils/argumentsCheck");
7+
const args = require("../utils/getArgs");
88
const {getFunctionsForExport} = require("../helpers/unitTests");
99

1010

src/scripts/unit.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const {generateTestsForDirectory} = require("../helpers/unitTests");
2-
let args = require('../utils/argumentsCheck.js');
2+
let args = require('../utils/getArgs.js');
33

44
if (!args.path && !args.func) {
55
console.log("Please provide a path or a function name to test");

src/utils/cmdPrint.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
let { cutWithDots, compareJson, compareJsonDetailed } = require('./common');
22
let pythagoraErrors = require('../const/errors');
3-
let args = require('../utils/argumentsCheck.js');
3+
let args = require('../utils/getArgs.js');
44
let { PYTHAGORA_DELIMITER } = require('../const/common');
55

66
let red = '\x1b[31m',

src/utils/code.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,19 @@ function processAst(ast, cb) {
211211
if (expression.right.type === 'Identifier') {
212212
return cb(expression.right.name, null, 'exportFn');
213213
}
214+
// When module.exports is set to an anonymous function
215+
else if (expression.right.type === 'FunctionExpression') {
216+
let funcName;
217+
if (expression.right.id) {
218+
funcName = expression.right.id.name;
219+
} else {
220+
// If function is anonymous, we will generate a name
221+
// based on the file name, line and column number
222+
const loc = path.node.loc.start;
223+
funcName = `anon_func_${loc.line}_${loc.column}`;
224+
}
225+
return cb(funcName, path, 'exportFnDef');
226+
}
214227
// When module.exports is set to an object containing multiple functions
215228
else if (expression.right.type === 'ObjectExpression') {
216229
expression.right.properties.forEach(prop => {

0 commit comments

Comments
 (0)