add path solving while searching

add path solving while searching
This commit is contained in:
Luis Tejeda
2022-04-25 12:33:54 -05:00
committed by Erik Mendoza
parent 0f23e60afd
commit c65d80ae05
5 changed files with 59 additions and 61 deletions

View File

@@ -1,5 +1,6 @@
const traverseUtility = require('traverse'),
parse = require('./parse.js'),
BROWSER = 'browser',
{ DFS } = require('./dfs');
let path = require('path'),
pathBrowserify = require('path-browserify');
@@ -36,15 +37,6 @@ function isExtRef(obj, key) {
!stringIsAValidUrl(obj[key]);
}
// /**
// * returns the dirName of the file
// * @param {string} filePath - path to find
// * @returns {string} - the dirName of the path
// */
// function getDirName(filePath) {
// return path.getDirName(filePath);
// }
/**
* verifies if the path has been added to the result
* @param {string} path - path to find
@@ -93,6 +85,18 @@ function comparePaths(path1, path2) {
return path1 === path2; // todo change this comparision
}
/**
* Calculates the path relative to parent
* @param {string} parentFileName - parent file name of the current node
* @param {string} referencePath - value of the $ref property
* @returns {object} - Detect root files result object
*/
function calculatePath(parentFileName, referencePath) {
let currentDirName = path.dirname(parentFileName),
refDirName = path.join(currentDirName, referencePath);
return refDirName;
}
/**
* Locates a referenced node from the data input by path
* @param {string} referencePath - value from the $ref property
@@ -105,25 +109,6 @@ function findNodeFromPath(referencePath, allData) {
});
}
// /**
// * Calculates the path relative to root file
// * @param {object} currentNodeRelativeToRootPath -
// * the relative to root path of the current node.
// * @param {Array} referencePath - the path in the $ref
// * @param {object} specRoot - root file information
// * @returns {object} - Detect root files result object
// */
// function calculatePathToRoot(currentNodeRelativeToRootPath, referencePath) {
// let currentDirName = '',
// refDirName = '';
// if (!currentNodeRelativeToRootPath) {
// currentDirName = getDirName(currentNodeRelativeToRootPath);
// }
// refDirName = path.join(currentDirName, referencePath);
// return refDirName;
// }
/**
* Maps the output from get root files to detect root files
* @param {object} currentNode - current { path, content} object
@@ -138,13 +123,12 @@ function getAdjacentAndMissing (currentNode, allData, specRoot) {
currentNodeReferences.forEach((reference) => {
let referencePath = reference.path,
adjacentNode = findNodeFromPath(referencePath, allData);
adjacentNode = findNodeFromPath(calculatePath(currentNode.fileName, referencePath), allData);
if (adjacentNode) {
adjacentNode.relativeToRootPath = referencePath;
graphAdj.push(adjacentNode);
}
else if (!comparePaths(referencePath, specRoot.path)) {
missingNodes.push({ relativeToRootPath: referencePath });
else if (!comparePaths(referencePath, specRoot.fileName)) {
missingNodes.push({ path: referencePath });
}
});
return { graphAdj, missingNodes };
@@ -156,12 +140,13 @@ module.exports = {
* Maps the output from get root files to detect root files
* @param {object} specRoot - root file information
* @param {Array} allData - array of { path, content} objects
* @param {Array} origin - process origin (BROWSER or node)
* @returns {object} - Detect root files result object
*/
getRelatedFiles: function (specRoot, allData) {
// if (origin === BROWSER) {
// path = pathBrowserify;
// }
getRelatedFiles: function (specRoot, allData, origin) {
if (origin === BROWSER) {
path = pathBrowserify;
}
let algorithm = new DFS(),
{ traverseOrder, missing } =
algorithm.traverse(specRoot, (currentNode) => {
@@ -169,12 +154,12 @@ module.exports = {
}),
outputRelatedFiles = traverseOrder.slice(1).map((relatedFile) => {
return {
relativeToRootPath: relatedFile.relativeToRootPath,
path: relatedFile.fileName
};
});
return { relatedFiles: outputRelatedFiles, missingRelatedFiles: missing };
},
getReferences,
getAdjacentAndMissing
getAdjacentAndMissing,
calculatePath
};

View File

@@ -4845,11 +4845,11 @@ module.exports = {
};
},
mapRootFiles(rootFiles, inputData) {
mapRootFiles(rootFiles, inputData, origin) {
let data = rootFiles.map((root) => {
let { relatedFiles, missingRelatedFiles } = getRelatedFiles(root, inputData);
let { relatedFiles, missingRelatedFiles } = getRelatedFiles(root, inputData, origin);
return {
rootFile: { path: root.path },
rootFile: { path: root.fileName },
relatedFiles,
missingRelatedFiles };
});
@@ -4871,7 +4871,8 @@ module.exports = {
}
};
if (inputRelatedFiles.rootFiles && inputRelatedFiles.rootFiles.length > 0) {
res.output.data = this.mapRootFiles(inputRelatedFiles.rootFiles, inputRelatedFiles.data);
res.output.data = this.mapRootFiles(inputRelatedFiles.rootFiles, inputRelatedFiles.data,
inputRelatedFiles.origin);
}
return res;
}

View File

@@ -670,13 +670,17 @@ class SchemaPack {
rootFiles.output.data.forEach((rootFile) => {
let founInData = input.data.find((dataFile) => { return dataFile.fileName === rootFile.path; });
if (founInData) {
inputContent.push({ path: founInData.fileName, content: founInData.content });
inputContent.push({ fileName: founInData.fileName, content: founInData.content });
}
});
input.rootFiles = inputContent;
return schemaUtils.processRelatedFiles(input);
}
}
let adaptedRootFiles = input.rootFiles.map((rootFile) => {
return { fileName: rootFile.path, content: rootFile.content };
});
input.rootFiles = adaptedRootFiles;
return schemaUtils.processRelatedFiles(input);
}
}

View File

@@ -80,7 +80,7 @@ describe('detectRoot method', function() {
],
data: [
{
path: 'Pet.yaml',
path: '/Pet.yaml',
content: contentFilePet
}
]
@@ -89,9 +89,8 @@ describe('detectRoot method', function() {
expect(res).to.not.be.empty;
expect(res.result).to.be.true;
expect(res.output.data[0].rootFile.path).to.equal('/missedRef.yaml');
expect(res.output.data[0].relatedFiles[0].path).to.equal('Pet.yaml');
expect(res.output.data[0].relatedFiles[0].relativeToRootPath).to.equal('Pet.yaml');
expect(res.output.data[0].missingRelatedFiles[0].relativeToRootPath).to.equal('../common/Error.yaml');
expect(res.output.data[0].relatedFiles[0].path).to.equal('/Pet.yaml');
expect(res.output.data[0].missingRelatedFiles[0].path).to.equal('../common/Error.yaml');
});
@@ -118,9 +117,8 @@ describe('detectRoot method', function() {
expect(res).to.not.be.empty;
expect(res.result).to.be.true;
expect(res.output.data[0].relatedFiles[0].path).to.equal('NewPet.yaml');
expect(res.output.data[0].relatedFiles[0].relativeToRootPath).to.equal('NewPet.yaml');
expect(res.output.data[0].missingRelatedFiles.length).to.equal(1);
expect(res.output.data[0].missingRelatedFiles[0].relativeToRootPath).to.equal('Pet.yaml');
expect(res.output.data[0].missingRelatedFiles[0].path).to.equal('Pet.yaml');
});
@@ -176,9 +174,7 @@ describe('detectRoot method', function() {
expect(res.result).to.be.true;
expect(res.output.data[0].relatedFiles.length).to.equal(2);
expect(res.output.data[0].relatedFiles[0].path).to.equal('oldPet.yaml');
expect(res.output.data[0].relatedFiles[0].relativeToRootPath).to.equal('oldPet.yaml');
expect(res.output.data[0].relatedFiles[1].path).to.equal('Pet.yaml');
expect(res.output.data[0].relatedFiles[1].relativeToRootPath).to.equal('Pet.yaml');
expect(res.output.data[0].missingRelatedFiles.length).to.equal(0);
});

View File

@@ -1,4 +1,5 @@
const { getRelatedFiles, getReferences, getAdjacentAndMissing } = require('./../../lib/relatedFiles'),
const { getRelatedFiles, getReferences, getAdjacentAndMissing,
calculatePath } = require('./../../lib/relatedFiles'),
expect = require('chai').expect,
fs = require('fs'),
path = require('path'),
@@ -21,12 +22,12 @@ describe('getAdjacentAndMissing function', function () {
content: contentFileNewPet
},
inputData = [{
fileName: 'Pet.yaml',
fileName: '/Pet.yaml',
content: contentFilePet
}],
{ graphAdj, missingNodes } = getAdjacentAndMissing(inputNode, inputData, inputNode);
expect(graphAdj.length).to.equal(1);
expect(graphAdj[0].fileName).to.equal('Pet.yaml');
expect(graphAdj[0].fileName).to.equal('/Pet.yaml');
expect(missingNodes.length).to.equal(0);
});
@@ -34,18 +35,18 @@ describe('getAdjacentAndMissing function', function () {
const contentFileMissedRef = fs.readFileSync(missedRef, 'utf8'),
contentFilePet = fs.readFileSync(petstoreSeparatedPet, 'utf8'),
inputNode = {
path: '/missedRef.yaml',
fileName: '/missedRef.yaml',
content: contentFileMissedRef
},
inputData = [{
fileName: 'Pet.yaml',
fileName: '/Pet.yaml',
content: contentFilePet
}],
{ graphAdj, missingNodes } = getAdjacentAndMissing(inputNode, inputData, inputNode);
expect(graphAdj.length).to.equal(1);
expect(graphAdj[0].fileName).to.equal('Pet.yaml');
expect(graphAdj[0].fileName).to.equal('/Pet.yaml');
expect(missingNodes.length).to.equal(1);
expect(missingNodes[0].relativeToRootPath).to.equal('../common/Error.yaml');
expect(missingNodes[0].path).to.equal('../common/Error.yaml');
});
});
@@ -92,14 +93,14 @@ describe('getRelatedFiles function ', function () {
content: contentFileMissedRef
},
inputData = [{
fileName: 'Pet.yaml',
fileName: '/Pet.yaml',
content: contentFilePet
}],
{ relatedFiles, missingRelatedFiles } = getRelatedFiles(rootNode, inputData);
expect(relatedFiles.length).to.equal(1);
expect(relatedFiles[0].path).to.equal('Pet.yaml');
expect(relatedFiles[0].path).to.equal('/Pet.yaml');
expect(missingRelatedFiles.length).to.equal(1);
expect(missingRelatedFiles[0].relativeToRootPath).to.equal('../common/Error.yaml');
expect(missingRelatedFiles[0].path).to.equal('../common/Error.yaml');
});
@@ -116,3 +117,14 @@ describe('getRelatedFiles function ', function () {
});
});
describe('calculatePath function', function () {
it('should return the path from the parent', function () {
const result = calculatePath('sf/newpet.yaml', '../error.yaml');
expect(result).to.equal('error.yaml');
});
it('should return localhost:3000 for entry "http://localhost:3000/projects"', function () {
const result = calculatePath('sf/spec/newpet.yaml', '../common/error.yaml');
expect(result).to.equal('sf/common/error.yaml');
});
});