Skip to content

Commit

Permalink
fix(view): fix non-registry specs
Browse files Browse the repository at this point in the history
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 aren't asking for info from a
registry spec, it goes ahead and looks for the `latest` tag (or whatever
tag you have configured as your default).
  • Loading branch information
wraithgar committed May 7, 2021
1 parent 049166b commit 291ab47
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 16 deletions.
5 changes: 4 additions & 1 deletion lib/view.js
Expand Up @@ -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)

Expand Down
16 changes: 10 additions & 6 deletions tap-snapshots/test/lib/view.js.test.cjs
Expand Up @@ -80,7 +80,7 @@ dist
.unpackedSize:1 B
dist-tags:
[1m[32mlatest[39m[22m: 1.0.0
published a year ago
`
Expand All @@ -97,7 +97,7 @@ dist
.unpackedSize:1 B
dist-tags:
[1m[32mlatest[39m[22m: 1.0.0
published a year ago
`
Expand All @@ -108,7 +108,9 @@ exports[`test/lib/view.js TAP should log package info package with --json and se
{
"_npmUser": "claudia <claudia@cyan.com>",
"name": "cyan",
"dist-tags": {},
"dist-tags": {
"latest": "1.0.0"
},
"versions": [
"1.0.0",
"1.0.1"
Expand All @@ -125,7 +127,9 @@ exports[`test/lib/view.js TAP should log package info package with --json and se
{
"_npmUser": "claudia <claudia@cyan.com>",
"name": "cyan",
"dist-tags": {},
"dist-tags": {
"latest": "1.0.0"
},
"versions": [
"1.0.0",
"1.0.1"
Expand Down Expand Up @@ -249,7 +253,7 @@ dist
.unpackedSize:1 B
dist-tags:
[1m[32mlatest[39m[22m: 1.0.0
published by claudia <claudia@cyan.com>
`
Expand All @@ -266,7 +270,7 @@ dist
.unpackedSize:1 B
dist-tags:
[1m[32mlatest[39m[22m: 1.0.0
published a year ago
`
Expand Down
45 changes: 36 additions & 9 deletions test/lib/view.js
Expand Up @@ -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',
},
Expand All @@ -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',
Expand Down Expand Up @@ -248,7 +252,10 @@ t.test('should log package info', t => {
},
})
const npm = mockNpm({
config: { global: false },
config: {
global: false,
tag: 'latest',
},
})
const view = new View(npm)

Expand All @@ -258,7 +265,10 @@ t.test('should log package info', t => {
},
})
const jsonNpm = mockNpm({
config: { json: true },
config: {
json: true,
tag: 'latest',
},
})
const viewJson = new ViewJson(jsonNpm)

Expand All @@ -269,6 +279,7 @@ t.test('should log package info', t => {
})
const unicodeNpm = mockNpm({
config: {
tag: 'latest',
global: false,
unicode: true,
},
Expand Down Expand Up @@ -388,6 +399,7 @@ t.test('should log info by field name', t => {
})
const jsonNpm = mockNpm({
config: {
tag: 'latest',
json: true,
global: false,
},
Expand All @@ -401,7 +413,10 @@ t.test('should log info by field name', t => {
},
})
const npm = mockNpm({
config: { global: false },
config: {
global: false,
tag: 'latest',
},
})
const view = new View(npm)

Expand Down Expand Up @@ -474,7 +489,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) => {
Expand All @@ -489,7 +507,10 @@ t.test('throw ENOENT error if package.json misisng', (t) => {
const View = t.mock('../../lib/view.js')
const npm = mockNpm({
prefix: testDir,
config: { global: false },
config: {
global: false,
tag: 'latest',
},
})
const view = new View(npm)
view.exec([], (err) => {
Expand All @@ -506,7 +527,10 @@ t.test('throw EJSONPARSE error if package.json not json', (t) => {
const View = t.mock('../../lib/view.js')
const npm = mockNpm({
prefix: testDir,
config: { global: false },
config: {
global: false,
tag: 'latest',
},
})
const view = new View(npm)
view.exec([], (err) => {
Expand All @@ -523,7 +547,10 @@ t.test('throw error if package.json has no name', (t) => {
const View = t.mock('../../lib/view.js')
const npm = mockNpm({
prefix: testDir,
config: { global: false },
config: {
global: false,
tag: 'latest',
},
})
const view = new View(npm)
view.exec([], (err) => {
Expand Down

0 comments on commit 291ab47

Please sign in to comment.