| 1 |
const t = require('tap') |
| 2 |
|
| 3 |
const mockGetInstallScripts = (t, isNodeGypResult = () => false) => |
| 4 |
t.mock('../lib/install-scripts.js', { |
| 5 |
'@npmcli/node-gyp': { |
| 6 |
isNodeGypPackage: async (path) => { |
| 7 |
if (typeof isNodeGypResult === 'function') { |
| 8 |
return isNodeGypResult(path) |
| 9 |
} |
| 10 |
return !!isNodeGypResult |
| 11 |
}, |
| 12 |
}, |
| 13 |
}) |
| 14 |
|
| 15 |
const node = ({ |
| 16 |
scripts = {}, |
| 17 |
gypfile, |
| 18 |
resolved = 'https://registry.npmjs.org/pkg/-/pkg-1.0.0.tgz', |
| 19 |
path = '/fake', |
| 20 |
} = {}) => ({ |
| 21 |
resolved, |
| 22 |
path, |
| 23 |
package: { scripts, ...(gypfile !== undefined ? { gypfile } : {}) }, |
| 24 |
}) |
| 25 |
|
| 26 |
t.test('collects preinstall, install, postinstall', async t => { |
| 27 |
const getInstallScripts = mockGetInstallScripts(t) |
| 28 |
t.strictSame( |
| 29 |
await getInstallScripts(node({ scripts: { preinstall: 'pre' } })), |
| 30 |
{ preinstall: 'pre' } |
| 31 |
) |
| 32 |
t.strictSame( |
| 33 |
await getInstallScripts(node({ scripts: { install: 'inst' } })), |
| 34 |
{ install: 'inst' } |
| 35 |
) |
| 36 |
t.strictSame( |
| 37 |
await getInstallScripts(node({ scripts: { postinstall: 'post' } })), |
| 38 |
{ postinstall: 'post' } |
| 39 |
) |
| 40 |
t.strictSame( |
| 41 |
await getInstallScripts(node({ scripts: {} })), |
| 42 |
{} |
| 43 |
) |
| 44 |
}) |
| 45 |
|
| 46 |
t.test('ignores unrelated scripts', async t => { |
| 47 |
const getInstallScripts = mockGetInstallScripts(t) |
| 48 |
t.strictSame( |
| 49 |
await getInstallScripts(node({ scripts: { test: 'x', build: 'y' } })), |
| 50 |
{} |
| 51 |
) |
| 52 |
}) |
| 53 |
|
| 54 |
t.test('prepare only counts for non-registry sources', async t => { |
| 55 |
const getInstallScripts = mockGetInstallScripts(t) |
| 56 |
// registry: prepare ignored |
| 57 |
t.strictSame( |
| 58 |
await getInstallScripts(node({ |
| 59 |
scripts: { prepare: 'do' }, |
| 60 |
resolved: 'https://registry.npmjs.org/x/-/x-1.0.0.tgz', |
| 61 |
})), |
| 62 |
{} |
| 63 |
) |
| 64 |
// git: prepare counts |
| 65 |
t.strictSame( |
| 66 |
await getInstallScripts(node({ |
| 67 |
scripts: { prepare: 'do' }, |
| 68 |
resolved: 'git+ssh://[email protected]/foo/bar.git#abc', |
| 69 |
})), |
| 70 |
{ prepare: 'do' } |
| 71 |
) |
| 72 |
// file: prepare counts |
| 73 |
t.strictSame( |
| 74 |
await getInstallScripts(node({ |
| 75 |
scripts: { prepare: 'do' }, |
| 76 |
resolved: 'file:../local', |
| 77 |
})), |
| 78 |
{ prepare: 'do' } |
| 79 |
) |
| 80 |
}) |
| 81 |
|
| 82 |
t.test('synthetic node-gyp install detected via binding.gyp', async t => { |
| 83 |
const getInstallScripts = mockGetInstallScripts(t, () => true) |
| 84 |
t.strictSame( |
| 85 |
await getInstallScripts(node()), |
| 86 |
{ install: 'node-gyp rebuild' } |
| 87 |
) |
| 88 |
}) |
| 89 |
|
| 90 |
t.test('synthetic node-gyp suppressed when gypfile: false', async t => { |
| 91 |
const getInstallScripts = mockGetInstallScripts(t, () => true) |
| 92 |
t.strictSame( |
| 93 |
await getInstallScripts(node({ gypfile: false })), |
| 94 |
{} |
| 95 |
) |
| 96 |
}) |
| 97 |
|
| 98 |
t.test('synthetic node-gyp suppressed when explicit install is present', async t => { |
| 99 |
const getInstallScripts = mockGetInstallScripts(t, () => true) |
| 100 |
t.strictSame( |
| 101 |
await getInstallScripts(node({ scripts: { install: 'real-install' } })), |
| 102 |
{ install: 'real-install' } |
| 103 |
) |
| 104 |
}) |
| 105 |
|
| 106 |
t.test('synthetic node-gyp suppressed when explicit preinstall is present', async t => { |
| 107 |
const getInstallScripts = mockGetInstallScripts(t, () => true) |
| 108 |
t.strictSame( |
| 109 |
await getInstallScripts(node({ scripts: { preinstall: 'real-pre' } })), |
| 110 |
{ preinstall: 'real-pre' } |
| 111 |
) |
| 112 |
}) |
| 113 |
|
| 114 |
t.test('node-gyp detection error is treated as not-gyp', async t => { |
| 115 |
const getInstallScripts = t.mock('../lib/install-scripts.js', { |
| 116 |
'@npmcli/node-gyp': { |
| 117 |
isNodeGypPackage: async () => { |
| 118 |
throw new Error('fs blew up') |
| 119 |
}, |
| 120 |
}, |
| 121 |
}) |
| 122 |
t.strictSame(await getInstallScripts(node()), {}) |
| 123 |
}) |
| 124 |
|
| 125 |
t.test('missing resolved treated as registry (prepare ignored)', async t => { |
| 126 |
const getInstallScripts = mockGetInstallScripts(t) |
| 127 |
// Construct the node directly so the destructuring default in the test |
| 128 |
// helper does not substitute a registry URL for the missing resolved |
| 129 |
// field. Exercises the fallback path in isRegistrySource. |
| 130 |
const missingResolved = { |
| 131 |
path: '/fake', |
| 132 |
package: { scripts: { prepare: 'do' } }, |
| 133 |
} |
| 134 |
t.strictSame(await getInstallScripts(missingResolved), {}) |
| 135 |
}) |
| 136 |
|
| 137 |
t.test('prepare counts for non-registry deps even when resolved URL looks registry-like', async t => { |
| 138 |
const getInstallScripts = mockGetInstallScripts(t) |
| 139 |
// A fork hosted at a URL that happens to follow the npm registry tarball |
| 140 |
// shape. Arborist's edge-based check (isRegistryDependency=false) is |
| 141 |
// authoritative — prepare must NOT be skipped just because the URL pattern |
| 142 |
// matches. |
| 143 |
const nonRegistry = { |
| 144 |
resolved: 'https://corp.example.com/mirror/sharp/-/sharp-1.0.0.tgz', |
| 145 |
path: '/fake', |
| 146 |
isRegistryDependency: false, |
| 147 |
package: { scripts: { prepare: 'do' } }, |
| 148 |
} |
| 149 |
t.strictSame(await getInstallScripts(nonRegistry), { prepare: 'do' }) |
| 150 |
}) |
| 151 |
|
| 152 |
t.test('prepare is skipped for registry deps regardless of resolved URL shape', async t => { |
| 153 |
const getInstallScripts = mockGetInstallScripts(t) |
| 154 |
const registryNode = { |
| 155 |
resolved: 'https://internal.corp/private-registry/sharp/-/sharp-1.0.0.tgz', |
| 156 |
path: '/fake', |
| 157 |
isRegistryDependency: true, |
| 158 |
package: { scripts: { prepare: 'do' } }, |
| 159 |
} |
| 160 |
t.strictSame(await getInstallScripts(registryNode), {}) |
| 161 |
}) |
| 162 |
|
| 163 |
t.test('lockfile-only node with hasInstallScript=true emits a sentinel', async t => { |
| 164 |
// Loaded from a lockfile (e.g. `npm ci` virtualTree, or strict mode's |
| 165 |
// preflight against the idealTree before reify has fetched manifests): |
| 166 |
// the lockfile records `hasInstallScript: true` but does not inline |
| 167 |
// the script bodies. Without a sentinel the preflight would miss this |
| 168 |
// node entirely and let install scripts run. |
| 169 |
const getInstallScripts = mockGetInstallScripts(t) |
| 170 |
const lockfileNode = { |
| 171 |
resolved: 'https://registry.npmjs.org/pkg/-/pkg-1.0.0.tgz', |
| 172 |
path: '/fake', |
| 173 |
isRegistryDependency: true, |
| 174 |
hasInstallScript: true, |
| 175 |
package: { name: 'pkg', version: '1.0.0' }, |
| 176 |
} |
| 177 |
t.strictSame( |
| 178 |
await getInstallScripts(lockfileNode), |
| 179 |
{ install: '(install scripts present)' } |
| 180 |
) |
| 181 |
}) |
| 182 |
|
| 183 |
t.test('sentinel is not emitted when scripts are already enumerated', async t => { |
| 184 |
// If `hasInstallScript: true` coexists with a real `scripts` map, we |
| 185 |
// surface the real names — the sentinel must not overwrite them. |
| 186 |
const getInstallScripts = mockGetInstallScripts(t) |
| 187 |
const node = { |
| 188 |
resolved: 'https://registry.npmjs.org/pkg/-/pkg-1.0.0.tgz', |
| 189 |
path: '/fake', |
| 190 |
isRegistryDependency: true, |
| 191 |
hasInstallScript: true, |
| 192 |
package: { scripts: { postinstall: 'echo hi' } }, |
| 193 |
} |
| 194 |
t.strictSame(await getInstallScripts(node), { postinstall: 'echo hi' }) |
| 195 |
}) |
| 196 |
|
| 197 |
t.test('sentinel is not emitted when hasInstallScript is absent', async t => { |
| 198 |
// Defensive: a lockfile entry without `hasInstallScript` (the common |
| 199 |
// case) still returns {} so we don't false-positive every dep. |
| 200 |
const getInstallScripts = mockGetInstallScripts(t) |
| 201 |
const node = { |
| 202 |
resolved: 'https://registry.npmjs.org/pkg/-/pkg-1.0.0.tgz', |
| 203 |
path: '/fake', |
| 204 |
isRegistryDependency: true, |
| 205 |
package: { name: 'pkg', version: '1.0.0' }, |
| 206 |
} |
| 207 |
t.strictSame(await getInstallScripts(node), {}) |
| 208 |
}) |