11cb0ef41Sopenharmony_ciconst t = require('tap')
21cb0ef41Sopenharmony_ciconst fs = require('fs/promises')
31cb0ef41Sopenharmony_ciconst path = require('path')
41cb0ef41Sopenharmony_ciconst { load: loadMockNpm } = require('../../fixtures/mock-npm.js')
51cb0ef41Sopenharmony_ciconst MockRegistry = require('@npmcli/mock-registry')
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_cit.test('call with args', async t => {
81cb0ef41Sopenharmony_ci  const { npm } = await loadMockNpm(t, {
91cb0ef41Sopenharmony_ci    config: {
101cb0ef41Sopenharmony_ci      call: 'foo',
111cb0ef41Sopenharmony_ci    },
121cb0ef41Sopenharmony_ci  })
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ci  await t.rejects(
151cb0ef41Sopenharmony_ci    npm.exec('exec', ['bar']),
161cb0ef41Sopenharmony_ci    { code: 'EUSAGE' }
171cb0ef41Sopenharmony_ci  )
181cb0ef41Sopenharmony_ci})
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_cit.test('registry package', async t => {
211cb0ef41Sopenharmony_ci  const registry = new MockRegistry({
221cb0ef41Sopenharmony_ci    tap: t,
231cb0ef41Sopenharmony_ci    registry: 'https://registry.npmjs.org/',
241cb0ef41Sopenharmony_ci  })
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci  const manifest = registry.manifest({ name: '@npmcli/npx-test' })
271cb0ef41Sopenharmony_ci  manifest.versions['1.0.0'].bin = { 'npx-test': 'index.js' }
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci  const { npm } = await loadMockNpm(t, {
301cb0ef41Sopenharmony_ci    config: {
311cb0ef41Sopenharmony_ci      audit: false,
321cb0ef41Sopenharmony_ci      yes: true,
331cb0ef41Sopenharmony_ci    },
341cb0ef41Sopenharmony_ci    prefixDir: {
351cb0ef41Sopenharmony_ci      'npm-exec-test': {
361cb0ef41Sopenharmony_ci        'package.json': JSON.stringify(manifest),
371cb0ef41Sopenharmony_ci        'index.js': `#!/usr/bin/env node
381cb0ef41Sopenharmony_ci  require('fs').writeFileSync('npm-exec-test-success', '')`,
391cb0ef41Sopenharmony_ci      },
401cb0ef41Sopenharmony_ci    },
411cb0ef41Sopenharmony_ci  })
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci  await registry.package({
441cb0ef41Sopenharmony_ci    times: 2,
451cb0ef41Sopenharmony_ci    manifest,
461cb0ef41Sopenharmony_ci    tarballs: {
471cb0ef41Sopenharmony_ci      '1.0.0': path.join(npm.prefix, 'npm-exec-test'),
481cb0ef41Sopenharmony_ci    } })
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci  await npm.exec('exec', ['@npmcli/npx-test'])
511cb0ef41Sopenharmony_ci  const exists = await fs.stat(path.join(npm.prefix, 'npm-exec-test-success'))
521cb0ef41Sopenharmony_ci  t.ok(exists.isFile(), 'bin ran, creating file')
531cb0ef41Sopenharmony_ci})
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_cit.test('--prefix', async t => {
561cb0ef41Sopenharmony_ci  const registry = new MockRegistry({
571cb0ef41Sopenharmony_ci    tap: t,
581cb0ef41Sopenharmony_ci    registry: 'https://registry.npmjs.org/',
591cb0ef41Sopenharmony_ci  })
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci  const manifest = registry.manifest({ name: '@npmcli/npx-test' })
621cb0ef41Sopenharmony_ci  manifest.versions['1.0.0'].bin = { 'npx-test': 'index.js' }
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci  const { npm } = await loadMockNpm(t, {
651cb0ef41Sopenharmony_ci    config: {
661cb0ef41Sopenharmony_ci      audit: false,
671cb0ef41Sopenharmony_ci      yes: true,
681cb0ef41Sopenharmony_ci    },
691cb0ef41Sopenharmony_ci    prefixDir: {
701cb0ef41Sopenharmony_ci      'npm-exec-test': {
711cb0ef41Sopenharmony_ci        'package.json': JSON.stringify(manifest),
721cb0ef41Sopenharmony_ci        'index.js': `#!/usr/bin/env node
731cb0ef41Sopenharmony_ci  require('fs').writeFileSync('npm-exec-test-success', '')`,
741cb0ef41Sopenharmony_ci      },
751cb0ef41Sopenharmony_ci    },
761cb0ef41Sopenharmony_ci  })
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ci  // This is what `--prefix` does
791cb0ef41Sopenharmony_ci  npm.globalPrefix = npm.localPrefix
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci  await registry.package({
821cb0ef41Sopenharmony_ci    manifest,
831cb0ef41Sopenharmony_ci    tarballs: {
841cb0ef41Sopenharmony_ci      '1.0.0': path.join(npm.prefix, 'npm-exec-test'),
851cb0ef41Sopenharmony_ci    } })
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ci  await npm.exec('exec', ['@npmcli/npx-test'])
881cb0ef41Sopenharmony_ci  const exists = await fs.stat(path.join(npm.prefix, 'npm-exec-test-success'))
891cb0ef41Sopenharmony_ci  t.ok(exists.isFile(), 'bin ran, creating file')
901cb0ef41Sopenharmony_ci})
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_cit.test('runs in workspace path', async t => {
931cb0ef41Sopenharmony_ci  const registry = new MockRegistry({
941cb0ef41Sopenharmony_ci    tap: t,
951cb0ef41Sopenharmony_ci    registry: 'https://registry.npmjs.org/',
961cb0ef41Sopenharmony_ci  })
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_ci  const manifest = registry.manifest({ name: '@npmcli/npx-test' })
991cb0ef41Sopenharmony_ci  manifest.versions['1.0.0'].bin = { 'npx-test': 'index.js' }
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_ci  const { npm } = await loadMockNpm(t, {
1021cb0ef41Sopenharmony_ci    config: {
1031cb0ef41Sopenharmony_ci      audit: false,
1041cb0ef41Sopenharmony_ci      yes: true,
1051cb0ef41Sopenharmony_ci      workspace: ['workspace-a'],
1061cb0ef41Sopenharmony_ci    },
1071cb0ef41Sopenharmony_ci    prefixDir: {
1081cb0ef41Sopenharmony_ci      'npm-exec-test': {
1091cb0ef41Sopenharmony_ci        'package.json': JSON.stringify(manifest),
1101cb0ef41Sopenharmony_ci        'index.js': `#!/usr/bin/env node
1111cb0ef41Sopenharmony_ci  require('fs').writeFileSync('npm-exec-test-success', '')`,
1121cb0ef41Sopenharmony_ci      },
1131cb0ef41Sopenharmony_ci      'package.json': JSON.stringify({
1141cb0ef41Sopenharmony_ci        name: '@npmcli/npx-workspace-test',
1151cb0ef41Sopenharmony_ci        workspaces: ['workspace-a'],
1161cb0ef41Sopenharmony_ci      }),
1171cb0ef41Sopenharmony_ci      'workspace-a': {
1181cb0ef41Sopenharmony_ci        'package.json': JSON.stringify({
1191cb0ef41Sopenharmony_ci          name: 'workspace-a',
1201cb0ef41Sopenharmony_ci        }),
1211cb0ef41Sopenharmony_ci      },
1221cb0ef41Sopenharmony_ci    },
1231cb0ef41Sopenharmony_ci  })
1241cb0ef41Sopenharmony_ci
1251cb0ef41Sopenharmony_ci  await registry.package({ manifest,
1261cb0ef41Sopenharmony_ci    tarballs: {
1271cb0ef41Sopenharmony_ci      '1.0.0': path.join(npm.prefix, 'npm-exec-test'),
1281cb0ef41Sopenharmony_ci    },
1291cb0ef41Sopenharmony_ci  })
1301cb0ef41Sopenharmony_ci  await npm.exec('exec', ['@npmcli/npx-test'])
1311cb0ef41Sopenharmony_ci  const exists = await fs.stat(path.join(npm.prefix, 'workspace-a', 'npm-exec-test-success'))
1321cb0ef41Sopenharmony_ci  t.ok(exists.isFile(), 'bin ran, creating file inside workspace')
1331cb0ef41Sopenharmony_ci})
1341cb0ef41Sopenharmony_ci
1351cb0ef41Sopenharmony_cit.test('finds workspace bin first', async t => {
1361cb0ef41Sopenharmony_ci  const { npm } = await loadMockNpm(t, {
1371cb0ef41Sopenharmony_ci    config: {
1381cb0ef41Sopenharmony_ci      workspace: ['workspace-a'],
1391cb0ef41Sopenharmony_ci    },
1401cb0ef41Sopenharmony_ci    prefixDir: {
1411cb0ef41Sopenharmony_ci      'package.json': JSON.stringify({
1421cb0ef41Sopenharmony_ci        name: '@npmcli/npx-workspace-root-test',
1431cb0ef41Sopenharmony_ci        bin: { 'npx-test': 'index.js' },
1441cb0ef41Sopenharmony_ci        workspaces: ['workspace-a'],
1451cb0ef41Sopenharmony_ci      }),
1461cb0ef41Sopenharmony_ci      'index.js': `#!/usr/bin/env node
1471cb0ef41Sopenharmony_ci  require('fs').writeFileSync('npm-exec-test-fail', '')`,
1481cb0ef41Sopenharmony_ci      'workspace-a': {
1491cb0ef41Sopenharmony_ci        'package.json': JSON.stringify({
1501cb0ef41Sopenharmony_ci          name: '@npmcli/npx-workspace-test',
1511cb0ef41Sopenharmony_ci          bin: { 'npx-test': 'index.js' },
1521cb0ef41Sopenharmony_ci        }),
1531cb0ef41Sopenharmony_ci        'index.js': `#!/usr/bin/env node
1541cb0ef41Sopenharmony_ci  require('fs').writeFileSync('npm-exec-test-success', '')`,
1551cb0ef41Sopenharmony_ci      },
1561cb0ef41Sopenharmony_ci    },
1571cb0ef41Sopenharmony_ci  })
1581cb0ef41Sopenharmony_ci
1591cb0ef41Sopenharmony_ci  await npm.exec('install', []) // reify
1601cb0ef41Sopenharmony_ci  await npm.exec('exec', ['npx-test'])
1611cb0ef41Sopenharmony_ci  const exists = await fs.stat(path.join(npm.prefix, 'workspace-a', 'npm-exec-test-success'))
1621cb0ef41Sopenharmony_ci  t.ok(exists.isFile(), 'bin ran, creating file inside workspace')
1631cb0ef41Sopenharmony_ci  t.rejects(fs.stat(path.join(npm.prefix, 'npm-exec-test-fail')))
1641cb0ef41Sopenharmony_ci})
1651cb0ef41Sopenharmony_ci
1661cb0ef41Sopenharmony_cit.test('finds workspace dep first', async t => {
1671cb0ef41Sopenharmony_ci  const registry = new MockRegistry({
1681cb0ef41Sopenharmony_ci    tap: t,
1691cb0ef41Sopenharmony_ci    registry: 'https://registry.npmjs.org/',
1701cb0ef41Sopenharmony_ci  })
1711cb0ef41Sopenharmony_ci
1721cb0ef41Sopenharmony_ci  const manifest = registry.manifest({ name: '@npmcli/subdep', versions: ['1.0.0', '2.0.0'] })
1731cb0ef41Sopenharmony_ci  manifest.versions['1.0.0'].bin = { 'npx-test': 'index.js' }
1741cb0ef41Sopenharmony_ci  manifest.versions['2.0.0'].bin = { 'npx-test': 'index.js' }
1751cb0ef41Sopenharmony_ci
1761cb0ef41Sopenharmony_ci  const { npm } = await loadMockNpm(t, {
1771cb0ef41Sopenharmony_ci    prefixDir: {
1781cb0ef41Sopenharmony_ci      subdep: {
1791cb0ef41Sopenharmony_ci        one: {
1801cb0ef41Sopenharmony_ci          'package.json': JSON.stringify(manifest.versions['1.0.0']),
1811cb0ef41Sopenharmony_ci          'index.js': `#!/usr/bin/env node
1821cb0ef41Sopenharmony_ci  require('fs').writeFileSync('npm-exec-test-one', '')`,
1831cb0ef41Sopenharmony_ci        },
1841cb0ef41Sopenharmony_ci        two: {
1851cb0ef41Sopenharmony_ci          'package.json': JSON.stringify(manifest.versions['2.0.0']),
1861cb0ef41Sopenharmony_ci          'index.js': `#!/usr/bin/env node
1871cb0ef41Sopenharmony_ci  require('fs').writeFileSync('npm-exec-test-two', '')`,
1881cb0ef41Sopenharmony_ci        },
1891cb0ef41Sopenharmony_ci      },
1901cb0ef41Sopenharmony_ci      'package.json': JSON.stringify({
1911cb0ef41Sopenharmony_ci        name: '@npmcli/npx-workspace-root-test',
1921cb0ef41Sopenharmony_ci        dependencies: { '@npmcli/subdep': '1.0.0' },
1931cb0ef41Sopenharmony_ci        bin: { 'npx-test': 'index.js' },
1941cb0ef41Sopenharmony_ci        workspaces: ['workspace-a'],
1951cb0ef41Sopenharmony_ci      }),
1961cb0ef41Sopenharmony_ci      'index.js': `#!/usr/bin/env node
1971cb0ef41Sopenharmony_ci  require('fs').writeFileSync('npm-exec-test-fail', '')`,
1981cb0ef41Sopenharmony_ci      'workspace-a': {
1991cb0ef41Sopenharmony_ci        'package.json': JSON.stringify({
2001cb0ef41Sopenharmony_ci          name: '@npmcli/npx-workspace-test',
2011cb0ef41Sopenharmony_ci          dependencies: { '@npmcli/subdep': '2.0.0' },
2021cb0ef41Sopenharmony_ci          bin: { 'npx-test': 'index.js' },
2031cb0ef41Sopenharmony_ci        }),
2041cb0ef41Sopenharmony_ci        'index.js': `#!/usr/bin/env node
2051cb0ef41Sopenharmony_ci  require('fs').writeFileSync('npm-exec-test-success', '')`,
2061cb0ef41Sopenharmony_ci      },
2071cb0ef41Sopenharmony_ci    },
2081cb0ef41Sopenharmony_ci  })
2091cb0ef41Sopenharmony_ci
2101cb0ef41Sopenharmony_ci  await registry.package({ manifest,
2111cb0ef41Sopenharmony_ci    tarballs: {
2121cb0ef41Sopenharmony_ci      '1.0.0': path.join(npm.prefix, 'subdep', 'one'),
2131cb0ef41Sopenharmony_ci      '2.0.0': path.join(npm.prefix, 'subdep', 'two'),
2141cb0ef41Sopenharmony_ci    },
2151cb0ef41Sopenharmony_ci  })
2161cb0ef41Sopenharmony_ci  await npm.exec('install', [])
2171cb0ef41Sopenharmony_ci  npm.config.set('workspace', ['workspace-a'])
2181cb0ef41Sopenharmony_ci  await npm.exec('exec', ['npx-test'])
2191cb0ef41Sopenharmony_ci  const exists = await fs.stat(path.join(npm.prefix, 'workspace-a', 'npm-exec-test-success'))
2201cb0ef41Sopenharmony_ci  t.ok(exists.isFile(), 'bin ran, creating file')
2211cb0ef41Sopenharmony_ci})
2221cb0ef41Sopenharmony_ci
2231cb0ef41Sopenharmony_cit.test('npx --no-install @npmcli/npx-test', async t => {
2241cb0ef41Sopenharmony_ci  const registry = new MockRegistry({
2251cb0ef41Sopenharmony_ci    tap: t,
2261cb0ef41Sopenharmony_ci    registry: 'https://registry.npmjs.org/',
2271cb0ef41Sopenharmony_ci  })
2281cb0ef41Sopenharmony_ci
2291cb0ef41Sopenharmony_ci  const manifest = registry.manifest({ name: '@npmcli/npx-test' })
2301cb0ef41Sopenharmony_ci  manifest.versions['1.0.0'].bin = { 'npx-test': 'index.js' }
2311cb0ef41Sopenharmony_ci
2321cb0ef41Sopenharmony_ci  const { npm } = await loadMockNpm(t, {
2331cb0ef41Sopenharmony_ci    config: {
2341cb0ef41Sopenharmony_ci      audit: false,
2351cb0ef41Sopenharmony_ci      yes: false,
2361cb0ef41Sopenharmony_ci    },
2371cb0ef41Sopenharmony_ci    prefixDir: {
2381cb0ef41Sopenharmony_ci      'npm-exec-test': {
2391cb0ef41Sopenharmony_ci        'package.json': JSON.stringify(manifest),
2401cb0ef41Sopenharmony_ci        'index.js': `#!/usr/bin/env node
2411cb0ef41Sopenharmony_ci  require('fs').writeFileSync('npm-exec-test-success', '')`,
2421cb0ef41Sopenharmony_ci      },
2431cb0ef41Sopenharmony_ci    },
2441cb0ef41Sopenharmony_ci  })
2451cb0ef41Sopenharmony_ci
2461cb0ef41Sopenharmony_ci  try {
2471cb0ef41Sopenharmony_ci    await npm.exec('exec', ['@npmcli/npx-test'])
2481cb0ef41Sopenharmony_ci    t.fail('Expected error was not thrown')
2491cb0ef41Sopenharmony_ci  } catch (error) {
2501cb0ef41Sopenharmony_ci    t.match(
2511cb0ef41Sopenharmony_ci      error.message,
2521cb0ef41Sopenharmony_ci      'npx canceled due to missing packages and no YES option: ',
2531cb0ef41Sopenharmony_ci      'Expected error message thrown'
2541cb0ef41Sopenharmony_ci    )
2551cb0ef41Sopenharmony_ci  }
2561cb0ef41Sopenharmony_ci})
257