1const t = require('tap')
2const mockGlobals = require('@npmcli/mock-globals')
3const tmock = require('../fixtures/tmock')
4
5const npm = require.resolve('../../bin/npm-cli.js')
6const npx = require.resolve('../../bin/npx-cli.js')
7
8const mockNpx = (t, argv) => {
9  const logs = []
10  mockGlobals(t, {
11    'process.argv': argv,
12    'console.error': (...msg) => logs.push(msg),
13  })
14  tmock(t, '{BIN}/npx-cli.js', { '{LIB}/cli.js': () => {} })
15  return {
16    logs,
17    argv: process.argv,
18  }
19}
20
21t.test('npx foo -> npm exec -- foo', async t => {
22  const { argv } = mockNpx(t, ['node', npx, 'foo'])
23  t.strictSame(argv, ['node', npm, 'exec', '--', 'foo'])
24})
25
26t.test('npx -- foo -> npm exec -- foo', async t => {
27  const { argv } = mockNpx(t, ['node', npx, '--', 'foo'])
28  t.strictSame(argv, ['node', npm, 'exec', '--', 'foo'])
29})
30
31t.test('npx -x y foo -z -> npm exec -x y -- foo -z', async t => {
32  const { argv } = mockNpx(t, ['node', npx, '-x', 'y', 'foo', '-z'])
33  t.strictSame(argv, ['node', npm, 'exec', '-x', 'y', '--', 'foo', '-z'])
34})
35
36t.test('npx --x=y --no-install foo -z -> npm exec --x=y -- foo -z', async t => {
37  const { argv } = mockNpx(t, ['node', npx, '--x=y', '--no-install', 'foo', '-z'])
38  t.strictSame(argv, ['node', npm, 'exec', '--x=y', '--yes=false', '--', 'foo', '-z'])
39})
40
41t.test('transform renamed options into proper values', async t => {
42  const { argv } = mockNpx(t, ['node', npx, '-y', '--shell=bash', '-p', 'foo', '-c', 'asdf'])
43  t.strictSame(argv, [
44    'node',
45    npm,
46    'exec',
47    '--yes',
48    '--script-shell=bash',
49    '--package',
50    'foo',
51    '--call',
52    'asdf',
53  ])
54})
55
56// warn if deprecated switches/options are used
57t.test('use a bunch of deprecated switches and options', async t => {
58  const { argv, logs } = mockNpx(t, [
59    'node',
60    npx,
61    '--npm',
62    '/some/npm/bin',
63    '--node-arg=--harmony',
64    '-n',
65    '--require=foobar',
66    '--reg=http://localhost:12345/',
67    '-p',
68    'foo',
69    '--always-spawn',
70    '--shell-auto-fallback',
71    '--ignore-existing',
72    '-q',
73    'foobar',
74  ])
75
76  const expect = [
77    'node',
78    npm,
79    'exec',
80    '--registry',
81    'http://localhost:12345/',
82    '--package',
83    'foo',
84    '--loglevel',
85    'warn',
86    '--',
87    'foobar',
88  ]
89  t.strictSame(argv, expect)
90  t.strictSame(logs, [
91    ['npx: the --npm argument has been removed.'],
92    ['npx: the --node-arg argument has been removed.'],
93    ['npx: the --n argument has been removed.'],
94    ['npx: the --always-spawn argument has been removed.'],
95    ['npx: the --shell-auto-fallback argument has been removed.'],
96    ['npx: the --ignore-existing argument has been removed.'],
97    ['See `npm help exec` for more information'],
98  ])
99})
100