Skip to content

Commit

Permalink
[FIX] npm t8r: Handle npm optionalDependencies correctly
Browse files Browse the repository at this point in the history
In the past, optionalDependencies that could not be located caused a
"Failed to locate module".

Fixes #51
  • Loading branch information
RandomByte committed Nov 16, 2018
1 parent 45a25c2 commit da707d7
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/translators/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,26 @@ class NpmTranslator {

let dependencies = pkg.dependencies || {};
let optDependencies = pkg.devDependencies || {};

// Due to normalization done by by the "read-pkg-up" module the values
// in optionalDependencies get added to dependencies. Also as described here:
// https://github.com/npm/normalize-package-data#what-normalization-currently-entails
// Note: optionalDependencies are treated the same as devDependencies in the npm translator
if (pkg.optionalDependencies) {
for (let depName in pkg.optionalDependencies) {
if (pkg.optionalDependencies.hasOwnProperty(depName)) {
// Remove entry from "hard" dependencies map
if (dependencies[depName]) {
delete dependencies[depName];
}
// Add to optional dependencies map (of not already done)
if (optDependencies.hasOwnProperty(depName)) {
optDependencies[depName] = pkg.optionalDependencies[depName];
}
}
}
}

const version = pkg.version;

// Also look for "napa" dependencies (see https://github.com/shama/napa)
Expand Down
12 changes: 12 additions & 0 deletions test/fixtures/application.g/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "application.g",
"version": "1.0.0",
"description": "Simple SAPUI5 based application - test for npm optionalDependencies",
"main": "index.html",
"optionalDependencies": {
"library.nonexistent": "file:../library.nonexistent"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
}
}
5 changes: 5 additions & 0 deletions test/fixtures/application.g/ui5.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
specVersion: "0.1"
type: application
metadata:
name: application.g
13 changes: 13 additions & 0 deletions test/fixtures/application.g/webapp/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"_version": "1.1.0",
"sap.app": {
"_version": "1.1.0",
"id": "id1",
"type": "application",
"applicationVersion": {
"version": "1.2.2"
},
"embeds": ["embedded"],
"title": "{{title}}"
}
}
14 changes: 14 additions & 0 deletions test/lib/translators/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const applicationC2Path = path.join(__dirname, "..", "..", "fixtures", "applicat
const applicationC3Path = path.join(__dirname, "..", "..", "fixtures", "application.c3");
const applicationDPath = path.join(__dirname, "..", "..", "fixtures", "application.d");
const applicationFPath = path.join(__dirname, "..", "..", "fixtures", "application.f");
const applicationGPath = path.join(__dirname, "..", "..", "fixtures", "application.g");
const errApplicationAPath = path.join(__dirname, "..", "..", "fixtures", "err.application.a");

test("AppA: project with collection dependency", (t) => {
Expand Down Expand Up @@ -48,6 +49,12 @@ test("AppF: project with UI5-dependencies", (t) => {
});
});

test("AppG: project with npm 'optionalDependencies' should not fail if optional dependency cannot be resolved", (t) => {
return npmTranslator.generateDependencyTree(applicationGPath).then((parsedTree) => {
t.deepEqual(parsedTree, applicationGTree, "Parsed correctly");
});
});

test("Error: missing package.json", async (t) => {
const dir = path.parse(__dirname).root;
const error = await t.throws(npmTranslator.generateDependencyTree(dir));
Expand Down Expand Up @@ -226,3 +233,10 @@ const applicationFTree = {
}
]
};

const applicationGTree = {
id: "application.g",
version: "1.0.0",
path: applicationGPath,
dependencies: []
};

0 comments on commit da707d7

Please sign in to comment.