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

Commit 138007f

Browse files
committed
test: setup fixtures
1 parent 5c17375 commit 138007f

File tree

6 files changed

+88
-2
lines changed

6 files changed

+88
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.idea
22
node_modules
3+
test/tmp/

package.json

Lines changed: 3 additions & 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 73 -m 5000"
10+
"test": "lab -L -t 78 -m 5000"
1111
},
1212
"bin": {
1313
"allow-scripts": "./bin/allow-scripts.js"
@@ -22,6 +22,8 @@
2222
"devDependencies": {
2323
"code": "5.x.x",
2424
"lab": "18.x.x",
25+
"mkdirp": "0.5.x",
26+
"rimraf": "2.x.x",
2527
"semantic-release": "15.x.x",
2628
"sinon": "7.x.x"
2729
},

test/fixtures/basic.json

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

test/fixtures/index.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
'use strict';
2+
3+
4+
const Fs = require('fs');
5+
const Mkdirp = require('mkdirp');
6+
const Path = require('path');
7+
const Rimraf = require('rimraf');
8+
9+
10+
const internals = {
11+
restore: []
12+
};
13+
14+
15+
exports.setup = (main, deps) => {
16+
17+
const cwd = Path.join(__dirname, '..', 'tmp');
18+
const output = Path.join(cwd, 'res.txt');
19+
20+
Rimraf.sync(cwd);
21+
Mkdirp.sync(cwd);
22+
Fs.copyFileSync(Path.join(__dirname, `${main}.json`), Path.join(cwd, 'package.json'));
23+
24+
deps.forEach((dep) => {
25+
26+
const pkg = require(`./${dep}.json`);
27+
28+
Mkdirp.sync(Path.join(cwd, 'node_modules', pkg.name));
29+
Fs.copyFileSync(Path.join(__dirname, `${dep}.json`), Path.join(cwd, 'node_modules', pkg.name, 'package.json'));
30+
});
31+
32+
process.chdir(cwd);
33+
34+
const originalOutput = process.env.output;
35+
process.env.OUTPUT = output;
36+
37+
internals.restore.push(() => {
38+
39+
process.env.OUTPUT = originalOutput;
40+
});
41+
42+
return {
43+
getResults: () => {
44+
45+
return Fs.readFileSync(output).toString().trim();
46+
}
47+
};
48+
};
49+
50+
exports.restore = () => {
51+
52+
internals.restore.forEach((restore) => restore());
53+
internals.restore = [];
54+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"private": true,
3+
"name": "@example/with-install-script",
4+
"version": "0.0.0",
5+
"scripts": {
6+
"install": "echo install from with-install-script >>${OUTPUT}"
7+
}
8+
}

test/index.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,41 @@
11
'use strict';
22

3+
const Fixtures = require('./fixtures');
34
const Sinon = require('sinon');
45

6+
57
const Allow = require('..');
68

9+
710
const { describe, it, beforeEach, afterEach } = exports.lab = require('lab').script();
811
const { expect } = require('code');
912

1013
describe('allow-scripts', () => {
1114

1215
describe('run()', () => {
1316

17+
let cwd;
1418
beforeEach(() => {
1519

1620
Sinon.stub(console, 'log');
21+
Sinon.stub(console, 'info');
22+
cwd = process.cwd();
1723
});
1824

1925
afterEach(() => {
2026

27+
Fixtures.restore();
2128
Sinon.restore();
29+
process.chdir(cwd);
2230
});
2331

2432
it('executes allowed scripts', async () => {
2533

34+
const { getResults } = Fixtures.setup('basic', ['with-install-script']);
35+
2636
await Allow.run({});
2737

28-
expect(true).to.equal(true);
38+
expect(getResults()).to.equal('install from with-install-script');
2939
});
3040
});
3141
});

0 commit comments

Comments
 (0)