Skip to content

Commit da39e80

Browse files
committed
fix(doctor): allow for missing local bin and node_modules
1 parent 506be10 commit da39e80

3 files changed

Lines changed: 93 additions & 17 deletions

File tree

lib/commands/doctor.js

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,29 +58,33 @@ class Doctor extends BaseCommand {
5858
...(isWindows
5959
? []
6060
: [
61-
['Perms check on cached files', 'checkFilesPermission', [this.npm.cache, true, R_OK]],
6261
[
62+
'Perms check on cached files',
63+
'checkFilesPermission',
64+
[this.npm.cache, true, R_OK]
65+
], [
6366
'Perms check on local node_modules',
6467
'checkFilesPermission',
65-
[this.npm.localDir, true],
66-
],
67-
[
68+
[this.npm.localDir, true, R_OK | W_OK, true],
69+
], [
6870
'Perms check on global node_modules',
6971
'checkFilesPermission',
70-
[this.npm.globalDir, false],
71-
],
72-
[
72+
[this.npm.globalDir, false, R_OK],
73+
], [
7374
'Perms check on local bin folder',
7475
'checkFilesPermission',
75-
[this.npm.localBin, false, R_OK | W_OK | X_OK],
76-
],
77-
[
76+
[this.npm.localBin, false, R_OK | W_OK | X_OK, true],
77+
], [
7878
'Perms check on global bin folder',
7979
'checkFilesPermission',
8080
[this.npm.globalBin, false, X_OK],
8181
],
8282
]),
83-
['Verify cache contents', 'verifyCachedFiles', [this.npm.flatOptions.cache]],
83+
[
84+
'Verify cache contents',
85+
'verifyCachedFiles',
86+
[this.npm.flatOptions.cache]
87+
],
8488
// TODO:
8589
// - ensure arborist.loadActual() runs without errors and no invalid edges
8690
// - ensure package-lock.json matches loadActual()
@@ -202,10 +206,7 @@ class Doctor extends BaseCommand {
202206
}
203207
}
204208

205-
async checkFilesPermission (root, shouldOwn, mask = null) {
206-
if (mask === null) {
207-
mask = shouldOwn ? R_OK | W_OK : R_OK
208-
}
209+
async checkFilesPermission (root, shouldOwn, mask, missingOk) {
209210

210211
let ok = true
211212

@@ -218,8 +219,10 @@ class Doctor extends BaseCommand {
218219
for (const f of files) {
219220
tracker.silly('checkFilesPermission', f.substr(root.length + 1))
220221
const st = await lstat(f).catch(er => {
221-
ok = false
222-
tracker.warn('checkFilesPermission', 'error getting info for ' + f)
222+
if (!missingOk || err.code === 'ENOENT') {
223+
ok = false
224+
tracker.warn('checkFilesPermission', 'error getting info for ' + f)
225+
}
223226
})
224227

225228
tracker.completeWork(1)

tap-snapshots/test/lib/commands/doctor.js.test.cjs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,64 @@ Perms check on global bin folder not ok Check the permissions of files in {C
826826
Verify cache contents ok verified 0 tarballs
827827
`
828828

829+
exports[`test/lib/commands/doctor.js TAP missing local node_modules > logs 1`] = `
830+
Object {
831+
"error": Array [],
832+
"info": Array [
833+
Array [
834+
"Running checkup",
835+
],
836+
Array [
837+
"checkPing",
838+
"Pinging registry",
839+
],
840+
Array [
841+
"getLatestNpmVersion",
842+
"Getting npm package information",
843+
],
844+
Array [
845+
"getLatestNodejsVersion",
846+
"Getting Node.js release information",
847+
],
848+
Array [
849+
"getGitPath",
850+
"Finding git in your PATH",
851+
],
852+
Array [
853+
"verifyCachedFiles",
854+
"Verifying the npm cache",
855+
],
856+
Array [
857+
"verifyCachedFiles",
858+
String(
859+
Verification complete. Stats: {
860+
"badContentCount": 0,
861+
"reclaimedCount": 0,
862+
"missingContent": 0,
863+
"verifiedContent": 0
864+
}
865+
),
866+
],
867+
],
868+
"warn": Array [],
869+
}
870+
`
871+
872+
exports[`test/lib/commands/doctor.js TAP missing local node_modules > missing local node_modules 1`] = `
873+
Check Value Recommendation/Notes
874+
npm ping ok
875+
npm -v ok current: v1.0.0, latest: v1.0.0
876+
node -v ok current: v1.0.0, recommended: v1.0.0
877+
npm config get registry ok using default registry (https://registry.npmjs.org/)
878+
which git ok /path/to/git
879+
Perms check on cached files ok
880+
Perms check on local node_modules ok
881+
Perms check on global node_modules ok
882+
Perms check on local bin folder ok
883+
Perms check on global bin folder ok
884+
Verify cache contents ok verified 0 tarballs
885+
`
886+
829887
exports[`test/lib/commands/doctor.js TAP node out of date - current > logs 1`] = `
830888
Object {
831889
"error": Array [],

test/lib/commands/doctor.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,21 @@ t.test('missing global directories', async t => {
324324
t.matchSnapshot({ info: logs.info, warn: logs.warn, error: logs.error }, 'logs')
325325
})
326326

327+
t.test('missing local node_modules', async t => {
328+
const { joinedOutput, logs, npm } = await loadMockNpm(t, {
329+
mocks,
330+
globalPrefixDir: dirs.globalPrefixDir,
331+
})
332+
tnock(t, npm.config.get('registry'))
333+
.get('/-/ping?write=true').reply(200, '{}')
334+
.get('/npm').reply(200, npmManifest(npm.version))
335+
tnock(t, 'https://nodejs.org')
336+
.get('/dist/index.json').reply(200, nodeVersions)
337+
await npm.exec('doctor', [])
338+
t.matchSnapshot(joinedOutput(), 'missing local node_modules')
339+
t.matchSnapshot({ info: logs.info, warn: logs.warn, error: logs.error }, 'logs')
340+
})
341+
327342
t.test('incorrect owner', async t => {
328343
const { joinedOutput, logs, npm } = await loadMockNpm(t, {
329344
mocks: {

0 commit comments

Comments
 (0)