11cb0ef41Sopenharmony_ciconst t = require('tap') 21cb0ef41Sopenharmony_ciconst { load: loadMockNpm } = require('../../fixtures/mock-npm') 31cb0ef41Sopenharmony_ciconst { cleanZlib } = require('../../fixtures/clean-snapshot') 41cb0ef41Sopenharmony_ciconst path = require('path') 51cb0ef41Sopenharmony_ciconst fs = require('fs') 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_cit.cleanSnapshot = data => cleanZlib(data) 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_cit.test('should pack current directory with no arguments', async t => { 101cb0ef41Sopenharmony_ci const { npm, outputs, logs } = await loadMockNpm(t, { 111cb0ef41Sopenharmony_ci prefixDir: { 121cb0ef41Sopenharmony_ci 'package.json': JSON.stringify({ 131cb0ef41Sopenharmony_ci name: 'test-package', 141cb0ef41Sopenharmony_ci version: '1.0.0', 151cb0ef41Sopenharmony_ci }), 161cb0ef41Sopenharmony_ci }, 171cb0ef41Sopenharmony_ci }) 181cb0ef41Sopenharmony_ci await npm.exec('pack', []) 191cb0ef41Sopenharmony_ci const filename = 'test-package-1.0.0.tgz' 201cb0ef41Sopenharmony_ci t.strictSame(outputs, [[filename]]) 211cb0ef41Sopenharmony_ci t.matchSnapshot(logs.notice.map(([, m]) => m), 'logs pack contents') 221cb0ef41Sopenharmony_ci t.ok(fs.statSync(path.resolve(npm.prefix, filename))) 231cb0ef41Sopenharmony_ci}) 241cb0ef41Sopenharmony_ci 251cb0ef41Sopenharmony_cit.test('follows pack-destination config', async t => { 261cb0ef41Sopenharmony_ci const { npm, outputs } = await loadMockNpm(t, { 271cb0ef41Sopenharmony_ci prefixDir: { 281cb0ef41Sopenharmony_ci 'package.json': JSON.stringify({ 291cb0ef41Sopenharmony_ci name: 'test-package', 301cb0ef41Sopenharmony_ci version: '1.0.0', 311cb0ef41Sopenharmony_ci }), 321cb0ef41Sopenharmony_ci 'tar-destination': {}, 331cb0ef41Sopenharmony_ci }, 341cb0ef41Sopenharmony_ci config: ({ prefix }) => ({ 'pack-destination': path.join(prefix, 'tar-destination') }), 351cb0ef41Sopenharmony_ci }) 361cb0ef41Sopenharmony_ci await npm.exec('pack', []) 371cb0ef41Sopenharmony_ci const filename = 'test-package-1.0.0.tgz' 381cb0ef41Sopenharmony_ci t.strictSame(outputs, [[filename]]) 391cb0ef41Sopenharmony_ci t.ok(fs.statSync(path.resolve(npm.prefix, 'tar-destination', filename))) 401cb0ef41Sopenharmony_ci}) 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_cit.test('should pack given directory for scoped package', async t => { 431cb0ef41Sopenharmony_ci const { npm, outputs } = await loadMockNpm(t, { 441cb0ef41Sopenharmony_ci prefixDir: { 451cb0ef41Sopenharmony_ci 'package.json': JSON.stringify({ 461cb0ef41Sopenharmony_ci name: '@npm/test-package', 471cb0ef41Sopenharmony_ci version: '1.0.0', 481cb0ef41Sopenharmony_ci }), 491cb0ef41Sopenharmony_ci }, 501cb0ef41Sopenharmony_ci }) 511cb0ef41Sopenharmony_ci await npm.exec('pack', []) 521cb0ef41Sopenharmony_ci const filename = 'npm-test-package-1.0.0.tgz' 531cb0ef41Sopenharmony_ci t.strictSame(outputs, [[filename]]) 541cb0ef41Sopenharmony_ci t.ok(fs.statSync(path.resolve(npm.prefix, filename))) 551cb0ef41Sopenharmony_ci}) 561cb0ef41Sopenharmony_ci 571cb0ef41Sopenharmony_cit.test('should log output as valid json', async t => { 581cb0ef41Sopenharmony_ci const { npm, outputs, logs } = await loadMockNpm(t, { 591cb0ef41Sopenharmony_ci prefixDir: { 601cb0ef41Sopenharmony_ci 'package.json': JSON.stringify({ 611cb0ef41Sopenharmony_ci name: 'test-package', 621cb0ef41Sopenharmony_ci version: '1.0.0', 631cb0ef41Sopenharmony_ci }), 641cb0ef41Sopenharmony_ci }, 651cb0ef41Sopenharmony_ci config: { json: true }, 661cb0ef41Sopenharmony_ci }) 671cb0ef41Sopenharmony_ci await npm.exec('pack', []) 681cb0ef41Sopenharmony_ci const filename = 'test-package-1.0.0.tgz' 691cb0ef41Sopenharmony_ci t.matchSnapshot(outputs.map(JSON.parse), 'outputs as json') 701cb0ef41Sopenharmony_ci t.matchSnapshot(logs.notice.map(([, m]) => m), 'logs pack contents') 711cb0ef41Sopenharmony_ci t.ok(fs.statSync(path.resolve(npm.prefix, filename))) 721cb0ef41Sopenharmony_ci}) 731cb0ef41Sopenharmony_ci 741cb0ef41Sopenharmony_cit.test('should log scoped package output as valid json', async t => { 751cb0ef41Sopenharmony_ci const { npm, outputs, logs } = await loadMockNpm(t, { 761cb0ef41Sopenharmony_ci prefixDir: { 771cb0ef41Sopenharmony_ci 'package.json': JSON.stringify({ 781cb0ef41Sopenharmony_ci name: '@myscope/test-package', 791cb0ef41Sopenharmony_ci version: '1.0.0', 801cb0ef41Sopenharmony_ci }), 811cb0ef41Sopenharmony_ci }, 821cb0ef41Sopenharmony_ci config: { json: true }, 831cb0ef41Sopenharmony_ci }) 841cb0ef41Sopenharmony_ci await npm.exec('pack', []) 851cb0ef41Sopenharmony_ci const filename = 'myscope-test-package-1.0.0.tgz' 861cb0ef41Sopenharmony_ci t.matchSnapshot(outputs.map(JSON.parse), 'outputs as json') 871cb0ef41Sopenharmony_ci t.matchSnapshot(logs.notice.map(([, m]) => m), 'logs pack contents') 881cb0ef41Sopenharmony_ci t.ok(fs.statSync(path.resolve(npm.prefix, filename))) 891cb0ef41Sopenharmony_ci}) 901cb0ef41Sopenharmony_ci 911cb0ef41Sopenharmony_cit.test('dry run', async t => { 921cb0ef41Sopenharmony_ci const { npm, outputs, logs } = await loadMockNpm(t, { 931cb0ef41Sopenharmony_ci prefixDir: { 941cb0ef41Sopenharmony_ci 'package.json': JSON.stringify({ 951cb0ef41Sopenharmony_ci name: 'test-package', 961cb0ef41Sopenharmony_ci version: '1.0.0', 971cb0ef41Sopenharmony_ci }), 981cb0ef41Sopenharmony_ci }, 991cb0ef41Sopenharmony_ci config: { 'dry-run': true }, 1001cb0ef41Sopenharmony_ci }) 1011cb0ef41Sopenharmony_ci await npm.exec('pack', []) 1021cb0ef41Sopenharmony_ci const filename = 'test-package-1.0.0.tgz' 1031cb0ef41Sopenharmony_ci t.strictSame(outputs, [[filename]]) 1041cb0ef41Sopenharmony_ci t.matchSnapshot(logs.notice.map(([, m]) => m), 'logs pack contents') 1051cb0ef41Sopenharmony_ci t.throws(() => fs.statSync(path.resolve(npm.prefix, filename))) 1061cb0ef41Sopenharmony_ci}) 1071cb0ef41Sopenharmony_ci 1081cb0ef41Sopenharmony_cit.test('foreground-scripts defaults to true', async t => { 1091cb0ef41Sopenharmony_ci const { npm, outputs, logs } = await loadMockNpm(t, { 1101cb0ef41Sopenharmony_ci prefixDir: { 1111cb0ef41Sopenharmony_ci 'package.json': JSON.stringify({ 1121cb0ef41Sopenharmony_ci name: 'test-fg-scripts', 1131cb0ef41Sopenharmony_ci version: '0.0.0', 1141cb0ef41Sopenharmony_ci scripts: { 1151cb0ef41Sopenharmony_ci prepack: 'echo prepack!', 1161cb0ef41Sopenharmony_ci postpack: 'echo postpack!', 1171cb0ef41Sopenharmony_ci }, 1181cb0ef41Sopenharmony_ci } 1191cb0ef41Sopenharmony_ci ), 1201cb0ef41Sopenharmony_ci }, 1211cb0ef41Sopenharmony_ci config: { 'dry-run': true }, 1221cb0ef41Sopenharmony_ci }) 1231cb0ef41Sopenharmony_ci 1241cb0ef41Sopenharmony_ci /* eslint no-console: 0 */ 1251cb0ef41Sopenharmony_ci // TODO: replace this with `const results = t.intercept(console, 'log')` 1261cb0ef41Sopenharmony_ci const log = console.log 1271cb0ef41Sopenharmony_ci t.teardown(() => { 1281cb0ef41Sopenharmony_ci console.log = log 1291cb0ef41Sopenharmony_ci }) 1301cb0ef41Sopenharmony_ci const caughtLogs = [] 1311cb0ef41Sopenharmony_ci console.log = (...args) => { 1321cb0ef41Sopenharmony_ci caughtLogs.push(args) 1331cb0ef41Sopenharmony_ci } 1341cb0ef41Sopenharmony_ci // end TODO 1351cb0ef41Sopenharmony_ci 1361cb0ef41Sopenharmony_ci await npm.exec('pack', []) 1371cb0ef41Sopenharmony_ci const filename = 'test-fg-scripts-0.0.0.tgz' 1381cb0ef41Sopenharmony_ci t.same( 1391cb0ef41Sopenharmony_ci caughtLogs, 1401cb0ef41Sopenharmony_ci [ 1411cb0ef41Sopenharmony_ci ['\n> test-fg-scripts@0.0.0 prepack\n> echo prepack!\n'], 1421cb0ef41Sopenharmony_ci ['\n> test-fg-scripts@0.0.0 postpack\n> echo postpack!\n'], 1431cb0ef41Sopenharmony_ci ], 1441cb0ef41Sopenharmony_ci 'prepack and postpack log to stdout') 1451cb0ef41Sopenharmony_ci t.strictSame(outputs, [[filename]]) 1461cb0ef41Sopenharmony_ci t.matchSnapshot(logs.notice.map(([, m]) => m), 'logs pack contents') 1471cb0ef41Sopenharmony_ci t.throws(() => fs.statSync(path.resolve(npm.prefix, filename))) 1481cb0ef41Sopenharmony_ci}) 1491cb0ef41Sopenharmony_ci 1501cb0ef41Sopenharmony_cit.test('foreground-scripts can still be set to false', async t => { 1511cb0ef41Sopenharmony_ci const { npm, outputs, logs } = await loadMockNpm(t, { 1521cb0ef41Sopenharmony_ci prefixDir: { 1531cb0ef41Sopenharmony_ci 'package.json': JSON.stringify({ 1541cb0ef41Sopenharmony_ci name: 'test-fg-scripts', 1551cb0ef41Sopenharmony_ci version: '0.0.0', 1561cb0ef41Sopenharmony_ci scripts: { 1571cb0ef41Sopenharmony_ci prepack: 'echo prepack!', 1581cb0ef41Sopenharmony_ci postpack: 'echo postpack!', 1591cb0ef41Sopenharmony_ci }, 1601cb0ef41Sopenharmony_ci } 1611cb0ef41Sopenharmony_ci ), 1621cb0ef41Sopenharmony_ci }, 1631cb0ef41Sopenharmony_ci config: { 'dry-run': true, 'foreground-scripts': false }, 1641cb0ef41Sopenharmony_ci }) 1651cb0ef41Sopenharmony_ci 1661cb0ef41Sopenharmony_ci /* eslint no-console: 0 */ 1671cb0ef41Sopenharmony_ci // TODO: replace this with `const results = t.intercept(console, 'log')` 1681cb0ef41Sopenharmony_ci const log = console.log 1691cb0ef41Sopenharmony_ci t.teardown(() => { 1701cb0ef41Sopenharmony_ci console.log = log 1711cb0ef41Sopenharmony_ci }) 1721cb0ef41Sopenharmony_ci const caughtLogs = [] 1731cb0ef41Sopenharmony_ci console.log = (...args) => { 1741cb0ef41Sopenharmony_ci caughtLogs.push(args) 1751cb0ef41Sopenharmony_ci } 1761cb0ef41Sopenharmony_ci // end TODO 1771cb0ef41Sopenharmony_ci 1781cb0ef41Sopenharmony_ci await npm.exec('pack', []) 1791cb0ef41Sopenharmony_ci const filename = 'test-fg-scripts-0.0.0.tgz' 1801cb0ef41Sopenharmony_ci t.same( 1811cb0ef41Sopenharmony_ci caughtLogs, 1821cb0ef41Sopenharmony_ci [], 1831cb0ef41Sopenharmony_ci 'prepack and postpack do not log to stdout') 1841cb0ef41Sopenharmony_ci t.strictSame(outputs, [[filename]]) 1851cb0ef41Sopenharmony_ci t.matchSnapshot(logs.notice.map(([, m]) => m), 'logs pack contents') 1861cb0ef41Sopenharmony_ci t.throws(() => fs.statSync(path.resolve(npm.prefix, filename))) 1871cb0ef41Sopenharmony_ci}) 1881cb0ef41Sopenharmony_ci 1891cb0ef41Sopenharmony_cit.test('invalid packument', async t => { 1901cb0ef41Sopenharmony_ci const { npm, outputs } = await loadMockNpm(t, { 1911cb0ef41Sopenharmony_ci prefixDir: { 1921cb0ef41Sopenharmony_ci 'package.json': '{}', 1931cb0ef41Sopenharmony_ci }, 1941cb0ef41Sopenharmony_ci }) 1951cb0ef41Sopenharmony_ci await t.rejects( 1961cb0ef41Sopenharmony_ci npm.exec('pack', []), 1971cb0ef41Sopenharmony_ci /Invalid package, must have name and version/ 1981cb0ef41Sopenharmony_ci ) 1991cb0ef41Sopenharmony_ci t.strictSame(outputs, []) 2001cb0ef41Sopenharmony_ci}) 2011cb0ef41Sopenharmony_ci 2021cb0ef41Sopenharmony_cit.test('workspaces', async t => { 2031cb0ef41Sopenharmony_ci const loadWorkspaces = (t) => loadMockNpm(t, { 2041cb0ef41Sopenharmony_ci prefixDir: { 2051cb0ef41Sopenharmony_ci 'package.json': JSON.stringify( 2061cb0ef41Sopenharmony_ci { 2071cb0ef41Sopenharmony_ci name: 'workspaces-test', 2081cb0ef41Sopenharmony_ci version: '1.0.0', 2091cb0ef41Sopenharmony_ci workspaces: ['workspace-a', 'workspace-b'], 2101cb0ef41Sopenharmony_ci }, 2111cb0ef41Sopenharmony_ci null, 2121cb0ef41Sopenharmony_ci 2 2131cb0ef41Sopenharmony_ci ), 2141cb0ef41Sopenharmony_ci 'workspace-a': { 2151cb0ef41Sopenharmony_ci 'package.json': JSON.stringify({ 2161cb0ef41Sopenharmony_ci name: 'workspace-a', 2171cb0ef41Sopenharmony_ci version: '1.0.0', 2181cb0ef41Sopenharmony_ci }), 2191cb0ef41Sopenharmony_ci }, 2201cb0ef41Sopenharmony_ci 'workspace-b': { 2211cb0ef41Sopenharmony_ci 'package.json': JSON.stringify({ 2221cb0ef41Sopenharmony_ci name: 'workspace-b', 2231cb0ef41Sopenharmony_ci version: '1.0.0', 2241cb0ef41Sopenharmony_ci }), 2251cb0ef41Sopenharmony_ci }, 2261cb0ef41Sopenharmony_ci }, 2271cb0ef41Sopenharmony_ci config: { 2281cb0ef41Sopenharmony_ci workspaces: true, 2291cb0ef41Sopenharmony_ci // TODO: this is a workaround for npm run test-all 2301cb0ef41Sopenharmony_ci // somehow leaking include-workspace-root 2311cb0ef41Sopenharmony_ci 'include-workspace-root': false, 2321cb0ef41Sopenharmony_ci }, 2331cb0ef41Sopenharmony_ci }) 2341cb0ef41Sopenharmony_ci 2351cb0ef41Sopenharmony_ci t.test('all workspaces', async t => { 2361cb0ef41Sopenharmony_ci const { npm, outputs } = await loadWorkspaces(t) 2371cb0ef41Sopenharmony_ci await npm.exec('pack', []) 2381cb0ef41Sopenharmony_ci t.strictSame(outputs, [['workspace-a-1.0.0.tgz'], ['workspace-b-1.0.0.tgz']]) 2391cb0ef41Sopenharmony_ci }) 2401cb0ef41Sopenharmony_ci 2411cb0ef41Sopenharmony_ci t.test('all workspaces, `.` first arg', async t => { 2421cb0ef41Sopenharmony_ci const { npm, outputs } = await loadWorkspaces(t) 2431cb0ef41Sopenharmony_ci await npm.exec('pack', ['.']) 2441cb0ef41Sopenharmony_ci t.strictSame(outputs, [['workspace-a-1.0.0.tgz'], ['workspace-b-1.0.0.tgz']]) 2451cb0ef41Sopenharmony_ci }) 2461cb0ef41Sopenharmony_ci 2471cb0ef41Sopenharmony_ci t.test('one workspace', async t => { 2481cb0ef41Sopenharmony_ci const { npm, outputs } = await loadWorkspaces(t) 2491cb0ef41Sopenharmony_ci await npm.exec('pack', ['workspace-a']) 2501cb0ef41Sopenharmony_ci t.strictSame(outputs, [['workspace-a-1.0.0.tgz']]) 2511cb0ef41Sopenharmony_ci }) 2521cb0ef41Sopenharmony_ci 2531cb0ef41Sopenharmony_ci t.test('specific package', async t => { 2541cb0ef41Sopenharmony_ci const { npm, outputs } = await loadWorkspaces(t) 2551cb0ef41Sopenharmony_ci await npm.exec('pack', [npm.prefix]) 2561cb0ef41Sopenharmony_ci t.strictSame(outputs, [['workspaces-test-1.0.0.tgz']]) 2571cb0ef41Sopenharmony_ci }) 2581cb0ef41Sopenharmony_ci}) 259