From 88299c9d765e2587118cdc54abb2e168b66d2bac Mon Sep 17 00:00:00 2001 From: Gar Date: Tue, 22 Nov 2022 09:29:59 -0800 Subject: [PATCH 1/4] fix: output doctor checks as they finish --- lib/commands/doctor.js | 50 +- .../test/lib/commands/doctor.js.test.cjs | 494 +++++++++--------- 2 files changed, 278 insertions(+), 266 deletions(-) diff --git a/lib/commands/doctor.js b/lib/commands/doctor.js index f5bee1eb83f6c..1c722713bff8f 100644 --- a/lib/commands/doctor.js +++ b/lib/commands/doctor.js @@ -1,7 +1,7 @@ const cacache = require('cacache') const fs = require('fs') const fetch = require('make-fetch-happen') -const table = require('text-table') +const Table = require('cli-table3') const which = require('which') const pacote = require('pacote') const { resolve } = require('path') @@ -17,6 +17,10 @@ const lstat = promisify(fs.lstat) const readdir = promisify(fs.readdir) const access = promisify(fs.access) const { R_OK, W_OK, X_OK } = fs.constants + +const table = (colWidths, rows) => { +} + const maskLabel = mask => { const label = [] if (mask & R_OK) { @@ -44,9 +48,6 @@ class Doctor extends BaseCommand { async exec (args) { log.info('Running checkup') - // each message is [title, ok, message] - const messages = [] - const actions = [ ['npm ping', 'checkPing', []], ['npm -v', 'getLatestNpmVersion', []], @@ -90,20 +91,19 @@ class Doctor extends BaseCommand { // - verify all local packages have bins linked ] + let allOk = true + this.checkWidth = actions.reduce((length, item) => Math.max(item[0].length, length), 0) + if (!this.npm.silent) { + this.output(['Check', 'Value', 'Recommendation/Notes'].map(h => this.npm.chalk.underline(h))) + } // Do the actual work for (const [msg, fn, args] of actions) { - const line = [msg] + const item = [msg] try { - line.push(true, await this[fn](...args)) + item.push(true, await this[fn](...args)) } catch (er) { - line.push(false, er) + item.push(false, er) } - messages.push(line) - } - - const outHead = ['Check', 'Value', 'Recommendation/Notes'].map(h => this.npm.chalk.underline(h)) - let allOk = true - const outBody = messages.map(item => { if (!item[1]) { allOk = false item[0] = this.npm.chalk.red(item[0]) @@ -112,15 +112,14 @@ class Doctor extends BaseCommand { } else { item[1] = this.npm.chalk.green('ok') } - return item - }) - const outTable = [outHead, ...outBody] - const tableOpts = { - stringLength: s => ansiTrim(s).length, + if (!this.npm.silent) { + this.output(item) + } } + if (!this.npm.silent) { - this.npm.output(table(outTable, tableOpts)) + // this.npm.output(table(outTable, tableOpts)) } if (!allOk) { throw new Error('Some problems found. See above for recommendations.') @@ -312,6 +311,19 @@ class Doctor extends BaseCommand { return `using default registry (${defaultRegistry})` } } + + output (row) { + const t = new Table({ + chars: { 'top': '' , 'top-mid': '' , 'top-left': '' , 'top-right': '' + , 'bottom': '' , 'bottom-mid': '' , 'bottom-left': '' , 'bottom-right': '' + , 'left': '' , 'left-mid': '' , 'mid': '' , 'mid-mid': '' + , 'right': '' , 'right-mid': '' , 'middle': ' ' }, + style: { 'padding-left': 0, 'padding-right': 0 }, + colWidths: [this.checkWidth, 6], + }); + t.push(row) + this.npm.output(t.toString()) + } } module.exports = Doctor diff --git a/tap-snapshots/test/lib/commands/doctor.js.test.cjs b/tap-snapshots/test/lib/commands/doctor.js.test.cjs index d84d7d368580c..cccf13807c83b 100644 --- a/tap-snapshots/test/lib/commands/doctor.js.test.cjs +++ b/tap-snapshots/test/lib/commands/doctor.js.test.cjs @@ -49,33 +49,33 @@ Object { ` exports[`test/lib/commands/doctor.js TAP all clear > output 1`] = ` -Check Value Recommendation/Notes -npm ping ok -npm -v ok current: v1.0.0, latest: v1.0.0 -node -v ok current: v1.0.0, recommended: v1.0.0 -npm config get registry ok using default registry (https://registry.npmjs.org/) -which git ok /path/to/git -Perms check on cached files ok -Perms check on local node_modules ok -Perms check on global node_modules ok -Perms check on local bin folder ok -Perms check on global bin folder ok -Verify cache contents ok verified 0 tarballs +Check  Value  Recommendation/Notes +npm ping  ok   +npm -v  ok  current: v1.0.0, latest: v1.0.0 +node -v  ok  current: v1.0.0, recommended: v1.0.0 +npm config get registry  ok  using default registry (https://registry.npmjs.org/) +which git  ok  /path/to/git +Perms check on cached files  ok   +Perms check on local node_modules  ok   +Perms check on global node_modules ok   +Perms check on local bin folder  ok   +Perms check on global bin folder  ok   +Verify cache contents  ok  verified 0 tarballs ` exports[`test/lib/commands/doctor.js TAP all clear in color > everything is ok in color 1`] = ` -Check Value Recommendation/Notes -npm ping ok -npm -v ok current: v1.0.0, latest: v1.0.0 -node -v ok current: v1.0.0, recommended: v1.0.0 -npm config get registry ok using default registry (https://registry.npmjs.org/) -which git ok /path/to/git -Perms check on cached files ok -Perms check on local node_modules ok -Perms check on global node_modules ok -Perms check on local bin folder ok -Perms check on global bin folder ok -Verify cache contents ok verified 0 tarballs +Check  Value  Recommendation/Notes +npm ping  ok   +npm -v  ok  current: v1.0.0, latest: v1.0.0 +node -v  ok  current: v1.0.0, recommended: v1.0.0 +npm config get registry  ok  using default registry (https://registry.npmjs.org/) +which git  ok  /path/to/git +Perms check on cached files  ok   +Perms check on local node_modules  ok   +Perms check on global node_modules ok   +Perms check on local bin folder  ok   +Perms check on global bin folder  ok   +Verify cache contents  ok  verified 0 tarballs ` exports[`test/lib/commands/doctor.js TAP all clear in color > logs 1`] = ` @@ -165,33 +165,33 @@ Object { ` exports[`test/lib/commands/doctor.js TAP bad proxy > output 1`] = ` -Check Value Recommendation/Notes -npm ping not ok unsupported proxy protocol: 'ssh:' -npm -v not ok Error: unsupported proxy protocol: 'ssh:' -node -v not ok Error: unsupported proxy protocol: 'ssh:' -npm config get registry ok using default registry (https://registry.npmjs.org/) -which git ok /path/to/git -Perms check on cached files ok -Perms check on local node_modules ok -Perms check on global node_modules ok -Perms check on local bin folder ok -Perms check on global bin folder ok -Verify cache contents ok verified 0 tarballs +Check  Value  Recommendation/Notes +npm ping  not ok unsupported proxy protocol: 'ssh:' +npm -v  not ok Error: unsupported proxy protocol: 'ssh:' +node -v  not ok Error: unsupported proxy protocol: 'ssh:' +npm config get registry  ok  using default registry (https://registry.npmjs.org/) +which git  ok  /path/to/git +Perms check on cached files  ok   +Perms check on local node_modules  ok   +Perms check on global node_modules ok   +Perms check on local bin folder  ok   +Perms check on global bin folder  ok   +Verify cache contents  ok  verified 0 tarballs ` exports[`test/lib/commands/doctor.js TAP cacache badContent > corrupted cache content 1`] = ` -Check Value Recommendation/Notes -npm ping ok -npm -v ok current: v1.0.0, latest: v1.0.0 -node -v ok current: v1.0.0, recommended: v1.0.0 -npm config get registry ok using default registry (https://registry.npmjs.org/) -which git ok /path/to/git -Perms check on cached files ok -Perms check on local node_modules ok -Perms check on global node_modules ok -Perms check on local bin folder ok -Perms check on global bin folder ok -Verify cache contents ok verified 2 tarballs +Check  Value  Recommendation/Notes +npm ping  ok   +npm -v  ok  current: v1.0.0, latest: v1.0.0 +node -v  ok  current: v1.0.0, recommended: v1.0.0 +npm config get registry  ok  using default registry (https://registry.npmjs.org/) +which git  ok  /path/to/git +Perms check on cached files  ok   +Perms check on local node_modules  ok   +Perms check on global node_modules ok   +Perms check on local bin folder  ok   +Perms check on global bin folder  ok   +Verify cache contents  ok  verified 2 tarballs ` exports[`test/lib/commands/doctor.js TAP cacache badContent > logs 1`] = ` @@ -299,33 +299,33 @@ Object { ` exports[`test/lib/commands/doctor.js TAP cacache missingContent > missing content 1`] = ` -Check Value Recommendation/Notes -npm ping ok -npm -v ok current: v1.0.0, latest: v1.0.0 -node -v ok current: v1.0.0, recommended: v1.0.0 -npm config get registry ok using default registry (https://registry.npmjs.org/) -which git ok /path/to/git -Perms check on cached files ok -Perms check on local node_modules ok -Perms check on global node_modules ok -Perms check on local bin folder ok -Perms check on global bin folder ok -Verify cache contents ok verified 2 tarballs +Check  Value  Recommendation/Notes +npm ping  ok   +npm -v  ok  current: v1.0.0, latest: v1.0.0 +node -v  ok  current: v1.0.0, recommended: v1.0.0 +npm config get registry  ok  using default registry (https://registry.npmjs.org/) +which git  ok  /path/to/git +Perms check on cached files  ok   +Perms check on local node_modules  ok   +Perms check on global node_modules ok   +Perms check on local bin folder  ok   +Perms check on global bin folder  ok   +Verify cache contents  ok  verified 2 tarballs ` exports[`test/lib/commands/doctor.js TAP cacache reclaimedCount > content garbage collected 1`] = ` -Check Value Recommendation/Notes -npm ping ok -npm -v ok current: v1.0.0, latest: v1.0.0 -node -v ok current: v1.0.0, recommended: v1.0.0 -npm config get registry ok using default registry (https://registry.npmjs.org/) -which git ok /path/to/git -Perms check on cached files ok -Perms check on local node_modules ok -Perms check on global node_modules ok -Perms check on local bin folder ok -Perms check on global bin folder ok -Verify cache contents ok verified 2 tarballs +Check  Value  Recommendation/Notes +npm ping  ok   +npm -v  ok  current: v1.0.0, latest: v1.0.0 +node -v  ok  current: v1.0.0, recommended: v1.0.0 +npm config get registry  ok  using default registry (https://registry.npmjs.org/) +which git  ok  /path/to/git +Perms check on cached files  ok   +Perms check on local node_modules  ok   +Perms check on global node_modules ok   +Perms check on local bin folder  ok   +Perms check on global bin folder  ok   +Verify cache contents  ok  verified 2 tarballs ` exports[`test/lib/commands/doctor.js TAP cacache reclaimedCount > logs 1`] = ` @@ -445,33 +445,33 @@ Object { ` exports[`test/lib/commands/doctor.js TAP error reading directory > readdir error 1`] = ` -Check Value Recommendation/Notes -npm ping ok -npm -v ok current: v1.0.0, latest: v1.0.0 -node -v ok current: v1.0.0, recommended: v1.0.0 -npm config get registry ok using default registry (https://registry.npmjs.org/) -which git ok /path/to/git -Perms check on cached files not ok Check the permissions of files in {CWD}/test/lib/commands/tap-testdir-doctor-error-reading-directory/cache (should be owned by current user) -Perms check on local node_modules not ok Check the permissions of files in {CWD}/test/lib/commands/tap-testdir-doctor-error-reading-directory/prefix/node_modules (should be owned by current user) -Perms check on global node_modules not ok Check the permissions of files in {CWD}/test/lib/commands/tap-testdir-doctor-error-reading-directory/global/lib/node_modules -Perms check on local bin folder not ok Check the permissions of files in {CWD}/test/lib/commands/tap-testdir-doctor-error-reading-directory/prefix/node_modules/.bin -Perms check on global bin folder not ok Check the permissions of files in {CWD}/test/lib/commands/tap-testdir-doctor-error-reading-directory/global/bin -Verify cache contents ok verified 0 tarballs +Check  Value  Recommendation/Notes +npm ping  ok   +npm -v  ok  current: v1.0.0, latest: v1.0.0 +node -v  ok  current: v1.0.0, recommended: v1.0.0 +npm config get registry  ok  using default registry (https://registry.npmjs.org/) +which git  ok  /path/to/git +Perms check on cached files  not ok Check the permissions of files in {CWD}/test/lib/commands/tap-testdir-doctor-error-reading-directory/cache (should be owned by current user) +Perms check on local node_modules  not ok Check the permissions of files in {CWD}/test/lib/commands/tap-testdir-doctor-error-reading-directory/prefix/node_modules (should be owned by current user) +Perms check on global node_modules not ok Check the permissions of files in {CWD}/test/lib/commands/tap-testdir-doctor-error-reading-directory/global/lib/node_modules +Perms check on local bin folder  not ok Check the permissions of files in {CWD}/test/lib/commands/tap-testdir-doctor-error-reading-directory/prefix/node_modules/.bin +Perms check on global bin folder  not ok Check the permissions of files in {CWD}/test/lib/commands/tap-testdir-doctor-error-reading-directory/global/bin +Verify cache contents  ok  verified 0 tarballs ` exports[`test/lib/commands/doctor.js TAP incorrect owner > incorrect owner 1`] = ` -Check Value Recommendation/Notes -npm ping ok -npm -v ok current: v1.0.0, latest: v1.0.0 -node -v ok current: v1.0.0, recommended: v1.0.0 -npm config get registry ok using default registry (https://registry.npmjs.org/) -which git ok /path/to/git -Perms check on cached files not ok Check the permissions of files in {CWD}/test/lib/commands/tap-testdir-doctor-incorrect-owner/cache (should be owned by current user) -Perms check on local node_modules ok -Perms check on global node_modules ok -Perms check on local bin folder ok -Perms check on global bin folder ok -Verify cache contents ok verified 0 tarballs +Check  Value  Recommendation/Notes +npm ping  ok   +npm -v  ok  current: v1.0.0, latest: v1.0.0 +node -v  ok  current: v1.0.0, recommended: v1.0.0 +npm config get registry  ok  using default registry (https://registry.npmjs.org/) +which git  ok  /path/to/git +Perms check on cached files  not ok Check the permissions of files in {CWD}/test/lib/commands/tap-testdir-doctor-incorrect-owner/cache (should be owned by current user) +Perms check on local node_modules  ok   +Perms check on global node_modules ok   +Perms check on local bin folder  ok   +Perms check on global bin folder  ok   +Verify cache contents  ok  verified 0 tarballs ` exports[`test/lib/commands/doctor.js TAP incorrect owner > logs 1`] = ` @@ -523,18 +523,18 @@ Object { ` exports[`test/lib/commands/doctor.js TAP incorrect permissions > incorrect owner 1`] = ` -Check Value Recommendation/Notes -npm ping ok -npm -v ok current: v1.0.0, latest: v1.0.0 -node -v ok current: v1.0.0, recommended: v1.0.0 -npm config get registry ok using default registry (https://registry.npmjs.org/) -which git ok /path/to/git -Perms check on cached files not ok Check the permissions of files in {CWD}/test/lib/commands/tap-testdir-doctor-incorrect-permissions/cache (should be owned by current user) -Perms check on local node_modules not ok Check the permissions of files in {CWD}/test/lib/commands/tap-testdir-doctor-incorrect-permissions/prefix/node_modules (should be owned by current user) -Perms check on global node_modules not ok Check the permissions of files in {CWD}/test/lib/commands/tap-testdir-doctor-incorrect-permissions/global/lib/node_modules -Perms check on local bin folder not ok Check the permissions of files in {CWD}/test/lib/commands/tap-testdir-doctor-incorrect-permissions/prefix/node_modules/.bin -Perms check on global bin folder not ok Check the permissions of files in {CWD}/test/lib/commands/tap-testdir-doctor-incorrect-permissions/global/bin -Verify cache contents ok verified 0 tarballs +Check  Value  Recommendation/Notes +npm ping  ok   +npm -v  ok  current: v1.0.0, latest: v1.0.0 +node -v  ok  current: v1.0.0, recommended: v1.0.0 +npm config get registry  ok  using default registry (https://registry.npmjs.org/) +which git  ok  /path/to/git +Perms check on cached files  not ok Check the permissions of files in {CWD}/test/lib/commands/tap-testdir-doctor-incorrect-permissions/cache (should be owned by current user) +Perms check on local node_modules  not ok Check the permissions of files in {CWD}/test/lib/commands/tap-testdir-doctor-incorrect-permissions/prefix/node_modules (should be owned by current user) +Perms check on global node_modules not ok Check the permissions of files in {CWD}/test/lib/commands/tap-testdir-doctor-incorrect-permissions/global/lib/node_modules +Perms check on local bin folder  not ok Check the permissions of files in {CWD}/test/lib/commands/tap-testdir-doctor-incorrect-permissions/prefix/node_modules/.bin +Perms check on global bin folder  not ok Check the permissions of files in {CWD}/test/lib/commands/tap-testdir-doctor-incorrect-permissions/global/bin +Verify cache contents  ok  verified 0 tarballs ` exports[`test/lib/commands/doctor.js TAP incorrect permissions > logs 1`] = ` @@ -649,18 +649,18 @@ Object { ` exports[`test/lib/commands/doctor.js TAP missing git > missing git 1`] = ` -Check Value Recommendation/Notes -npm ping ok -npm -v ok current: v1.0.0, latest: v1.0.0 -node -v ok current: v1.0.0, recommended: v1.0.0 -npm config get registry ok using default registry (https://registry.npmjs.org/) -which git not ok Install git and ensure it's in your PATH. -Perms check on cached files ok -Perms check on local node_modules ok -Perms check on global node_modules ok -Perms check on local bin folder ok -Perms check on global bin folder ok -Verify cache contents ok verified 0 tarballs +Check  Value  Recommendation/Notes +npm ping  ok   +npm -v  ok  current: v1.0.0, latest: v1.0.0 +node -v  ok  current: v1.0.0, recommended: v1.0.0 +npm config get registry  ok  using default registry (https://registry.npmjs.org/) +which git  not ok Install git and ensure it's in your PATH. +Perms check on cached files  ok   +Perms check on local node_modules  ok   +Perms check on global node_modules ok   +Perms check on local bin folder  ok   +Perms check on global bin folder  ok   +Verify cache contents  ok  verified 0 tarballs ` exports[`test/lib/commands/doctor.js TAP missing global directories > logs 1`] = ` @@ -716,18 +716,18 @@ Object { ` exports[`test/lib/commands/doctor.js TAP missing global directories > missing global directories 1`] = ` -Check Value Recommendation/Notes -npm ping ok -npm -v ok current: v1.0.0, latest: v1.0.0 -node -v ok current: v1.0.0, recommended: v1.0.0 -npm config get registry ok using default registry (https://registry.npmjs.org/) -which git ok /path/to/git -Perms check on cached files ok -Perms check on local node_modules ok -Perms check on global node_modules not ok Check the permissions of files in {CWD}/test/lib/commands/tap-testdir-doctor-missing-global-directories/global/lib/node_modules -Perms check on local bin folder ok -Perms check on global bin folder not ok Check the permissions of files in {CWD}/test/lib/commands/tap-testdir-doctor-missing-global-directories/global/bin -Verify cache contents ok verified 0 tarballs +Check  Value  Recommendation/Notes +npm ping  ok   +npm -v  ok  current: v1.0.0, latest: v1.0.0 +node -v  ok  current: v1.0.0, recommended: v1.0.0 +npm config get registry  ok  using default registry (https://registry.npmjs.org/) +which git  ok  /path/to/git +Perms check on cached files  ok   +Perms check on local node_modules  ok   +Perms check on global node_modules not ok Check the permissions of files in {CWD}/test/lib/commands/tap-testdir-doctor-missing-global-directories/global/lib/node_modules +Perms check on local bin folder  ok   +Perms check on global bin folder  not ok Check the permissions of files in {CWD}/test/lib/commands/tap-testdir-doctor-missing-global-directories/global/bin +Verify cache contents  ok  verified 0 tarballs ` exports[`test/lib/commands/doctor.js TAP missing local node_modules > logs 1`] = ` @@ -774,18 +774,18 @@ Object { ` exports[`test/lib/commands/doctor.js TAP missing local node_modules > missing local node_modules 1`] = ` -Check Value Recommendation/Notes -npm ping ok -npm -v ok current: v1.0.0, latest: v1.0.0 -node -v ok current: v1.0.0, recommended: v1.0.0 -npm config get registry ok using default registry (https://registry.npmjs.org/) -which git ok /path/to/git -Perms check on cached files ok -Perms check on local node_modules ok -Perms check on global node_modules ok -Perms check on local bin folder ok -Perms check on global bin folder ok -Verify cache contents ok verified 0 tarballs +Check  Value  Recommendation/Notes +npm ping  ok   +npm -v  ok  current: v1.0.0, latest: v1.0.0 +node -v  ok  current: v1.0.0, recommended: v1.0.0 +npm config get registry  ok  using default registry (https://registry.npmjs.org/) +which git  ok  /path/to/git +Perms check on cached files  ok   +Perms check on local node_modules  ok   +Perms check on global node_modules ok   +Perms check on local bin folder  ok   +Perms check on global bin folder  ok   +Verify cache contents  ok  verified 0 tarballs ` exports[`test/lib/commands/doctor.js TAP node out of date - current > logs 1`] = ` @@ -832,18 +832,18 @@ Object { ` exports[`test/lib/commands/doctor.js TAP node out of date - current > node is out of date 1`] = ` -Check Value Recommendation/Notes -npm ping ok -npm -v ok current: v1.0.0, latest: v1.0.0 -node -v not ok Use node v2.0.1 (current: v2.0.0) -npm config get registry ok using default registry (https://registry.npmjs.org/) -which git ok /path/to/git -Perms check on cached files ok -Perms check on local node_modules ok -Perms check on global node_modules ok -Perms check on local bin folder ok -Perms check on global bin folder ok -Verify cache contents ok verified 0 tarballs +Check  Value  Recommendation/Notes +npm ping  ok   +npm -v  ok  current: v1.0.0, latest: v1.0.0 +node -v  not ok Use node v2.0.1 (current: v2.0.0) +npm config get registry  ok  using default registry (https://registry.npmjs.org/) +which git  ok  /path/to/git +Perms check on cached files  ok   +Perms check on local node_modules  ok   +Perms check on global node_modules ok   +Perms check on local bin folder  ok   +Perms check on global bin folder  ok   +Verify cache contents  ok  verified 0 tarballs ` exports[`test/lib/commands/doctor.js TAP node out of date - lts > logs 1`] = ` @@ -890,18 +890,18 @@ Object { ` exports[`test/lib/commands/doctor.js TAP node out of date - lts > node is out of date 1`] = ` -Check Value Recommendation/Notes -npm ping ok -npm -v ok current: v1.0.0, latest: v1.0.0 -node -v not ok Use node v1.0.0 (current: v0.0.1) -npm config get registry ok using default registry (https://registry.npmjs.org/) -which git ok /path/to/git -Perms check on cached files ok -Perms check on local node_modules ok -Perms check on global node_modules ok -Perms check on local bin folder ok -Perms check on global bin folder ok -Verify cache contents ok verified 0 tarballs +Check  Value  Recommendation/Notes +npm ping  ok   +npm -v  ok  current: v1.0.0, latest: v1.0.0 +node -v  not ok Use node v1.0.0 (current: v0.0.1) +npm config get registry  ok  using default registry (https://registry.npmjs.org/) +which git  ok  /path/to/git +Perms check on cached files  ok   +Perms check on local node_modules  ok   +Perms check on global node_modules ok   +Perms check on local bin folder  ok   +Perms check on global bin folder  ok   +Verify cache contents  ok  verified 0 tarballs ` exports[`test/lib/commands/doctor.js TAP non-default registry > logs 1`] = ` @@ -948,18 +948,18 @@ Object { ` exports[`test/lib/commands/doctor.js TAP non-default registry > non default registry 1`] = ` -Check Value Recommendation/Notes -npm ping ok -npm -v ok current: v1.0.0, latest: v1.0.0 -node -v ok current: v1.0.0, recommended: v1.0.0 -npm config get registry not ok Try \`npm config set registry=https://registry.npmjs.org/\` -which git ok /path/to/git -Perms check on cached files ok -Perms check on local node_modules ok -Perms check on global node_modules ok -Perms check on local bin folder ok -Perms check on global bin folder ok -Verify cache contents ok verified 0 tarballs +Check  Value  Recommendation/Notes +npm ping  ok   +npm -v  ok  current: v1.0.0, latest: v1.0.0 +node -v  ok  current: v1.0.0, recommended: v1.0.0 +npm config get registry  not ok Try \`npm config set registry=https://registry.npmjs.org/\` +which git  ok  /path/to/git +Perms check on cached files  ok   +Perms check on local node_modules  ok   +Perms check on global node_modules ok   +Perms check on local bin folder  ok   +Perms check on global bin folder  ok   +Verify cache contents  ok  verified 0 tarballs ` exports[`test/lib/commands/doctor.js TAP npm out of date > logs 1`] = ` @@ -1006,18 +1006,18 @@ Object { ` exports[`test/lib/commands/doctor.js TAP npm out of date > npm is out of date 1`] = ` -Check Value Recommendation/Notes -npm ping ok -npm -v not ok Use npm v2.0.0 -node -v ok current: v1.0.0, recommended: v1.0.0 -npm config get registry ok using default registry (https://registry.npmjs.org/) -which git ok /path/to/git -Perms check on cached files ok -Perms check on local node_modules ok -Perms check on global node_modules ok -Perms check on local bin folder ok -Perms check on global bin folder ok -Verify cache contents ok verified 0 tarballs +Check  Value  Recommendation/Notes +npm ping  ok   +npm -v  not ok Use npm v2.0.0 +node -v  ok  current: v1.0.0, recommended: v1.0.0 +npm config get registry  ok  using default registry (https://registry.npmjs.org/) +which git  ok  /path/to/git +Perms check on cached files  ok   +Perms check on local node_modules  ok   +Perms check on global node_modules ok   +Perms check on local bin folder  ok   +Perms check on global bin folder  ok   +Verify cache contents  ok  verified 0 tarballs ` exports[`test/lib/commands/doctor.js TAP ping 404 > logs 1`] = ` @@ -1064,18 +1064,18 @@ Object { ` exports[`test/lib/commands/doctor.js TAP ping 404 > ping 404 1`] = ` -Check Value Recommendation/Notes -npm ping not ok 404 404 Not Found - GET https://registry.npmjs.org/-/ping?write=true -npm -v ok current: v1.0.0, latest: v1.0.0 -node -v ok current: v1.0.0, recommended: v1.0.0 -npm config get registry ok using default registry (https://registry.npmjs.org/) -which git ok /path/to/git -Perms check on cached files ok -Perms check on local node_modules ok -Perms check on global node_modules ok -Perms check on local bin folder ok -Perms check on global bin folder ok -Verify cache contents ok verified 0 tarballs +Check  Value  Recommendation/Notes +npm ping  not ok 404 404 Not Found - GET https://registry.npmjs.org/-/ping?write=true +npm -v  ok  current: v1.0.0, latest: v1.0.0 +node -v  ok  current: v1.0.0, recommended: v1.0.0 +npm config get registry  ok  using default registry (https://registry.npmjs.org/) +which git  ok  /path/to/git +Perms check on cached files  ok   +Perms check on local node_modules  ok   +Perms check on global node_modules ok   +Perms check on local bin folder  ok   +Perms check on global bin folder  ok   +Verify cache contents  ok  verified 0 tarballs ` exports[`test/lib/commands/doctor.js TAP ping 404 in color > logs 1`] = ` @@ -1122,18 +1122,18 @@ Object { ` exports[`test/lib/commands/doctor.js TAP ping 404 in color > ping 404 in color 1`] = ` -Check Value Recommendation/Notes -npm ping not ok 404 404 Not Found - GET https://registry.npmjs.org/-/ping?write=true -npm -v ok current: v1.0.0, latest: v1.0.0 -node -v ok current: v1.0.0, recommended: v1.0.0 -npm config get registry ok using default registry (https://registry.npmjs.org/) -which git ok /path/to/git -Perms check on cached files ok -Perms check on local node_modules ok -Perms check on global node_modules ok -Perms check on local bin folder ok -Perms check on global bin folder ok -Verify cache contents ok verified 0 tarballs +Check  Value  Recommendation/Notes +npm ping  not ok 404 404 Not Found - GET https://registry.npmjs.org/-/ping?write=true +npm -v  ok  current: v1.0.0, latest: v1.0.0 +node -v  ok  current: v1.0.0, recommended: v1.0.0 +npm config get registry  ok  using default registry (https://registry.npmjs.org/) +which git  ok  /path/to/git +Perms check on cached files  ok   +Perms check on local node_modules  ok   +Perms check on global node_modules ok   +Perms check on local bin folder  ok   +Perms check on global bin folder  ok   +Verify cache contents  ok  verified 0 tarballs ` exports[`test/lib/commands/doctor.js TAP ping exception with code > logs 1`] = ` @@ -1180,18 +1180,18 @@ Object { ` exports[`test/lib/commands/doctor.js TAP ping exception with code > ping failure 1`] = ` -Check Value Recommendation/Notes -npm ping not ok request to https://registry.npmjs.org/-/ping?write=true failed, reason: Test Error -npm -v ok current: v1.0.0, latest: v1.0.0 -node -v ok current: v1.0.0, recommended: v1.0.0 -npm config get registry ok using default registry (https://registry.npmjs.org/) -which git ok /path/to/git -Perms check on cached files ok -Perms check on local node_modules ok -Perms check on global node_modules ok -Perms check on local bin folder ok -Perms check on global bin folder ok -Verify cache contents ok verified 0 tarballs +Check  Value  Recommendation/Notes +npm ping  not ok request to https://registry.npmjs.org/-/ping?write=true failed, reason: Test Error +npm -v  ok  current: v1.0.0, latest: v1.0.0 +node -v  ok  current: v1.0.0, recommended: v1.0.0 +npm config get registry  ok  using default registry (https://registry.npmjs.org/) +which git  ok  /path/to/git +Perms check on cached files  ok   +Perms check on local node_modules  ok   +Perms check on global node_modules ok   +Perms check on local bin folder  ok   +Perms check on global bin folder  ok   +Verify cache contents  ok  verified 0 tarballs ` exports[`test/lib/commands/doctor.js TAP ping exception without code > logs 1`] = ` @@ -1238,18 +1238,18 @@ Object { ` exports[`test/lib/commands/doctor.js TAP ping exception without code > ping failure 1`] = ` -Check Value Recommendation/Notes -npm ping not ok request to https://registry.npmjs.org/-/ping?write=true failed, reason: Test Error -npm -v ok current: v1.0.0, latest: v1.0.0 -node -v ok current: v1.0.0, recommended: v1.0.0 -npm config get registry ok using default registry (https://registry.npmjs.org/) -which git ok /path/to/git -Perms check on cached files ok -Perms check on local node_modules ok -Perms check on global node_modules ok -Perms check on local bin folder ok -Perms check on global bin folder ok -Verify cache contents ok verified 0 tarballs +Check  Value  Recommendation/Notes +npm ping  not ok request to https://registry.npmjs.org/-/ping?write=true failed, reason: Test Error +npm -v  ok  current: v1.0.0, latest: v1.0.0 +node -v  ok  current: v1.0.0, recommended: v1.0.0 +npm config get registry  ok  using default registry (https://registry.npmjs.org/) +which git  ok  /path/to/git +Perms check on cached files  ok   +Perms check on local node_modules  ok   +Perms check on global node_modules ok   +Perms check on local bin folder  ok   +Perms check on global bin folder  ok   +Verify cache contents  ok  verified 0 tarballs ` exports[`test/lib/commands/doctor.js TAP silent > logs 1`] = ` @@ -1343,11 +1343,11 @@ Object { ` exports[`test/lib/commands/doctor.js TAP windows skips permissions checks > no permissions checks 1`] = ` -Check Value Recommendation/Notes -npm ping ok -npm -v ok current: v1.0.0, latest: v1.0.0 -node -v ok current: v1.0.0, recommended: v1.0.0 -npm config get registry ok using default registry (https://registry.npmjs.org/) -which git ok /path/to/git -Verify cache contents ok verified 0 tarballs +Check  Value  Recommendation/Notes +npm ping  ok   +npm -v  ok  current: v1.0.0, latest: v1.0.0 +node -v  ok  current: v1.0.0, recommended: v1.0.0 +npm config get registry ok  using default registry (https://registry.npmjs.org/) +which git  ok  /path/to/git +Verify cache contents  ok  verified 0 tarballs ` From 427ddc9378b456672d29034eea1eb950a956c605 Mon Sep 17 00:00:00 2001 From: Gar Date: Tue, 22 Nov 2022 10:09:07 -0800 Subject: [PATCH 2/4] feat: allow for discrete npm doctor checks --- lib/commands/doctor.js | 142 ++++++++++------ .../test/lib/commands/doctor.js.test.cjs | 158 ++++++++++++++++++ test/lib/commands/doctor.js | 94 +++++++++++ 3 files changed, 340 insertions(+), 54 deletions(-) diff --git a/lib/commands/doctor.js b/lib/commands/doctor.js index 1c722713bff8f..c2b2127611791 100644 --- a/lib/commands/doctor.js +++ b/lib/commands/doctor.js @@ -8,7 +8,6 @@ const { resolve } = require('path') const semver = require('semver') const { promisify } = require('util') const log = require('../utils/log-shim.js') -const ansiTrim = require('../utils/ansi-trim.js') const ping = require('../utils/ping.js') const { registry: { default: defaultRegistry }, @@ -18,9 +17,6 @@ const readdir = promisify(fs.readdir) const access = promisify(fs.access) const { R_OK, W_OK, X_OK } = fs.constants -const table = (colWidths, rows) => { -} - const maskLabel = mask => { const label = [] if (mask & R_OK) { @@ -47,52 +43,11 @@ class Doctor extends BaseCommand { async exec (args) { log.info('Running checkup') + let allOk = true - const actions = [ - ['npm ping', 'checkPing', []], - ['npm -v', 'getLatestNpmVersion', []], - ['node -v', 'getLatestNodejsVersion', []], - ['npm config get registry', 'checkNpmRegistry', []], - ['which git', 'getGitPath', []], - ...(process.platform === 'win32' - ? [] - : [ - [ - 'Perms check on cached files', - 'checkFilesPermission', - [this.npm.cache, true, R_OK], - ], [ - 'Perms check on local node_modules', - 'checkFilesPermission', - [this.npm.localDir, true, R_OK | W_OK, true], - ], [ - 'Perms check on global node_modules', - 'checkFilesPermission', - [this.npm.globalDir, false, R_OK], - ], [ - 'Perms check on local bin folder', - 'checkFilesPermission', - [this.npm.localBin, false, R_OK | W_OK | X_OK, true], - ], [ - 'Perms check on global bin folder', - 'checkFilesPermission', - [this.npm.globalBin, false, X_OK], - ], - ]), - [ - 'Verify cache contents', - 'verifyCachedFiles', - [this.npm.flatOptions.cache], - ], - // TODO: - // - ensure arborist.loadActual() runs without errors and no invalid edges - // - ensure package-lock.json matches loadActual() - // - verify loadActual without hidden lock file matches hidden lockfile - // - verify all local packages have bins linked - ] + const actions = this.actions(args) + this.checkWidth = actions.reduce((length, item) => Math.max(item[0].length, length), 5) - let allOk = true - this.checkWidth = actions.reduce((length, item) => Math.max(item[0].length, length), 0) if (!this.npm.silent) { this.output(['Check', 'Value', 'Recommendation/Notes'].map(h => this.npm.chalk.underline(h))) } @@ -117,7 +72,6 @@ class Doctor extends BaseCommand { } } - if (!this.npm.silent) { // this.npm.output(table(outTable, tableOpts)) } @@ -314,16 +268,96 @@ class Doctor extends BaseCommand { output (row) { const t = new Table({ - chars: { 'top': '' , 'top-mid': '' , 'top-left': '' , 'top-right': '' - , 'bottom': '' , 'bottom-mid': '' , 'bottom-left': '' , 'bottom-right': '' - , 'left': '' , 'left-mid': '' , 'mid': '' , 'mid-mid': '' - , 'right': '' , 'right-mid': '' , 'middle': ' ' }, + chars: { top: '', + 'top-mid': '', + 'top-left': '', + 'top-right': '', + bottom: '', + 'bottom-mid': '', + 'bottom-left': '', + 'bottom-right': '', + left: '', + 'left-mid': '', + mid: '', + 'mid-mid': '', + right: '', + 'right-mid': '', + middle: ' ' }, style: { 'padding-left': 0, 'padding-right': 0 }, colWidths: [this.checkWidth, 6], - }); + }) t.push(row) this.npm.output(t.toString()) } + + actions (subcmds) { + const a = [] + if (!subcmds.length || subcmds.includes('ping')) { + a.push(['npm ping', 'checkPing', []]) + } + if (!subcmds.length || subcmds.includes('versions')) { + a.push(['npm -v', 'getLatestNpmVersion', []]) + a.push(['node -v', 'getLatestNodejsVersion', []]) + } + if (!subcmds.length || subcmds.includes('registry')) { + a.push(['npm config get registry', 'checkNpmRegistry', []]) + } + if (!subcmds.length || subcmds.includes('git')) { + a.push(['which git', 'getGitPath', []]) + } + if (!subcmds.length || subcmds.includes('permissions')) { + if (subcmds.includes('permissions') && process.platform === 'win32') { + log.warn('Ignoring permissions checks for windows') + } else if (process.platform !== 'win32') { + a.push([ + 'Perms check on cached files', + 'checkFilesPermission', + [this.npm.cache, true, R_OK], + ]) + a.push([ + 'Perms check on local node_modules', + 'checkFilesPermission', + [this.npm.localDir, true, R_OK | W_OK, true], + ]) + a.push([ + 'Perms check on global node_modules', + 'checkFilesPermission', + [this.npm.globalDir, false, R_OK], + ]) + a.push([ + 'Perms check on local bin folder', + 'checkFilesPermission', + [this.npm.localBin, false, R_OK | W_OK | X_OK, true], + ]) + a.push([ + 'Perms check on global bin folder', + 'checkFilesPermission', + [this.npm.globalBin, false, X_OK], + ]) + } + } + if (!subcmds.length || subcmds.includes('cache')) { + a.push([ + 'Verify cache contents', + 'verifyCachedFiles', + [this.npm.flatOptions.cache], + ]) + } + + // TODO + // subcmd === 'environment' + // - ensure global bin is in process.env.PATH + + // TODO: + // subcmd === 'dependencies'? + // - ensure arborist.loadActual() runs without errors and no invalid edges + // - ensure package-lock.json matches loadActual() + // - verify loadActual without hidden lock file matches hidden lockfile + // subcmd === '???' + // - verify all local packages have bins linked + // What is the fix for these? + return a + } } module.exports = Doctor diff --git a/tap-snapshots/test/lib/commands/doctor.js.test.cjs b/tap-snapshots/test/lib/commands/doctor.js.test.cjs index cccf13807c83b..1b4ae20b6712e 100644 --- a/tap-snapshots/test/lib/commands/doctor.js.test.cjs +++ b/tap-snapshots/test/lib/commands/doctor.js.test.cjs @@ -380,6 +380,164 @@ Object { } ` +exports[`test/lib/commands/doctor.js TAP discrete checks cache > logs 1`] = ` +Object { + "error": Array [], + "info": Array [ + Array [ + "Running checkup", + ], + Array [ + "verifyCachedFiles", + "Verifying the npm cache", + ], + Array [ + "verifyCachedFiles", + String( + Verification complete. Stats: { + "badContentCount": 0, + "reclaimedCount": 0, + "missingContent": 0, + "verifiedContent": 0 + } + ), + ], + ], + "warn": Array [], +} +` + +exports[`test/lib/commands/doctor.js TAP discrete checks cache > output 1`] = ` +Check  Value  Recommendation/Notes +Verify cache contents ok  verified 0 tarballs +` + +exports[`test/lib/commands/doctor.js TAP discrete checks git > logs 1`] = ` +Object { + "error": Array [], + "info": Array [ + Array [ + "Running checkup", + ], + Array [ + "getGitPath", + "Finding git in your PATH", + ], + ], + "warn": Array [], +} +` + +exports[`test/lib/commands/doctor.js TAP discrete checks git > output 1`] = ` +Check  Value  Recommendation/Notes +which git ok  /path/to/git +` + +exports[`test/lib/commands/doctor.js TAP discrete checks permissions - not windows > logs 1`] = ` +Object { + "error": Array [], + "info": Array [ + Array [ + "Running checkup", + ], + ], + "warn": Array [], +} +` + +exports[`test/lib/commands/doctor.js TAP discrete checks permissions - not windows > output 1`] = ` +Check  Value  Recommendation/Notes +Perms check on cached files  ok   +Perms check on local node_modules  ok   +Perms check on global node_modules ok   +Perms check on local bin folder  ok   +Perms check on global bin folder  ok   +` + +exports[`test/lib/commands/doctor.js TAP discrete checks permissions - windows > logs 1`] = ` +Object { + "error": Array [], + "info": Array [ + Array [ + "Running checkup", + ], + ], + "warn": Array [ + Array [ + "Ignoring permissions checks for windows", + ], + ], +} +` + +exports[`test/lib/commands/doctor.js TAP discrete checks permissions - windows > output 1`] = ` +Check Value  Recommendation/Notes +` + +exports[`test/lib/commands/doctor.js TAP discrete checks ping > logs 1`] = ` +Object { + "error": Array [], + "info": Array [ + Array [ + "Running checkup", + ], + Array [ + "checkPing", + "Pinging registry", + ], + ], + "warn": Array [], +} +` + +exports[`test/lib/commands/doctor.js TAP discrete checks ping > output 1`] = ` +Check  Value  Recommendation/Notes +npm ping ok   +` + +exports[`test/lib/commands/doctor.js TAP discrete checks registry > logs 1`] = ` +Object { + "error": Array [], + "info": Array [ + Array [ + "Running checkup", + ], + ], + "warn": Array [], +} +` + +exports[`test/lib/commands/doctor.js TAP discrete checks registry > output 1`] = ` +Check  Value  Recommendation/Notes +npm config get registry ok  using default registry (https://registry.npmjs.org/) +` + +exports[`test/lib/commands/doctor.js TAP discrete checks versions > logs 1`] = ` +Object { + "error": Array [], + "info": Array [ + Array [ + "Running checkup", + ], + Array [ + "getLatestNpmVersion", + "Getting npm package information", + ], + Array [ + "getLatestNodejsVersion", + "Getting Node.js release information", + ], + ], + "warn": Array [], +} +` + +exports[`test/lib/commands/doctor.js TAP discrete checks versions > output 1`] = ` +Check  Value  Recommendation/Notes +npm -v  ok  current: v1.0.0, latest: v1.0.0 +node -v ok  current: v1.0.0, recommended: v1.0.0 +` + exports[`test/lib/commands/doctor.js TAP error reading directory > logs 1`] = ` Object { "error": Array [], diff --git a/test/lib/commands/doctor.js b/test/lib/commands/doctor.js index eaf2ee6c6f8a2..a233531d0f858 100644 --- a/test/lib/commands/doctor.js +++ b/test/lib/commands/doctor.js @@ -510,3 +510,97 @@ t.test('bad proxy', async t => { t.matchSnapshot(joinedOutput(), 'output') t.matchSnapshot({ info: logs.info, warn: logs.warn, error: logs.error }, 'logs') }) + +t.test('discrete checks', t => { + t.test('ping', async t => { + const { joinedOutput, logs, npm } = await loadMockNpm(t, { + mocks, + globals, + ...dirs, + }) + tnock(t, npm.config.get('registry')) + .get('/-/ping?write=true').reply(200, '{}') + await npm.exec('doctor', ['ping']) + t.matchSnapshot(joinedOutput(), 'output') + t.matchSnapshot({ info: logs.info, warn: logs.warn, error: logs.error }, 'logs') + }) + + t.test('versions', async t => { + const { joinedOutput, logs, npm } = await loadMockNpm(t, { + mocks, + globals, + ...dirs, + }) + tnock(t, npm.config.get('registry')) + .get('/npm').reply(200, npmManifest(npm.version)) + tnock(t, 'https://nodejs.org') + .get('/dist/index.json').reply(200, nodeVersions) + await npm.exec('doctor', ['versions']) + t.matchSnapshot(joinedOutput(), 'output') + t.matchSnapshot({ info: logs.info, warn: logs.warn, error: logs.error }, 'logs') + }) + + t.test('registry', async t => { + const { joinedOutput, logs, npm } = await loadMockNpm(t, { + mocks, + globals, + ...dirs, + }) + await npm.exec('doctor', ['registry']) + t.matchSnapshot(joinedOutput(), 'output') + t.matchSnapshot({ info: logs.info, warn: logs.warn, error: logs.error }, 'logs') + }) + + t.test('git', async t => { + const { joinedOutput, logs, npm } = await loadMockNpm(t, { + mocks, + globals, + ...dirs, + }) + await npm.exec('doctor', ['git']) + t.matchSnapshot(joinedOutput(), 'output') + t.matchSnapshot({ info: logs.info, warn: logs.warn, error: logs.error }, 'logs') + }) + + t.test('permissions - not windows', async t => { + const { joinedOutput, logs, npm } = await loadMockNpm(t, { + mocks, + globals, + ...dirs, + }) + await npm.exec('doctor', ['permissions']) + t.matchSnapshot(joinedOutput(), 'output') + t.matchSnapshot({ info: logs.info, warn: logs.warn, error: logs.error }, 'logs') + }) + + t.test('cache', async t => { + const { joinedOutput, logs, npm } = await loadMockNpm(t, { + mocks, + globals, + ...dirs, + }) + await npm.exec('doctor', ['cache']) + t.matchSnapshot(joinedOutput(), 'output') + t.matchSnapshot({ info: logs.info, warn: logs.warn, error: logs.error }, 'logs') + }) + + t.test('permissions - windows', async t => { + const { joinedOutput, logs, npm } = await loadMockNpm(t, { + mocks, + globals: { + ...globals, + process: { + ...globals.process, + platform: 'win32', + }, + }, + prefixDir: {}, + globalPrefixDir: {}, + }) + await npm.exec('doctor', ['permissions']) + t.matchSnapshot(joinedOutput(), 'output') + t.matchSnapshot({ info: logs.info, warn: logs.warn, error: logs.error }, 'logs') + }) + + t.end() +}) From 95afc187f9aa8cb99fc8d0ac0cd50f90b86ea56a Mon Sep 17 00:00:00 2001 From: Gar Date: Tue, 22 Nov 2022 12:49:30 -0800 Subject: [PATCH 3/4] feat: add environment check to npm doctor currently just checks if global bin is in $PATH --- lib/commands/doctor.js | 17 +- .../test/lib/commands/doctor.js.test.cjs | 146 +++++++++++++++++- test/lib/commands/doctor.js | 96 ++++++++---- 3 files changed, 218 insertions(+), 41 deletions(-) diff --git a/lib/commands/doctor.js b/lib/commands/doctor.js index c2b2127611791..5dc3aa0db55a8 100644 --- a/lib/commands/doctor.js +++ b/lib/commands/doctor.js @@ -144,6 +144,15 @@ class Doctor extends BaseCommand { } } + async checkBinPath (dir) { + const tracker = log.newItem('checkBinPath', 1) + tracker.info('checkBinPath', 'Finding npm global bin in your PATH') + if (!process.env.PATH.includes(this.npm.globalBin)) { + throw new Error(`Add ${this.npm.globalBin} to your $PATH`) + } + return this.npm.globalBin + } + async checkFilesPermission (root, shouldOwn, mask, missingOk) { let ok = true @@ -217,7 +226,7 @@ class Doctor extends BaseCommand { try { return await which('git').catch(er => { tracker.warn(er) - throw "Install git and ensure it's in your PATH." + throw new Error("Install git and ensure it's in your PATH.") }) } finally { tracker.finish() @@ -344,9 +353,9 @@ class Doctor extends BaseCommand { ]) } - // TODO - // subcmd === 'environment' - // - ensure global bin is in process.env.PATH + if (!subcmds.length || subcmds.includes('environment')) { + a.push(['Global bin folder in PATH', 'checkBinPath', []]) + } // TODO: // subcmd === 'dependencies'? diff --git a/tap-snapshots/test/lib/commands/doctor.js.test.cjs b/tap-snapshots/test/lib/commands/doctor.js.test.cjs index 1b4ae20b6712e..52a90f6843be2 100644 --- a/tap-snapshots/test/lib/commands/doctor.js.test.cjs +++ b/tap-snapshots/test/lib/commands/doctor.js.test.cjs @@ -43,6 +43,10 @@ Object { } ), ], + Array [ + "checkBinPath", + "Finding npm global bin in your PATH", + ], ], "warn": Array [], } @@ -61,6 +65,7 @@ Perms check on global node_modules ok   Perms check on local bin folder  ok   Perms check on global bin folder  ok   Verify cache contents  ok  verified 0 tarballs +Global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-all-clear/global/bin ` exports[`test/lib/commands/doctor.js TAP all clear in color > everything is ok in color 1`] = ` @@ -76,6 +81,7 @@ Perms check on global node_modules ok   Perms check on local bin folder  ok   Perms check on global bin folder  ok   Verify cache contents  ok  verified 0 tarballs +Global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-all-clear-in-color/global/bin ` exports[`test/lib/commands/doctor.js TAP all clear in color > logs 1`] = ` @@ -116,6 +122,10 @@ Object { } ), ], + Array [ + "checkBinPath", + "Finding npm global bin in your PATH", + ], ], "warn": Array [], } @@ -159,6 +169,10 @@ Object { } ), ], + Array [ + "checkBinPath", + "Finding npm global bin in your PATH", + ], ], "warn": Array [], } @@ -177,6 +191,7 @@ Perms check on global node_modules ok   Perms check on local bin folder  ok   Perms check on global bin folder  ok   Verify cache contents  ok  verified 0 tarballs +Global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-bad-proxy/global/bin ` exports[`test/lib/commands/doctor.js TAP cacache badContent > corrupted cache content 1`] = ` @@ -192,6 +207,7 @@ Perms check on global node_modules ok   Perms check on local bin folder  ok   Perms check on global bin folder  ok   Verify cache contents  ok  verified 2 tarballs +Global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-cacache-badContent/global/bin ` exports[`test/lib/commands/doctor.js TAP cacache badContent > logs 1`] = ` @@ -232,6 +248,10 @@ Object { } ), ], + Array [ + "checkBinPath", + "Finding npm global bin in your PATH", + ], ], "warn": Array [ Array [ @@ -284,6 +304,10 @@ Object { } ), ], + Array [ + "checkBinPath", + "Finding npm global bin in your PATH", + ], ], "warn": Array [ Array [ @@ -311,6 +335,7 @@ Perms check on global node_modules ok   Perms check on local bin folder  ok   Perms check on global bin folder  ok   Verify cache contents  ok  verified 2 tarballs +Global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-cacache-missingContent/global/bin ` exports[`test/lib/commands/doctor.js TAP cacache reclaimedCount > content garbage collected 1`] = ` @@ -326,6 +351,7 @@ Perms check on global node_modules ok   Perms check on local bin folder  ok   Perms check on global bin folder  ok   Verify cache contents  ok  verified 2 tarballs +Global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-cacache-reclaimedCount/global/bin ` exports[`test/lib/commands/doctor.js TAP cacache reclaimedCount > logs 1`] = ` @@ -366,6 +392,10 @@ Object { } ), ], + Array [ + "checkBinPath", + "Finding npm global bin in your PATH", + ], ], "warn": Array [ Array [ @@ -433,6 +463,27 @@ Check  Value  Recommendation/Notes which git ok  /path/to/git ` +exports[`test/lib/commands/doctor.js TAP discrete checks invalid environment > logs 1`] = ` +Object { + "error": Array [], + "info": Array [ + Array [ + "Running checkup", + ], + Array [ + "checkBinPath", + "Finding npm global bin in your PATH", + ], + ], + "warn": Array [], +} +` + +exports[`test/lib/commands/doctor.js TAP discrete checks invalid environment > output 1`] = ` +Check  Value  Recommendation/Notes +Global bin folder in PATH not ok Error: Add {CWD}/test/lib/commands/tap-testdir-doctor-discrete-checks-invalid-environment/global/bin to your $PATH +` + exports[`test/lib/commands/doctor.js TAP discrete checks permissions - not windows > logs 1`] = ` Object { "error": Array [], @@ -576,6 +627,10 @@ Object { } ), ], + Array [ + "checkBinPath", + "Finding npm global bin in your PATH", + ], ], "warn": Array [ Array [ @@ -615,6 +670,7 @@ Perms check on global node_modules not ok Check the permis Perms check on local bin folder  not ok Check the permissions of files in {CWD}/test/lib/commands/tap-testdir-doctor-error-reading-directory/prefix/node_modules/.bin Perms check on global bin folder  not ok Check the permissions of files in {CWD}/test/lib/commands/tap-testdir-doctor-error-reading-directory/global/bin Verify cache contents  ok  verified 0 tarballs +Global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-error-reading-directory/global/bin ` exports[`test/lib/commands/doctor.js TAP incorrect owner > incorrect owner 1`] = ` @@ -630,6 +686,7 @@ Perms check on global node_modules ok   Perms check on local bin folder  ok   Perms check on global bin folder  ok   Verify cache contents  ok  verified 0 tarballs +Global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-incorrect-owner/global/bin ` exports[`test/lib/commands/doctor.js TAP incorrect owner > logs 1`] = ` @@ -670,6 +727,10 @@ Object { } ), ], + Array [ + "checkBinPath", + "Finding npm global bin in your PATH", + ], ], "warn": Array [ Array [ @@ -693,6 +754,7 @@ Perms check on global node_modules not ok Check the permis Perms check on local bin folder  not ok Check the permissions of files in {CWD}/test/lib/commands/tap-testdir-doctor-incorrect-permissions/prefix/node_modules/.bin Perms check on global bin folder  not ok Check the permissions of files in {CWD}/test/lib/commands/tap-testdir-doctor-incorrect-permissions/global/bin Verify cache contents  ok  verified 0 tarballs +Global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-incorrect-permissions/global/bin ` exports[`test/lib/commands/doctor.js TAP incorrect permissions > logs 1`] = ` @@ -754,6 +816,10 @@ Object { } ), ], + Array [ + "checkBinPath", + "Finding npm global bin in your PATH", + ], ], "warn": Array [], } @@ -797,6 +863,10 @@ Object { } ), ], + Array [ + "checkBinPath", + "Finding npm global bin in your PATH", + ], ], "warn": Array [ Array [ @@ -812,13 +882,14 @@ npm ping  ok   npm -v  ok  current: v1.0.0, latest: v1.0.0 node -v  ok  current: v1.0.0, recommended: v1.0.0 npm config get registry  ok  using default registry (https://registry.npmjs.org/) -which git  not ok Install git and ensure it's in your PATH. +which git  not ok Error: Install git and ensure it's in your PATH. Perms check on cached files  ok   Perms check on local node_modules  ok   Perms check on global node_modules ok   Perms check on local bin folder  ok   Perms check on global bin folder  ok   Verify cache contents  ok  verified 0 tarballs +Global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-missing-git/global/bin ` exports[`test/lib/commands/doctor.js TAP missing global directories > logs 1`] = ` @@ -859,6 +930,10 @@ Object { } ), ], + Array [ + "checkBinPath", + "Finding npm global bin in your PATH", + ], ], "warn": Array [ Array [ @@ -886,6 +961,7 @@ Perms check on global node_modules not ok Check the permis Perms check on local bin folder  ok   Perms check on global bin folder  not ok Check the permissions of files in {CWD}/test/lib/commands/tap-testdir-doctor-missing-global-directories/global/bin Verify cache contents  ok  verified 0 tarballs +Global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-missing-global-directories/global/bin ` exports[`test/lib/commands/doctor.js TAP missing local node_modules > logs 1`] = ` @@ -926,6 +1002,10 @@ Object { } ), ], + Array [ + "checkBinPath", + "Finding npm global bin in your PATH", + ], ], "warn": Array [], } @@ -944,6 +1024,7 @@ Perms check on global node_modules ok   Perms check on local bin folder  ok   Perms check on global bin folder  ok   Verify cache contents  ok  verified 0 tarballs +Global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-missing-local-node_modules/global/bin ` exports[`test/lib/commands/doctor.js TAP node out of date - current > logs 1`] = ` @@ -984,6 +1065,10 @@ Object { } ), ], + Array [ + "checkBinPath", + "Finding npm global bin in your PATH", + ], ], "warn": Array [], } @@ -1002,6 +1087,7 @@ Perms check on global node_modules ok   Perms check on local bin folder  ok   Perms check on global bin folder  ok   Verify cache contents  ok  verified 0 tarballs +Global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-node-out-of-date---current/global/bin ` exports[`test/lib/commands/doctor.js TAP node out of date - lts > logs 1`] = ` @@ -1042,6 +1128,10 @@ Object { } ), ], + Array [ + "checkBinPath", + "Finding npm global bin in your PATH", + ], ], "warn": Array [], } @@ -1060,6 +1150,7 @@ Perms check on global node_modules ok   Perms check on local bin folder  ok   Perms check on global bin folder  ok   Verify cache contents  ok  verified 0 tarballs +Global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-node-out-of-date---lts/global/bin ` exports[`test/lib/commands/doctor.js TAP non-default registry > logs 1`] = ` @@ -1100,6 +1191,10 @@ Object { } ), ], + Array [ + "checkBinPath", + "Finding npm global bin in your PATH", + ], ], "warn": Array [], } @@ -1118,6 +1213,7 @@ Perms check on global node_modules ok   Perms check on local bin folder  ok   Perms check on global bin folder  ok   Verify cache contents  ok  verified 0 tarballs +Global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-non-default-registry/global/bin ` exports[`test/lib/commands/doctor.js TAP npm out of date > logs 1`] = ` @@ -1158,6 +1254,10 @@ Object { } ), ], + Array [ + "checkBinPath", + "Finding npm global bin in your PATH", + ], ], "warn": Array [], } @@ -1176,6 +1276,7 @@ Perms check on global node_modules ok   Perms check on local bin folder  ok   Perms check on global bin folder  ok   Verify cache contents  ok  verified 0 tarballs +Global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-npm-out-of-date/global/bin ` exports[`test/lib/commands/doctor.js TAP ping 404 > logs 1`] = ` @@ -1216,6 +1317,10 @@ Object { } ), ], + Array [ + "checkBinPath", + "Finding npm global bin in your PATH", + ], ], "warn": Array [], } @@ -1234,6 +1339,7 @@ Perms check on global node_modules ok   Perms check on local bin folder  ok   Perms check on global bin folder  ok   Verify cache contents  ok  verified 0 tarballs +Global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-ping-404/global/bin ` exports[`test/lib/commands/doctor.js TAP ping 404 in color > logs 1`] = ` @@ -1274,6 +1380,10 @@ Object { } ), ], + Array [ + "checkBinPath", + "Finding npm global bin in your PATH", + ], ], "warn": Array [], } @@ -1292,6 +1402,7 @@ Perms check on global node_modules ok   Perms check on local bin folder  ok   Perms check on global bin folder  ok   Verify cache contents  ok  verified 0 tarballs +Global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-ping-404-in-color/global/bin ` exports[`test/lib/commands/doctor.js TAP ping exception with code > logs 1`] = ` @@ -1332,6 +1443,10 @@ Object { } ), ], + Array [ + "checkBinPath", + "Finding npm global bin in your PATH", + ], ], "warn": Array [], } @@ -1350,6 +1465,7 @@ Perms check on global node_modules ok   Perms check on local bin folder  ok   Perms check on global bin folder  ok   Verify cache contents  ok  verified 0 tarballs +Global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-ping-exception-with-code/global/bin ` exports[`test/lib/commands/doctor.js TAP ping exception without code > logs 1`] = ` @@ -1390,6 +1506,10 @@ Object { } ), ], + Array [ + "checkBinPath", + "Finding npm global bin in your PATH", + ], ], "warn": Array [], } @@ -1408,6 +1528,7 @@ Perms check on global node_modules ok   Perms check on local bin folder  ok   Perms check on global bin folder  ok   Verify cache contents  ok  verified 0 tarballs +Global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-ping-exception-without-code/global/bin ` exports[`test/lib/commands/doctor.js TAP silent > logs 1`] = ` @@ -1448,6 +1569,10 @@ Object { } ), ], + Array [ + "checkBinPath", + "Finding npm global bin in your PATH", + ], ], "warn": Array [], } @@ -1495,17 +1620,22 @@ Object { } ), ], + Array [ + "checkBinPath", + "Finding npm global bin in your PATH", + ], ], "warn": Array [], } ` exports[`test/lib/commands/doctor.js TAP windows skips permissions checks > no permissions checks 1`] = ` -Check  Value  Recommendation/Notes -npm ping  ok   -npm -v  ok  current: v1.0.0, latest: v1.0.0 -node -v  ok  current: v1.0.0, recommended: v1.0.0 -npm config get registry ok  using default registry (https://registry.npmjs.org/) -which git  ok  /path/to/git -Verify cache contents  ok  verified 0 tarballs +Check  Value  Recommendation/Notes +npm ping  ok   +npm -v  ok  current: v1.0.0, latest: v1.0.0 +node -v  ok  current: v1.0.0, recommended: v1.0.0 +npm config get registry  ok  using default registry (https://registry.npmjs.org/) +which git  ok  /path/to/git +Verify cache contents  ok  verified 0 tarballs +Global bin folder in PATH ok  {CWD}/test/lib/commands/tap-testdir-doctor-windows-skips-permissions-checks/global ` diff --git a/test/lib/commands/doctor.js b/test/lib/commands/doctor.js index a233531d0f858..7fb78cf2b5c6e 100644 --- a/test/lib/commands/doctor.js +++ b/test/lib/commands/doctor.js @@ -1,5 +1,6 @@ const t = require('tap') const fs = require('fs') +const path = require('path') const { load: loadMockNpm } = require('../../fixtures/mock-npm') const tnock = require('../../fixtures/tnock.js') @@ -52,11 +53,14 @@ const dirs = { }, } -const globals = { - process: { - platform: 'test-not-windows', - version: 'v1.0.0', - }, +const globals = ({ globalPrefix }) => { + return { + process: { + 'env.PATH': `${globalPrefix}:${path.join(globalPrefix, 'bin')}`, + platform: 'test-not-windows', + version: 'v1.0.0', + }, + } } // getuid and getgid do not exist in windows, so we shim them @@ -217,12 +221,16 @@ t.test('npm out of date', async t => { t.test('node out of date - lts', async t => { const { joinedOutput, logs, npm } = await loadMockNpm(t, { mocks, - globals: { - ...globals, - process: { - platform: 'test-not-windows', - version: 'v0.0.1', - }, + globals: (context) => { + const g = globals(context) + return { + ...g, + process: { + ...g.process, + platform: 'test-not-windows', + version: 'v0.0.1', + }, + } }, ...dirs, }) @@ -239,12 +247,15 @@ t.test('node out of date - lts', async t => { t.test('node out of date - current', async t => { const { joinedOutput, logs, npm } = await loadMockNpm(t, { mocks, - globals: { - ...globals, - process: { - ...globals.process, - version: 'v2.0.0', - }, + globals: (context) => { + const g = globals(context) + return { + ...g, + process: { + ...g.process, + version: 'v2.0.0', + }, + } }, ...dirs, }) @@ -299,12 +310,15 @@ t.test('missing git', async t => { t.test('windows skips permissions checks', async t => { const { joinedOutput, logs, npm } = await loadMockNpm(t, { mocks, - globals: { - ...globals, - process: { - ...globals.process, - platform: 'win32', - }, + globals: (context) => { + const g = globals(context) + return { + ...g, + process: { + ...g.process, + platform: 'win32', + }, + } }, prefixDir: {}, globalPrefixDir: {}, @@ -587,12 +601,15 @@ t.test('discrete checks', t => { t.test('permissions - windows', async t => { const { joinedOutput, logs, npm } = await loadMockNpm(t, { mocks, - globals: { - ...globals, - process: { - ...globals.process, - platform: 'win32', - }, + globals: (context) => { + const g = globals(context) + return { + ...g, + process: { + ...g.process, + platform: 'win32', + }, + } }, prefixDir: {}, globalPrefixDir: {}, @@ -602,5 +619,26 @@ t.test('discrete checks', t => { t.matchSnapshot({ info: logs.info, warn: logs.warn, error: logs.error }, 'logs') }) + t.test('invalid environment', async t => { + const { joinedOutput, logs, npm } = await loadMockNpm(t, { + mocks, + globals: (context) => { + const g = globals(context) + return { + ...g, + process: { + ...g.process, + 'env.PATH': '/nope', + }, + } + }, + prefixDir: {}, + globalPrefixDir: {}, + }) + await t.rejects(npm.exec('doctor', ['environment'])) + t.matchSnapshot(joinedOutput(), 'output') + t.matchSnapshot({ info: logs.info, warn: logs.warn, error: logs.error }, 'logs') + }) + t.end() }) From bf7fb3b31721a61dba4a58745131c2d58421fb78 Mon Sep 17 00:00:00 2001 From: Gar Date: Tue, 6 Dec 2022 10:31:24 -0800 Subject: [PATCH 4/4] chore: refactor command Usage is driven by actual code, commands are tagged for grouping --- docs/lib/content/commands/npm-doctor.md | 6 +- lib/commands/doctor.js | 199 ++++++----- .../test/lib/commands/doctor.js.test.cjs | 326 +++++++++--------- tap-snapshots/test/lib/docs.js.test.cjs | 4 +- test/lib/commands/doctor.js | 26 +- 5 files changed, 315 insertions(+), 246 deletions(-) diff --git a/docs/lib/content/commands/npm-doctor.md b/docs/lib/content/commands/npm-doctor.md index 3abb1e29a24e2..5682f20b4cb5b 100644 --- a/docs/lib/content/commands/npm-doctor.md +++ b/docs/lib/content/commands/npm-doctor.md @@ -31,8 +31,10 @@ Also, in addition to this, there are also very many issue reports due to using old versions of npm. Since npm is constantly improving, running `npm@latest` is better than an old version. -`npm doctor` verifies the following items in your environment, and if there -are any recommended changes, it will display them. +`npm doctor` verifies the following items in your environment, and if +there are any recommended changes, it will display them. By default npm +runs all of these checks. You can limit what checks are ran by +specifying them as extra arguments. #### `npm ping` diff --git a/lib/commands/doctor.js b/lib/commands/doctor.js index 5dc3aa0db55a8..19262e537dbe0 100644 --- a/lib/commands/doctor.js +++ b/lib/commands/doctor.js @@ -34,30 +34,104 @@ const maskLabel = mask => { return label.join(', ') } +const subcommands = [ + { + groups: ['ping', 'registry'], + title: 'npm ping', + cmd: 'checkPing', + }, { + groups: ['versions'], + title: 'npm -v', + cmd: 'getLatestNpmVersion', + }, { + groups: ['versions'], + title: 'node -v', + cmd: 'getLatestNodejsVersion', + }, { + groups: ['registry'], + title: 'npm config get registry', + cmd: 'checkNpmRegistry', + }, { + groups: ['environment'], + title: 'git executable in PATH', + cmd: 'getGitPath', + }, { + groups: ['environment'], + title: 'global bin folder in PATH', + cmd: 'getBinPath', + }, { + groups: ['permissions', 'cache'], + title: 'Perms check on cached files', + cmd: 'checkCachePermission', + windows: false, + }, { + groups: ['permissions'], + title: 'Perms check on local node_modules', + cmd: 'checkLocalModulesPermission', + windows: false, + }, { + groups: ['permissions'], + title: 'Perms check on global node_modules', + cmd: 'checkGlobalModulesPermission', + windows: false, + }, { + groups: ['permissions'], + title: 'Perms check on local bin folder', + cmd: 'checkLocalBinPermission', + windows: false, + }, { + groups: ['permissions'], + title: 'Perms check on global bin folder', + cmd: 'checkGlobalBinPermission', + windows: false, + }, { + groups: ['cache'], + title: 'Verify cache contents', + cmd: 'verifyCachedFiles', + windows: false, + }, + // TODO: + // group === 'dependencies'? + // - ensure arborist.loadActual() runs without errors and no invalid edges + // - ensure package-lock.json matches loadActual() + // - verify loadActual without hidden lock file matches hidden lockfile + // group === '???' + // - verify all local packages have bins linked + // What is the fix for these? +] const BaseCommand = require('../base-command.js') class Doctor extends BaseCommand { static description = 'Check your npm environment' static name = 'doctor' static params = ['registry'] static ignoreImplicitWorkspace = false + static usage = [`[${subcommands.flatMap(s => s.groups) + .filter((value, index, self) => self.indexOf(value) === index) + .join('] [')}]`] + + static subcommands = subcommands + + // minimum width of check column, enough for the word `Check` + #checkWidth = 5 async exec (args) { log.info('Running checkup') let allOk = true const actions = this.actions(args) - this.checkWidth = actions.reduce((length, item) => Math.max(item[0].length, length), 5) + this.#checkWidth = actions.reduce((length, item) => + Math.max(item.title.length, length), this.#checkWidth) if (!this.npm.silent) { this.output(['Check', 'Value', 'Recommendation/Notes'].map(h => this.npm.chalk.underline(h))) } // Do the actual work - for (const [msg, fn, args] of actions) { - const item = [msg] + for (const { title, cmd } of actions) { + const item = [title] try { - item.push(true, await this[fn](...args)) - } catch (er) { - item.push(false, er) + item.push(true, await this[cmd]()) + } catch (err) { + item.push(false, err) } if (!item[1]) { allOk = false @@ -72,11 +146,13 @@ class Doctor extends BaseCommand { } } - if (!this.npm.silent) { - // this.npm.output(table(outTable, tableOpts)) - } if (!allOk) { - throw new Error('Some problems found. See above for recommendations.') + if (this.npm.silent) { + /* eslint-disable-next-line max-len */ + throw new Error('Some problems found. Check logs or disable silent mode for recommendations.') + } else { + throw new Error('Some problems found. See above for recommendations.') + } } } @@ -144,15 +220,35 @@ class Doctor extends BaseCommand { } } - async checkBinPath (dir) { - const tracker = log.newItem('checkBinPath', 1) - tracker.info('checkBinPath', 'Finding npm global bin in your PATH') + async getBinPath (dir) { + const tracker = log.newItem('getBinPath', 1) + tracker.info('getBinPath', 'Finding npm global bin in your PATH') if (!process.env.PATH.includes(this.npm.globalBin)) { throw new Error(`Add ${this.npm.globalBin} to your $PATH`) } return this.npm.globalBin } + async checkCachePermission () { + return this.checkFilesPermission(this.npm.cache, true, R_OK) + } + + async checkLocalModulesPermission () { + return this.checkFilesPermission(this.npm.localDir, true, R_OK | W_OK, true) + } + + async checkGlobalModulesPermission () { + return this.checkFilesPermission(this.npm.globalDir, false, R_OK) + } + + async checkLocalBinPermission () { + return this.checkFilesPermission(this.npm.localBin, false, R_OK | W_OK | X_OK, true) + } + + async checkGlobalBinPermission () { + return this.checkFilesPermission(this.npm.globalBin, false, X_OK) + } + async checkFilesPermission (root, shouldOwn, mask, missingOk) { let ok = true @@ -293,79 +389,22 @@ class Doctor extends BaseCommand { 'right-mid': '', middle: ' ' }, style: { 'padding-left': 0, 'padding-right': 0 }, - colWidths: [this.checkWidth, 6], + colWidths: [this.#checkWidth, 6], }) t.push(row) this.npm.output(t.toString()) } - actions (subcmds) { - const a = [] - if (!subcmds.length || subcmds.includes('ping')) { - a.push(['npm ping', 'checkPing', []]) - } - if (!subcmds.length || subcmds.includes('versions')) { - a.push(['npm -v', 'getLatestNpmVersion', []]) - a.push(['node -v', 'getLatestNodejsVersion', []]) - } - if (!subcmds.length || subcmds.includes('registry')) { - a.push(['npm config get registry', 'checkNpmRegistry', []]) - } - if (!subcmds.length || subcmds.includes('git')) { - a.push(['which git', 'getGitPath', []]) - } - if (!subcmds.length || subcmds.includes('permissions')) { - if (subcmds.includes('permissions') && process.platform === 'win32') { - log.warn('Ignoring permissions checks for windows') - } else if (process.platform !== 'win32') { - a.push([ - 'Perms check on cached files', - 'checkFilesPermission', - [this.npm.cache, true, R_OK], - ]) - a.push([ - 'Perms check on local node_modules', - 'checkFilesPermission', - [this.npm.localDir, true, R_OK | W_OK, true], - ]) - a.push([ - 'Perms check on global node_modules', - 'checkFilesPermission', - [this.npm.globalDir, false, R_OK], - ]) - a.push([ - 'Perms check on local bin folder', - 'checkFilesPermission', - [this.npm.localBin, false, R_OK | W_OK | X_OK, true], - ]) - a.push([ - 'Perms check on global bin folder', - 'checkFilesPermission', - [this.npm.globalBin, false, X_OK], - ]) + actions (params) { + return this.constructor.subcommands.filter(subcmd => { + if (process.platform === 'win32' && subcmd.windows === false) { + return false } - } - if (!subcmds.length || subcmds.includes('cache')) { - a.push([ - 'Verify cache contents', - 'verifyCachedFiles', - [this.npm.flatOptions.cache], - ]) - } - - if (!subcmds.length || subcmds.includes('environment')) { - a.push(['Global bin folder in PATH', 'checkBinPath', []]) - } - - // TODO: - // subcmd === 'dependencies'? - // - ensure arborist.loadActual() runs without errors and no invalid edges - // - ensure package-lock.json matches loadActual() - // - verify loadActual without hidden lock file matches hidden lockfile - // subcmd === '???' - // - verify all local packages have bins linked - // What is the fix for these? - return a + if (params.length) { + return params.some(param => subcmd.groups.includes(param)) + } + return true + }) } } diff --git a/tap-snapshots/test/lib/commands/doctor.js.test.cjs b/tap-snapshots/test/lib/commands/doctor.js.test.cjs index 52a90f6843be2..8d21f98e4892c 100644 --- a/tap-snapshots/test/lib/commands/doctor.js.test.cjs +++ b/tap-snapshots/test/lib/commands/doctor.js.test.cjs @@ -28,6 +28,10 @@ Object { "getGitPath", "Finding git in your PATH", ], + Array [ + "getBinPath", + "Finding npm global bin in your PATH", + ], Array [ "verifyCachedFiles", "Verifying the npm cache", @@ -43,10 +47,6 @@ Object { } ), ], - Array [ - "checkBinPath", - "Finding npm global bin in your PATH", - ], ], "warn": Array [], } @@ -58,14 +58,14 @@ npm ping  ok   npm -v  ok  current: v1.0.0, latest: v1.0.0 node -v  ok  current: v1.0.0, recommended: v1.0.0 npm config get registry  ok  using default registry (https://registry.npmjs.org/) -which git  ok  /path/to/git +git executable in PATH  ok  /path/to/git +global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-all-clear/global/bin Perms check on cached files  ok   Perms check on local node_modules  ok   Perms check on global node_modules ok   Perms check on local bin folder  ok   Perms check on global bin folder  ok   Verify cache contents  ok  verified 0 tarballs -Global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-all-clear/global/bin ` exports[`test/lib/commands/doctor.js TAP all clear in color > everything is ok in color 1`] = ` @@ -74,14 +74,14 @@ npm ping  ok   npm -v  ok  current: v1.0.0, latest: v1.0.0 node -v  ok  current: v1.0.0, recommended: v1.0.0 npm config get registry  ok  using default registry (https://registry.npmjs.org/) -which git  ok  /path/to/git +git executable in PATH  ok  /path/to/git +global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-all-clear-in-color/global/bin Perms check on cached files  ok   Perms check on local node_modules  ok   Perms check on global node_modules ok   Perms check on local bin folder  ok   Perms check on global bin folder  ok   Verify cache contents  ok  verified 0 tarballs -Global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-all-clear-in-color/global/bin ` exports[`test/lib/commands/doctor.js TAP all clear in color > logs 1`] = ` @@ -107,6 +107,10 @@ Object { "getGitPath", "Finding git in your PATH", ], + Array [ + "getBinPath", + "Finding npm global bin in your PATH", + ], Array [ "verifyCachedFiles", "Verifying the npm cache", @@ -122,10 +126,6 @@ Object { } ), ], - Array [ - "checkBinPath", - "Finding npm global bin in your PATH", - ], ], "warn": Array [], } @@ -154,6 +154,10 @@ Object { "getGitPath", "Finding git in your PATH", ], + Array [ + "getBinPath", + "Finding npm global bin in your PATH", + ], Array [ "verifyCachedFiles", "Verifying the npm cache", @@ -169,10 +173,6 @@ Object { } ), ], - Array [ - "checkBinPath", - "Finding npm global bin in your PATH", - ], ], "warn": Array [], } @@ -184,14 +184,14 @@ npm ping  not ok unsupported prox npm -v  not ok Error: unsupported proxy protocol: 'ssh:' node -v  not ok Error: unsupported proxy protocol: 'ssh:' npm config get registry  ok  using default registry (https://registry.npmjs.org/) -which git  ok  /path/to/git +git executable in PATH  ok  /path/to/git +global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-bad-proxy/global/bin Perms check on cached files  ok   Perms check on local node_modules  ok   Perms check on global node_modules ok   Perms check on local bin folder  ok   Perms check on global bin folder  ok   Verify cache contents  ok  verified 0 tarballs -Global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-bad-proxy/global/bin ` exports[`test/lib/commands/doctor.js TAP cacache badContent > corrupted cache content 1`] = ` @@ -200,14 +200,14 @@ npm ping  ok   npm -v  ok  current: v1.0.0, latest: v1.0.0 node -v  ok  current: v1.0.0, recommended: v1.0.0 npm config get registry  ok  using default registry (https://registry.npmjs.org/) -which git  ok  /path/to/git +git executable in PATH  ok  /path/to/git +global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-cacache-badContent/global/bin Perms check on cached files  ok   Perms check on local node_modules  ok   Perms check on global node_modules ok   Perms check on local bin folder  ok   Perms check on global bin folder  ok   Verify cache contents  ok  verified 2 tarballs -Global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-cacache-badContent/global/bin ` exports[`test/lib/commands/doctor.js TAP cacache badContent > logs 1`] = ` @@ -233,6 +233,10 @@ Object { "getGitPath", "Finding git in your PATH", ], + Array [ + "getBinPath", + "Finding npm global bin in your PATH", + ], Array [ "verifyCachedFiles", "Verifying the npm cache", @@ -248,10 +252,6 @@ Object { } ), ], - Array [ - "checkBinPath", - "Finding npm global bin in your PATH", - ], ], "warn": Array [ Array [ @@ -289,6 +289,10 @@ Object { "getGitPath", "Finding git in your PATH", ], + Array [ + "getBinPath", + "Finding npm global bin in your PATH", + ], Array [ "verifyCachedFiles", "Verifying the npm cache", @@ -304,10 +308,6 @@ Object { } ), ], - Array [ - "checkBinPath", - "Finding npm global bin in your PATH", - ], ], "warn": Array [ Array [ @@ -328,14 +328,14 @@ npm ping  ok   npm -v  ok  current: v1.0.0, latest: v1.0.0 node -v  ok  current: v1.0.0, recommended: v1.0.0 npm config get registry  ok  using default registry (https://registry.npmjs.org/) -which git  ok  /path/to/git +git executable in PATH  ok  /path/to/git +global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-cacache-missingContent/global/bin Perms check on cached files  ok   Perms check on local node_modules  ok   Perms check on global node_modules ok   Perms check on local bin folder  ok   Perms check on global bin folder  ok   Verify cache contents  ok  verified 2 tarballs -Global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-cacache-missingContent/global/bin ` exports[`test/lib/commands/doctor.js TAP cacache reclaimedCount > content garbage collected 1`] = ` @@ -344,14 +344,14 @@ npm ping  ok   npm -v  ok  current: v1.0.0, latest: v1.0.0 node -v  ok  current: v1.0.0, recommended: v1.0.0 npm config get registry  ok  using default registry (https://registry.npmjs.org/) -which git  ok  /path/to/git +git executable in PATH  ok  /path/to/git +global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-cacache-reclaimedCount/global/bin Perms check on cached files  ok   Perms check on local node_modules  ok   Perms check on global node_modules ok   Perms check on local bin folder  ok   Perms check on global bin folder  ok   Verify cache contents  ok  verified 2 tarballs -Global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-cacache-reclaimedCount/global/bin ` exports[`test/lib/commands/doctor.js TAP cacache reclaimedCount > logs 1`] = ` @@ -377,6 +377,10 @@ Object { "getGitPath", "Finding git in your PATH", ], + Array [ + "getBinPath", + "Finding npm global bin in your PATH", + ], Array [ "verifyCachedFiles", "Verifying the npm cache", @@ -392,10 +396,6 @@ Object { } ), ], - Array [ - "checkBinPath", - "Finding npm global bin in your PATH", - ], ], "warn": Array [ Array [ @@ -438,8 +438,9 @@ Object { ` exports[`test/lib/commands/doctor.js TAP discrete checks cache > output 1`] = ` -Check  Value  Recommendation/Notes -Verify cache contents ok  verified 0 tarballs +Check  Value  Recommendation/Notes +Perms check on cached files ok   +Verify cache contents  ok  verified 0 tarballs ` exports[`test/lib/commands/doctor.js TAP discrete checks git > logs 1`] = ` @@ -449,18 +450,13 @@ Object { Array [ "Running checkup", ], - Array [ - "getGitPath", - "Finding git in your PATH", - ], ], "warn": Array [], } ` exports[`test/lib/commands/doctor.js TAP discrete checks git > output 1`] = ` -Check  Value  Recommendation/Notes -which git ok  /path/to/git +Check Value  Recommendation/Notes ` exports[`test/lib/commands/doctor.js TAP discrete checks invalid environment > logs 1`] = ` @@ -471,7 +467,11 @@ Object { "Running checkup", ], Array [ - "checkBinPath", + "getGitPath", + "Finding git in your PATH", + ], + Array [ + "getBinPath", "Finding npm global bin in your PATH", ], ], @@ -481,7 +481,8 @@ Object { exports[`test/lib/commands/doctor.js TAP discrete checks invalid environment > output 1`] = ` Check  Value  Recommendation/Notes -Global bin folder in PATH not ok Error: Add {CWD}/test/lib/commands/tap-testdir-doctor-discrete-checks-invalid-environment/global/bin to your $PATH +git executable in PATH  ok  /path/to/git +global bin folder in PATH not ok Error: Add {CWD}/test/lib/commands/tap-testdir-doctor-discrete-checks-invalid-environment/global/bin to your $PATH ` exports[`test/lib/commands/doctor.js TAP discrete checks permissions - not windows > logs 1`] = ` @@ -513,11 +514,7 @@ Object { "Running checkup", ], ], - "warn": Array [ - Array [ - "Ignoring permissions checks for windows", - ], - ], + "warn": Array [], } ` @@ -553,6 +550,10 @@ Object { Array [ "Running checkup", ], + Array [ + "checkPing", + "Pinging registry", + ], ], "warn": Array [], } @@ -560,6 +561,7 @@ Object { exports[`test/lib/commands/doctor.js TAP discrete checks registry > output 1`] = ` Check  Value  Recommendation/Notes +npm ping  ok   npm config get registry ok  using default registry (https://registry.npmjs.org/) ` @@ -612,6 +614,10 @@ Object { "getGitPath", "Finding git in your PATH", ], + Array [ + "getBinPath", + "Finding npm global bin in your PATH", + ], Array [ "verifyCachedFiles", "Verifying the npm cache", @@ -627,10 +633,6 @@ Object { } ), ], - Array [ - "checkBinPath", - "Finding npm global bin in your PATH", - ], ], "warn": Array [ Array [ @@ -663,14 +665,14 @@ npm ping  ok   npm -v  ok  current: v1.0.0, latest: v1.0.0 node -v  ok  current: v1.0.0, recommended: v1.0.0 npm config get registry  ok  using default registry (https://registry.npmjs.org/) -which git  ok  /path/to/git +git executable in PATH  ok  /path/to/git +global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-error-reading-directory/global/bin Perms check on cached files  not ok Check the permissions of files in {CWD}/test/lib/commands/tap-testdir-doctor-error-reading-directory/cache (should be owned by current user) Perms check on local node_modules  not ok Check the permissions of files in {CWD}/test/lib/commands/tap-testdir-doctor-error-reading-directory/prefix/node_modules (should be owned by current user) Perms check on global node_modules not ok Check the permissions of files in {CWD}/test/lib/commands/tap-testdir-doctor-error-reading-directory/global/lib/node_modules Perms check on local bin folder  not ok Check the permissions of files in {CWD}/test/lib/commands/tap-testdir-doctor-error-reading-directory/prefix/node_modules/.bin Perms check on global bin folder  not ok Check the permissions of files in {CWD}/test/lib/commands/tap-testdir-doctor-error-reading-directory/global/bin Verify cache contents  ok  verified 0 tarballs -Global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-error-reading-directory/global/bin ` exports[`test/lib/commands/doctor.js TAP incorrect owner > incorrect owner 1`] = ` @@ -679,14 +681,14 @@ npm ping  ok   npm -v  ok  current: v1.0.0, latest: v1.0.0 node -v  ok  current: v1.0.0, recommended: v1.0.0 npm config get registry  ok  using default registry (https://registry.npmjs.org/) -which git  ok  /path/to/git +git executable in PATH  ok  /path/to/git +global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-incorrect-owner/global/bin Perms check on cached files  not ok Check the permissions of files in {CWD}/test/lib/commands/tap-testdir-doctor-incorrect-owner/cache (should be owned by current user) Perms check on local node_modules  ok   Perms check on global node_modules ok   Perms check on local bin folder  ok   Perms check on global bin folder  ok   Verify cache contents  ok  verified 0 tarballs -Global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-incorrect-owner/global/bin ` exports[`test/lib/commands/doctor.js TAP incorrect owner > logs 1`] = ` @@ -712,6 +714,10 @@ Object { "getGitPath", "Finding git in your PATH", ], + Array [ + "getBinPath", + "Finding npm global bin in your PATH", + ], Array [ "verifyCachedFiles", "Verifying the npm cache", @@ -727,10 +733,6 @@ Object { } ), ], - Array [ - "checkBinPath", - "Finding npm global bin in your PATH", - ], ], "warn": Array [ Array [ @@ -747,14 +749,14 @@ npm ping  ok   npm -v  ok  current: v1.0.0, latest: v1.0.0 node -v  ok  current: v1.0.0, recommended: v1.0.0 npm config get registry  ok  using default registry (https://registry.npmjs.org/) -which git  ok  /path/to/git +git executable in PATH  ok  /path/to/git +global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-incorrect-permissions/global/bin Perms check on cached files  not ok Check the permissions of files in {CWD}/test/lib/commands/tap-testdir-doctor-incorrect-permissions/cache (should be owned by current user) Perms check on local node_modules  not ok Check the permissions of files in {CWD}/test/lib/commands/tap-testdir-doctor-incorrect-permissions/prefix/node_modules (should be owned by current user) Perms check on global node_modules not ok Check the permissions of files in {CWD}/test/lib/commands/tap-testdir-doctor-incorrect-permissions/global/lib/node_modules Perms check on local bin folder  not ok Check the permissions of files in {CWD}/test/lib/commands/tap-testdir-doctor-incorrect-permissions/prefix/node_modules/.bin Perms check on global bin folder  not ok Check the permissions of files in {CWD}/test/lib/commands/tap-testdir-doctor-incorrect-permissions/global/bin Verify cache contents  ok  verified 0 tarballs -Global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-incorrect-permissions/global/bin ` exports[`test/lib/commands/doctor.js TAP incorrect permissions > logs 1`] = ` @@ -801,6 +803,10 @@ Object { "getGitPath", "Finding git in your PATH", ], + Array [ + "getBinPath", + "Finding npm global bin in your PATH", + ], Array [ "verifyCachedFiles", "Verifying the npm cache", @@ -816,10 +822,6 @@ Object { } ), ], - Array [ - "checkBinPath", - "Finding npm global bin in your PATH", - ], ], "warn": Array [], } @@ -848,6 +850,10 @@ Object { "getGitPath", "Finding git in your PATH", ], + Array [ + "getBinPath", + "Finding npm global bin in your PATH", + ], Array [ "verifyCachedFiles", "Verifying the npm cache", @@ -863,10 +869,6 @@ Object { } ), ], - Array [ - "checkBinPath", - "Finding npm global bin in your PATH", - ], ], "warn": Array [ Array [ @@ -882,14 +884,14 @@ npm ping  ok   npm -v  ok  current: v1.0.0, latest: v1.0.0 node -v  ok  current: v1.0.0, recommended: v1.0.0 npm config get registry  ok  using default registry (https://registry.npmjs.org/) -which git  not ok Error: Install git and ensure it's in your PATH. +git executable in PATH  not ok Error: Install git and ensure it's in your PATH. +global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-missing-git/global/bin Perms check on cached files  ok   Perms check on local node_modules  ok   Perms check on global node_modules ok   Perms check on local bin folder  ok   Perms check on global bin folder  ok   Verify cache contents  ok  verified 0 tarballs -Global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-missing-git/global/bin ` exports[`test/lib/commands/doctor.js TAP missing global directories > logs 1`] = ` @@ -915,6 +917,10 @@ Object { "getGitPath", "Finding git in your PATH", ], + Array [ + "getBinPath", + "Finding npm global bin in your PATH", + ], Array [ "verifyCachedFiles", "Verifying the npm cache", @@ -930,10 +936,6 @@ Object { } ), ], - Array [ - "checkBinPath", - "Finding npm global bin in your PATH", - ], ], "warn": Array [ Array [ @@ -954,14 +956,14 @@ npm ping  ok   npm -v  ok  current: v1.0.0, latest: v1.0.0 node -v  ok  current: v1.0.0, recommended: v1.0.0 npm config get registry  ok  using default registry (https://registry.npmjs.org/) -which git  ok  /path/to/git +git executable in PATH  ok  /path/to/git +global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-missing-global-directories/global/bin Perms check on cached files  ok   Perms check on local node_modules  ok   Perms check on global node_modules not ok Check the permissions of files in {CWD}/test/lib/commands/tap-testdir-doctor-missing-global-directories/global/lib/node_modules Perms check on local bin folder  ok   Perms check on global bin folder  not ok Check the permissions of files in {CWD}/test/lib/commands/tap-testdir-doctor-missing-global-directories/global/bin Verify cache contents  ok  verified 0 tarballs -Global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-missing-global-directories/global/bin ` exports[`test/lib/commands/doctor.js TAP missing local node_modules > logs 1`] = ` @@ -987,6 +989,10 @@ Object { "getGitPath", "Finding git in your PATH", ], + Array [ + "getBinPath", + "Finding npm global bin in your PATH", + ], Array [ "verifyCachedFiles", "Verifying the npm cache", @@ -1002,10 +1008,6 @@ Object { } ), ], - Array [ - "checkBinPath", - "Finding npm global bin in your PATH", - ], ], "warn": Array [], } @@ -1017,14 +1019,14 @@ npm ping  ok   npm -v  ok  current: v1.0.0, latest: v1.0.0 node -v  ok  current: v1.0.0, recommended: v1.0.0 npm config get registry  ok  using default registry (https://registry.npmjs.org/) -which git  ok  /path/to/git +git executable in PATH  ok  /path/to/git +global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-missing-local-node_modules/global/bin Perms check on cached files  ok   Perms check on local node_modules  ok   Perms check on global node_modules ok   Perms check on local bin folder  ok   Perms check on global bin folder  ok   Verify cache contents  ok  verified 0 tarballs -Global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-missing-local-node_modules/global/bin ` exports[`test/lib/commands/doctor.js TAP node out of date - current > logs 1`] = ` @@ -1050,6 +1052,10 @@ Object { "getGitPath", "Finding git in your PATH", ], + Array [ + "getBinPath", + "Finding npm global bin in your PATH", + ], Array [ "verifyCachedFiles", "Verifying the npm cache", @@ -1065,10 +1071,6 @@ Object { } ), ], - Array [ - "checkBinPath", - "Finding npm global bin in your PATH", - ], ], "warn": Array [], } @@ -1080,14 +1082,14 @@ npm ping  ok   npm -v  ok  current: v1.0.0, latest: v1.0.0 node -v  not ok Use node v2.0.1 (current: v2.0.0) npm config get registry  ok  using default registry (https://registry.npmjs.org/) -which git  ok  /path/to/git +git executable in PATH  ok  /path/to/git +global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-node-out-of-date---current/global/bin Perms check on cached files  ok   Perms check on local node_modules  ok   Perms check on global node_modules ok   Perms check on local bin folder  ok   Perms check on global bin folder  ok   Verify cache contents  ok  verified 0 tarballs -Global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-node-out-of-date---current/global/bin ` exports[`test/lib/commands/doctor.js TAP node out of date - lts > logs 1`] = ` @@ -1113,6 +1115,10 @@ Object { "getGitPath", "Finding git in your PATH", ], + Array [ + "getBinPath", + "Finding npm global bin in your PATH", + ], Array [ "verifyCachedFiles", "Verifying the npm cache", @@ -1128,10 +1134,6 @@ Object { } ), ], - Array [ - "checkBinPath", - "Finding npm global bin in your PATH", - ], ], "warn": Array [], } @@ -1143,14 +1145,14 @@ npm ping  ok   npm -v  ok  current: v1.0.0, latest: v1.0.0 node -v  not ok Use node v1.0.0 (current: v0.0.1) npm config get registry  ok  using default registry (https://registry.npmjs.org/) -which git  ok  /path/to/git +git executable in PATH  ok  /path/to/git +global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-node-out-of-date---lts/global/bin Perms check on cached files  ok   Perms check on local node_modules  ok   Perms check on global node_modules ok   Perms check on local bin folder  ok   Perms check on global bin folder  ok   Verify cache contents  ok  verified 0 tarballs -Global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-node-out-of-date---lts/global/bin ` exports[`test/lib/commands/doctor.js TAP non-default registry > logs 1`] = ` @@ -1176,6 +1178,10 @@ Object { "getGitPath", "Finding git in your PATH", ], + Array [ + "getBinPath", + "Finding npm global bin in your PATH", + ], Array [ "verifyCachedFiles", "Verifying the npm cache", @@ -1191,10 +1197,6 @@ Object { } ), ], - Array [ - "checkBinPath", - "Finding npm global bin in your PATH", - ], ], "warn": Array [], } @@ -1206,14 +1208,14 @@ npm ping  ok   npm -v  ok  current: v1.0.0, latest: v1.0.0 node -v  ok  current: v1.0.0, recommended: v1.0.0 npm config get registry  not ok Try \`npm config set registry=https://registry.npmjs.org/\` -which git  ok  /path/to/git +git executable in PATH  ok  /path/to/git +global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-non-default-registry/global/bin Perms check on cached files  ok   Perms check on local node_modules  ok   Perms check on global node_modules ok   Perms check on local bin folder  ok   Perms check on global bin folder  ok   Verify cache contents  ok  verified 0 tarballs -Global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-non-default-registry/global/bin ` exports[`test/lib/commands/doctor.js TAP npm out of date > logs 1`] = ` @@ -1239,6 +1241,10 @@ Object { "getGitPath", "Finding git in your PATH", ], + Array [ + "getBinPath", + "Finding npm global bin in your PATH", + ], Array [ "verifyCachedFiles", "Verifying the npm cache", @@ -1254,10 +1260,6 @@ Object { } ), ], - Array [ - "checkBinPath", - "Finding npm global bin in your PATH", - ], ], "warn": Array [], } @@ -1269,14 +1271,14 @@ npm ping  ok   npm -v  not ok Use npm v2.0.0 node -v  ok  current: v1.0.0, recommended: v1.0.0 npm config get registry  ok  using default registry (https://registry.npmjs.org/) -which git  ok  /path/to/git +git executable in PATH  ok  /path/to/git +global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-npm-out-of-date/global/bin Perms check on cached files  ok   Perms check on local node_modules  ok   Perms check on global node_modules ok   Perms check on local bin folder  ok   Perms check on global bin folder  ok   Verify cache contents  ok  verified 0 tarballs -Global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-npm-out-of-date/global/bin ` exports[`test/lib/commands/doctor.js TAP ping 404 > logs 1`] = ` @@ -1302,6 +1304,10 @@ Object { "getGitPath", "Finding git in your PATH", ], + Array [ + "getBinPath", + "Finding npm global bin in your PATH", + ], Array [ "verifyCachedFiles", "Verifying the npm cache", @@ -1317,10 +1323,6 @@ Object { } ), ], - Array [ - "checkBinPath", - "Finding npm global bin in your PATH", - ], ], "warn": Array [], } @@ -1332,14 +1334,14 @@ npm ping  not ok 404 404 Not Foun npm -v  ok  current: v1.0.0, latest: v1.0.0 node -v  ok  current: v1.0.0, recommended: v1.0.0 npm config get registry  ok  using default registry (https://registry.npmjs.org/) -which git  ok  /path/to/git +git executable in PATH  ok  /path/to/git +global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-ping-404/global/bin Perms check on cached files  ok   Perms check on local node_modules  ok   Perms check on global node_modules ok   Perms check on local bin folder  ok   Perms check on global bin folder  ok   Verify cache contents  ok  verified 0 tarballs -Global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-ping-404/global/bin ` exports[`test/lib/commands/doctor.js TAP ping 404 in color > logs 1`] = ` @@ -1365,6 +1367,10 @@ Object { "getGitPath", "Finding git in your PATH", ], + Array [ + "getBinPath", + "Finding npm global bin in your PATH", + ], Array [ "verifyCachedFiles", "Verifying the npm cache", @@ -1380,10 +1386,6 @@ Object { } ), ], - Array [ - "checkBinPath", - "Finding npm global bin in your PATH", - ], ], "warn": Array [], } @@ -1395,14 +1397,14 @@ exports[`test/lib/commands/doctor.js TAP ping 404 in color > ping 404 in color 1 npm -v  ok  current: v1.0.0, latest: v1.0.0 node -v  ok  current: v1.0.0, recommended: v1.0.0 npm config get registry  ok  using default registry (https://registry.npmjs.org/) -which git  ok  /path/to/git +git executable in PATH  ok  /path/to/git +global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-ping-404-in-color/global/bin Perms check on cached files  ok   Perms check on local node_modules  ok   Perms check on global node_modules ok   Perms check on local bin folder  ok   Perms check on global bin folder  ok   Verify cache contents  ok  verified 0 tarballs -Global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-ping-404-in-color/global/bin ` exports[`test/lib/commands/doctor.js TAP ping exception with code > logs 1`] = ` @@ -1428,6 +1430,10 @@ Object { "getGitPath", "Finding git in your PATH", ], + Array [ + "getBinPath", + "Finding npm global bin in your PATH", + ], Array [ "verifyCachedFiles", "Verifying the npm cache", @@ -1443,10 +1449,6 @@ Object { } ), ], - Array [ - "checkBinPath", - "Finding npm global bin in your PATH", - ], ], "warn": Array [], } @@ -1458,14 +1460,14 @@ npm ping  not ok request to https npm -v  ok  current: v1.0.0, latest: v1.0.0 node -v  ok  current: v1.0.0, recommended: v1.0.0 npm config get registry  ok  using default registry (https://registry.npmjs.org/) -which git  ok  /path/to/git +git executable in PATH  ok  /path/to/git +global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-ping-exception-with-code/global/bin Perms check on cached files  ok   Perms check on local node_modules  ok   Perms check on global node_modules ok   Perms check on local bin folder  ok   Perms check on global bin folder  ok   Verify cache contents  ok  verified 0 tarballs -Global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-ping-exception-with-code/global/bin ` exports[`test/lib/commands/doctor.js TAP ping exception without code > logs 1`] = ` @@ -1491,6 +1493,10 @@ Object { "getGitPath", "Finding git in your PATH", ], + Array [ + "getBinPath", + "Finding npm global bin in your PATH", + ], Array [ "verifyCachedFiles", "Verifying the npm cache", @@ -1506,10 +1512,6 @@ Object { } ), ], - Array [ - "checkBinPath", - "Finding npm global bin in your PATH", - ], ], "warn": Array [], } @@ -1521,17 +1523,37 @@ npm ping  not ok request to https npm -v  ok  current: v1.0.0, latest: v1.0.0 node -v  ok  current: v1.0.0, recommended: v1.0.0 npm config get registry  ok  using default registry (https://registry.npmjs.org/) -which git  ok  /path/to/git +git executable in PATH  ok  /path/to/git +global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-ping-exception-without-code/global/bin Perms check on cached files  ok   Perms check on local node_modules  ok   Perms check on global node_modules ok   Perms check on local bin folder  ok   Perms check on global bin folder  ok   Verify cache contents  ok  verified 0 tarballs -Global bin folder in PATH  ok  {CWD}/test/lib/commands/tap-testdir-doctor-ping-exception-without-code/global/bin ` -exports[`test/lib/commands/doctor.js TAP silent > logs 1`] = ` +exports[`test/lib/commands/doctor.js TAP silent errors > logs 1`] = ` +Object { + "error": Array [], + "info": Array [ + Array [ + "Running checkup", + ], + Array [ + "checkPing", + "Pinging registry", + ], + ], + "warn": Array [], +} +` + +exports[`test/lib/commands/doctor.js TAP silent errors > output 1`] = ` + +` + +exports[`test/lib/commands/doctor.js TAP silent success > logs 1`] = ` Object { "error": Array [], "info": Array [ @@ -1554,6 +1576,10 @@ Object { "getGitPath", "Finding git in your PATH", ], + Array [ + "getBinPath", + "Finding npm global bin in your PATH", + ], Array [ "verifyCachedFiles", "Verifying the npm cache", @@ -1569,16 +1595,12 @@ Object { } ), ], - Array [ - "checkBinPath", - "Finding npm global bin in your PATH", - ], ], "warn": Array [], } ` -exports[`test/lib/commands/doctor.js TAP silent > output 1`] = ` +exports[`test/lib/commands/doctor.js TAP silent success > output 1`] = ` ` @@ -1606,22 +1628,7 @@ Object { "Finding git in your PATH", ], Array [ - "verifyCachedFiles", - "Verifying the npm cache", - ], - Array [ - "verifyCachedFiles", - String( - Verification complete. Stats: { - "badContentCount": 0, - "reclaimedCount": 0, - "missingContent": 0, - "verifiedContent": 0 - } - ), - ], - Array [ - "checkBinPath", + "getBinPath", "Finding npm global bin in your PATH", ], ], @@ -1635,7 +1642,6 @@ npm ping  ok   npm -v  ok  current: v1.0.0, latest: v1.0.0 node -v  ok  current: v1.0.0, recommended: v1.0.0 npm config get registry  ok  using default registry (https://registry.npmjs.org/) -which git  ok  /path/to/git -Verify cache contents  ok  verified 0 tarballs -Global bin folder in PATH ok  {CWD}/test/lib/commands/tap-testdir-doctor-windows-skips-permissions-checks/global +git executable in PATH  ok  /path/to/git +global bin folder in PATH ok  {CWD}/test/lib/commands/tap-testdir-doctor-windows-skips-permissions-checks/global ` diff --git a/tap-snapshots/test/lib/docs.js.test.cjs b/tap-snapshots/test/lib/docs.js.test.cjs index 03913a0453ada..e5de69968dac9 100644 --- a/tap-snapshots/test/lib/docs.js.test.cjs +++ b/tap-snapshots/test/lib/docs.js.test.cjs @@ -2861,7 +2861,7 @@ exports[`test/lib/docs.js TAP usage doctor > must match snapshot 1`] = ` Check your npm environment Usage: -npm doctor +npm doctor [ping] [registry] [versions] [environment] [permissions] [cache] Options: [--registry ] @@ -2869,7 +2869,7 @@ Options: Run "npm help doctor" for more info \`\`\`bash -npm doctor +npm doctor [ping] [registry] [versions] [environment] [permissions] [cache] \`\`\` #### \`registry\` diff --git a/test/lib/commands/doctor.js b/test/lib/commands/doctor.js index 7fb78cf2b5c6e..a4602183e6938 100644 --- a/test/lib/commands/doctor.js +++ b/test/lib/commands/doctor.js @@ -118,7 +118,7 @@ t.test('all clear in color', async t => { t.matchSnapshot({ info: logs.info, warn: logs.warn, error: logs.error }, 'logs') }) -t.test('silent', async t => { +t.test('silent success', async t => { const { joinedOutput, logs, npm } = await loadMockNpm(t, { mocks, globals, @@ -137,6 +137,24 @@ t.test('silent', async t => { t.matchSnapshot({ info: logs.info, warn: logs.warn, error: logs.error }, 'logs') }) +t.test('silent errors', async t => { + const { joinedOutput, logs, npm } = await loadMockNpm(t, { + mocks, + globals, + config: { + loglevel: 'silent', + }, + ...dirs, + }) + tnock(t, npm.config.get('registry')) + .get('/-/ping?write=true').reply(404, '{}') + await t.rejects(npm.exec('doctor', ['ping']), { + message: /Check logs/, + }) + t.matchSnapshot(joinedOutput(), 'output') + t.matchSnapshot({ info: logs.info, warn: logs.warn, error: logs.error }, 'logs') +}) + t.test('ping 404', async t => { const { joinedOutput, logs, npm } = await loadMockNpm(t, { mocks, @@ -148,7 +166,9 @@ t.test('ping 404', async t => { .get('/npm').reply(200, npmManifest(npm.version)) tnock(t, 'https://nodejs.org') .get('/dist/index.json').reply(200, nodeVersions) - await t.rejects(npm.exec('doctor', [])) + await t.rejects(npm.exec('doctor', []), { + message: /See above/, + }) t.matchSnapshot(joinedOutput(), 'ping 404') t.matchSnapshot({ info: logs.info, warn: logs.warn, error: logs.error }, 'logs') }) @@ -560,6 +580,8 @@ t.test('discrete checks', t => { globals, ...dirs, }) + tnock(t, npm.config.get('registry')) + .get('/-/ping?write=true').reply(200, '{}') await npm.exec('doctor', ['registry']) t.matchSnapshot(joinedOutput(), 'output') t.matchSnapshot({ info: logs.info, warn: logs.warn, error: logs.error }, 'logs')