Skip to content

Commit 9f329a5

Browse files
author
Zvonimir Sabljic
committed
Refactored the way we determine the relative path from the test file to the .js file so we can remove 'brija'
1 parent ef34fbc commit 9f329a5

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

src/helpers/unitTests.js

Lines changed: 9 additions & 9 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, getTestFilePath, checkPathType, isPathInside} = require("../utils/files");
16+
const {getRelativePath, getFolderTreeItem, getTestFolderPath, checkPathType, isPathInside} = require("../utils/files");
1717
const {initScreenForUnitTests} = require("./cmdGUI");
1818
const {green, red, blue, bold, reset} = require('../utils/cmdPrint').colors;
1919

@@ -94,23 +94,23 @@ async function reformatDataForPythagoraAPI(funcData, filePath, testFilePath) {
9494
if (file.fileName === filePath) {
9595
relatedCodeInSameFile = relatedCodeInSameFile.concat(file.functionNames);
9696
} else {
97-
let fileName = getRelativePath(file.fileName, filePath);
97+
let fileName = getRelativePath(file.fileName, path.dirname(filePath));
9898
let code = await stripUnrelatedFunctions(file.fileName, file.functionNames);
9999
let fullPath = filePath.substring(0, filePath.lastIndexOf('/')) + '/' + fileName;
100-
code = replaceRequirePaths(code, filePath, path.resolve(PYTHAGORA_UNIT_DIR) + '/brija.test.js');
100+
code = replaceRequirePaths(code, filePath, getTestFolderPath(filePath, rootPath));
101101
funcData.relatedCode.push({
102102
fileName,
103103
code,
104104
functionNames: file.functionNames,
105105
exportedAsObject: file.exportedAsObject,
106106
syntaxType: file.syntaxType,
107-
pathRelativeToTest: getRelativePath(fullPath, testFilePath + '/brija.test.js')
107+
pathRelativeToTest: getRelativePath(fullPath, testFilePath)
108108
});
109109
}
110110
}
111111
funcData.functionCode = await stripUnrelatedFunctions(filePath, relatedCodeInSameFile);
112-
funcData.functionCode = replaceRequirePaths(funcData.functionCode, path.dirname(filePath), path.resolve(PYTHAGORA_UNIT_DIR) + '/brija.test.js');
113-
funcData.pathRelativeToTest = getRelativePath(filePath, testFilePath + '/brija.test.js');
112+
funcData.functionCode = replaceRequirePaths(funcData.functionCode, path.dirname(filePath), getTestFolderPath(filePath, rootPath));
113+
funcData.pathRelativeToTest = getRelativePath(filePath, testFilePath);
114114
return funcData;
115115
}
116116

@@ -155,14 +155,14 @@ async function createTests(filePath, prefix, funcToTest) {
155155
);
156156
spinner.start(folderStructureTree, indexToPush);
157157

158-
let testFilePath = path.join(getTestFilePath(filePath, rootPath), `/${funcData.functionName}.test.js`);
158+
let testFilePath = path.join(getTestFolderPath(filePath, rootPath), `/${funcData.functionName}.test.js`);
159159
if (fs.existsSync(testFilePath)) {
160160
await spinner.stop();
161161
folderStructureTree[indexToPush].line = `${green}${folderStructureTree[indexToPush].line}${reset}`;
162162
continue;
163163
}
164164

165-
let formattedData = await reformatDataForPythagoraAPI(funcData, filePath, getTestFilePath(filePath, rootPath));
165+
let formattedData = await reformatDataForPythagoraAPI(funcData, filePath, getTestFolderPath(filePath, rootPath));
166166
let { tests, error } = await getUnitTests(formattedData, (content) => {
167167
scrollableContent.setContent(content);
168168
scrollableContent.setScrollPerc(100);
@@ -196,7 +196,7 @@ async function createTests(filePath, prefix, funcToTest) {
196196
}
197197

198198
async function saveTests(filePath, name, testData) {
199-
let dir = getTestFilePath(filePath, rootPath);
199+
let dir = getTestFolderPath(filePath, rootPath);
200200

201201
if (!await checkDirectoryExists(dir)) {
202202
fs.mkdirSync(dir, { recursive: true });

src/utils/files.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ async function checkPathType(path) {
88
return stats.isFile() ? 'file' : 'directory';
99
}
1010

11-
function getRelativePath(filePath, referenceFilePath) {
12-
let relativePath = path.relative(path.dirname(referenceFilePath), filePath);
11+
function getRelativePath(filePath, referenceFolderPath) {
12+
let relativePath = path.relative(path.resolve(referenceFolderPath), filePath);
1313
if (!relativePath.startsWith('../') && !relativePath.startsWith('./')) {
1414
relativePath = './' + relativePath;
1515
}
@@ -29,7 +29,7 @@ function isPathInside(basePath, targetPath) {
2929
return !relativePath.startsWith('..') && !path.isAbsolute(relativePath);
3030
}
3131

32-
function getTestFilePath(filePath, rootPath) {
32+
function getTestFolderPath(filePath, rootPath) {
3333
return path.join(
3434
path.resolve(PYTHAGORA_UNIT_DIR),
3535
path.dirname(filePath).replace(rootPath, ''),
@@ -42,5 +42,5 @@ module.exports = {
4242
getRelativePath,
4343
getFolderTreeItem,
4444
isPathInside,
45-
getTestFilePath
45+
getTestFolderPath
4646
}

0 commit comments

Comments
 (0)