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

Commit 717bb0c

Browse files
committed
test: happy path
1 parent 138007f commit 717bb0c

9 files changed

+73
-6
lines changed

test/fixtures/basic.json

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,22 @@
33
"name": "@example/basic",
44
"version": "0.0.0",
55
"dependencies": {
6-
"@example/with-install-script": "*"
6+
"@example/with-preinstall-script": "*",
7+
"@example/with-install-script": "*",
8+
"@example/with-postinstall-script": "*",
9+
"@example/without-scripts": "*",
10+
"@example/without-install-scripts": "*"
711
},
812
"allowScripts": {
9-
"@example/with-install-script": "*"
13+
"@example/with-preinstall-script": "*",
14+
"@example/with-install-script": "*",
15+
"@example/with-postinstall-script": "*"
16+
},
17+
"scripts": {
18+
"preinstall": "echo preinstall from basic >> ${OUTPUT}",
19+
"install": "echo install from basic >> ${OUTPUT}",
20+
"postinstall": "echo postinstall from basic >> ${OUTPUT}",
21+
"prepublish": "echo prepublish from basic >> ${OUTPUT}",
22+
"prepare": "echo prepare from basic >> ${OUTPUT}"
1023
}
1124
}

test/fixtures/basic.txt

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

test/fixtures/index.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@ exports.setup = (main, deps) => {
2020
Rimraf.sync(cwd);
2121
Mkdirp.sync(cwd);
2222
Fs.copyFileSync(Path.join(__dirname, `${main}.json`), Path.join(cwd, 'package.json'));
23+
delete require.cache[Path.join(cwd, 'package.json')];
2324

2425
deps.forEach((dep) => {
2526

2627
const pkg = require(`./${dep}.json`);
2728

2829
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+
Fs.writeFileSync(Path.join(cwd, 'node_modules', pkg.name, 'package.json'), JSON.stringify(Object.assign({}, pkg, {
31+
_id: `${pkg.name}@${pkg.version}`
32+
})));
3033
});
3134

3235
process.chdir(cwd);
@@ -40,9 +43,15 @@ exports.setup = (main, deps) => {
4043
});
4144

4245
return {
46+
expectedResult: Fs.readFileSync(Path.join(__dirname, `${main}.txt`)).toString().trim(),
4347
getResults: () => {
4448

4549
return Fs.readFileSync(output).toString().trim();
50+
},
51+
52+
getLog: () => {
53+
54+
return console.log.args.map((args) => args[0] || '').join('\n').replace(new RegExp(cwd, 'g'), '.').trim();
4655
}
4756
};
4857
};

test/fixtures/with-install-script.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
"name": "@example/with-install-script",
44
"version": "0.0.0",
55
"scripts": {
6-
"install": "echo install from with-install-script >>${OUTPUT}"
6+
"install": "echo install from with-install-script >> ${OUTPUT}"
77
}
88
}
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-postinstall-script",
4+
"version": "0.0.0",
5+
"scripts": {
6+
"postinstall": "echo postinstall from with-postinstall-script >> ${OUTPUT}"
7+
}
8+
}
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-preinstall-script",
4+
"version": "0.0.0",
5+
"scripts": {
6+
"preinstall": "echo preinstall from with-preinstall-script >> ${OUTPUT}"
7+
}
8+
}
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/without-install-scripts",
4+
"version": "0.0.0",
5+
"scripts": {
6+
"test": "echo ok"
7+
}
8+
}

test/fixtures/without-scripts.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"private": true,
3+
"name": "@example/without-scripts",
4+
"version": "0.0.0"
5+
}

test/index.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,19 @@ describe('allow-scripts', () => {
3131

3232
it('executes allowed scripts', async () => {
3333

34-
const { getResults } = Fixtures.setup('basic', ['with-install-script']);
34+
const fixture = Fixtures.setup('basic', [
35+
'with-preinstall-script',
36+
'with-install-script',
37+
'with-postinstall-script',
38+
'without-scripts',
39+
'without-install-scripts'
40+
]);
3541

3642
await Allow.run({});
3743

38-
expect(getResults()).to.equal('install from with-install-script');
44+
expect(fixture.getResults()).to.equal(fixture.expectedResult);
45+
expect(fixture.getLog()).not.to.contain('without-scripts');
46+
expect(fixture.getLog()).not.to.contain('without-install-scripts');
3947
});
4048
});
4149
});

0 commit comments

Comments
 (0)