Skip to content

Commit

Permalink
install, doctor: don't crash if registry unset
Browse files Browse the repository at this point in the history
Close: #222
PR-URL: #226
Credit: @isaacs
Close: #226
  • Loading branch information
isaacs committed Aug 1, 2019
1 parent 27cccfb commit d5e865e
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/doctor.js
Expand Up @@ -87,7 +87,7 @@ function makePretty (p) {
const cacheStatus = p[8] ? `verified ${p[8].verifiedContent} tarballs` : 'notOk'
const npmV = npm.version
const nodeV = process.version.replace('v', '')
const registry = npm.config.get('registry')
const registry = npm.config.get('registry') || ''
const list = [
['npm ping', ping],
['npm -v', 'v' + npmV],
Expand Down
2 changes: 1 addition & 1 deletion lib/install/inflate-shrinkwrap.js
Expand Up @@ -74,7 +74,7 @@ function quotemeta (str) {
}

function tarballToVersion (name, tb) {
const registry = quotemeta(npm.config.get('registry'))
const registry = quotemeta(npm.config.get('registry') || '')
.replace(/https?:/, 'https?:')
.replace(/([^/])$/, '$1/')
let matchRegTarball
Expand Down
48 changes: 37 additions & 11 deletions test/tap/doctor.js
Expand Up @@ -5,9 +5,8 @@ const http = require('http')
const mr = require('npm-registry-mock')
const npm = require('../../lib/npm.js')
const path = require('path')
const rimraf = require('rimraf')
const Tacks = require('tacks')
const test = require('tap').test
const t = require('tap')
const which = require('which')

const Dir = Tacks.Dir
Expand Down Expand Up @@ -44,12 +43,23 @@ const npmResponse = {
}
}

test('setup', (t) => {
let nodeServer

t.teardown(() => {
if (server) {
server.close()
}
if (nodeServer) {
nodeServer.close()
}
})

t.test('setup', (t) => {
const port = common.port + 1
http.createServer(function (q, s) {
nodeServer = http.createServer(function (q, s) {
s.end(JSON.stringify([{lts: true, version: '0.0.0'}]))
this.close()
}).listen(port, () => {
})
nodeServer.listen(port, () => {
node_url = 'http://localhost:' + port
mr({port: common.port}, (err, s) => {
t.ifError(err, 'registry mocked successfully')
Expand Down Expand Up @@ -78,7 +88,7 @@ test('setup', (t) => {
})
})

test('npm doctor', function (t) {
t.test('npm doctor', function (t) {
npm.commands.doctor({'node-url': node_url}, true, function (e, list) {
t.ifError(e, 'npm loaded successfully')
t.same(list.length, 9, 'list should have 9 prop')
Expand All @@ -93,13 +103,29 @@ test('npm doctor', function (t) {
which('git', function (e, resolvedPath) {
t.ifError(e, 'git command is installed')
t.same(list[4][1], resolvedPath, 'which git')
server.close()
t.done()
})
})
})

test('cleanup', (t) => {
rimraf.sync(ROOT)
t.done()
t.test('npm doctor works without registry', function (t) {
npm.config.set('registry', false)
npm.commands.doctor({'node-url': node_url}, true, function (e, list) {
t.ifError(e, 'npm loaded successfully')
t.same(list.length, 9, 'list should have 9 prop')
t.same(list[0][1], 'OK', 'npm ping')
t.same(list[1][1], 'v' + npm.version, 'npm -v')
t.same(list[2][1], process.version, 'node -v')
t.same(list[3][1], '', 'no registry, but no crash')
t.same(list[5][1], 'ok', 'Perms check on cached files')
t.same(list[6][1], 'ok', 'Perms check on global node_modules')
t.same(list[7][1], 'ok', 'Perms check on local node_modules')
t.match(list[8][1], /^verified \d+ tarballs?$/, 'Cache verified')
which('git', function (e, resolvedPath) {
t.ifError(e, 'git command is installed')
t.same(list[4][1], resolvedPath, 'which git')
server.close()
t.done()
})
})
})
35 changes: 35 additions & 0 deletions test/tap/install-without-registry-config.js
@@ -0,0 +1,35 @@
const t = require('tap')
const { pkg, npm } = require('../common-tap.js')
const { writeFileSync, statSync, readFileSync } = require('fs')
const mkdirp = require('mkdirp')
const proj = pkg + '/project'
const dep = pkg + '/dep'
mkdirp.sync(proj)
mkdirp.sync(dep)
writeFileSync(dep + '/package.json', JSON.stringify({
name: 'dependency',
version: '1.2.3'
}))
writeFileSync(proj + '/package.json', JSON.stringify({
name: 'project',
version: '4.2.0'
}))

const runTest = t => npm([
'install',
'../dep',
'--no-registry'
], { cwd: proj }).then(([code, out, err]) => {
t.equal(code, 0)
t.match(out, /^\+ dependency@1\.2\.3\n.* 1 package in [0-9.]+m?s\n$/)
t.equal(err, '')
const data = readFileSync(proj + '/node_modules/dependency/package.json', 'utf8')
t.same(JSON.parse(data), {
name: 'dependency',
version: '1.2.3'
}, 'dep got installed')
t.ok(statSync(proj + '/package-lock.json').isFile(), 'package-lock exists')
})

t.test('install without a registry, no package lock', t => runTest(t))
t.test('install without a registry, with package lock', t => runTest(t))

0 comments on commit d5e865e

Please sign in to comment.