Created
August 1, 2019 23:46
-
-
Save isaacs/209868f5be65ee1b461566e403c6addf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs') | |
if (process.argv[2] === 'install') { | |
const eacces = (path, dest) => { | |
const er = new Error('EACCES: I can\'t go for that') | |
er.path = path | |
er.dest = dest | |
er.code = 'EACCES' | |
er.syscall = 'rename' | |
er.errno = -13 | |
console.error(er.stack) | |
return er | |
} | |
const cache = process.env.TEST_COMMON_CACHE | |
const link = fs.link | |
fs.link = (path, dest, cb) => { | |
if (path.startsWith(cache) || dest.startsWith(cache)) { | |
const er = eacces(path, dest) | |
console.error('link eacces', path, dest) | |
process.nextTick(() => cb(er)) | |
} else { | |
return link(path, dest, cb) | |
} | |
} | |
const rename = fs.rename | |
fs.rename = (path, dest, cb) => { | |
if (path.startsWith(cache) || dest.startsWith(cache)) { | |
const er = eacces(path, dest) | |
console.error('rename eacces', path, dest) | |
process.nextTick(() => cb(er)) | |
} else { | |
return rename(path, dest, cb) | |
} | |
} | |
if (fs.promises) { | |
fs.promises.rename = (path, dest) => { | |
const er = eacces(path, dest) | |
console.error('patched fs.promises.rename', path, dest) | |
return Promise.reject(er) | |
} | |
fs.promises.link = (path, dest) => { | |
const er = eacces(path, dest) | |
console.error('patched fs.promises.link', path, dest) | |
return Promise.reject(er) | |
} | |
} | |
require('../../bin/npm-cli.js') | |
} else { | |
const common = require('../common-tap.js') | |
const pkg = common.pkg | |
const mr = require('npm-registry-mock') | |
const t = require('tap') | |
let server | |
t.teardown(() => { | |
if (server) { | |
server.close() | |
} | |
}) | |
t.test('setup', tt => { | |
fs.writeFileSync(pkg + '/package.json', JSON.stringify({ | |
name: 'denied-access', | |
version: '4.2.0', | |
dependencies: { | |
underscore: '*' | |
} | |
})) | |
mr({ port: common.port }, function (er, s) { | |
server = s | |
tt.end() | |
}) | |
}) | |
t.test('install', t => { | |
process.chdir(pkg) | |
const { spawn } = require('child_process') | |
const child = spawn( | |
process.execPath, | |
[ | |
__filename, | |
'install', | |
'--cache=' + common.cache, | |
'--registry=' + common.registry, | |
'--no-send-metrics', | |
'--unsafe-perm', | |
'--loglevel=silly', | |
'--prefix=' + pkg | |
], | |
{ | |
cwd: pkg, | |
env: Object.keys(process.env).reduce((env, k) => { | |
env[k] = env[k] || process.env[k] | |
return env | |
}, { | |
TEST_COMMON_CACHE: common.cache | |
}) | |
} | |
) | |
const out = [] | |
const err = [] | |
child.stdout.on('data', c => out.push(c)) | |
child.stderr.on('data', c => err.push(c)) | |
child.on('exit', (code, signal) => { | |
const outs = Buffer.concat(out).toString('utf8') | |
const errs = Buffer.concat(err).toString('utf8') | |
console.error({code, signal, outs, errs}) | |
t.end() | |
}) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment