Skip to content
This repository was archived by the owner on Aug 18, 2024. It is now read-only.

Commit d86370b

Browse files
committed
test: npm ls errors
1 parent 226e5b6 commit d86370b

File tree

7 files changed

+42
-22
lines changed

7 files changed

+42
-22
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Only the explicitly allowed `[pre|post]install` scripts will be executed.
1717
$ npx allow-scripts [--dry-run]
1818
```
1919

20-
Running the command will scan the list of installed dependencies (from the first source available: `npm-shrinkwrap.json`, `package-lock.json`, `npm ls --json`). It will then execute the scripts for allowed dependencies that have them in the following order:
20+
Running the command will scan the list of installed dependencies using `npm ls --json`. It will then execute the scripts for allowed dependencies that have them in the following order:
2121

2222
- `preinstall` in the main package
2323
- `preinstall` in dependencies

lib/index.js

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const Cp = require('child_process');
4-
const Fs = require('fs');
54
const Npm = require('libnpm');
65
const Path = require('path');
76
const Semver = require('semver');
@@ -74,30 +73,21 @@ internals.runScript = (stage, { pkg, path, cwd, unsafePerm }, options) => {
7473
});
7574
};
7675

77-
internals.getLockFile = (cwd) => {
78-
79-
if (Fs.existsSync(Path.join(cwd, 'npm-shrinkwrap.json'))) {
80-
return require(Path.join(cwd, 'npm-shrinkwrap.json'));
81-
}
82-
83-
if (Fs.existsSync(Path.join(cwd, 'package-lock.json'))) {
84-
return require(Path.join(cwd, 'package-lock.json'));
85-
}
76+
internals.getInstalledContents = (cwd) => {
8677

8778
let output;
8879
try {
8980
output = Cp.execSync('npm ls --json', { cwd });
9081
}
9182
catch (err) {
92-
output = err.output[1]; // npm will exist with an error when e.g. there's peer deps missing - attempt to ignore that
83+
output = err.output[1]; // npm will exit with an error when e.g. there's peer deps missing - attempt to ignore that
9384
}
9485

9586
try {
9687
return JSON.parse(output.toString());
9788
}
9889
catch (err) {
99-
console.error(err);
100-
throw new Error('Failed to read the contents of node_modules');
90+
throw new Error(`Failed to read the contents of node_modules. \`npm ls --json\` returned: ${output}`);
10191
}
10292
};
10393

@@ -108,7 +98,7 @@ exports.run = async (options) => {
10898

10999
pkg._id = `${pkg.name}@${pkg.version}`; // @todo: find an official way to do this for top level package
110100

111-
const tree = Npm.logicalTree(pkg, internals.getLockFile(cwd));
101+
const tree = Npm.logicalTree(pkg, internals.getInstalledContents(cwd));
112102
const queue = internals.queue(tree);
113103

114104
const allowScripts = pkg.allowScripts || {};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"url": "https://github.com/dominykas/allow-scripts.git"
88
},
99
"scripts": {
10-
"test": "lab -L -t 86 -m 5000"
10+
"test": "lab -L -t 93 -m 5000"
1111
},
1212
"bin": {
1313
"allow-scripts": "./bin/allow-scripts.js"
File renamed without changes.

test/fixtures/basic.incomplete.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
preinstall from basic
2+
install from with-install-script
3+
install from basic
4+
postinstall from basic
5+
prepublish from basic
6+
prepare from basic

test/fixtures/index.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,9 @@ exports.setup = (main, deps) => {
5353
Sinon.stub(console, 'info').callsFake(appendLog);
5454
Sinon.stub(console, 'log').callsFake(appendLog);
5555
Sinon.stub(console, 'warn').callsFake(appendLog);
56+
Sinon.stub(process.stderr, 'write').callsFake((data) => appendLog(data.toString()));
5657

5758
return {
58-
getExpectedResult: () => {
59-
60-
return Fs.readFileSync(Path.join(__dirname, `${main}.txt`)).toString().trim();
61-
},
6259
getActualResult: () => {
6360

6461
return Fs.readFileSync(output).toString().trim();

test/index.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
'use strict';
22

3+
const Cp = require('child_process');
4+
const Fs = require('fs');
35
const Fixtures = require('./fixtures');
4-
6+
const Path = require('path');
7+
const Sinon = require('sinon');
58

69
const Allow = require('..');
710

@@ -37,7 +40,7 @@ describe('allow-scripts', () => {
3740

3841
await Allow.run({});
3942

40-
expect(fixture.getActualResult()).to.equal(fixture.getExpectedResult());
43+
expect(fixture.getActualResult()).to.equal(Fs.readFileSync(Path.join(__dirname, 'fixtures', 'basic.full.txt')).toString().trim());
4144
expect(fixture.getLog()).not.to.contain('without-scripts');
4245
expect(fixture.getLog()).not.to.contain('without-install-scripts');
4346
});
@@ -113,5 +116,29 @@ describe('allow-scripts', () => {
113116
expect(fixture.getActualResult()).to.equal('');
114117
expect(fixture.getLog()).to.equal('');
115118
});
119+
120+
it('deals with incomplete installed tree', async () => {
121+
122+
const fixture = Fixtures.setup('basic', [
123+
// 'with-preinstall-script',
124+
'with-install-script',
125+
// 'with-postinstall-script',
126+
'without-scripts',
127+
'without-install-scripts'
128+
]);
129+
130+
await Allow.run({});
131+
132+
expect(fixture.getActualResult()).to.equal(Fs.readFileSync(Path.join(__dirname, 'fixtures', 'basic.incomplete.txt')).toString().trim());
133+
});
134+
135+
it('deals with unparseable tree', async () => {
136+
137+
Sinon.stub(Cp, 'execSync').returns('not-json');
138+
139+
Fixtures.setup('basic', []);
140+
141+
await expect(Allow.run({})).to.reject('Failed to read the contents of node_modules. `npm ls --json` returned: not-json');
142+
});
116143
});
117144
});

0 commit comments

Comments
 (0)