Skip to content

Commit

Permalink
chore: refactor windows shim tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lukekarrys committed Jun 20, 2023
1 parent 939a188 commit fafabe5
Showing 1 changed file with 55 additions and 73 deletions.
128 changes: 55 additions & 73 deletions test/bin/windows-shims.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,41 +39,40 @@ t.test('npm vs npx', t => {

t.test('basic', async t => {
if (process.platform !== 'win32') {
t.plan(0, 'test only relevant on windows')
t.comment('test only relevant on windows')
return
}

const has = path => {
const { ProgramFiles, SystemRoot, NYC_CONFIG } = process.env
const gitBash = resolve(ProgramFiles, 'Git', 'bin', 'bash.exe')
const gitUsrBinBash = resolve(ProgramFiles, 'Git', 'usr', 'bin', 'bash.exe')
const wslBash = resolve(SystemRoot, 'System32', 'bash.exe')
const cygwinBash = resolve(SystemRoot, '/', 'cygwin64', 'bin', 'bash.exe')

const skipBash = path => {
if (path === cygwinBash && NYC_CONFIG) {
return 'does not play nicely with NYC, run without coverage'
}
try {
// If WSL is installed, it *has* a bash.exe, but it fails if
// there is no distro installed, so we need to detect that.
const result = spawnSync(path, ['-l', '-c', 'exit 0'])
if (result.status === 0) {
return true
} else {
// print whatever error we got
throw result.error || Object.assign(new Error(String(result.stderr)), {
code: result.status,
})
if (spawnSync(path, ['-l', '-c', 'exit 0']).status === 0) {
return
}
} catch (er) {
t.comment(`not installed: ${path}`, er)
return false
}
} catch { /* not installed */ }
return 'not installed'
}

const { ProgramFiles, SystemRoot } = process.env
const gitBash = resolve(ProgramFiles, 'Git', 'bin', 'bash.exe')
const gitUsrBinBash = resolve(ProgramFiles, 'Git', 'usr', 'bin', 'bash.exe')
const wslBash = resolve(SystemRoot, 'System32', 'bash.exe')
const cygwinBash = resolve(SystemRoot, '/', 'cygwin64', 'bin', 'bash.exe')

const bashes = Object.entries({
'wsl bash': wslBash,
'git bash': gitBash,
'git internal bash': gitUsrBinBash,
'cygwin bash': cygwinBash,
})
}).map(([name, bash]) => ({
name,
bash,
skip: skipBash(bash),
}))

const path = t.testdir({
'node.exe': readFileSync(process.execPath),
Expand All @@ -92,73 +91,56 @@ t.test('basic', async t => {
npm: {
bin: {
'npx-cli.js': `
throw new Error('this should not be called')
`,
throw new Error('this should not be called')
`,
'npm-cli.js': `
const assert = require('assert')
const args = process.argv.slice(2)
assert.equal(args[0], 'prefix')
assert.equal(args[1], '-g')
const { resolve } = require('path')
console.log(resolve(__dirname, '../../../global-prefix'))
`,
const assert = require('assert')
const args = process.argv.slice(2)
assert.equal(args[0], 'prefix')
assert.equal(args[1], '-g')
const { resolve } = require('path')
console.log(resolve(__dirname, '../../../global-prefix'))
`,
},
},
},
})

chmodSync(resolve(path, 'npm'), 0o755)
chmodSync(resolve(path, 'npx'), 0o755)

for (const [name, bash] of bashes) {
if (!has(bash)) {
t.skip(`${name} not installed`, { bin: bash, diagnostic: true })
continue
}

if (bash === cygwinBash && process.env.NYC_CONFIG) {
t.skip('Cygwin does not play nicely with NYC, run without coverage')
for (const { name, bash, skip } of bashes) {
if (skip) {
t.skip(name, { diagnostic: true, bin: bash, reason: skip })
continue
}

await t.test(name, async t => {
t.plan(2)
t.test('npm', async t => {
// only cygwin *requires* the -l, but the others are ok with it
// don't hit the registry for the update check
const args = ['-l', 'npm', 'help']

const result = await spawn(bash, args, {
env: { PATH: path, npm_config_update_notifier: 'false' },
cwd: path,
})
t.match(result, {
cmd: bash,
args: ['-l', 'npm', 'help'],
code: 0,
signal: null,
stderr: String,
// should have loaded this instance of npm we symlinked in
stdout: `npm@${version} ${resolve(__dirname, '../..')}`,
})
const cases = Object.entries({
// should have loaded this instance of npm we symlinked in
npm: [['help'], `npm@${version} ${resolve(__dirname, '../..')}`],
npx: [['--version'], version],
})

t.test('npx', async t => {
const args = ['-l', 'npx', '--version']

const result = await spawn(bash, args, {
env: { PATH: path, npm_config_update_notifier: 'false' },
cwd: path,
for (const [binName, [cmdArgs, stdout]] of cases) {
await t.test(binName, async t => {
// only cygwin *requires* the -l, but the others are ok with it
const args = ['-l', binName, ...cmdArgs]
const result = await spawn(bash, args, {
// don't hit the registry for the update check
env: { PATH: path, npm_config_update_notifier: 'false' },
cwd: path,
})
t.match(result, {
cmd: bash,
args: args,
code: 0,
signal: null,
stderr: String,
stdout,
})
})
t.match(result, {
cmd: bash,
args: ['-l', 'npx', '--version'],
code: 0,
signal: null,
stderr: String,
// should have loaded this instance of npm we symlinked in
stdout: version,
})
})
}
})
}
})

0 comments on commit fafabe5

Please sign in to comment.