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

Commit 73db129

Browse files
committed
fix: actually skip scripts set to false
1 parent e3e8502 commit 73db129

File tree

6 files changed

+45
-8
lines changed

6 files changed

+45
-8
lines changed

lib/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ exports.run = async (options) => {
134134

135135
if (allowScripts[name] === false) {
136136
console.warn(`==========> skip ${path} (because it is not allowed in package.json)`);
137+
return false;
137138
}
138139

139140
if (allowScripts[name] === true) {

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 78 -m 5000"
10+
"test": "lab -L -t 80 -m 5000"
1111
},
1212
"bin": {
1313
"allow-scripts": "./bin/allow-scripts.js"

test/fixtures/allowed-false.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"private": true,
3+
"name": "@example/allowed-false",
4+
"version": "0.0.0",
5+
"dependencies": {
6+
"@example/with-install-script": "*",
7+
"@example/with-postinstall-script": "*",
8+
"@example/without-scripts": "*"
9+
},
10+
"allowScripts": {
11+
"@example/with-install-script": false,
12+
"@example/with-postinstall-script": "*"
13+
}
14+
}

test/fixtures/basic.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"allowScripts": {
1313
"@example/with-preinstall-script": "*",
1414
"@example/with-install-script": "*",
15-
"@example/with-postinstall-script": "*"
15+
"@example/with-postinstall-script": true
1616
},
1717
"scripts": {
1818
"preinstall": "echo preinstall from basic >> ${OUTPUT}",

test/fixtures/index.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const Fs = require('fs');
55
const Mkdirp = require('mkdirp');
66
const Path = require('path');
77
const Rimraf = require('rimraf');
8+
const Sinon = require('sinon');
89

910

1011
const internals = {
@@ -43,6 +44,16 @@ exports.setup = (main, deps) => {
4344
process.env.OUTPUT = originalOutput;
4445
});
4546

47+
const log = [];
48+
const appendLog = (...items) => {
49+
50+
log.push(items.map((i) => i || '').join(' ').replace(new RegExp(cwd, 'g'), '.'));
51+
};
52+
53+
Sinon.stub(console, 'info').callsFake(appendLog);
54+
Sinon.stub(console, 'log').callsFake(appendLog);
55+
Sinon.stub(console, 'warn').callsFake(appendLog);
56+
4657
return {
4758
getExpectedResult: () => {
4859

@@ -55,7 +66,7 @@ exports.setup = (main, deps) => {
5566

5667
getLog: () => {
5768

58-
return console.log.args.map((args) => args[0] || '').join('\n').replace(new RegExp(cwd, 'g'), '.').trim();
69+
return log.join('\n').trim();
5970
}
6071
};
6172
};
@@ -64,4 +75,5 @@ exports.restore = () => {
6475

6576
internals.restore.forEach((restore) => restore());
6677
internals.restore = [];
78+
Sinon.restore();
6779
};

test/index.js

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

33
const Fixtures = require('./fixtures');
4-
const Sinon = require('sinon');
54

65

76
const Allow = require('..');
@@ -17,15 +16,12 @@ describe('allow-scripts', () => {
1716
let cwd;
1817
beforeEach(() => {
1918

20-
Sinon.stub(console, 'log');
21-
Sinon.stub(console, 'info');
2219
cwd = process.cwd();
2320
});
2421

2522
afterEach(() => {
2623

2724
Fixtures.restore();
28-
Sinon.restore();
2925
process.chdir(cwd);
3026
});
3127

@@ -56,9 +52,23 @@ describe('allow-scripts', () => {
5652

5753
await expect(Allow.run({})).to.reject('No entry for @example/with-install-script');
5854

59-
expect(fixture.getActualResult()).not.to.contain('with-postinstall-script');
6055
expect(fixture.getActualResult()).to.equal('');
6156
expect(fixture.getLog()).to.equal('');
6257
});
58+
59+
it('skips scripts which are forbidden', async () => {
60+
61+
const fixture = Fixtures.setup('allowed-false', [
62+
'with-install-script',
63+
'with-postinstall-script',
64+
'without-scripts'
65+
]);
66+
67+
await Allow.run({});
68+
69+
expect(fixture.getActualResult()).not.to.contain('with-install-script');
70+
expect(fixture.getActualResult()).to.equal('postinstall from with-postinstall-script');
71+
expect(fixture.getLog()).to.contain('skip node_modules/@example/with-install-script (because it is not allowed in package.json)');
72+
});
6373
});
6474
});

0 commit comments

Comments
 (0)