Skip to content

Commit d5e865e

Browse files
committed
install, doctor: don't crash if registry unset
Close: #222 PR-URL: #226 Credit: @isaacs Close: #226
1 parent 27cccfb commit d5e865e

File tree

4 files changed

+74
-13
lines changed

4 files changed

+74
-13
lines changed

lib/doctor.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ function makePretty (p) {
8787
const cacheStatus = p[8] ? `verified ${p[8].verifiedContent} tarballs` : 'notOk'
8888
const npmV = npm.version
8989
const nodeV = process.version.replace('v', '')
90-
const registry = npm.config.get('registry')
90+
const registry = npm.config.get('registry') || ''
9191
const list = [
9292
['npm ping', ping],
9393
['npm -v', 'v' + npmV],

lib/install/inflate-shrinkwrap.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ function quotemeta (str) {
7474
}
7575

7676
function tarballToVersion (name, tb) {
77-
const registry = quotemeta(npm.config.get('registry'))
77+
const registry = quotemeta(npm.config.get('registry') || '')
7878
.replace(/https?:/, 'https?:')
7979
.replace(/([^/])$/, '$1/')
8080
let matchRegTarball

test/tap/doctor.js

+37-11
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ const http = require('http')
55
const mr = require('npm-registry-mock')
66
const npm = require('../../lib/npm.js')
77
const path = require('path')
8-
const rimraf = require('rimraf')
98
const Tacks = require('tacks')
10-
const test = require('tap').test
9+
const t = require('tap')
1110
const which = require('which')
1211

1312
const Dir = Tacks.Dir
@@ -44,12 +43,23 @@ const npmResponse = {
4443
}
4544
}
4645

47-
test('setup', (t) => {
46+
let nodeServer
47+
48+
t.teardown(() => {
49+
if (server) {
50+
server.close()
51+
}
52+
if (nodeServer) {
53+
nodeServer.close()
54+
}
55+
})
56+
57+
t.test('setup', (t) => {
4858
const port = common.port + 1
49-
http.createServer(function (q, s) {
59+
nodeServer = http.createServer(function (q, s) {
5060
s.end(JSON.stringify([{lts: true, version: '0.0.0'}]))
51-
this.close()
52-
}).listen(port, () => {
61+
})
62+
nodeServer.listen(port, () => {
5363
node_url = 'http://localhost:' + port
5464
mr({port: common.port}, (err, s) => {
5565
t.ifError(err, 'registry mocked successfully')
@@ -78,7 +88,7 @@ test('setup', (t) => {
7888
})
7989
})
8090

81-
test('npm doctor', function (t) {
91+
t.test('npm doctor', function (t) {
8292
npm.commands.doctor({'node-url': node_url}, true, function (e, list) {
8393
t.ifError(e, 'npm loaded successfully')
8494
t.same(list.length, 9, 'list should have 9 prop')
@@ -93,13 +103,29 @@ test('npm doctor', function (t) {
93103
which('git', function (e, resolvedPath) {
94104
t.ifError(e, 'git command is installed')
95105
t.same(list[4][1], resolvedPath, 'which git')
96-
server.close()
97106
t.done()
98107
})
99108
})
100109
})
101110

102-
test('cleanup', (t) => {
103-
rimraf.sync(ROOT)
104-
t.done()
111+
t.test('npm doctor works without registry', function (t) {
112+
npm.config.set('registry', false)
113+
npm.commands.doctor({'node-url': node_url}, true, function (e, list) {
114+
t.ifError(e, 'npm loaded successfully')
115+
t.same(list.length, 9, 'list should have 9 prop')
116+
t.same(list[0][1], 'OK', 'npm ping')
117+
t.same(list[1][1], 'v' + npm.version, 'npm -v')
118+
t.same(list[2][1], process.version, 'node -v')
119+
t.same(list[3][1], '', 'no registry, but no crash')
120+
t.same(list[5][1], 'ok', 'Perms check on cached files')
121+
t.same(list[6][1], 'ok', 'Perms check on global node_modules')
122+
t.same(list[7][1], 'ok', 'Perms check on local node_modules')
123+
t.match(list[8][1], /^verified \d+ tarballs?$/, 'Cache verified')
124+
which('git', function (e, resolvedPath) {
125+
t.ifError(e, 'git command is installed')
126+
t.same(list[4][1], resolvedPath, 'which git')
127+
server.close()
128+
t.done()
129+
})
130+
})
105131
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const t = require('tap')
2+
const { pkg, npm } = require('../common-tap.js')
3+
const { writeFileSync, statSync, readFileSync } = require('fs')
4+
const mkdirp = require('mkdirp')
5+
const proj = pkg + '/project'
6+
const dep = pkg + '/dep'
7+
mkdirp.sync(proj)
8+
mkdirp.sync(dep)
9+
writeFileSync(dep + '/package.json', JSON.stringify({
10+
name: 'dependency',
11+
version: '1.2.3'
12+
}))
13+
writeFileSync(proj + '/package.json', JSON.stringify({
14+
name: 'project',
15+
version: '4.2.0'
16+
}))
17+
18+
const runTest = t => npm([
19+
'install',
20+
'../dep',
21+
'--no-registry'
22+
], { cwd: proj }).then(([code, out, err]) => {
23+
t.equal(code, 0)
24+
t.match(out, /^\+ dependency@1\.2\.3\n.* 1 package in [0-9.]+m?s\n$/)
25+
t.equal(err, '')
26+
const data = readFileSync(proj + '/node_modules/dependency/package.json', 'utf8')
27+
t.same(JSON.parse(data), {
28+
name: 'dependency',
29+
version: '1.2.3'
30+
}, 'dep got installed')
31+
t.ok(statSync(proj + '/package-lock.json').isFile(), 'package-lock exists')
32+
})
33+
34+
t.test('install without a registry, no package lock', t => runTest(t))
35+
t.test('install without a registry, with package lock', t => runTest(t))

0 commit comments

Comments
 (0)