11cb0ef41Sopenharmony_ciconst t = require('tap')
21cb0ef41Sopenharmony_ciconst path = require('path')
31cb0ef41Sopenharmony_ciconst tspawk = require('../../fixtures/tspawk')
41cb0ef41Sopenharmony_ciconst { load: loadMockNpm } = require('../../fixtures/mock-npm')
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst spawk = tspawk(t)
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst npmConfig = {
91cb0ef41Sopenharmony_ci  config: {
101cb0ef41Sopenharmony_ci    'ignore-scripts': false,
111cb0ef41Sopenharmony_ci    editor: 'testeditor',
121cb0ef41Sopenharmony_ci    'script-shell': process.platform === 'win32' ? process.env.COMSPEC : 'sh',
131cb0ef41Sopenharmony_ci  },
141cb0ef41Sopenharmony_ci  prefixDir: {
151cb0ef41Sopenharmony_ci    node_modules: {
161cb0ef41Sopenharmony_ci      semver: {
171cb0ef41Sopenharmony_ci        'package.json': JSON.stringify({
181cb0ef41Sopenharmony_ci          scripts: {
191cb0ef41Sopenharmony_ci            install: 'testinstall',
201cb0ef41Sopenharmony_ci          },
211cb0ef41Sopenharmony_ci        }),
221cb0ef41Sopenharmony_ci        node_modules: {
231cb0ef41Sopenharmony_ci          abbrev: {},
241cb0ef41Sopenharmony_ci        },
251cb0ef41Sopenharmony_ci      },
261cb0ef41Sopenharmony_ci      '@npmcli': {
271cb0ef41Sopenharmony_ci        'scoped-package': {},
281cb0ef41Sopenharmony_ci      },
291cb0ef41Sopenharmony_ci    },
301cb0ef41Sopenharmony_ci  },
311cb0ef41Sopenharmony_ci}
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ciconst isCmdRe = /(?:^|\\)cmd(?:\.exe)?$/i
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_cit.test('npm edit', async t => {
361cb0ef41Sopenharmony_ci  const { npm, joinedOutput } = await loadMockNpm(t, npmConfig)
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci  const semverPath = path.resolve(npm.prefix, 'node_modules', 'semver')
391cb0ef41Sopenharmony_ci  spawk.spawn('testeditor', [semverPath])
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci  const scriptShell = npm.config.get('script-shell')
421cb0ef41Sopenharmony_ci  const scriptArgs = isCmdRe.test(scriptShell)
431cb0ef41Sopenharmony_ci    ? ['/d', '/s', '/c', 'testinstall']
441cb0ef41Sopenharmony_ci    : ['-c', 'testinstall']
451cb0ef41Sopenharmony_ci  spawk.spawn(scriptShell, scriptArgs, { cwd: semverPath })
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci  await npm.exec('edit', ['semver'])
481cb0ef41Sopenharmony_ci  t.match(joinedOutput(), 'rebuilt dependencies successfully')
491cb0ef41Sopenharmony_ci})
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_cit.test('rebuild failure', async t => {
521cb0ef41Sopenharmony_ci  const { npm } = await loadMockNpm(t, npmConfig)
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci  const semverPath = path.resolve(npm.prefix, 'node_modules', 'semver')
551cb0ef41Sopenharmony_ci  spawk.spawn('testeditor', [semverPath])
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci  const scriptShell = npm.config.get('script-shell')
581cb0ef41Sopenharmony_ci  const scriptArgs = isCmdRe.test(scriptShell)
591cb0ef41Sopenharmony_ci    ? ['/d', '/s', '/c', 'testinstall']
601cb0ef41Sopenharmony_ci    : ['-c', 'testinstall']
611cb0ef41Sopenharmony_ci  spawk.spawn(scriptShell, scriptArgs, { cwd: semverPath }).exit(1).stdout('test error')
621cb0ef41Sopenharmony_ci  await t.rejects(
631cb0ef41Sopenharmony_ci    npm.exec('edit', ['semver']),
641cb0ef41Sopenharmony_ci    { message: 'command failed' }
651cb0ef41Sopenharmony_ci  )
661cb0ef41Sopenharmony_ci})
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_cit.test('editor failure', async t => {
691cb0ef41Sopenharmony_ci  const { npm } = await loadMockNpm(t, npmConfig)
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci  const semverPath = path.resolve(npm.prefix, 'node_modules', 'semver')
721cb0ef41Sopenharmony_ci  spawk.spawn('testeditor', [semverPath]).exit(1).stdout('test editor failure')
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci  await t.rejects(
751cb0ef41Sopenharmony_ci    npm.exec('edit', ['semver']),
761cb0ef41Sopenharmony_ci    { message: 'editor process exited with code: 1' }
771cb0ef41Sopenharmony_ci  )
781cb0ef41Sopenharmony_ci})
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_cit.test('npm edit editor has flags', async t => {
811cb0ef41Sopenharmony_ci  const { npm } = await loadMockNpm(t, {
821cb0ef41Sopenharmony_ci    ...npmConfig,
831cb0ef41Sopenharmony_ci    config: {
841cb0ef41Sopenharmony_ci      ...npmConfig.config,
851cb0ef41Sopenharmony_ci      editor: 'testeditor --flag',
861cb0ef41Sopenharmony_ci    },
871cb0ef41Sopenharmony_ci  })
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_ci  const semverPath = path.resolve(npm.prefix, 'node_modules', 'semver')
901cb0ef41Sopenharmony_ci  spawk.spawn('testeditor', ['--flag', semverPath])
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ci  const scriptShell = npm.config.get('script-shell')
931cb0ef41Sopenharmony_ci  const scriptArgs = isCmdRe.test(scriptShell)
941cb0ef41Sopenharmony_ci    ? ['/d', '/s', '/c', 'testinstall']
951cb0ef41Sopenharmony_ci    : ['-c', 'testinstall']
961cb0ef41Sopenharmony_ci  spawk.spawn(scriptShell, scriptArgs, { cwd: semverPath })
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_ci  await npm.exec('edit', ['semver'])
991cb0ef41Sopenharmony_ci})
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_cit.test('npm edit no args', async t => {
1021cb0ef41Sopenharmony_ci  const { npm } = await loadMockNpm(t)
1031cb0ef41Sopenharmony_ci  await t.rejects(
1041cb0ef41Sopenharmony_ci    npm.exec('edit', []),
1051cb0ef41Sopenharmony_ci    { code: 'EUSAGE' },
1061cb0ef41Sopenharmony_ci    'throws usage error'
1071cb0ef41Sopenharmony_ci  )
1081cb0ef41Sopenharmony_ci})
1091cb0ef41Sopenharmony_ci
1101cb0ef41Sopenharmony_cit.test('npm edit nonexistent package', async t => {
1111cb0ef41Sopenharmony_ci  const { npm } = await loadMockNpm(t, npmConfig)
1121cb0ef41Sopenharmony_ci
1131cb0ef41Sopenharmony_ci  await t.rejects(
1141cb0ef41Sopenharmony_ci    npm.exec('edit', ['abbrev']),
1151cb0ef41Sopenharmony_ci    /lstat/
1161cb0ef41Sopenharmony_ci  )
1171cb0ef41Sopenharmony_ci})
1181cb0ef41Sopenharmony_ci
1191cb0ef41Sopenharmony_cit.test('scoped package', async t => {
1201cb0ef41Sopenharmony_ci  const { npm } = await loadMockNpm(t, npmConfig)
1211cb0ef41Sopenharmony_ci  const scopedPath = path.resolve(npm.prefix, 'node_modules', '@npmcli', 'scoped-package')
1221cb0ef41Sopenharmony_ci  spawk.spawn('testeditor', [scopedPath])
1231cb0ef41Sopenharmony_ci  await npm.exec('edit', ['@npmcli/scoped-package'])
1241cb0ef41Sopenharmony_ci})
1251cb0ef41Sopenharmony_ci
1261cb0ef41Sopenharmony_cit.test('subdependency', async t => {
1271cb0ef41Sopenharmony_ci  const { npm } = await loadMockNpm(t, npmConfig)
1281cb0ef41Sopenharmony_ci  const subdepPath = path.resolve(npm.prefix, 'node_modules', 'semver', 'node_modules', 'abbrev')
1291cb0ef41Sopenharmony_ci  spawk.spawn('testeditor', [subdepPath])
1301cb0ef41Sopenharmony_ci  await npm.exec('edit', ['semver/abbrev'])
1311cb0ef41Sopenharmony_ci})
132