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

Commit d36866a

Browse files
committed
test: existing lock files
1 parent 5156b2f commit d36866a

File tree

3 files changed

+60
-13
lines changed

3 files changed

+60
-13
lines changed

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

test/fixtures/index.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ const internals = {
1313
};
1414

1515

16+
internals.readFile = (path) => Fs.readFileSync(path).toString().trim();
17+
18+
19+
exports.expectedResults = {
20+
basicFull: internals.readFile(Path.join(__dirname, 'basic.full.txt')),
21+
basicDryRun: internals.readFile(Path.join(__dirname, 'basic.dry-run.txt')),
22+
deep: internals.readFile(Path.join(__dirname, 'deep.txt'))
23+
};
24+
25+
1626
exports.setup = (main, deps) => {
1727

1828
const cwd = Path.join(__dirname, '..', 'tmp');
@@ -67,15 +77,9 @@ exports.setup = (main, deps) => {
6777
Sinon.stub(process.stderr, 'write').callsFake((data) => appendLog(data.toString()));
6878

6979
return {
70-
getActualResult: () => {
71-
72-
return Fs.readFileSync(output).toString().trim();
73-
},
74-
75-
getLog: () => {
76-
77-
return log.join('\n').trim();
78-
}
80+
cwd,
81+
getActualResult: () => internals.readFile(output),
82+
getLog: () => log.join('\n').trim()
7983
};
8084
};
8185

test/index.js

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

3+
const Cp = require('child_process');
34
const Fs = require('fs');
45
const Fixtures = require('./fixtures');
56
const Path = require('path');
@@ -38,9 +39,11 @@ describe('allow-scripts', () => {
3839

3940
await Allow.run({});
4041

41-
expect(fixture.getActualResult()).to.equal(Fs.readFileSync(Path.join(__dirname, 'fixtures', 'basic.full.txt')).toString().trim());
42+
expect(fixture.getActualResult()).to.equal(Fixtures.expectedResults.basicFull);
4243
expect(fixture.getLog()).not.to.contain('without-scripts');
4344
expect(fixture.getLog()).not.to.contain('without-install-scripts');
45+
46+
expect(Fs.existsSync(Path.join(fixture.cwd, 'npm-shrinkwrap.json'))).to.equal(false);
4447
});
4548

4649
it('dry run: reports allowed scripts', async () => {
@@ -56,7 +59,7 @@ describe('allow-scripts', () => {
5659
await Allow.run({ dryRun: true });
5760

5861
expect(fixture.getActualResult()).to.equal('');
59-
expect(fixture.getLog()).to.equal(Fs.readFileSync(Path.join(__dirname, 'fixtures', 'basic.dry-run.txt')).toString().trim());
62+
expect(fixture.getLog()).to.equal(Fixtures.expectedResults.basicDryRun);
6063
});
6164

6265
it('executes allowed scripts (subdeps)', async () => {
@@ -72,7 +75,7 @@ describe('allow-scripts', () => {
7275

7376
await Allow.run({});
7477

75-
expect(fixture.getActualResult()).to.equal(Fs.readFileSync(Path.join(__dirname, 'fixtures', 'deep.txt')).toString().trim());
78+
expect(fixture.getActualResult()).to.equal(Fixtures.expectedResults.deep);
7679
expect(fixture.getLog()).not.to.contain('without-scripts');
7780
expect(fixture.getLog()).not.to.contain('without-install-scripts');
7881
});
@@ -92,6 +95,46 @@ describe('allow-scripts', () => {
9295
expect(fixture.getLog()).to.contain('skip node_modules/@example/cycle-b (because it has a cycle in dependencies)');
9396
});
9497

98+
it('executes allowed scripts (existing shrinkwrap)', async () => {
99+
100+
const fixture = Fixtures.setup('basic', [
101+
'with-preinstall-script',
102+
'with-install-script',
103+
'with-postinstall-script',
104+
'without-scripts',
105+
'without-install-scripts'
106+
]);
107+
108+
Cp.execSync('npm shrinkwrap', { cwd: fixture.cwd });
109+
110+
await Allow.run({});
111+
112+
expect(fixture.getActualResult()).to.equal(Fixtures.expectedResults.basicFull);
113+
114+
expect(Fs.existsSync(Path.join(fixture.cwd, 'npm-shrinkwrap.json'))).to.equal(true);
115+
});
116+
117+
it('executes allowed scripts (existing package-lock)', async () => {
118+
119+
const fixture = Fixtures.setup('basic', [
120+
'with-preinstall-script',
121+
'with-install-script',
122+
'with-postinstall-script',
123+
'without-scripts',
124+
'without-install-scripts'
125+
]);
126+
127+
Cp.execSync('npm shrinkwrap', { cwd: fixture.cwd });
128+
Cp.execSync('mv npm-shrinkwrap.json package-lock.json', { cwd: fixture.cwd });
129+
130+
await Allow.run({});
131+
132+
expect(fixture.getActualResult()).to.equal(Fixtures.expectedResults.basicFull);
133+
134+
expect(Fs.existsSync(Path.join(fixture.cwd, 'package-lock.json'))).to.equal(true);
135+
expect(Fs.existsSync(Path.join(fixture.cwd, 'npm-shrinkwrap.json'))).to.equal(false);
136+
});
137+
95138
it('crashes on script not in allowed list', async () => {
96139

97140
const fixture = Fixtures.setup('not-in-allowed', [

0 commit comments

Comments
 (0)