summaryrefslogtreecommitdiff
path: root/deps/npm/test/lib/run-script.js
blob: bad8a63c0d778343516d2de869e5ff599f290905 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
const t = require('tap')
const requireInject = require('require-inject')

const RUN_SCRIPTS = []
const npm = {
  localPrefix: __dirname,
  flatOptions: {
    scriptShell: undefined,
    json: false,
    parseable: false,
  },
  config: {
    settings: {
      'if-present': false,
    },
    get: k => npm.config.settings[k],
    set: (k, v) => {
      npm.config.settings[k] = v
    },
  },
}

const output = []

const npmlog = { level: 'warn' }
const getRS = windows => requireInject('../../lib/run-script.js', {
  '@npmcli/run-script': Object.assign(async opts => {
    RUN_SCRIPTS.push(opts)
  }, {
    isServerPackage: require('@npmcli/run-script').isServerPackage,
  }),
  npmlog,
  '../../lib/npm.js': npm,
  '../../lib/utils/is-windows-shell.js': windows,
  '../../lib/utils/output.js': (...msg) => output.push(msg),
})

const runScript = getRS(false)
const runScriptWin = getRS(true)

const { writeFileSync } = require('fs')
t.test('completion', t => {
  const dir = t.testdir()
  npm.localPrefix = dir
  t.test('already have a script name', t => {
    runScript.completion({conf: {argv: {remain: ['npm', 'run', 'x']}}}, (er, results) => {
      if (er)
        throw er

      t.equal(results, undefined)
      t.end()
    })
  })
  t.test('no package.json', t => {
    runScript.completion({conf: {argv: {remain: ['npm', 'run']}}}, (er, results) => {
      if (er)
        throw er

      t.strictSame(results, [])
      t.end()
    })
  })
  t.test('has package.json, no scripts', t => {
    writeFileSync(`${dir}/package.json`, JSON.stringify({}))
    runScript.completion({conf: {argv: {remain: ['npm', 'run']}}}, (er, results) => {
      if (er)
        throw er

      t.strictSame(results, [])
      t.end()
    })
  })
  t.test('has package.json, with scripts', t => {
    writeFileSync(`${dir}/package.json`, JSON.stringify({
      scripts: { hello: 'echo hello', world: 'echo world' },
    }))
    runScript.completion({conf: {argv: {remain: ['npm', 'run']}}}, (er, results) => {
      if (er)
        throw er

      t.strictSame(results, ['hello', 'world'])
      t.end()
    })
  })
  t.end()
})

t.test('fail if no package.json', async t => {
  npm.localPrefix = t.testdir()
  await runScript([], er => t.match(er, { code: 'ENOENT' }))
  await runScript(['test'], er => t.match(er, { code: 'ENOENT' }))
})

t.test('default env, start, and restart scripts', async t => {
  npm.localPrefix = t.testdir({
    'package.json': JSON.stringify({ name: 'x', version: '1.2.3' }),
    'server.js': 'console.log("hello, world")',
  })

  await runScript(['start'], er => {
    if (er)
      throw er

    t.match(RUN_SCRIPTS, [
      {
        path: npm.localPrefix,
        args: [],
        scriptShell: undefined,
        stdio: 'inherit',
        stdioString: true,
        pkg: { name: 'x', version: '1.2.3', _id: 'x@1.2.3', scripts: {}},
        event: 'start',
      },
    ])
  })
  RUN_SCRIPTS.length = 0

  await runScript(['env'], er => {
    if (er)
      throw er

    t.match(RUN_SCRIPTS, [
      {
        path: npm.localPrefix,
        args: [],
        scriptShell: undefined,
        stdio: 'inherit',
        stdioString: true,
        pkg: { name: 'x',
          version: '1.2.3',
          _id: 'x@1.2.3',
          scripts: {
            env: 'env',
          } },
        event: 'env',
      },
    ])
  })
  RUN_SCRIPTS.length = 0

  await runScriptWin(['env'], er => {
    if (er)
      throw er

    t.match(RUN_SCRIPTS, [
      {
        path: npm.localPrefix,
        args: [],
        scriptShell: undefined,
        stdio: 'inherit',
        stdioString: true,
        pkg: { name: 'x',
          version: '1.2.3',
          _id: 'x@1.2.3',
          scripts: {
            env: 'SET',
          } },
        event: 'env',
      },
    ])
  })
  RUN_SCRIPTS.length = 0

  await runScript(['restart'], er => {
    if (er)
      throw er

    t.match(RUN_SCRIPTS, [
      {
        path: npm.localPrefix,
        args: [],
        scriptShell: undefined,
        stdio: 'inherit',
        stdioString: true,
        pkg: { name: 'x',
          version: '1.2.3',
          _id: 'x@1.2.3',
          scripts: {
            restart: 'npm stop --if-present && npm start',
          } },
        event: 'restart',
      },
    ])
  })
  RUN_SCRIPTS.length = 0
})

t.test('try to run missing script', t => {
  npm.localPrefix = t.testdir({
    'package.json': JSON.stringify({
      scripts: { hello: 'world' },
    }),
  })
  t.test('no suggestions', async t => {
    await runScript(['notevenclose'], er => {
      t.match(er, {
        message: 'missing script: notevenclose',
      })
    })
  })
  t.test('suggestions', async t => {
    await runScript(['helo'], er => {
      t.match(er, {
        message: 'missing script: helo\n\nDid you mean this?\n    hello',
      })
    })
  })
  t.test('with --if-present', async t => {
    npm.config.set('if-present', true)
    await runScript(['goodbye'], er => {
      if (er)
        throw er

      t.strictSame(RUN_SCRIPTS, [], 'did not try to run anything')
    })
  })
  t.end()
})

t.test('run pre/post hooks', async t => {
  npm.localPrefix = t.testdir({
    'package.json': JSON.stringify({
      name: 'x',
      version: '1.2.3',
      scripts: {
        preenv: 'echo before the env',
        postenv: 'echo after the env',
      },
    }),
  })

  await runScript(['env'], er => {
    if (er)
      throw er

    t.match(RUN_SCRIPTS, [
      { event: 'preenv' },
      {
        path: npm.localPrefix,
        args: [],
        scriptShell: undefined,
        stdio: 'inherit',
        stdioString: true,
        pkg: { name: 'x',
          version: '1.2.3',
          _id: 'x@1.2.3',
          scripts: {
            env: 'env',
          } },
        event: 'env',
      },
      { event: 'postenv' },
    ])
  })
  RUN_SCRIPTS.length = 0
})

t.test('skip pre/post hooks when using ignoreScripts', async t => {
  npm.flatOptions.ignoreScripts = true

  npm.localPrefix = t.testdir({
    'package.json': JSON.stringify({
      name: 'x',
      version: '1.2.3',
      scripts: {
        preenv: 'echo before the env',
        postenv: 'echo after the env',
      },
    }),
  })

  await runScript(['env'], er => {
    if (er)
      throw er

    t.deepEqual(RUN_SCRIPTS, [
      {
        path: npm.localPrefix,
        args: [],
        scriptShell: undefined,
        stdio: 'inherit',
        stdioString: true,
        pkg: { name: 'x',
          version: '1.2.3',
          _id: 'x@1.2.3',
          scripts: {
            preenv: 'echo before the env',
            postenv: 'echo after the env',
            env: 'env',
          } },
        banner: true,
        event: 'env',
      },
    ])

    delete npm.flatOptions.ignoreScripts
  })
  RUN_SCRIPTS.length = 0
})

t.test('run silent', async t => {
  npmlog.level = 'silent'
  t.teardown(() => {
    npmlog.level = 'warn'
  })

  npm.localPrefix = t.testdir({
    'package.json': JSON.stringify({
      name: 'x',
      version: '1.2.3',
      scripts: {
        preenv: 'echo before the env',
        postenv: 'echo after the env',
      },
    }),
  })

  await runScript(['env'], er => {
    if (er)
      throw er

    t.match(RUN_SCRIPTS, [
      {
        event: 'preenv',
        stdio: 'inherit',
      },
      {
        path: npm.localPrefix,
        args: [],
        scriptShell: undefined,
        stdio: 'inherit',
        stdioString: true,
        pkg: { name: 'x',
          version: '1.2.3',
          _id: 'x@1.2.3',
          scripts: {
            env: 'env',
          } },
        event: 'env',
        banner: false,
      },
      {
        event: 'postenv',
        stdio: 'inherit',
      },
    ])
  })
  RUN_SCRIPTS.length = 0
})

t.test('list scripts', async t => {
  const scripts = {
    test: 'exit 2',
    start: 'node server.js',
    stop: 'node kill-server.js',
    preenv: 'echo before the env',
    postenv: 'echo after the env',
  }
  npm.localPrefix = t.testdir({
    'package.json': JSON.stringify({
      name: 'x',
      version: '1.2.3',
      scripts,
    }),
  })

  await runScript([], er => {
    if (er)
      throw er
  })
  t.strictSame(output, [
    ['Lifecycle scripts included in x:'],
    ['  test\n    exit 2'],
    ['  start\n    node server.js'],
    ['  stop\n    node kill-server.js'],
    ['\navailable via `npm run-script`:'],
    ['  preenv\n    echo before the env'],
    ['  postenv\n    echo after the env'],
  ], 'basic report')
  output.length = 0

  npmlog.level = 'silent'
  await runScript([], er => {
    if (er)
      throw er
  })
  t.strictSame(output, [])
  npmlog.level = 'warn'

  npm.flatOptions.json = true
  await runScript([], er => {
    if (er)
      throw er
  })
  t.strictSame(output, [[JSON.stringify(scripts, 0, 2)]], 'json report')
  output.length = 0
  npm.flatOptions.json = false

  npm.flatOptions.parseable = true
  await runScript([], er => {
    if (er)
      throw er
  })
  t.strictSame(output, [
    ['test:exit 2'],
    ['start:node server.js'],
    ['stop:node kill-server.js'],
    ['preenv:echo before the env'],
    ['postenv:echo after the env'],
  ])
  output.length = 0
  npm.flatOptions.parseable = false
})

t.test('list scripts when no scripts', async t => {
  npm.localPrefix = t.testdir({
    'package.json': JSON.stringify({
      name: 'x',
      version: '1.2.3',
    }),
  })

  await runScript([], er => {
    if (er)
      throw er
  })
  t.strictSame(output, [], 'nothing to report')
  output.length = 0
})

t.test('list scripts, only commands', async t => {
  npm.localPrefix = t.testdir({
    'package.json': JSON.stringify({
      name: 'x',
      version: '1.2.3',
      scripts: { preversion: 'echo doing the version dance' },
    }),
  })

  await runScript([], er => {
    if (er)
      throw er
  })
  t.strictSame(output, [
    ['Lifecycle scripts included in x:'],
    ['  preversion\n    echo doing the version dance'],
  ])
  output.length = 0
})

t.test('list scripts, only non-commands', async t => {
  npm.localPrefix = t.testdir({
    'package.json': JSON.stringify({
      name: 'x',
      version: '1.2.3',
      scripts: { glorp: 'echo doing the glerp glop' },
    }),
  })

  await runScript([], er => {
    if (er)
      throw er
  })
  t.strictSame(output, [
    ['Scripts available in x via `npm run-script`:'],
    ['  glorp\n    echo doing the glerp glop'],
  ])
  output.length = 0
})