Skip to content

Commit 1352ffc

Browse files
committed
[INTERNAL] Static translator: Ensure that all project paths are absolute
1 parent 078cf64 commit 1352ffc

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

lib/translators/static.js

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
const path = require("path");
22
const fs = require("graceful-fs");
3+
const {promisify} = require("util");
4+
const readFile = promisify(fs.readFile);
35
const parseYaml = require("js-yaml").safeLoad;
46

7+
function resolveProjectPaths(project) {
8+
project.path = path.resolve(project.path);
9+
if (project.dependencies) {
10+
project.dependencies.forEach(resolveProjectPaths);
11+
}
12+
return project;
13+
}
14+
515
/**
616
* Translator for static resources
717
*
@@ -21,22 +31,22 @@ module.exports = {
2131
* @param {Array} [options.parameters] CLI configuration options
2232
* @returns {Promise} Promise resolving with a dependency tree
2333
*/
24-
generateDependencyTree(dirPath, options = {}) {
34+
async generateDependencyTree(dirPath, options = {}) {
2535
const depFilePath = options.parameters && options.parameters[0] ||
2636
path.join(dirPath, "projectDependencies.yaml");
27-
28-
return new Promise(function(resolve, reject) {
29-
fs.readFile(depFilePath, function(err, buffer) {
30-
if (err) {
31-
reject(new Error(
32-
`[static translator] Failed to locate projectDependencies.json at path: "${dirPath}" `+
33-
`- Error: ${err.message}`));
34-
} else {
35-
resolve(parseYaml(buffer.toString(), {
36-
filename: depFilePath
37-
}));
38-
}
37+
try {
38+
const buffer = await readFile(depFilePath);
39+
const tree = parseYaml(buffer.toString(), {
40+
filename: depFilePath
3941
});
40-
});
42+
43+
// Ensure that all project paths are absolute
44+
resolveProjectPaths(tree);
45+
return tree;
46+
} catch (err) {
47+
throw new Error(
48+
`[static translator] Failed to load dependency tree from path ${depFilePath} `+
49+
`- Error: ${err.message}`);
50+
}
4151
}
4252
};

test/lib/translators/static.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ test("Generates dependency tree for project with projectDependencies.yaml", (t)
1212
});
1313
});
1414

15-
1615
test("Error: Throws if projectDependencies.yaml was not found", async (t) => {
1716
const projectPath = "notExistingPath";
1817
const fsError = new Error("File not found");
1918
const fsStub = sinon.stub(fs, "readFile");
2019
fsStub.callsArgWith(1, fsError);
2120
const error = await t.throwsAsync(staticTranslator.generateDependencyTree(projectPath));
22-
t.is(error.message, `[static translator] Failed to locate projectDependencies.json at path: "${projectPath}" - Error: ${fsError.message}`);
21+
t.regex(error.message,
22+
new RegExp("\\[static translator\\] Failed to load dependency tree from path " +
23+
"notExistingPath\\/projectDependencies\\.yaml - Error: ENOENT:"));
2324
fsStub.restore();
2425
});
2526

@@ -28,17 +29,17 @@ const expectedTree = {
2829
version: "0.0.1",
2930
description: "Sample App",
3031
main: "index.html",
31-
path: "./",
32+
path: path.resolve("./"),
3233
dependencies: [
3334
{
3435
id: "sap.f",
3536
version: "1.56.1",
36-
path: "../sap.f"
37+
path: path.resolve("../sap.f")
3738
},
3839
{
3940
id: "sap.m",
4041
version: "1.61.0",
41-
path: "../sap.m"
42+
path: path.resolve("../sap.m")
4243
}
4344
]
4445
};

0 commit comments

Comments
 (0)