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 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).
  • Loading branch information
wraithgar committed May 10, 2021
1 parent f3a662f commit b6b373e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 11 deletions.
5 changes: 4 additions & 1 deletion lib/view.js
Original file line number Diff line number Diff line change
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
46 changes: 40 additions & 6 deletions tap-snapshots/test/lib/view.js.test.cjs
Original file line number Diff line number Diff line change
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,18 +97,50 @@ dist
.unpackedSize:1 B
dist-tags:
[1m[32mlatest[39m[22m: 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 <claudia@cyan.com>",
"name": "cyan",
"dist-tags": {},
"dist-tags": {
"latest": "1.0.0"
},
"versions": [
"1.0.0",
"1.0.1"
Expand All @@ -125,7 +157,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 +283,7 @@ dist
.unpackedSize:1 B
dist-tags:
[1m[32mlatest[39m[22m: 1.0.0
published by claudia <claudia@cyan.com>
`
Expand All @@ -266,7 +300,7 @@ dist
.unpackedSize:1 B
dist-tags:
[1m[32mlatest[39m[22m: 1.0.0
published a year ago
`
Expand Down
28 changes: 24 additions & 4 deletions test/lib/view.js
Original file line number Diff line number Diff line change
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 @@ -236,6 +240,8 @@ const packument = (nv, opts) => {
},
},
}
if (nv.type === 'git')
return mocks[nv.hosted.project]
return mocks[nv.name]
}

Expand All @@ -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)

Expand All @@ -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)
Expand Down Expand Up @@ -384,6 +400,7 @@ t.test('should log info by field name', t => {
})
const jsonNpm = mockNpm({
config: {
tag: 'latest',
json: true,
},
})
Expand Down Expand Up @@ -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) => {
Expand Down

0 comments on commit b6b373e

Please sign in to comment.