|
1 | 1 | 'use strict' |
2 | 2 |
|
3 | 3 | const t = require('tap') |
| 4 | + |
| 5 | +const tspawk = require('./fixtures/tspawk.js') |
| 6 | +const spawk = tspawk(t) |
| 7 | + |
4 | 8 | const fs = require('fs') |
5 | 9 | const path = require('path') |
6 | 10 | const pack = require('../lib/index.js') |
7 | 11 | const tnock = require('./fixtures/tnock.js') |
| 12 | +const { load: loadMockNpm } = require('../../../test/fixtures/mock-npm.js') |
8 | 13 |
|
9 | 14 | const OPTS = { |
10 | 15 | registry: 'https://mock.reg/', |
11 | 16 | } |
12 | 17 |
|
13 | 18 | const REG = OPTS.registry |
14 | 19 |
|
| 20 | +// TODO this ... smells. npm "script-shell" config mentions defaults but those |
| 21 | +// are handled by run-script, not npm. So for now we have to tie tests to some |
| 22 | +// pretty specific internals of runScript |
| 23 | +const makeSpawnArgs = require('@npmcli/run-script/lib/make-spawn-args.js') |
| 24 | + |
15 | 25 | t.test('packs from local directory', async t => { |
16 | 26 | const testDir = t.testdir({ |
17 | 27 | 'package.json': JSON.stringify({ |
@@ -128,3 +138,40 @@ t.test('packs from registry spec', async t => { |
128 | 138 | const tarball = await pack(spec, { ...OPTS }) |
129 | 139 | t.ok(tarball) |
130 | 140 | }) |
| 141 | + |
| 142 | +t.test('runs scripts in foreground when foregroundScripts === true', async t => { |
| 143 | + const { npm } = await loadMockNpm(t, {}) |
| 144 | + |
| 145 | + const testDir = t.testdir({ |
| 146 | + 'package.json': JSON.stringify({ |
| 147 | + name: 'my-cool-pkg', |
| 148 | + version: '1.0.0', |
| 149 | + scripts: { |
| 150 | + prepack: 'touch prepack', |
| 151 | + postpack: 'touch postpack', |
| 152 | + }, |
| 153 | + }, null, 2), |
| 154 | + }) |
| 155 | + |
| 156 | + const cwd = process.cwd() |
| 157 | + process.chdir(testDir) |
| 158 | + |
| 159 | + const [scriptShell, scriptArgs] = makeSpawnArgs({ |
| 160 | + event: 'prepack', |
| 161 | + path: npm.prefix, |
| 162 | + cmd: 'touch prepack', |
| 163 | + }) |
| 164 | + |
| 165 | + const prepack = spawk.spawn(scriptShell, scriptArgs) |
| 166 | + |
| 167 | + await pack('file:.', { |
| 168 | + packDestination: testDir, |
| 169 | + foregroundScripts: true, |
| 170 | + }) |
| 171 | + |
| 172 | + t.ok(prepack.called) |
| 173 | + |
| 174 | + t.teardown(async () => { |
| 175 | + process.chdir(cwd) |
| 176 | + }) |
| 177 | +}) |
0 commit comments