Skip to content

Commit 907b34b

Browse files
wraithgarisaacs
authored andcommitted
fix(ci): pay attention to --ignore-scripts
Added a test for install too for this specific condition PR-URL: #2455 Credit: @wraithgar Close: #2455 Reviewed-by: @isaacs
1 parent 99156df commit 907b34b

File tree

3 files changed

+91
-20
lines changed

3 files changed

+91
-20
lines changed

lib/ci.js

+21-19
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const ci = async () => {
2121
}
2222

2323
const where = npm.prefix
24-
const { scriptShell } = npm.flatOptions
24+
const { scriptShell, ignoreScripts } = npm.flatOptions
2525
const arb = new Arborist({ ...npm.flatOptions, path: where })
2626

2727
await Promise.all([
@@ -39,24 +39,26 @@ const ci = async () => {
3939
await arb.reify({ ...npm.flatOptions, save: false })
4040

4141
// run the same set of scripts that `npm install` runs.
42-
const scripts = [
43-
'preinstall',
44-
'install',
45-
'postinstall',
46-
'prepublish', // XXX should we remove this finally??
47-
'preprepare',
48-
'prepare',
49-
'postprepare',
50-
]
51-
for (const event of scripts) {
52-
await runScript({
53-
path: where,
54-
args: [],
55-
scriptShell,
56-
stdio: 'inherit',
57-
stdioString: true,
58-
event,
59-
})
42+
if (!ignoreScripts) {
43+
const scripts = [
44+
'preinstall',
45+
'install',
46+
'postinstall',
47+
'prepublish', // XXX should we remove this finally??
48+
'preprepare',
49+
'prepare',
50+
'postprepare',
51+
]
52+
for (const event of scripts) {
53+
await runScript({
54+
path: where,
55+
args: [],
56+
scriptShell,
57+
stdio: 'inherit',
58+
stdioString: true,
59+
event,
60+
})
61+
}
6062
}
6163
await reifyFinish(arb)
6264
}

test/lib/ci.js

+35
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,41 @@ const { test } = require('tap')
66

77
const requireInject = require('require-inject')
88

9+
test('should ignore scripts with --ignore-scripts', (t) => {
10+
const SCRIPTS = []
11+
let REIFY_CALLED = false
12+
const ci = requireInject('../../lib/ci.js', {
13+
'../../lib/utils/reify-finish.js': async () => {},
14+
'../../lib/npm.js': {
15+
globalDir: 'path/to/node_modules/',
16+
prefix: 'foo',
17+
flatOptions: {
18+
global: false,
19+
ignoreScripts: true
20+
},
21+
config: {
22+
get: () => false,
23+
},
24+
},
25+
'@npmcli/run-script': ({ event }) => {
26+
SCRIPTS.push(event)
27+
},
28+
'@npmcli/arborist': function () {
29+
this.loadVirtual = async () => {}
30+
this.reify = () => {
31+
REIFY_CALLED = true
32+
}
33+
},
34+
})
35+
ci([], er => {
36+
if (er)
37+
throw er
38+
t.equal(REIFY_CALLED, true, 'called reify')
39+
t.strictSame(SCRIPTS, [], 'no scripts when running ci')
40+
t.end()
41+
})
42+
})
43+
944
test('should use Arborist and run-script', (t) => {
1045
const scripts = [
1146
'preinstall',

test/lib/install.js

+35-1
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,48 @@ test('should install using Arborist', (t) => {
7272
t.end()
7373
})
7474

75+
test('should ignore scripts with --ignore-scripts', (t) => {
76+
const SCRIPTS = []
77+
let REIFY_CALLED = false
78+
const install = requireInject('../../lib/install.js', {
79+
'../../lib/utils/reify-finish.js': async () => {},
80+
'../../lib/npm.js': {
81+
globalDir: 'path/to/node_modules/',
82+
prefix: 'foo',
83+
flatOptions: {
84+
global: false,
85+
ignoreScripts: true
86+
},
87+
config: {
88+
get: () => false,
89+
},
90+
},
91+
'@npmcli/run-script': ({ event }) => {
92+
SCRIPTS.push(event)
93+
},
94+
'@npmcli/arborist': function () {
95+
this.reify = () => {
96+
REIFY_CALLED = true
97+
}
98+
},
99+
})
100+
install([], er => {
101+
if (er)
102+
throw er
103+
t.equal(REIFY_CALLED, true, 'called reify')
104+
t.strictSame(SCRIPTS, [], 'no scripts when adding dep')
105+
t.end()
106+
})
107+
})
108+
75109
test('should install globally using Arborist', (t) => {
76110
const install = requireInject('../../lib/install.js', {
77111
'../../lib/utils/reify-finish.js': async () => {},
78112
'../../lib/npm.js': {
79113
globalDir: 'path/to/node_modules/',
80114
prefix: 'foo',
81115
flatOptions: {
82-
global: 'true',
116+
global: true,
83117
},
84118
config: {
85119
get: () => false,

0 commit comments

Comments
 (0)