From 0696fca13d10726e04ca97ff50eef7bd7455a3ab Mon Sep 17 00:00:00 2001 From: Gar Date: Fri, 7 May 2021 09:41:55 -0700 Subject: [PATCH] fix(view): fix non-registry specs This was working by coincidence in 7.7.6 and before, and broken in the 7.8.0 refactor. Before, it would see there was no "name" in the spec, and then read your local package.json, and from that get a latest tag. So, if you didn't have a package.json in your CWD it would fail with an ENOENT trying to read it. This fixes it for real, so that if you are asking for info from a git spec, it goes ahead and looks for the `latest` tag (or whatever tag you have configured as your default). PR-URL: https://github.com/npm/cli/pull/3209 Credit: @wraithgar Close: #3209 Reviewed-by: @ruyadorno --- lib/view.js | 5 ++- tap-snapshots/test/lib/view.js.test.cjs | 46 +++++++++++++++++++++---- test/lib/view.js | 28 ++++++++++++--- 3 files changed, 68 insertions(+), 11 deletions(-) diff --git a/lib/view.js b/lib/view.js index 91b32e2fd38fa..9cc1aed914488 100644 --- a/lib/view.js +++ b/lib/view.js @@ -202,7 +202,10 @@ class View extends BaseCommand { const spec = npa(pkg) // get the data about this package - let version = spec.rawSpec || this.npm.config.get('tag') + let version = this.npm.config.get('tag') + // rawSpec is the git url if this is from git + if (spec.type !== 'git' && spec.rawSpec) + version = spec.rawSpec const pckmnt = await packument(spec, opts) diff --git a/tap-snapshots/test/lib/view.js.test.cjs b/tap-snapshots/test/lib/view.js.test.cjs index 1cdf356356af9..9aafe50e41896 100644 --- a/tap-snapshots/test/lib/view.js.test.cjs +++ b/tap-snapshots/test/lib/view.js.test.cjs @@ -80,7 +80,7 @@ dist .unpackedSize:1 B dist-tags: - +latest: 1.0.0 published a year ago ` @@ -97,18 +97,50 @@ dist .unpackedSize:1 B dist-tags: - +latest: 1.0.0 published a year ago ` +exports[`test/lib/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 + +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> + +dist-tags: +latest: 1.0.0 +` + exports[`test/lib/view.js TAP should log package info package with --json and semver range > must match snapshot 1`] = ` [ { "_npmUser": "claudia ", "name": "cyan", - "dist-tags": {}, + "dist-tags": { + "latest": "1.0.0" + }, "versions": [ "1.0.0", "1.0.1" @@ -125,7 +157,9 @@ exports[`test/lib/view.js TAP should log package info package with --json and se { "_npmUser": "claudia ", "name": "cyan", - "dist-tags": {}, + "dist-tags": { + "latest": "1.0.0" + }, "versions": [ "1.0.0", "1.0.1" @@ -249,7 +283,7 @@ dist .unpackedSize:1 B dist-tags: - +latest: 1.0.0 published by claudia <claudia@cyan.com> ` @@ -266,7 +300,7 @@ dist .unpackedSize:1 B dist-tags: - +latest: 1.0.0 published a year ago ` diff --git a/test/lib/view.js b/test/lib/view.js index 3816ce02352b4..4544d7d5d1a94 100644 --- a/test/lib/view.js +++ b/test/lib/view.js @@ -34,7 +34,9 @@ const packument = (nv, opts) => { }, blue: { name: 'blue', - 'dist-tags': {}, + 'dist-tags': { + latest: '1.0.0', + }, time: { '1.0.0': '2019-08-06T16:21:09.842Z', }, @@ -59,7 +61,9 @@ const packument = (nv, opts) => { email: 'claudia@cyan.com', }, name: 'cyan', - 'dist-tags': {}, + 'dist-tags': { + latest: '1.0.0', + }, versions: { '1.0.0': { version: '1.0.0', @@ -236,6 +240,8 @@ const packument = (nv, opts) => { }, }, } + if (nv.type === 'git') + return mocks[nv.hosted.project] return mocks[nv.name] } @@ -258,7 +264,10 @@ t.test('should log package info', t => { }, }) const jsonNpm = mockNpm({ - config: { json: true }, + config: { + json: true, + tag: 'latest', + }, }) const viewJson = new ViewJson(jsonNpm) @@ -272,6 +281,13 @@ t.test('should log package info', t => { }) const viewUnicode = new ViewUnicode(unicodeNpm) + t.test('package from git', t => { + view.exec(['https://github.com/npm/green'], () => { + t.matchSnapshot(logs) + t.end() + }) + }) + t.test('package with license, bugs, repository and other fields', t => { view.exec(['green@1.0.0'], () => { t.matchSnapshot(logs) @@ -384,6 +400,7 @@ t.test('should log info by field name', t => { }) const jsonNpm = mockNpm({ config: { + tag: 'latest', json: true, }, }) @@ -467,7 +484,10 @@ t.test('should log info by field name', t => { t.test('throw error if global mode', (t) => { const View = t.mock('../../lib/view.js') const npm = mockNpm({ - config: { global: true }, + config: { + global: true, + tag: 'latest', + }, }) const view = new View(npm) view.exec([], (err) => {