Skip to content

Commit

Permalink
fix(doctor): allow for missing local bin and node_modules
Browse files Browse the repository at this point in the history
  • Loading branch information
wraithgar committed Mar 7, 2022
1 parent d4ff4fb commit b17e900
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 60 deletions.
42 changes: 22 additions & 20 deletions lib/commands/doctor.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const semver = require('semver')
const { promisify } = require('util')
const log = require('../utils/log-shim.js')
const ansiTrim = require('../utils/ansi-trim.js')
const isWindows = require('../utils/is-windows.js')
const ping = require('../utils/ping.js')
const {
registry: { default: defaultRegistry },
Expand Down Expand Up @@ -55,32 +54,36 @@ class Doctor extends BaseCommand {
['node -v', 'getLatestNodejsVersion', []],
['npm config get registry', 'checkNpmRegistry', []],
['which git', 'getGitPath', []],
...(isWindows
...(process.platform === 'win32'
? []
: [
['Perms check on cached files', 'checkFilesPermission', [this.npm.cache, true, R_OK]],
[
'Perms check on cached files',
'checkFilesPermission',
[this.npm.cache, true, R_OK],
], [
'Perms check on local node_modules',
'checkFilesPermission',
[this.npm.localDir, true],
],
[
[this.npm.localDir, true, R_OK | W_OK, true],
], [
'Perms check on global node_modules',
'checkFilesPermission',
[this.npm.globalDir, false],
],
[
[this.npm.globalDir, false, R_OK],
], [
'Perms check on local bin folder',
'checkFilesPermission',
[this.npm.localBin, false, R_OK | W_OK | X_OK],
],
[
[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]],
[
'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()
Expand Down Expand Up @@ -202,11 +205,7 @@ class Doctor extends BaseCommand {
}
}

async checkFilesPermission (root, shouldOwn, mask = null) {
if (mask === null) {
mask = shouldOwn ? R_OK | W_OK : R_OK
}

async checkFilesPermission (root, shouldOwn, mask, missingOk) {
let ok = true

const tracker = log.newItem(root, 1)
Expand All @@ -218,8 +217,11 @@ class Doctor extends BaseCommand {
for (const f of files) {
tracker.silly('checkFilesPermission', f.substr(root.length + 1))
const st = await lstat(f).catch(er => {
ok = false
tracker.warn('checkFilesPermission', 'error getting info for ' + f)
// if it can't be missing, or if it can and the error wasn't that it was missing
if (!missingOk || er.code !== 'ENOENT') {
ok = false
tracker.warn('checkFilesPermission', 'error getting info for ' + f)
}
})

tracker.completeWork(1)
Expand Down
58 changes: 58 additions & 0 deletions tap-snapshots/test/lib/commands/doctor.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,64 @@ Perms check on global bin folder not ok Check the permissions of files in {C
Verify cache contents ok verified 0 tarballs
`

exports[`test/lib/commands/doctor.js TAP missing local node_modules > logs 1`] = `
Object {
"error": Array [],
"info": Array [
Array [
"Running checkup",
],
Array [
"checkPing",
"Pinging registry",
],
Array [
"getLatestNpmVersion",
"Getting npm package information",
],
Array [
"getLatestNodejsVersion",
"Getting Node.js release information",
],
Array [
"getGitPath",
"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
}
),
],
],
"warn": Array [],
}
`

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
`

exports[`test/lib/commands/doctor.js TAP node out of date - current > logs 1`] = `
Object {
"error": Array [],
Expand Down
Loading

0 comments on commit b17e900

Please sign in to comment.