11cb0ef41Sopenharmony_ciconst t = require('tap') 21cb0ef41Sopenharmony_ciconst { load: loadMockNpm } = require('../../fixtures/mock-npm') 31cb0ef41Sopenharmony_ci 41cb0ef41Sopenharmony_ciconst MockRegistry = require('@npmcli/mock-registry') 51cb0ef41Sopenharmony_ciconst user = 'test-user' 61cb0ef41Sopenharmony_ciconst pkg = 'test-package' 71cb0ef41Sopenharmony_ciconst auth = { '//registry.npmjs.org/:_authToken': 'test-auth-token' } 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_cit.test('no args --force success', async t => { 101cb0ef41Sopenharmony_ci const { joinedOutput, npm } = await loadMockNpm(t, { 111cb0ef41Sopenharmony_ci config: { 121cb0ef41Sopenharmony_ci force: true, 131cb0ef41Sopenharmony_ci ...auth, 141cb0ef41Sopenharmony_ci }, 151cb0ef41Sopenharmony_ci prefixDir: { 161cb0ef41Sopenharmony_ci 'package.json': JSON.stringify({ 171cb0ef41Sopenharmony_ci name: pkg, 181cb0ef41Sopenharmony_ci version: '1.0.0', 191cb0ef41Sopenharmony_ci }, null, 2), 201cb0ef41Sopenharmony_ci }, 211cb0ef41Sopenharmony_ci }) 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_ci const registry = new MockRegistry({ 241cb0ef41Sopenharmony_ci tap: t, 251cb0ef41Sopenharmony_ci registry: npm.config.get('registry'), 261cb0ef41Sopenharmony_ci authorization: 'test-auth-token', 271cb0ef41Sopenharmony_ci }) 281cb0ef41Sopenharmony_ci const manifest = registry.manifest({ name: pkg }) 291cb0ef41Sopenharmony_ci await registry.package({ manifest, query: { write: true }, times: 2 }) 301cb0ef41Sopenharmony_ci registry.unpublish({ manifest }) 311cb0ef41Sopenharmony_ci await npm.exec('unpublish', []) 321cb0ef41Sopenharmony_ci t.equal(joinedOutput(), '- test-package') 331cb0ef41Sopenharmony_ci}) 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_cit.test('no args --force missing package.json', async t => { 361cb0ef41Sopenharmony_ci const { npm } = await loadMockNpm(t, { 371cb0ef41Sopenharmony_ci config: { 381cb0ef41Sopenharmony_ci force: true, 391cb0ef41Sopenharmony_ci }, 401cb0ef41Sopenharmony_ci }) 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_ci await t.rejects( 431cb0ef41Sopenharmony_ci npm.exec('unpublish', []), 441cb0ef41Sopenharmony_ci { code: 'EUSAGE' }, 451cb0ef41Sopenharmony_ci 'should throw usage instructions on missing package.json' 461cb0ef41Sopenharmony_ci ) 471cb0ef41Sopenharmony_ci}) 481cb0ef41Sopenharmony_ci 491cb0ef41Sopenharmony_cit.test('no args --force error reading package.json', async t => { 501cb0ef41Sopenharmony_ci const { npm } = await loadMockNpm(t, { 511cb0ef41Sopenharmony_ci config: { 521cb0ef41Sopenharmony_ci force: true, 531cb0ef41Sopenharmony_ci }, 541cb0ef41Sopenharmony_ci prefixDir: { 551cb0ef41Sopenharmony_ci 'package.json': '{ not valid json ]', 561cb0ef41Sopenharmony_ci }, 571cb0ef41Sopenharmony_ci }) 581cb0ef41Sopenharmony_ci 591cb0ef41Sopenharmony_ci await t.rejects( 601cb0ef41Sopenharmony_ci npm.exec('unpublish', []), 611cb0ef41Sopenharmony_ci /Invalid package.json/, 621cb0ef41Sopenharmony_ci 'should throw error from reading package.json' 631cb0ef41Sopenharmony_ci ) 641cb0ef41Sopenharmony_ci}) 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_cit.test('with args --force error reading package.json', async t => { 671cb0ef41Sopenharmony_ci const { npm } = await loadMockNpm(t, { 681cb0ef41Sopenharmony_ci config: { 691cb0ef41Sopenharmony_ci force: true, 701cb0ef41Sopenharmony_ci }, 711cb0ef41Sopenharmony_ci prefixDir: { 721cb0ef41Sopenharmony_ci 'package.json': '{ not valid json ]', 731cb0ef41Sopenharmony_ci }, 741cb0ef41Sopenharmony_ci }) 751cb0ef41Sopenharmony_ci 761cb0ef41Sopenharmony_ci await t.rejects( 771cb0ef41Sopenharmony_ci npm.exec('unpublish', [pkg]), 781cb0ef41Sopenharmony_ci /Invalid package.json/, 791cb0ef41Sopenharmony_ci 'should throw error from reading package.json' 801cb0ef41Sopenharmony_ci ) 811cb0ef41Sopenharmony_ci}) 821cb0ef41Sopenharmony_ci 831cb0ef41Sopenharmony_cit.test('no force entire project', async t => { 841cb0ef41Sopenharmony_ci const { npm } = await loadMockNpm(t) 851cb0ef41Sopenharmony_ci 861cb0ef41Sopenharmony_ci await t.rejects( 871cb0ef41Sopenharmony_ci npm.exec('unpublish', ['@npmcli/unpublish-test']), 881cb0ef41Sopenharmony_ci /Refusing to delete entire project/ 891cb0ef41Sopenharmony_ci ) 901cb0ef41Sopenharmony_ci}) 911cb0ef41Sopenharmony_ci 921cb0ef41Sopenharmony_cit.test('too many args', async t => { 931cb0ef41Sopenharmony_ci const { npm } = await loadMockNpm(t) 941cb0ef41Sopenharmony_ci 951cb0ef41Sopenharmony_ci await t.rejects( 961cb0ef41Sopenharmony_ci npm.exec('unpublish', ['a', 'b']), 971cb0ef41Sopenharmony_ci { code: 'EUSAGE' }, 981cb0ef41Sopenharmony_ci 'should throw usage instructions if too many args' 991cb0ef41Sopenharmony_ci ) 1001cb0ef41Sopenharmony_ci}) 1011cb0ef41Sopenharmony_ci 1021cb0ef41Sopenharmony_cit.test('range', async t => { 1031cb0ef41Sopenharmony_ci const { npm } = await loadMockNpm(t) 1041cb0ef41Sopenharmony_ci 1051cb0ef41Sopenharmony_ci await t.rejects( 1061cb0ef41Sopenharmony_ci npm.exec('unpublish', ['a@>1.0.0']), 1071cb0ef41Sopenharmony_ci { code: 'EUSAGE' }, 1081cb0ef41Sopenharmony_ci /single version/ 1091cb0ef41Sopenharmony_ci ) 1101cb0ef41Sopenharmony_ci}) 1111cb0ef41Sopenharmony_ci 1121cb0ef41Sopenharmony_cit.test('tag', async t => { 1131cb0ef41Sopenharmony_ci const { npm } = await loadMockNpm(t) 1141cb0ef41Sopenharmony_ci 1151cb0ef41Sopenharmony_ci await t.rejects( 1161cb0ef41Sopenharmony_ci npm.exec('unpublish', ['a@>1.0.0']), 1171cb0ef41Sopenharmony_ci { code: 'EUSAGE' }, 1181cb0ef41Sopenharmony_ci /single version/ 1191cb0ef41Sopenharmony_ci ) 1201cb0ef41Sopenharmony_ci}) 1211cb0ef41Sopenharmony_ci 1221cb0ef41Sopenharmony_cit.test('unpublish <pkg>@version not the last version', async t => { 1231cb0ef41Sopenharmony_ci const { joinedOutput, npm } = await loadMockNpm(t, { 1241cb0ef41Sopenharmony_ci config: { 1251cb0ef41Sopenharmony_ci force: true, 1261cb0ef41Sopenharmony_ci ...auth, 1271cb0ef41Sopenharmony_ci }, 1281cb0ef41Sopenharmony_ci }) 1291cb0ef41Sopenharmony_ci const registry = new MockRegistry({ 1301cb0ef41Sopenharmony_ci tap: t, 1311cb0ef41Sopenharmony_ci registry: npm.config.get('registry'), 1321cb0ef41Sopenharmony_ci authorization: 'test-auth-token', 1331cb0ef41Sopenharmony_ci }) 1341cb0ef41Sopenharmony_ci const manifest = registry.manifest({ 1351cb0ef41Sopenharmony_ci name: pkg, 1361cb0ef41Sopenharmony_ci packuments: ['1.0.0', '1.0.1'], 1371cb0ef41Sopenharmony_ci }) 1381cb0ef41Sopenharmony_ci await registry.package({ manifest, query: { write: true }, times: 3 }) 1391cb0ef41Sopenharmony_ci registry.nock.put(`/${pkg}/-rev/${manifest._rev}`, body => { 1401cb0ef41Sopenharmony_ci // sets latest and deletes version 1.0.1 1411cb0ef41Sopenharmony_ci return body['dist-tags'].latest === '1.0.0' && body.versions['1.0.1'] === undefined 1421cb0ef41Sopenharmony_ci }).reply(201) 1431cb0ef41Sopenharmony_ci .intercept(`/${pkg}/-/${pkg}-1.0.1.tgz/-rev/${manifest._rev}`, 'DELETE').reply(201) 1441cb0ef41Sopenharmony_ci 1451cb0ef41Sopenharmony_ci await npm.exec('unpublish', ['test-package@1.0.1']) 1461cb0ef41Sopenharmony_ci t.equal(joinedOutput(), '- test-package@1.0.1') 1471cb0ef41Sopenharmony_ci}) 1481cb0ef41Sopenharmony_ci 1491cb0ef41Sopenharmony_cit.test('unpublish <pkg>@version last version', async t => { 1501cb0ef41Sopenharmony_ci const { npm } = await loadMockNpm(t, { 1511cb0ef41Sopenharmony_ci config: { 1521cb0ef41Sopenharmony_ci ...auth, 1531cb0ef41Sopenharmony_ci }, 1541cb0ef41Sopenharmony_ci }) 1551cb0ef41Sopenharmony_ci const registry = new MockRegistry({ 1561cb0ef41Sopenharmony_ci tap: t, 1571cb0ef41Sopenharmony_ci registry: npm.config.get('registry'), 1581cb0ef41Sopenharmony_ci authorization: 'test-auth-token', 1591cb0ef41Sopenharmony_ci }) 1601cb0ef41Sopenharmony_ci const manifest = registry.manifest({ name: pkg }) 1611cb0ef41Sopenharmony_ci await registry.package({ manifest, query: { write: true } }) 1621cb0ef41Sopenharmony_ci 1631cb0ef41Sopenharmony_ci await t.rejects( 1641cb0ef41Sopenharmony_ci npm.exec('unpublish', ['test-package@1.0.0']), 1651cb0ef41Sopenharmony_ci /Refusing to delete the last version of the package/ 1661cb0ef41Sopenharmony_ci ) 1671cb0ef41Sopenharmony_ci}) 1681cb0ef41Sopenharmony_ci 1691cb0ef41Sopenharmony_cit.test('no version found in package.json no force', async t => { 1701cb0ef41Sopenharmony_ci const { npm } = await loadMockNpm(t, { 1711cb0ef41Sopenharmony_ci config: { 1721cb0ef41Sopenharmony_ci ...auth, 1731cb0ef41Sopenharmony_ci }, 1741cb0ef41Sopenharmony_ci prefixDir: { 1751cb0ef41Sopenharmony_ci 'package.json': JSON.stringify({ 1761cb0ef41Sopenharmony_ci name: pkg, 1771cb0ef41Sopenharmony_ci }, null, 2), 1781cb0ef41Sopenharmony_ci }, 1791cb0ef41Sopenharmony_ci }) 1801cb0ef41Sopenharmony_ci await t.rejects( 1811cb0ef41Sopenharmony_ci npm.exec('unpublish', []), 1821cb0ef41Sopenharmony_ci /Refusing to delete entire project/ 1831cb0ef41Sopenharmony_ci ) 1841cb0ef41Sopenharmony_ci}) 1851cb0ef41Sopenharmony_ci 1861cb0ef41Sopenharmony_cit.test('no version found in package.json with force', async t => { 1871cb0ef41Sopenharmony_ci const { joinedOutput, npm } = await loadMockNpm(t, { 1881cb0ef41Sopenharmony_ci config: { 1891cb0ef41Sopenharmony_ci force: true, 1901cb0ef41Sopenharmony_ci ...auth, 1911cb0ef41Sopenharmony_ci }, 1921cb0ef41Sopenharmony_ci prefixDir: { 1931cb0ef41Sopenharmony_ci 'package.json': JSON.stringify({ 1941cb0ef41Sopenharmony_ci name: pkg, 1951cb0ef41Sopenharmony_ci }, null, 2), 1961cb0ef41Sopenharmony_ci }, 1971cb0ef41Sopenharmony_ci }) 1981cb0ef41Sopenharmony_ci const registry = new MockRegistry({ 1991cb0ef41Sopenharmony_ci tap: t, 2001cb0ef41Sopenharmony_ci registry: npm.config.get('registry'), 2011cb0ef41Sopenharmony_ci authorization: 'test-auth-token', 2021cb0ef41Sopenharmony_ci }) 2031cb0ef41Sopenharmony_ci const manifest = registry.manifest({ name: pkg }) 2041cb0ef41Sopenharmony_ci await registry.package({ manifest, query: { write: true }, times: 2 }) 2051cb0ef41Sopenharmony_ci registry.unpublish({ manifest }) 2061cb0ef41Sopenharmony_ci 2071cb0ef41Sopenharmony_ci await npm.exec('unpublish', []) 2081cb0ef41Sopenharmony_ci t.equal(joinedOutput(), '- test-package') 2091cb0ef41Sopenharmony_ci}) 2101cb0ef41Sopenharmony_ci 2111cb0ef41Sopenharmony_cit.test('unpublish <pkg> --force no version set', async t => { 2121cb0ef41Sopenharmony_ci const { joinedOutput, npm } = await loadMockNpm(t, { 2131cb0ef41Sopenharmony_ci config: { 2141cb0ef41Sopenharmony_ci force: true, 2151cb0ef41Sopenharmony_ci ...auth, 2161cb0ef41Sopenharmony_ci }, 2171cb0ef41Sopenharmony_ci }) 2181cb0ef41Sopenharmony_ci const registry = new MockRegistry({ 2191cb0ef41Sopenharmony_ci tap: t, 2201cb0ef41Sopenharmony_ci registry: npm.config.get('registry'), 2211cb0ef41Sopenharmony_ci authorization: 'test-auth-token', 2221cb0ef41Sopenharmony_ci }) 2231cb0ef41Sopenharmony_ci const manifest = registry.manifest({ name: pkg }) 2241cb0ef41Sopenharmony_ci await registry.package({ manifest, query: { write: true }, times: 2 }) 2251cb0ef41Sopenharmony_ci registry.unpublish({ manifest }) 2261cb0ef41Sopenharmony_ci 2271cb0ef41Sopenharmony_ci await npm.exec('unpublish', ['test-package']) 2281cb0ef41Sopenharmony_ci t.equal(joinedOutput(), '- test-package') 2291cb0ef41Sopenharmony_ci}) 2301cb0ef41Sopenharmony_ci 2311cb0ef41Sopenharmony_cit.test('silent', async t => { 2321cb0ef41Sopenharmony_ci const { joinedOutput, npm } = await loadMockNpm(t, { 2331cb0ef41Sopenharmony_ci config: { 2341cb0ef41Sopenharmony_ci force: true, 2351cb0ef41Sopenharmony_ci loglevel: 'silent', 2361cb0ef41Sopenharmony_ci ...auth, 2371cb0ef41Sopenharmony_ci }, 2381cb0ef41Sopenharmony_ci }) 2391cb0ef41Sopenharmony_ci const registry = new MockRegistry({ 2401cb0ef41Sopenharmony_ci tap: t, 2411cb0ef41Sopenharmony_ci registry: npm.config.get('registry'), 2421cb0ef41Sopenharmony_ci authorization: 'test-auth-token', 2431cb0ef41Sopenharmony_ci }) 2441cb0ef41Sopenharmony_ci const manifest = registry.manifest({ 2451cb0ef41Sopenharmony_ci name: pkg, 2461cb0ef41Sopenharmony_ci packuments: ['1.0.0', '1.0.1'], 2471cb0ef41Sopenharmony_ci }) 2481cb0ef41Sopenharmony_ci await registry.package({ manifest, query: { write: true }, times: 3 }) 2491cb0ef41Sopenharmony_ci registry.nock.put(`/${pkg}/-rev/${manifest._rev}`, body => { 2501cb0ef41Sopenharmony_ci // sets latest and deletes version 1.0.1 2511cb0ef41Sopenharmony_ci return body['dist-tags'].latest === '1.0.0' && body.versions['1.0.1'] === undefined 2521cb0ef41Sopenharmony_ci }).reply(201) 2531cb0ef41Sopenharmony_ci .delete(`/${pkg}/-/${pkg}-1.0.1.tgz/-rev/${manifest._rev}`).reply(201) 2541cb0ef41Sopenharmony_ci 2551cb0ef41Sopenharmony_ci await npm.exec('unpublish', ['test-package@1.0.1']) 2561cb0ef41Sopenharmony_ci t.equal(joinedOutput(), '') 2571cb0ef41Sopenharmony_ci}) 2581cb0ef41Sopenharmony_ci 2591cb0ef41Sopenharmony_cit.test('workspaces', async t => { 2601cb0ef41Sopenharmony_ci const prefixDir = { 2611cb0ef41Sopenharmony_ci 'package.json': JSON.stringify({ 2621cb0ef41Sopenharmony_ci name: pkg, 2631cb0ef41Sopenharmony_ci version: '1.0.0', 2641cb0ef41Sopenharmony_ci workspaces: ['workspace-a', 'workspace-b', 'workspace-c'], 2651cb0ef41Sopenharmony_ci }, null, 2), 2661cb0ef41Sopenharmony_ci 'workspace-a': { 2671cb0ef41Sopenharmony_ci 'package.json': JSON.stringify({ 2681cb0ef41Sopenharmony_ci name: 'workspace-a', 2691cb0ef41Sopenharmony_ci version: '1.2.3-a', 2701cb0ef41Sopenharmony_ci repository: 'http://repo.workspace-a/', 2711cb0ef41Sopenharmony_ci }), 2721cb0ef41Sopenharmony_ci }, 2731cb0ef41Sopenharmony_ci 'workspace-b': { 2741cb0ef41Sopenharmony_ci 'package.json': JSON.stringify({ 2751cb0ef41Sopenharmony_ci name: 'workspace-b', 2761cb0ef41Sopenharmony_ci version: '1.2.3-b', 2771cb0ef41Sopenharmony_ci repository: 'https://github.com/npm/workspace-b', 2781cb0ef41Sopenharmony_ci }), 2791cb0ef41Sopenharmony_ci }, 2801cb0ef41Sopenharmony_ci 'workspace-c': { 2811cb0ef41Sopenharmony_ci 'package.json': JSON.stringify({ 2821cb0ef41Sopenharmony_ci name: 'workspace-n', 2831cb0ef41Sopenharmony_ci version: '1.2.3-n', 2841cb0ef41Sopenharmony_ci }), 2851cb0ef41Sopenharmony_ci }, 2861cb0ef41Sopenharmony_ci } 2871cb0ef41Sopenharmony_ci 2881cb0ef41Sopenharmony_ci t.test('with package name no force', async t => { 2891cb0ef41Sopenharmony_ci const { npm } = await loadMockNpm(t, { 2901cb0ef41Sopenharmony_ci config: { 2911cb0ef41Sopenharmony_ci workspace: ['workspace-a'], 2921cb0ef41Sopenharmony_ci }, 2931cb0ef41Sopenharmony_ci prefixDir, 2941cb0ef41Sopenharmony_ci }) 2951cb0ef41Sopenharmony_ci await t.rejects( 2961cb0ef41Sopenharmony_ci npm.exec('unpublish', ['workspace-a']), 2971cb0ef41Sopenharmony_ci /Refusing to delete entire project/ 2981cb0ef41Sopenharmony_ci ) 2991cb0ef41Sopenharmony_ci }) 3001cb0ef41Sopenharmony_ci 3011cb0ef41Sopenharmony_ci t.test('all workspaces last version --force', async t => { 3021cb0ef41Sopenharmony_ci const { joinedOutput, npm } = await loadMockNpm(t, { 3031cb0ef41Sopenharmony_ci config: { 3041cb0ef41Sopenharmony_ci workspaces: true, 3051cb0ef41Sopenharmony_ci force: true, 3061cb0ef41Sopenharmony_ci ...auth, 3071cb0ef41Sopenharmony_ci }, 3081cb0ef41Sopenharmony_ci prefixDir, 3091cb0ef41Sopenharmony_ci }) 3101cb0ef41Sopenharmony_ci const registry = new MockRegistry({ 3111cb0ef41Sopenharmony_ci tap: t, 3121cb0ef41Sopenharmony_ci registry: npm.config.get('registry'), 3131cb0ef41Sopenharmony_ci authorization: 'test-auth-token', 3141cb0ef41Sopenharmony_ci }) 3151cb0ef41Sopenharmony_ci const manifestA = registry.manifest({ name: 'workspace-a', versions: ['1.2.3-a'] }) 3161cb0ef41Sopenharmony_ci const manifestB = registry.manifest({ name: 'workspace-b', versions: ['1.2.3-b'] }) 3171cb0ef41Sopenharmony_ci const manifestN = registry.manifest({ name: 'workspace-n', versions: ['1.2.3-n'] }) 3181cb0ef41Sopenharmony_ci await registry.package({ manifest: manifestA, query: { write: true }, times: 2 }) 3191cb0ef41Sopenharmony_ci await registry.package({ manifest: manifestB, query: { write: true }, times: 2 }) 3201cb0ef41Sopenharmony_ci await registry.package({ manifest: manifestN, query: { write: true }, times: 2 }) 3211cb0ef41Sopenharmony_ci registry.nock.delete(`/workspace-a/-rev/${manifestA._rev}`).reply(201) 3221cb0ef41Sopenharmony_ci .delete(`/workspace-b/-rev/${manifestB._rev}`).reply(201) 3231cb0ef41Sopenharmony_ci .delete(`/workspace-n/-rev/${manifestN._rev}`).reply(201) 3241cb0ef41Sopenharmony_ci 3251cb0ef41Sopenharmony_ci await npm.exec('unpublish', []) 3261cb0ef41Sopenharmony_ci t.equal(joinedOutput(), '- workspace-a\n- workspace-b\n- workspace-n') 3271cb0ef41Sopenharmony_ci }) 3281cb0ef41Sopenharmony_ci}) 3291cb0ef41Sopenharmony_ci 3301cb0ef41Sopenharmony_cit.test('dryRun with spec', async t => { 3311cb0ef41Sopenharmony_ci const { joinedOutput, npm } = await loadMockNpm(t, { 3321cb0ef41Sopenharmony_ci config: { 3331cb0ef41Sopenharmony_ci 'dry-run': true, 3341cb0ef41Sopenharmony_ci ...auth, 3351cb0ef41Sopenharmony_ci }, 3361cb0ef41Sopenharmony_ci }) 3371cb0ef41Sopenharmony_ci const registry = new MockRegistry({ 3381cb0ef41Sopenharmony_ci tap: t, 3391cb0ef41Sopenharmony_ci registry: npm.config.get('registry'), 3401cb0ef41Sopenharmony_ci authorization: 'test-auth-token', 3411cb0ef41Sopenharmony_ci }) 3421cb0ef41Sopenharmony_ci const manifest = registry.manifest({ 3431cb0ef41Sopenharmony_ci name: pkg, 3441cb0ef41Sopenharmony_ci packuments: ['1.0.0', '1.0.1'], 3451cb0ef41Sopenharmony_ci }) 3461cb0ef41Sopenharmony_ci await registry.package({ manifest, query: { write: true } }) 3471cb0ef41Sopenharmony_ci 3481cb0ef41Sopenharmony_ci await npm.exec('unpublish', ['test-package@1.0.1']) 3491cb0ef41Sopenharmony_ci t.equal(joinedOutput(), '- test-package@1.0.1') 3501cb0ef41Sopenharmony_ci}) 3511cb0ef41Sopenharmony_ci 3521cb0ef41Sopenharmony_cit.test('dryRun with no args', async t => { 3531cb0ef41Sopenharmony_ci const { joinedOutput, npm } = await loadMockNpm(t, { 3541cb0ef41Sopenharmony_ci config: { 3551cb0ef41Sopenharmony_ci force: true, 3561cb0ef41Sopenharmony_ci 'dry-run': true, 3571cb0ef41Sopenharmony_ci ...auth, 3581cb0ef41Sopenharmony_ci }, 3591cb0ef41Sopenharmony_ci prefixDir: { 3601cb0ef41Sopenharmony_ci 'package.json': JSON.stringify({ 3611cb0ef41Sopenharmony_ci name: pkg, 3621cb0ef41Sopenharmony_ci version: '1.0.0', 3631cb0ef41Sopenharmony_ci }, null, 2), 3641cb0ef41Sopenharmony_ci }, 3651cb0ef41Sopenharmony_ci }) 3661cb0ef41Sopenharmony_ci const registry = new MockRegistry({ 3671cb0ef41Sopenharmony_ci tap: t, 3681cb0ef41Sopenharmony_ci registry: npm.config.get('registry'), 3691cb0ef41Sopenharmony_ci authorization: 'test-auth-token', 3701cb0ef41Sopenharmony_ci }) 3711cb0ef41Sopenharmony_ci const manifest = registry.manifest({ 3721cb0ef41Sopenharmony_ci name: pkg, 3731cb0ef41Sopenharmony_ci packuments: ['1.0.0', '1.0.1'], 3741cb0ef41Sopenharmony_ci }) 3751cb0ef41Sopenharmony_ci await registry.package({ manifest, query: { write: true } }) 3761cb0ef41Sopenharmony_ci 3771cb0ef41Sopenharmony_ci await npm.exec('unpublish', []) 3781cb0ef41Sopenharmony_ci t.equal(joinedOutput(), '- test-package@1.0.0') 3791cb0ef41Sopenharmony_ci}) 3801cb0ef41Sopenharmony_ci 3811cb0ef41Sopenharmony_cit.test('publishConfig no spec', async t => { 3821cb0ef41Sopenharmony_ci const alternateRegistry = 'https://other.registry.npmjs.org' 3831cb0ef41Sopenharmony_ci const { joinedOutput, npm } = await loadMockNpm(t, { 3841cb0ef41Sopenharmony_ci config: { 3851cb0ef41Sopenharmony_ci force: true, 3861cb0ef41Sopenharmony_ci '//other.registry.npmjs.org/:_authToken': 'test-other-token', 3871cb0ef41Sopenharmony_ci }, 3881cb0ef41Sopenharmony_ci prefixDir: { 3891cb0ef41Sopenharmony_ci 'package.json': JSON.stringify({ 3901cb0ef41Sopenharmony_ci name: pkg, 3911cb0ef41Sopenharmony_ci version: '1.0.0', 3921cb0ef41Sopenharmony_ci publishConfig: { 3931cb0ef41Sopenharmony_ci registry: alternateRegistry, 3941cb0ef41Sopenharmony_ci }, 3951cb0ef41Sopenharmony_ci }, null, 2), 3961cb0ef41Sopenharmony_ci }, 3971cb0ef41Sopenharmony_ci }) 3981cb0ef41Sopenharmony_ci 3991cb0ef41Sopenharmony_ci const registry = new MockRegistry({ 4001cb0ef41Sopenharmony_ci tap: t, 4011cb0ef41Sopenharmony_ci registry: alternateRegistry, 4021cb0ef41Sopenharmony_ci authorization: 'test-other-token', 4031cb0ef41Sopenharmony_ci }) 4041cb0ef41Sopenharmony_ci const manifest = registry.manifest({ name: pkg }) 4051cb0ef41Sopenharmony_ci await registry.package({ manifest, query: { write: true }, times: 2 }) 4061cb0ef41Sopenharmony_ci registry.unpublish({ manifest }) 4071cb0ef41Sopenharmony_ci await npm.exec('unpublish', []) 4081cb0ef41Sopenharmony_ci t.equal(joinedOutput(), '- test-package') 4091cb0ef41Sopenharmony_ci}) 4101cb0ef41Sopenharmony_ci 4111cb0ef41Sopenharmony_cit.test('publishConfig with spec', async t => { 4121cb0ef41Sopenharmony_ci const alternateRegistry = 'https://other.registry.npmjs.org' 4131cb0ef41Sopenharmony_ci const { joinedOutput, npm } = await loadMockNpm(t, { 4141cb0ef41Sopenharmony_ci config: { 4151cb0ef41Sopenharmony_ci force: true, 4161cb0ef41Sopenharmony_ci '//other.registry.npmjs.org/:_authToken': 'test-other-token', 4171cb0ef41Sopenharmony_ci }, 4181cb0ef41Sopenharmony_ci prefixDir: { 4191cb0ef41Sopenharmony_ci 'package.json': JSON.stringify({ 4201cb0ef41Sopenharmony_ci name: pkg, 4211cb0ef41Sopenharmony_ci version: '1.0.0', 4221cb0ef41Sopenharmony_ci publishConfig: { 4231cb0ef41Sopenharmony_ci registry: alternateRegistry, 4241cb0ef41Sopenharmony_ci }, 4251cb0ef41Sopenharmony_ci }, null, 2), 4261cb0ef41Sopenharmony_ci }, 4271cb0ef41Sopenharmony_ci }) 4281cb0ef41Sopenharmony_ci 4291cb0ef41Sopenharmony_ci const registry = new MockRegistry({ 4301cb0ef41Sopenharmony_ci tap: t, 4311cb0ef41Sopenharmony_ci registry: alternateRegistry, 4321cb0ef41Sopenharmony_ci authorization: 'test-other-token', 4331cb0ef41Sopenharmony_ci }) 4341cb0ef41Sopenharmony_ci const manifest = registry.manifest({ name: pkg }) 4351cb0ef41Sopenharmony_ci await registry.package({ manifest, query: { write: true }, times: 2 }) 4361cb0ef41Sopenharmony_ci registry.unpublish({ manifest }) 4371cb0ef41Sopenharmony_ci await npm.exec('unpublish', ['test-package']) 4381cb0ef41Sopenharmony_ci t.equal(joinedOutput(), '- test-package') 4391cb0ef41Sopenharmony_ci}) 4401cb0ef41Sopenharmony_ci 4411cb0ef41Sopenharmony_cit.test('scoped registry config', async t => { 4421cb0ef41Sopenharmony_ci const scopedPkg = `@npm/test-package` 4431cb0ef41Sopenharmony_ci const alternateRegistry = 'https://other.registry.npmjs.org' 4441cb0ef41Sopenharmony_ci const { npm } = await loadMockNpm(t, { 4451cb0ef41Sopenharmony_ci config: { 4461cb0ef41Sopenharmony_ci force: true, 4471cb0ef41Sopenharmony_ci '@npm:registry': alternateRegistry, 4481cb0ef41Sopenharmony_ci '//other.registry.npmjs.org/:_authToken': 'test-other-token', 4491cb0ef41Sopenharmony_ci }, 4501cb0ef41Sopenharmony_ci prefixDir: { 4511cb0ef41Sopenharmony_ci 'package.json': JSON.stringify({ 4521cb0ef41Sopenharmony_ci name: pkg, 4531cb0ef41Sopenharmony_ci version: '1.0.0', 4541cb0ef41Sopenharmony_ci publishConfig: { 4551cb0ef41Sopenharmony_ci registry: alternateRegistry, 4561cb0ef41Sopenharmony_ci }, 4571cb0ef41Sopenharmony_ci }, null, 2), 4581cb0ef41Sopenharmony_ci }, 4591cb0ef41Sopenharmony_ci }) 4601cb0ef41Sopenharmony_ci const registry = new MockRegistry({ 4611cb0ef41Sopenharmony_ci tap: t, 4621cb0ef41Sopenharmony_ci registry: alternateRegistry, 4631cb0ef41Sopenharmony_ci authorization: 'test-other-token', 4641cb0ef41Sopenharmony_ci }) 4651cb0ef41Sopenharmony_ci const manifest = registry.manifest({ name: scopedPkg }) 4661cb0ef41Sopenharmony_ci await registry.package({ manifest, query: { write: true }, times: 2 }) 4671cb0ef41Sopenharmony_ci registry.unpublish({ manifest }) 4681cb0ef41Sopenharmony_ci await npm.exec('unpublish', [scopedPkg]) 4691cb0ef41Sopenharmony_ci}) 4701cb0ef41Sopenharmony_ci 4711cb0ef41Sopenharmony_cit.test('completion', async t => { 4721cb0ef41Sopenharmony_ci const { npm, unpublish } = await loadMockNpm(t, { 4731cb0ef41Sopenharmony_ci command: 'unpublish', 4741cb0ef41Sopenharmony_ci config: { 4751cb0ef41Sopenharmony_ci ...auth, 4761cb0ef41Sopenharmony_ci }, 4771cb0ef41Sopenharmony_ci }) 4781cb0ef41Sopenharmony_ci 4791cb0ef41Sopenharmony_ci const testComp = 4801cb0ef41Sopenharmony_ci async (t, { argv, partialWord, expect, title }) => { 4811cb0ef41Sopenharmony_ci const res = await unpublish.completion( 4821cb0ef41Sopenharmony_ci { conf: { argv: { remain: argv } }, partialWord } 4831cb0ef41Sopenharmony_ci ) 4841cb0ef41Sopenharmony_ci t.strictSame(res, expect, title || argv.join(' ')) 4851cb0ef41Sopenharmony_ci } 4861cb0ef41Sopenharmony_ci 4871cb0ef41Sopenharmony_ci t.test('completing with multiple versions from the registry', async t => { 4881cb0ef41Sopenharmony_ci const registry = new MockRegistry({ 4891cb0ef41Sopenharmony_ci tap: t, 4901cb0ef41Sopenharmony_ci registry: npm.config.get('registry'), 4911cb0ef41Sopenharmony_ci authorization: 'test-auth-token', 4921cb0ef41Sopenharmony_ci }) 4931cb0ef41Sopenharmony_ci const manifest = registry.manifest({ 4941cb0ef41Sopenharmony_ci name: pkg, 4951cb0ef41Sopenharmony_ci packuments: ['1.0.0', '1.0.1'], 4961cb0ef41Sopenharmony_ci }) 4971cb0ef41Sopenharmony_ci await registry.package({ manifest, query: { write: true } }) 4981cb0ef41Sopenharmony_ci registry.whoami({ username: user }) 4991cb0ef41Sopenharmony_ci registry.getPackages({ team: user, packages: { [pkg]: 'write' } }) 5001cb0ef41Sopenharmony_ci 5011cb0ef41Sopenharmony_ci await testComp(t, { 5021cb0ef41Sopenharmony_ci argv: ['npm', 'unpublish'], 5031cb0ef41Sopenharmony_ci partialWord: 'test-package', 5041cb0ef41Sopenharmony_ci expect: [ 5051cb0ef41Sopenharmony_ci 'test-package@1.0.0', 5061cb0ef41Sopenharmony_ci 'test-package@1.0.1', 5071cb0ef41Sopenharmony_ci ], 5081cb0ef41Sopenharmony_ci }) 5091cb0ef41Sopenharmony_ci }) 5101cb0ef41Sopenharmony_ci 5111cb0ef41Sopenharmony_ci t.test('no versions retrieved', async t => { 5121cb0ef41Sopenharmony_ci const registry = new MockRegistry({ 5131cb0ef41Sopenharmony_ci tap: t, 5141cb0ef41Sopenharmony_ci registry: npm.config.get('registry'), 5151cb0ef41Sopenharmony_ci authorization: 'test-auth-token', 5161cb0ef41Sopenharmony_ci }) 5171cb0ef41Sopenharmony_ci const manifest = registry.manifest({ name: pkg }) 5181cb0ef41Sopenharmony_ci manifest.versions = {} 5191cb0ef41Sopenharmony_ci await registry.package({ manifest, query: { write: true } }) 5201cb0ef41Sopenharmony_ci registry.whoami({ username: user }) 5211cb0ef41Sopenharmony_ci registry.getPackages({ team: user, packages: { [pkg]: 'write' } }) 5221cb0ef41Sopenharmony_ci 5231cb0ef41Sopenharmony_ci await testComp(t, { 5241cb0ef41Sopenharmony_ci argv: ['npm', 'unpublish'], 5251cb0ef41Sopenharmony_ci partialWord: pkg, 5261cb0ef41Sopenharmony_ci expect: [ 5271cb0ef41Sopenharmony_ci pkg, 5281cb0ef41Sopenharmony_ci ], 5291cb0ef41Sopenharmony_ci title: 'should autocomplete package name only', 5301cb0ef41Sopenharmony_ci }) 5311cb0ef41Sopenharmony_ci }) 5321cb0ef41Sopenharmony_ci 5331cb0ef41Sopenharmony_ci t.test('packages starting with same letters', async t => { 5341cb0ef41Sopenharmony_ci const registry = new MockRegistry({ 5351cb0ef41Sopenharmony_ci tap: t, 5361cb0ef41Sopenharmony_ci registry: npm.config.get('registry'), 5371cb0ef41Sopenharmony_ci authorization: 'test-auth-token', 5381cb0ef41Sopenharmony_ci }) 5391cb0ef41Sopenharmony_ci registry.whoami({ username: user }) 5401cb0ef41Sopenharmony_ci registry.getPackages({ team: user, 5411cb0ef41Sopenharmony_ci packages: { 5421cb0ef41Sopenharmony_ci [pkg]: 'write', 5431cb0ef41Sopenharmony_ci [`${pkg}a`]: 'write', 5441cb0ef41Sopenharmony_ci [`${pkg}b`]: 'write', 5451cb0ef41Sopenharmony_ci } }) 5461cb0ef41Sopenharmony_ci 5471cb0ef41Sopenharmony_ci await testComp(t, { 5481cb0ef41Sopenharmony_ci argv: ['npm', 'unpublish'], 5491cb0ef41Sopenharmony_ci partialWord: pkg, 5501cb0ef41Sopenharmony_ci expect: [ 5511cb0ef41Sopenharmony_ci pkg, 5521cb0ef41Sopenharmony_ci `${pkg}a`, 5531cb0ef41Sopenharmony_ci `${pkg}b`, 5541cb0ef41Sopenharmony_ci ], 5551cb0ef41Sopenharmony_ci }) 5561cb0ef41Sopenharmony_ci }) 5571cb0ef41Sopenharmony_ci 5581cb0ef41Sopenharmony_ci t.test('no packages retrieved', async t => { 5591cb0ef41Sopenharmony_ci const registry = new MockRegistry({ 5601cb0ef41Sopenharmony_ci tap: t, 5611cb0ef41Sopenharmony_ci registry: npm.config.get('registry'), 5621cb0ef41Sopenharmony_ci authorization: 'test-auth-token', 5631cb0ef41Sopenharmony_ci }) 5641cb0ef41Sopenharmony_ci registry.whoami({ username: user }) 5651cb0ef41Sopenharmony_ci registry.getPackages({ team: user, packages: {} }) 5661cb0ef41Sopenharmony_ci 5671cb0ef41Sopenharmony_ci await testComp(t, { 5681cb0ef41Sopenharmony_ci argv: ['npm', 'unpublish'], 5691cb0ef41Sopenharmony_ci partialWord: 'pkg', 5701cb0ef41Sopenharmony_ci expect: [], 5711cb0ef41Sopenharmony_ci title: 'should have no autocompletion', 5721cb0ef41Sopenharmony_ci }) 5731cb0ef41Sopenharmony_ci }) 5741cb0ef41Sopenharmony_ci 5751cb0ef41Sopenharmony_ci t.test('no pkg name to complete', async t => { 5761cb0ef41Sopenharmony_ci const registry = new MockRegistry({ 5771cb0ef41Sopenharmony_ci tap: t, 5781cb0ef41Sopenharmony_ci registry: npm.config.get('registry'), 5791cb0ef41Sopenharmony_ci authorization: 'test-auth-token', 5801cb0ef41Sopenharmony_ci }) 5811cb0ef41Sopenharmony_ci registry.whoami({ username: user }) 5821cb0ef41Sopenharmony_ci registry.getPackages({ team: user, 5831cb0ef41Sopenharmony_ci packages: { 5841cb0ef41Sopenharmony_ci [pkg]: 'write', 5851cb0ef41Sopenharmony_ci [`${pkg}a`]: 'write', 5861cb0ef41Sopenharmony_ci } }) 5871cb0ef41Sopenharmony_ci 5881cb0ef41Sopenharmony_ci await testComp(t, { 5891cb0ef41Sopenharmony_ci argv: ['npm', 'unpublish'], 5901cb0ef41Sopenharmony_ci partialWord: undefined, 5911cb0ef41Sopenharmony_ci expect: [pkg, `${pkg}a`], 5921cb0ef41Sopenharmony_ci title: 'should autocomplete with available package names from user', 5931cb0ef41Sopenharmony_ci }) 5941cb0ef41Sopenharmony_ci }) 5951cb0ef41Sopenharmony_ci 5961cb0ef41Sopenharmony_ci t.test('logged out user', async t => { 5971cb0ef41Sopenharmony_ci const registry = new MockRegistry({ 5981cb0ef41Sopenharmony_ci tap: t, 5991cb0ef41Sopenharmony_ci registry: npm.config.get('registry'), 6001cb0ef41Sopenharmony_ci authorization: 'test-auth-token', 6011cb0ef41Sopenharmony_ci }) 6021cb0ef41Sopenharmony_ci registry.whoami({ responseCode: 404 }) 6031cb0ef41Sopenharmony_ci 6041cb0ef41Sopenharmony_ci await testComp(t, { 6051cb0ef41Sopenharmony_ci argv: ['npm', 'unpublish'], 6061cb0ef41Sopenharmony_ci partialWord: pkg, 6071cb0ef41Sopenharmony_ci expect: [], 6081cb0ef41Sopenharmony_ci }) 6091cb0ef41Sopenharmony_ci }) 6101cb0ef41Sopenharmony_ci 6111cb0ef41Sopenharmony_ci t.test('too many args', async t => { 6121cb0ef41Sopenharmony_ci await testComp(t, { 6131cb0ef41Sopenharmony_ci argv: ['npm', 'unpublish', 'foo'], 6141cb0ef41Sopenharmony_ci partialWord: undefined, 6151cb0ef41Sopenharmony_ci expect: [], 6161cb0ef41Sopenharmony_ci }) 6171cb0ef41Sopenharmony_ci }) 6181cb0ef41Sopenharmony_ci}) 619