Skip to content

Commit

Permalink
chore(tests): add some integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nlf committed Jun 22, 2022
1 parent 77b2d1a commit 8c62010
Showing 1 changed file with 51 additions and 2 deletions.
53 changes: 51 additions & 2 deletions test/escape.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
'use strict'

const { writeFileSync: writeFile, unlinkSync: unlink, chmodSync: chmod } = require('fs')
const { join } = require('path')
const t = require('tap')
const promiseSpawn = require('@npmcli/promise-spawn')

const escape = require('../lib/escape.js')
const isWindows = process.platform === 'win32'

t.test('sh', (t) => {
const expectations = [
Expand All @@ -17,11 +23,29 @@ t.test('sh', (t) => {
[`'--arg=npm exec -c "$1"'`, `\\''--arg=npm exec -c "$1"'\\'`],
]

t.plan(expectations.length)
for (const [input, expectation] of expectations) {
t.equal(escape.sh(input), expectation,
`expected to escape \`${input}\` to \`${expectation}\``)
}

t.test('integration', { skip: isWindows && 'posix only' }, async (t) => {
const dir = t.testdir()

for (const [input] of expectations) {
const filename = join(dir, 'posix.sh')
const script = `#!/usr/bin/env sh\nnode -p process.argv[1] -- ${escape.sh(input)}`
writeFile(filename, script)
chmod(filename, '0755')
const p = await promiseSpawn('sh', ['-c', filename], { stdioString: true })
const stdout = p.stdout.trim()
t.equal(input, stdout, 'actual output matches input')
unlink(filename)
}

t.end()
})

t.end()
})

t.test('cmd', (t) => {
Expand Down Expand Up @@ -72,9 +96,34 @@ t.test('cmd', (t) => {
['hello %PATH%', '^^^"hello %%PATH%%^^^"', true],
]

t.plan(expectations.length)
for (const [input, expectation, double] of expectations) {
const msg = `expected to${double ? ' double' : ''} escape \`${input}\` to \`${expectation}\``
t.equal(escape.cmd(input, double), expectation, msg)
}

t.test('integration', { skip: !isWindows && 'Windows only' }, async (t) => {
const dir = t.testdir()

for (const [input,, double] of expectations) {
const filename = join(dir, 'win.cmd')
if (double) {
const shimFile = join(dir, 'shim.cmd')
const shim = `@echo off\nnode -p process.argv[1] -- %*`
writeFile(shimFile, shim)
const script = `@echo off\n"${shimFile}" ${escape.cmd(input, double)}`
writeFile(filename, script)
} else {
const script = `@echo off\nnode -p process.argv[1] -- ${escape.cmd(input)}`
writeFile(filename, script)
}
const p = await promiseSpawn('cmd', ['/d', '/s', '/c', filename], { stdioString: true })
const stdout = p.stdout.trim()
t.equal(input, stdout, 'actual output matches input')
unlink(filename)
}

t.end()
})

t.end()
})

0 comments on commit 8c62010

Please sign in to comment.