diff --git a/lib/commands/view.js b/lib/commands/view.js index 403d09d813dc7..d78ee77dc0a3d 100644 --- a/lib/commands/view.js +++ b/lib/commands/view.js @@ -57,9 +57,6 @@ class View extends BaseCommand { function getFields (d, f, pref) { f = f || [] - if (!d) { - return f - } pref = pref || [] Object.keys(d).forEach((k) => { if (k.charAt(0) === '_' || k.indexOf('.') !== -1) { diff --git a/lib/utils/format-bytes.js b/lib/utils/format-bytes.js index d7cf6d144e339..d293001df5524 100644 --- a/lib/utils/format-bytes.js +++ b/lib/utils/format-bytes.js @@ -23,6 +23,7 @@ const formatBytes = (bytes, space = true) => { return `${(bytes / 1000000).toFixed(1)}${spacer}MB` } + // GB return `${(bytes / 1000000000).toFixed(1)}${spacer}GB` } diff --git a/package.json b/package.json index d3e55ad623a43..e463a4a42258f 100644 --- a/package.json +++ b/package.json @@ -230,8 +230,12 @@ ], "color": 1, "files": "test/{lib,bin,index.js}", - "coverage-map": "test/coverage-map.js", - "timeout": 600 + "timeout": 600, + "nyc-arg": [ + "--exclude", "lib/search/**", + "--exclude", "workspaces/**", + "--exclude", "tap-snapshots/**" + ] }, "templateOSS": { "rootRepo": false, diff --git a/tap-snapshots/test/lib/commands/view.js.test.cjs b/tap-snapshots/test/lib/commands/view.js.test.cjs index 5868e7b04babe..d5b7a3b4a7906 100644 --- a/tap-snapshots/test/lib/commands/view.js.test.cjs +++ b/tap-snapshots/test/lib/commands/view.js.test.cjs @@ -5,96 +5,102 @@ * Make sure to inspect the output below. Do not ignore changes! */ 'use strict' -exports[`test/lib/commands/view.js TAP should log info by field name array field - 1 element > must match snapshot 1`] = ` +exports[`test/lib/commands/view.js TAP deprecated package with license, bugs, repository and other fields > must match snapshot 1`] = ` -claudia -` +green@1.0.0 | ACME | deps: 2 | versions: 2 +green is a very important color -exports[`test/lib/commands/view.js TAP should log info by field name array field - 2 elements > must match snapshot 1`] = ` +DEPRECATED!! - true -maintainers[0].name = 'claudia' -maintainers[1].name = 'isaacs' -` +keywords:,colors, green, crayola -exports[`test/lib/commands/view.js TAP should log info by field name maintainers with email > must match snapshot 1`] = ` +bin:,green -{ - "maintainers": [ - { - "name": "claudia", - "email": "c@yellow.com", - "twitter": "cyellow" - }, - { - "name": "isaacs", - "email": "i@yellow.com", - "twitter": "iyellow" - } - ], - "name": "yellow" -} -` +dist +.tarball:,http://hm.green.com/1.0.0.tgz +.shasum:,123 +.integrity:,--- +.unpackedSize:,1.0 GB + +dependencies: +red: 1.0.0 +yellow: 1.0.0 -exports[`test/lib/commands/view.js TAP should log info by field name maintainers with url > must match snapshot 1`] = ` +maintainers: +-,claudia <c@yellow.com> +-,isaacs <i@yellow.com> -[ - "claudia (http://c.pink.com)", - "isaacs (http://i.pink.com)" -] +dist-tags: +latest: 1.0.0 ` -exports[`test/lib/commands/view.js TAP should log info by field name nested field with brackets > must match snapshot 1`] = ` +exports[`test/lib/commands/view.js TAP deprecated package with unicode > must match snapshot 1`] = ` -"123" -` +green@1.0.0 | ACME | deps: 2 | versions: 2 +green is a very important color -exports[`test/lib/commands/view.js TAP should log info by field name readme > must match snapshot 1`] = ` +DEPRECATED ⚠️ - true -a very useful readme -` +keywords:,colors, green, crayola -exports[`test/lib/commands/view.js TAP should log info by field name several fields > must match snapshot 1`] = ` +bin:,green -{ - "name": "yellow", - "version": "1.0.0" -} -` +dist +.tarball:,http://hm.green.com/1.0.0.tgz +.shasum:,123 +.integrity:,--- +.unpackedSize:,1.0 GB + +dependencies: +red: 1.0.0 +yellow: 1.0.0 -exports[`test/lib/commands/view.js TAP should log info by field name several fields with several versions > must match snapshot 1`] = ` +maintainers: +-,claudia <c@yellow.com> +-,isaacs <i@yellow.com> -yellow@1.0.0 'claudia' -yellow@1.0.1 'claudia' -yellow@1.0.2 'claudia' +dist-tags: +latest: 1.0.0 ` -exports[`test/lib/commands/view.js TAP should log info of package in current working dir directory > must match snapshot 1`] = ` +exports[`test/lib/commands/view.js TAP package from git > must match snapshot 1`] = ` + +green@1.0.0 | ACME | deps: 2 | versions: 2 +green is a very important color + +DEPRECATED!! - true +keywords:,colors, green, crayola -blue@1.0.0 | Proprietary | deps: none | versions: 2 +bin:,green dist -.tarball:http://hm.blue.com/1.0.0.tgz -.shasum:123 -.integrity:--- -.unpackedSize:1 B +.tarball:,http://hm.green.com/1.0.0.tgz +.shasum:,123 +.integrity:,--- +.unpackedSize:,1.0 GB + +dependencies: +red: 1.0.0 +yellow: 1.0.0 + +maintainers: +-,claudia <c@yellow.com> +-,isaacs <i@yellow.com> dist-tags: latest: 1.0.0 - -published {TIME} ago ` -exports[`test/lib/commands/view.js TAP should log info of package in current working dir non-specific version > must match snapshot 1`] = ` - +exports[`test/lib/commands/view.js TAP package in cwd directory > must match snapshot 1`] = ` blue@1.0.0 | Proprietary | deps: none | versions: 2 dist -.tarball:http://hm.blue.com/1.0.0.tgz -.shasum:123 -.integrity:--- -.unpackedSize:1 B +.tarball:,http://hm.blue.com/1.0.0.tgz +.shasum:,123 +.integrity:,--- +.unpackedSize:,1 B dist-tags: latest: 1.0.0 @@ -102,16 +108,15 @@ dist-tags: published {TIME} ago ` -exports[`test/lib/commands/view.js TAP should log info of package in current working dir specific version > must match snapshot 1`] = ` - +exports[`test/lib/commands/view.js TAP package in cwd non-specific version > must match snapshot 1`] = ` blue@1.0.0 | Proprietary | deps: none | versions: 2 dist -.tarball:http://hm.blue.com/1.0.0.tgz -.shasum:123 -.integrity:--- -.unpackedSize:1 B +.tarball:,http://hm.blue.com/1.0.0.tgz +.shasum:,123 +.integrity:,--- +.unpackedSize:,1 B dist-tags: latest: 1.0.0 @@ -119,38 +124,23 @@ dist-tags: published {TIME} ago ` -exports[`test/lib/commands/view.js TAP should log package info package from git > must match snapshot 1`] = ` - - -green@1.0.0 | ACME | deps: 2 | versions: 2 -green is a very important color - -DEPRECATED!! - true - -keywords:colors, green, crayola +exports[`test/lib/commands/view.js TAP package in cwd specific version > must match snapshot 1`] = ` -bin:green +blue@1.0.0 | Proprietary | deps: none | versions: 2 dist -.tarball:http://hm.green.com/1.0.0.tgz -.shasum:123 -.integrity:--- -.unpackedSize:1 B - -dependencies: -red: 1.0.0 -yellow: 1.0.0 - -maintainers: --claudia <c@yellow.com> --isaacs <i@yellow.com> +.tarball:,http://hm.blue.com/1.0.0.tgz +.shasum:,123 +.integrity:,--- +.unpackedSize:,1 B dist-tags: latest: 1.0.0 -` -exports[`test/lib/commands/view.js TAP should log package info package with --json and semver range > must match snapshot 1`] = ` +published {TIME} ago +` +exports[`test/lib/commands/view.js TAP package with --json and semver range > must match snapshot 1`] = ` [ { "_npmUser": "claudia ", @@ -168,7 +158,7 @@ exports[`test/lib/commands/view.js TAP should log package info package with --js "tarball": "http://hm.cyan.com/1.0.0.tgz", "integrity": "---", "fileCount": 1, - "unpackedSize": 1 + "unpackedSize": 1000000 } }, { @@ -185,77 +175,44 @@ exports[`test/lib/commands/view.js TAP should log package info package with --js ] ` -exports[`test/lib/commands/view.js TAP should log package info package with homepage > must match snapshot 1`] = ` - +exports[`test/lib/commands/view.js TAP package with homepage > must match snapshot 1`] = ` orange@1.0.0 | Proprietary | deps: none | versions: 2 http://hm.orange.com dist -.tarball:http://hm.orange.com/1.0.0.tgz -.shasum:123 -.integrity:--- -.unpackedSize:1 B - -dist-tags: -latest: 1.0.0 -` - -exports[`test/lib/commands/view.js TAP should log package info package with license, bugs, repository and other fields > must match snapshot 1`] = ` - - -green@1.0.0 | ACME | deps: 2 | versions: 2 -green is a very important color - -DEPRECATED!! - true - -keywords:colors, green, crayola - -bin:green - -dist -.tarball:http://hm.green.com/1.0.0.tgz -.shasum:123 -.integrity:--- -.unpackedSize:1 B - -dependencies: -red: 1.0.0 -yellow: 1.0.0 - -maintainers: --claudia <c@yellow.com> --isaacs <i@yellow.com> +.tarball:,http://hm.orange.com/1.0.0.tgz +.shasum:,123 +.integrity:,--- +.unpackedSize:,1 B dist-tags: latest: 1.0.0 ` -exports[`test/lib/commands/view.js TAP should log package info package with maintainers info as object > must match snapshot 1`] = ` - +exports[`test/lib/commands/view.js TAP package with maintainers info as object > must match snapshot 1`] = ` pink@1.0.0 | Proprietary | deps: none | versions: 2 dist -.tarball:http://hm.pink.com/1.0.0.tgz -.shasum:123 -.integrity:--- -.unpackedSize:1 B +.tarball:,http://hm.pink.com/1.0.0.tgz +.shasum:,123 +.integrity:,--- +.unpackedSize:,1 B dist-tags: latest: 1.0.0 ` -exports[`test/lib/commands/view.js TAP should log package info package with more than 25 deps > must match snapshot 1`] = ` - +exports[`test/lib/commands/view.js TAP package with more than 25 deps > must match snapshot 1`] = ` black@1.0.0 | Proprietary | deps: 25 | versions: 2 dist -.tarball:http://hm.black.com/1.0.0.tgz -.shasum:123 -.integrity:--- -.unpackedSize:1 B +.tarball:,http://hm.black.com/1.0.0.tgz +.shasum:,123 +.integrity:,--- +.unpackedSize:,1 B dependencies: 0: 1.0.0 @@ -288,16 +245,15 @@ dist-tags: latest: 1.0.0 ` -exports[`test/lib/commands/view.js TAP should log package info package with no modified time > must match snapshot 1`] = ` - +exports[`test/lib/commands/view.js TAP package with no modified time > must match snapshot 1`] = ` cyan@1.0.0 | Proprietary | deps: none | versions: 2 dist -.tarball:http://hm.cyan.com/1.0.0.tgz -.shasum:123 -.integrity:--- -.unpackedSize:1 B +.tarball:,http://hm.cyan.com/1.0.0.tgz +.shasum:,123 +.integrity:,--- +.unpackedSize:,1.0 MB dist-tags: latest: 1.0.0 @@ -305,16 +261,15 @@ dist-tags: published by claudia <claudia@cyan.com> ` -exports[`test/lib/commands/view.js TAP should log package info package with no repo or homepage > must match snapshot 1`] = ` - +exports[`test/lib/commands/view.js TAP package with no repo or homepage > must match snapshot 1`] = ` blue@1.0.0 | Proprietary | deps: none | versions: 2 dist -.tarball:http://hm.blue.com/1.0.0.tgz -.shasum:123 -.integrity:--- -.unpackedSize:1 B +.tarball:,http://hm.blue.com/1.0.0.tgz +.shasum:,123 +.integrity:,--- +.unpackedSize:,1 B dist-tags: latest: 1.0.0 @@ -322,16 +277,15 @@ dist-tags: published {TIME} ago ` -exports[`test/lib/commands/view.js TAP should log package info package with semver range > must match snapshot 1`] = ` - +exports[`test/lib/commands/view.js TAP package with semver range > must match snapshot 1`] = ` blue@1.0.0 | Proprietary | deps: none | versions: 2 dist -.tarball:http://hm.blue.com/1.0.0.tgz -.shasum:123 -.integrity:--- -.unpackedSize:1 B +.tarball:,http://hm.blue.com/1.0.0.tgz +.shasum:,123 +.integrity:,--- +.unpackedSize:,1 B dist-tags: latest: 1.0.0 @@ -341,10 +295,10 @@ published {TIME} ago blue@1.0.1 | Proprietary | deps: none | versions: 2 dist -.tarball:http://hm.blue.com/1.0.1.tgz -.shasum:124 -.integrity:--- -.unpackedSize:1 B +.tarball:,http://hm.blue.com/1.0.1.tgz +.shasum:,124 +.integrity:,--- +.unpackedSize:,1.0 kB dist-tags: latest: 1.0.0 @@ -352,8 +306,47 @@ dist-tags: published over a year from now ` -exports[`test/lib/commands/view.js TAP workspaces all workspaces --json > must match snapshot 1`] = ` +exports[`test/lib/commands/view.js TAP specific field names array field - 1 element > must match snapshot 1`] = ` +claudia +` + +exports[`test/lib/commands/view.js TAP specific field names array field - 2 elements > must match snapshot 1`] = ` +maintainers[0].name = 'claudia' +maintainers[1].name = 'isaacs' +` + +exports[`test/lib/commands/view.js TAP specific field names maintainers with email > must match snapshot 1`] = ` +maintainers = [ + { name: 'claudia', email: 'c@yellow.com', twitter: 'cyellow' }, + { name: 'isaacs', email: 'i@yellow.com', twitter: 'iyellow' } +] +name = 'yellow' +` + +exports[`test/lib/commands/view.js TAP specific field names maintainers with url > must match snapshot 1`] = ` +[ 'claudia (http://c.pink.com)', 'isaacs (http://i.pink.com)' ] +` + +exports[`test/lib/commands/view.js TAP specific field names nested field with brackets > must match snapshot 1`] = ` +123 +` + +exports[`test/lib/commands/view.js TAP specific field names readme > must match snapshot 1`] = ` +a very useful readme +` + +exports[`test/lib/commands/view.js TAP specific field names several fields > must match snapshot 1`] = ` +name = 'yellow' +version = '1.0.0' +` + +exports[`test/lib/commands/view.js TAP specific field names several fields with several versions > must match snapshot 1`] = ` +yellow@1.0.0 'claudia' +yellow@1.0.1 'claudia' +yellow@1.0.2 'claudia' +` +exports[`test/lib/commands/view.js TAP workspaces all workspaces --json > must match snapshot 1`] = ` { "green": { "_id": "green", @@ -406,7 +399,7 @@ exports[`test/lib/commands/view.js TAP workspaces all workspaces --json > must m "tarball": "http://hm.green.com/1.0.0.tgz", "integrity": "---", "fileCount": 1, - "unpackedSize": 1 + "unpackedSize": 1000000000 } }, "orange": { @@ -434,29 +427,28 @@ exports[`test/lib/commands/view.js TAP workspaces all workspaces --json > must m exports[`test/lib/commands/view.js TAP workspaces all workspaces > must match snapshot 1`] = ` - green@1.0.0 | ACME | deps: 2 | versions: 2 green is a very important color DEPRECATED!! - true -keywords:colors, green, crayola +keywords:,colors, green, crayola -bin:green +bin:,green dist -.tarball:http://hm.green.com/1.0.0.tgz -.shasum:123 -.integrity:--- -.unpackedSize:1 B +.tarball:,http://hm.green.com/1.0.0.tgz +.shasum:,123 +.integrity:,--- +.unpackedSize:,1.0 GB dependencies: red: 1.0.0 yellow: 1.0.0 maintainers: --claudia <c@yellow.com> --isaacs <i@yellow.com> +-,claudia <c@yellow.com> +-,isaacs <i@yellow.com> dist-tags: latest: 1.0.0 @@ -465,10 +457,10 @@ dist-tags: http://hm.orange.com dist -.tarball:http://hm.orange.com/1.0.0.tgz -.shasum:123 -.integrity:--- -.unpackedSize:1 B +.tarball:,http://hm.orange.com/1.0.0.tgz +.shasum:,123 +.integrity:,--- +.unpackedSize:,1 B dist-tags: latest: 1.0.0 @@ -479,13 +471,11 @@ exports[`test/lib/commands/view.js TAP workspaces all workspaces nonexistent fie ` exports[`test/lib/commands/view.js TAP workspaces all workspaces nonexistent field > must match snapshot 1`] = ` - green: orange: ` exports[`test/lib/commands/view.js TAP workspaces all workspaces single field --json > must match snapshot 1`] = ` - { "green": "green", "orange": "orange" @@ -493,7 +483,6 @@ exports[`test/lib/commands/view.js TAP workspaces all workspaces single field -- ` exports[`test/lib/commands/view.js TAP workspaces all workspaces single field > must match snapshot 1`] = ` - green: green orange: @@ -502,55 +491,56 @@ orange exports[`test/lib/commands/view.js TAP workspaces one specific workspace > must match snapshot 1`] = ` - green@1.0.0 | ACME | deps: 2 | versions: 2 green is a very important color DEPRECATED!! - true -keywords:colors, green, crayola +keywords:,colors, green, crayola -bin:green +bin:,green dist -.tarball:http://hm.green.com/1.0.0.tgz -.shasum:123 -.integrity:--- -.unpackedSize:1 B +.tarball:,http://hm.green.com/1.0.0.tgz +.shasum:,123 +.integrity:,--- +.unpackedSize:,1.0 GB dependencies: red: 1.0.0 yellow: 1.0.0 maintainers: --claudia <c@yellow.com> --isaacs <i@yellow.com> +-,claudia <c@yellow.com> +-,isaacs <i@yellow.com> dist-tags: latest: 1.0.0 ` exports[`test/lib/commands/view.js TAP workspaces remote package name > must match snapshot 1`] = ` -Ignoring workspaces for specified package(s) -` - -exports[`test/lib/commands/view.js TAP workspaces remote package name > must match snapshot 2`] = ` - pink@1.0.0 | Proprietary | deps: none | versions: 2 dist -.tarball:http://hm.pink.com/1.0.0.tgz -.shasum:123 -.integrity:--- -.unpackedSize:1 B +.tarball:,http://hm.pink.com/1.0.0.tgz +.shasum:,123 +.integrity:,--- +.unpackedSize:,1 B dist-tags: latest: 1.0.0 ` -exports[`test/lib/commands/view.js TAP workspaces single workspace --json > must match snapshot 1`] = ` +exports[`test/lib/commands/view.js TAP workspaces remote package name > should have warning of ignoring workspaces 1`] = ` +Array [ + Array [ + "Ignoring workspaces for specified package(s)", + ], +] +` +exports[`test/lib/commands/view.js TAP workspaces single workspace --json > must match snapshot 1`] = ` { "green": { "_id": "green", @@ -603,7 +593,7 @@ exports[`test/lib/commands/view.js TAP workspaces single workspace --json > must "tarball": "http://hm.green.com/1.0.0.tgz", "integrity": "---", "fileCount": 1, - "unpackedSize": 1 + "unpackedSize": 1000000000 } } } diff --git a/test/coverage-map.js b/test/coverage-map.js deleted file mode 100644 index 9a289b6489e3e..0000000000000 --- a/test/coverage-map.js +++ /dev/null @@ -1,26 +0,0 @@ -const coverageMap = (filename) => { - const { basename } = require('path') - const testbase = basename(filename) - if (filename === 'test/index.js') { - return ['index.js'] - } - if (testbase === 'load-all-commands.js') { - const { cmdList } = require('../lib/utils/cmd-list.js') - return cmdList.map(cmd => `lib/${cmd}.js`) - .concat('lib/base-command.js') - } - if (/^test\/lib\/commands/.test(filename) || filename === 'test/lib/npm.js') { - return [ - filename.replace(/^test\//, ''), - 'lib/npm.js', - 'lib/base-command.js', - 'lib/exec/get-workspace-location-msg.js', - ] - } - if (/^test\/(lib|bin)\//.test(filename)) { - return filename.replace(/^test\//, '') - } - return [] -} - -module.exports = coverageMap diff --git a/test/lib/commands/view.js b/test/lib/commands/view.js index 82be1201ee169..da823db5d7507 100644 --- a/test/lib/commands/view.js +++ b/test/lib/commands/view.js @@ -1,4 +1,5 @@ const t = require('tap') +const { load: _loadMockNpm } = require('../../fixtures/mock-npm.js') t.cleanSnapshot = str => str .replace(/(published ).*?( ago)/g, '$1{TIME}$2') @@ -6,18 +7,6 @@ t.cleanSnapshot = str => str // run the same as tap does when running directly with node process.stdout.columns = undefined -const { fake: mockNpm } = require('../../fixtures/mock-npm') - -let logs -const cleanLogs = () => { - logs = '' - const fn = (...args) => { - logs += '\n' - args.map(el => logs += el) - } - console.log = fn -} - // 3 days. its never yesterday and never a week ago const yesterday = new Date(Date.now() - 1000 * 60 * 60 * 24 * 3) @@ -71,7 +60,7 @@ const packument = (nv, opts) => { tarball: 'http://hm.blue.com/1.0.1.tgz', integrity: '---', fileCount: 1, - unpackedSize: 1, + unpackedSize: 1000, }, }, }, @@ -94,7 +83,7 @@ const packument = (nv, opts) => { tarball: 'http://hm.cyan.com/1.0.0.tgz', integrity: '---', fileCount: 1, - unpackedSize: 1, + unpackedSize: 1000000, }, }, '1.0.1': {}, @@ -180,7 +169,7 @@ const packument = (nv, opts) => { tarball: 'http://hm.green.com/1.0.0.tgz', integrity: '---', fileCount: 1, - unpackedSize: 1, + unpackedSize: 1000000000, }, }, '1.0.1': {}, @@ -271,289 +260,212 @@ const packument = (nv, opts) => { return mocks[nv.name] } -t.beforeEach(cleanLogs) - -t.test('should log package info', async t => { - const View = t.mock('../../../lib/commands/view.js', { - pacote: { - packument, - }, - }) - const npm = mockNpm({ - config: { unicode: false }, - }) - const view = new View(npm) - - const ViewJson = t.mock('../../../lib/commands/view.js', { - pacote: { - packument, +const loadMockNpm = async function (t, opts = {}) { + const consoleLogs = [] + const mockNpm = await _loadMockNpm(t, { + mocks: { + pacote: { + packument, + }, }, - }) - const jsonNpm = mockNpm({ - config: { - json: true, - tag: 'latest', + globals: { + 'console.log': (...args) => { + consoleLogs.push(args) + }, }, + ...opts, }) - const viewJson = new ViewJson(jsonNpm) + return { ...mockNpm, consoleLogs } +} - const ViewUnicode = t.mock('../../../lib/commands/view.js', { - pacote: { - packument, - }, - }) - const unicodeNpm = mockNpm({ - config: { unicode: true }, - }) - const viewUnicode = new ViewUnicode(unicodeNpm) +t.test('package from git', async t => { + const { npm, consoleLogs } = await loadMockNpm(t, { config: { unicode: false } }) + await npm.exec('view', ['https://github.com/npm/green']) + t.matchSnapshot(consoleLogs.join('\n')) +}) - t.test('package from git', async t => { - await view.exec(['https://github.com/npm/green']) - t.matchSnapshot(logs) - }) +t.test('deprecated package with license, bugs, repository and other fields', async t => { + const { npm, consoleLogs } = await loadMockNpm(t, { config: { unicode: false } }) + await npm.exec('view', ['green@1.0.0']) + t.matchSnapshot(consoleLogs.join('\n')) +}) - t.test('package with license, bugs, repository and other fields', async t => { - await view.exec(['green@1.0.0']) - t.matchSnapshot(logs) - }) +t.test('deprecated package with unicode', async t => { + const { npm, consoleLogs } = await loadMockNpm(t, { config: { unicode: true } }) + await npm.exec('view', ['green@1.0.0']) + t.matchSnapshot(consoleLogs.join('\n')) +}) - t.test('package with more than 25 deps', async t => { - await view.exec(['black@1.0.0']) - t.matchSnapshot(logs) - }) +t.test('package with more than 25 deps', async t => { + const { npm, consoleLogs } = await loadMockNpm(t, { config: { unicode: false } }) + await npm.exec('view', ['black@1.0.0']) + t.matchSnapshot(consoleLogs.join('\n')) +}) - t.test('package with maintainers info as object', async t => { - await view.exec(['pink@1.0.0']) - t.matchSnapshot(logs) - }) +t.test('package with maintainers info as object', async t => { + const { npm, consoleLogs } = await loadMockNpm(t, { config: { unicode: false } }) + await npm.exec('view', ['pink@1.0.0']) + t.matchSnapshot(consoleLogs.join('\n')) +}) - t.test('package with homepage', async t => { - await view.exec(['orange@1.0.0']) - t.matchSnapshot(logs) - }) +t.test('package with homepage', async t => { + const { npm, consoleLogs } = await loadMockNpm(t, { config: { unicode: false } }) + await npm.exec('view', ['orange@1.0.0']) + t.matchSnapshot(consoleLogs.join('\n')) +}) - t.test('package with no versions', async t => { - await view.exec(['brown']) - t.equal(logs, '', 'no info to display') - }) +t.test('package with no versions', async t => { + const { npm, consoleLogs } = await loadMockNpm(t, { config: { unicode: false } }) + await npm.exec('view', ['brown']) + t.equal(consoleLogs.join('\n'), '', 'no info to display') +}) - t.test('package with no repo or homepage', async t => { - await view.exec(['blue@1.0.0']) - t.matchSnapshot(logs) - }) +t.test('package with no repo or homepage', async t => { + const { npm, consoleLogs } = await loadMockNpm(t, { config: { unicode: false } }) + await npm.exec('view', ['blue@1.0.0']) + t.matchSnapshot(consoleLogs.join('\n')) +}) - t.test('package with semver range', async t => { - await view.exec(['blue@^1.0.0']) - t.matchSnapshot(logs) - }) +t.test('package with semver range', async t => { + const { npm, consoleLogs } = await loadMockNpm(t, { config: { unicode: false } }) + await npm.exec('view', ['blue@^1.0.0']) + t.matchSnapshot(consoleLogs.join('\n')) +}) - t.test('package with no modified time', async t => { - await viewUnicode.exec(['cyan@1.0.0']) - t.matchSnapshot(logs) - }) +t.test('package with no modified time', async t => { + const { npm, consoleLogs } = await loadMockNpm(t, { config: { unicode: false } }) + await npm.exec('view', ['cyan@1.0.0']) + t.matchSnapshot(consoleLogs.join('\n')) +}) - t.test('package with --json and semver range', async t => { - await viewJson.exec(['cyan@^1.0.0']) - t.matchSnapshot(logs) - }) +t.test('package with --json and semver range', async t => { + const { npm, consoleLogs } = await loadMockNpm(t, { config: { json: true } }) + await npm.exec('view', ['cyan@^1.0.0']) + t.matchSnapshot(consoleLogs.join('\n')) +}) - t.test('package with --json and no versions', async t => { - await viewJson.exec(['brown']) - t.equal(logs, '', 'no info to display') - }) +t.test('package with --json and no versions', async t => { + const { npm, consoleLogs } = await loadMockNpm(t, { config: { json: true } }) + await npm.exec('view', ['brown']) + t.equal(consoleLogs.join('\n'), '', 'no info to display') }) -t.test('should log info of package in current working dir', async t => { - const testDir = t.testdir({ +t.test('package in cwd', async t => { + const prefixDir = { 'package.json': JSON.stringify({ name: 'blue', version: '1.0.0', }, null, 2), - }) - - const View = t.mock('../../../lib/commands/view.js', { - pacote: { - packument, - }, - }) - const npm = mockNpm({ - prefix: testDir, - config: { - tag: '1.0.0', - }, - }) - const view = new View(npm) + } t.test('specific version', async t => { - await view.exec(['.@1.0.0']) - t.matchSnapshot(logs) + const { npm, consoleLogs } = await loadMockNpm(t, { prefixDir }) + await npm.exec('view', ['.@1.0.0']) + t.matchSnapshot(consoleLogs.join('\n')) }) t.test('non-specific version', async t => { - await view.exec(['.']) - t.matchSnapshot(logs) + const { npm, consoleLogs } = await loadMockNpm(t, { prefixDir }) + await npm.exec('view', ['.']) + t.matchSnapshot(consoleLogs.join('\n')) }) t.test('directory', async t => { - await view.exec(['./blue']) - t.matchSnapshot(logs) + const { npm, consoleLogs } = await loadMockNpm(t, { prefixDir }) + await npm.exec('view', ['./blue']) + t.matchSnapshot(consoleLogs.join('\n')) }) }) -t.test('should log info by field name', async t => { - const ViewJson = t.mock('../../../lib/commands/view.js', { - pacote: { - packument, - }, - }) - const jsonNpm = mockNpm({ - config: { - tag: 'latest', - json: true, - }, - }) - - const viewJson = new ViewJson(jsonNpm) - - const View = t.mock('../../../lib/commands/view.js', { - pacote: { - packument, - }, +t.test('specific field names', async t => { + const { npm, consoleLogs } = await loadMockNpm(t) + t.afterEach(() => { + consoleLogs.length = 0 }) - const npm = mockNpm() - const view = new View(npm) - t.test('readme', async t => { - await view.exec(['yellow@1.0.0', 'readme']) - t.matchSnapshot(logs) + await npm.exec('view', ['yellow@1.0.0', 'readme']) + t.matchSnapshot(consoleLogs.join('\n')) }) t.test('several fields', async t => { - await viewJson.exec(['yellow@1.0.0', 'name', 'version', 'foo[bar]']) - t.matchSnapshot(logs) + await npm.exec('view', ['yellow@1.0.0', 'name', 'version', 'foo[bar]']) + t.matchSnapshot(consoleLogs.join('\n')) }) t.test('several fields with several versions', async t => { - await view.exec(['yellow@1.x.x', 'author']) - t.matchSnapshot(logs) + await npm.exec('view', ['yellow@1.x.x', 'author']) + t.matchSnapshot(consoleLogs.join('\n')) }) t.test('nested field with brackets', async t => { - await viewJson.exec(['orange@1.0.0', 'dist[shasum]']) - t.matchSnapshot(logs) + await npm.exec('view', ['orange@1.0.0', 'dist[shasum]']) + t.matchSnapshot(consoleLogs.join('\n')) }) t.test('maintainers with email', async t => { - await viewJson.exec(['yellow@1.0.0', 'maintainers', 'name']) - t.matchSnapshot(logs) + await npm.exec('view', ['yellow@1.0.0', 'maintainers', 'name']) + t.matchSnapshot(consoleLogs.join('\n')) }) t.test('maintainers with url', async t => { - await viewJson.exec(['pink@1.0.0', 'maintainers']) - t.matchSnapshot(logs) + await npm.exec('view', ['pink@1.0.0', 'maintainers']) + t.matchSnapshot(consoleLogs.join('\n')) }) t.test('unknown nested field ', async t => { - await view.exec(['yellow@1.0.0', 'dist.foobar']) - t.equal(logs, '', 'no info to display') + await npm.exec('view', ['yellow@1.0.0', 'dist.foobar']) + t.equal(consoleLogs.join('\n'), '', 'no info to display') }) t.test('array field - 1 element', async t => { - await view.exec(['purple@1.0.0', 'maintainers.name']) - t.matchSnapshot(logs) + await npm.exec('view', ['purple@1.0.0', 'maintainers.name']) + t.matchSnapshot(consoleLogs.join('\n')) }) t.test('array field - 2 elements', async t => { - await view.exec(['yellow@1.x.x', 'maintainers.name']) - t.matchSnapshot(logs) + await npm.exec('view', ['yellow@1.x.x', 'maintainers.name']) + t.matchSnapshot(consoleLogs.join('\n')) }) }) t.test('throw error if global mode', async t => { - const View = t.mock('../../../lib/commands/view.js') - const npm = mockNpm({ - config: { - global: true, - tag: 'latest', - }, - }) - const view = new View(npm) + const { npm } = await loadMockNpm(t, { config: { global: true } }) await t.rejects( - view.exec([]), + npm.exec('view', []), /Cannot use view command in global mode./ ) }) t.test('throw ENOENT error if package.json missing', async t => { - const testDir = t.testdir({}) - - const View = t.mock('../../../lib/commands/view.js') - const npm = mockNpm({ - prefix: testDir, - }) - const view = new View(npm) + const { npm } = await loadMockNpm(t) await t.rejects( - view.exec([]), + npm.exec('view', []), { code: 'ENOENT' } ) }) -t.test('throw EJSONPARSE error if package.json not json', async t => { - const testDir = t.testdir({ - 'package.json': 'not json, nope, not even a little bit!', - }) - - const View = t.mock('../../../lib/commands/view.js') - const npm = mockNpm({ - prefix: testDir, - }) - const view = new View(npm) - await t.rejects( - view.exec([]), - { code: 'EJSONPARSE' } - ) -}) - t.test('throw error if package.json has no name', async t => { - const testDir = t.testdir({ - 'package.json': '{}', - }) - - const View = t.mock('../../../lib/commands/view.js') - const npm = mockNpm({ - prefix: testDir, + const { npm } = await loadMockNpm(t, { + prefixDir: { + 'package.json': '{}', + }, }) - const view = new View(npm) await t.rejects( - view.exec([]), + npm.exec('view', []), /Invalid package.json, no "name" field/ ) }) t.test('throws when unpublished', async t => { - const View = t.mock('../../../lib/commands/view.js', { - pacote: { - packument, - }, - }) - const npm = mockNpm({ - config: { - tag: '1.0.1', - }, - }) - const view = new View(npm) + const { npm } = await loadMockNpm(t) await t.rejects( - view.exec(['red']), + npm.exec('view', ['red']), { code: 'E404', pkgid: 'red@1.0.1', message: 'Unpublished on 2012-12-20T00:00:00.000Z' } ) }) t.test('workspaces', async t => { - t.beforeEach(() => { - warnMsg = undefined - config.json = false - }) - const testDir = t.testdir({ + const prefixDir = { 'package.json': JSON.stringify({ name: 'workspaces-test-package', version: '1.2.3', @@ -571,106 +483,103 @@ t.test('workspaces', async t => { version: '1.2.3', }), }, - }) - const View = t.mock('../../../lib/commands/view.js', { - pacote: { - packument, - }, - 'proc-log': { - warn: (msg) => { - warnMsg = msg - }, - silly: () => {}, - }, - }) - const config = { - unicode: false, - tag: 'latest', } - let warnMsg - const npm = mockNpm({ - config, - localPrefix: testDir, - }) - const view = new View(npm) t.test('all workspaces', async t => { - await view.execWorkspaces([], []) - t.matchSnapshot(logs) + const { npm, consoleLogs } = await loadMockNpm(t, { + prefixDir, + config: { unicode: false, workspaces: true }, + }) + await npm.exec('view', []) + t.matchSnapshot(consoleLogs.join('\n')) }) t.test('one specific workspace', async t => { - await view.execWorkspaces([], ['green']) - t.matchSnapshot(logs) + const { npm, consoleLogs } = await loadMockNpm(t, { + prefixDir, + config: { unicode: false, workspace: ['green'] }, + }) + await npm.exec('view', []) + t.matchSnapshot(consoleLogs.join('\n')) }) t.test('all workspaces --json', async t => { - config.json = true - await view.execWorkspaces([], []) - t.matchSnapshot(logs) + const { npm, consoleLogs } = await loadMockNpm(t, { + prefixDir, + config: { unicode: false, workspaces: true, json: true }, + }) + await npm.exec('view', []) + t.matchSnapshot(consoleLogs.join('\n')) }) t.test('all workspaces single field', async t => { - await view.execWorkspaces(['.', 'name'], []) - t.matchSnapshot(logs) + const { npm, consoleLogs } = await loadMockNpm(t, { + prefixDir, + config: { unicode: false, workspaces: true }, + }) + await npm.exec('view', ['.', 'name']) + t.matchSnapshot(consoleLogs.join('\n')) }) t.test('all workspaces nonexistent field', async t => { - await view.execWorkspaces(['.', 'foo'], []) - t.matchSnapshot(logs) + const { npm, consoleLogs } = await loadMockNpm(t, { + prefixDir, + config: { unicode: false, workspaces: true }, + }) + await npm.exec('view', ['.', 'foo']) + t.matchSnapshot(consoleLogs.join('\n')) }) t.test('all workspaces nonexistent field --json', async t => { - config.json = true - await view.execWorkspaces(['.', 'foo'], []) - t.matchSnapshot(logs) + const { npm, consoleLogs } = await loadMockNpm(t, { + prefixDir, + config: { unicode: false, workspaces: true, json: true }, + }) + await npm.exec('view', ['.', 'foo']) + t.matchSnapshot(consoleLogs.join('\n')) }) t.test('all workspaces single field --json', async t => { - config.json = true - await view.execWorkspaces(['.', 'name'], []) - t.matchSnapshot(logs) + const { npm, consoleLogs } = await loadMockNpm(t, { + prefixDir, + config: { unicode: false, workspaces: true, json: true }, + }) + await npm.exec('view', ['.', 'name']) + t.matchSnapshot(consoleLogs.join('\n')) }) t.test('single workspace --json', async t => { - config.json = true - await view.execWorkspaces([], ['green']) - t.matchSnapshot(logs) + const { npm, consoleLogs } = await loadMockNpm(t, { + prefixDir, + config: { unicode: false, workspace: ['green'], json: true }, + }) + await npm.exec('view', []) + t.matchSnapshot(consoleLogs.join('\n')) }) t.test('remote package name', async t => { - await view.execWorkspaces(['pink'], []) - t.matchSnapshot(warnMsg) - t.matchSnapshot(logs) + const { npm, logs, consoleLogs } = await loadMockNpm(t, { + prefixDir, + config: { unicode: false, workspaces: true }, + }) + await npm.exec('view', ['pink']) + t.matchSnapshot(consoleLogs.join('\n')) + t.matchSnapshot(logs.warn, 'should have warning of ignoring workspaces') }) }) t.test('completion', async t => { - const View = t.mock('../../../lib/commands/view.js', { - pacote: { - packument, - }, - }) - const npm = mockNpm({ - config: { - tag: '1.0.1', - }, - }) - const view = new View(npm) + const { npm } = await loadMockNpm(t) + const view = await npm.cmd('view') const res = await view.completion({ conf: { argv: { remain: ['npm', 'view', 'green@1.0.0'] } }, }) t.ok(res, 'returns back fields') }) -t.test('no registry completion', async t => { - const View = t.mock('../../../lib/commands/view.js') - const npm = mockNpm({ - config: { - tag: '1.0.1', - }, - }) - const view = new View(npm) +t.test('no package completion', async t => { + const { npm } = await loadMockNpm(t) + const view = await npm.cmd('view') const res = await view.completion({ conf: { argv: { remain: ['npm', 'view'] } } }) t.notOk(res, 'there is no package completion') t.end()