|
| 1 | +const t = require('tap') |
| 2 | + |
| 3 | +if (process.platform !== 'win32') { |
| 4 | + t.plan(0, 'test only relevant on windows') |
| 5 | + process.exit(0) |
| 6 | +} |
| 7 | + |
| 8 | +const has = path => { |
| 9 | + try { |
| 10 | + // If WSL is installed, it *has* a bash.exe, but it fails if |
| 11 | + // there is no distro installed, so we need to detect that. |
| 12 | + return spawnSync(path, ['-l', '-c', 'exit 0']).status === 0 |
| 13 | + } catch (er) { |
| 14 | + t.comment(`not installed: ${path}`, er) |
| 15 | + return false |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +const { version } = require('../../package.json') |
| 20 | +const spawn = require('@npmcli/promise-spawn') |
| 21 | +const { spawnSync } = require('child_process') |
| 22 | +const { resolve } = require('path') |
| 23 | +const { ProgramFiles, SystemRoot } = process.env |
| 24 | +const { readFileSync, statSync, chmodSync } = require('fs') |
| 25 | +const gitBash = resolve(ProgramFiles, 'Git', 'bin', 'bash.exe') |
| 26 | +const gitUsrBinBash = resolve(ProgramFiles, 'Git', 'usr', 'bin', 'bash.exe') |
| 27 | +const wslBash = resolve(SystemRoot, 'System32', 'bash.exe') |
| 28 | +const cygwinBash = resolve(SystemRoot, '/', 'cygwin64', 'bin', 'bash.exe') |
| 29 | + |
| 30 | +const bashes = Object.entries({ |
| 31 | + 'wsl bash': wslBash, |
| 32 | + 'git bash': gitBash, |
| 33 | + 'git internal bash': gitUsrBinBash, |
| 34 | + 'cygwin bash': cygwinBash, |
| 35 | +}) |
| 36 | + |
| 37 | +const npmShim = resolve(__dirname, '../../bin/npm') |
| 38 | +const npxShim = resolve(__dirname, '../../bin/npx') |
| 39 | + |
| 40 | +const path = t.testdir({ |
| 41 | + 'node.exe': readFileSync(process.execPath), |
| 42 | + npm: readFileSync(npmShim), |
| 43 | + npx: readFileSync(npxShim), |
| 44 | + // simulate the state where one version of npm is installed |
| 45 | + // with node, but we should load the globally installed one |
| 46 | + 'global-prefix': { |
| 47 | + node_modules: { |
| 48 | + npm: t.fixture('symlink', resolve(__dirname, '../..')), |
| 49 | + }, |
| 50 | + }, |
| 51 | + // put in a shim that ONLY prints the intended global prefix, |
| 52 | + // and should not be used for anything else. |
| 53 | + node_modules: { |
| 54 | + npm: { |
| 55 | + bin: { |
| 56 | + 'npx-cli.js': ` |
| 57 | + throw new Error('this should not be called') |
| 58 | + `, |
| 59 | + 'npm-cli.js': ` |
| 60 | + const assert = require('assert') |
| 61 | + const args = process.argv.slice(2) |
| 62 | + assert.equal(args[0], 'prefix') |
| 63 | + assert.equal(args[1], '-g') |
| 64 | + const { resolve } = require('path') |
| 65 | + console.log(resolve(__dirname, '../../../global-prefix')) |
| 66 | + `, |
| 67 | + }, |
| 68 | + }, |
| 69 | + }, |
| 70 | +}) |
| 71 | +chmodSync(resolve(path, 'npm'), 0o755) |
| 72 | +chmodSync(resolve(path, 'npx'), 0o755) |
| 73 | + |
| 74 | +for (const [name, bash] of bashes) { |
| 75 | + if (!has(bash)) { |
| 76 | + t.skip(`${name} not installed`, { bin: bash, diagnostic: true }) |
| 77 | + continue |
| 78 | + } |
| 79 | + |
| 80 | + t.test(name, async t => { |
| 81 | + t.plan(2) |
| 82 | + t.test('npm', async t => { |
| 83 | + // only cygwin *requires* the -l, but the others are ok with it |
| 84 | + // don't hit the registry for the update check |
| 85 | + const args = ['-l', 'npm', 'help'] |
| 86 | + |
| 87 | + const result = await spawn(bash, args, { |
| 88 | + env: { PATH: path, npm_config_update_notifier: 'false' }, |
| 89 | + cwd: path, |
| 90 | + stdioString: true, |
| 91 | + }) |
| 92 | + t.match(result, { |
| 93 | + cmd: bash, |
| 94 | + args: ['-l', 'npm', 'help'], |
| 95 | + code: 0, |
| 96 | + signal: null, |
| 97 | + stderr: String, |
| 98 | + // should have loaded this instance of npm we symlinked in |
| 99 | + stdout: `npm@${version} ${resolve(__dirname, '../..')}`, |
| 100 | + }) |
| 101 | + }) |
| 102 | + |
| 103 | + t.test('npx', async t => { |
| 104 | + const args = ['-l', 'npx', '--version'] |
| 105 | + |
| 106 | + const result = await spawn(bash, args, { |
| 107 | + env: { PATH: path, npm_config_update_notifier: 'false' }, |
| 108 | + cwd: path, |
| 109 | + stdioString: true, |
| 110 | + }) |
| 111 | + t.match(result, { |
| 112 | + cmd: bash, |
| 113 | + args: ['-l', 'npx', '--version'], |
| 114 | + code: 0, |
| 115 | + signal: null, |
| 116 | + stderr: String, |
| 117 | + // should have loaded this instance of npm we symlinked in |
| 118 | + stdout: version, |
| 119 | + }) |
| 120 | + }) |
| 121 | + }) |
| 122 | +} |
0 commit comments