Skip to content

Commit 7052862

Browse files
committed
chore(tests): add some integration tests
1 parent 77b2d1a commit 7052862

File tree

1 file changed

+51
-2
lines changed

1 file changed

+51
-2
lines changed

test/escape.js

+51-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
'use strict'
2+
3+
const { writeFileSync: writeFile, unlinkSync: unlink, chmodSync: chmod } = require('fs')
4+
const { join } = require('path')
15
const t = require('tap')
6+
const promiseSpawn = require('@npmcli/promise-spawn')
27

38
const escape = require('../lib/escape.js')
9+
const isWindows = process.platform === 'win32'
410

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

20-
t.plan(expectations.length)
2126
for (const [input, expectation] of expectations) {
2227
t.equal(escape.sh(input), expectation,
2328
`expected to escape \`${input}\` to \`${expectation}\``)
2429
}
30+
31+
t.test('integration', { skip: isWindows && 'posix only' }, async (t) => {
32+
const dir = t.testdir()
33+
34+
for (const [input] of expectations) {
35+
const filename = join(dir, 'posix.sh')
36+
const script = `#!/usr/bin/env sh\nnode -p process.argv[1] -- ${escape.sh(input)}`
37+
writeFile(filename, script)
38+
chmod(filename, '0755')
39+
const p = await promiseSpawn('sh', ['-c', filename], { stdioString: true })
40+
const stdout = p.stdout.trim()
41+
t.equal(input, stdout, 'actual output matches input')
42+
unlink(filename)
43+
}
44+
45+
t.end()
46+
})
47+
48+
t.end()
2549
})
2650

2751
t.test('cmd', (t) => {
@@ -72,9 +96,34 @@ t.test('cmd', (t) => {
7296
['hello %PATH%', '^^^"hello %%PATH%%^^^"', true],
7397
]
7498

75-
t.plan(expectations.length)
7699
for (const [input, expectation, double] of expectations) {
77100
const msg = `expected to${double ? ' double' : ''} escape \`${input}\` to \`${expectation}\``
78101
t.equal(escape.cmd(input, double), expectation, msg)
79102
}
103+
104+
t.test('integration', { skip: !isWindows && 'Windows only' }, async (t) => {
105+
const dir = t.testdir()
106+
107+
for (const [input,, double] of expectations) {
108+
const filename = join(dir, 'win.cmd')
109+
if (double) {
110+
const shimFile = join(dir, 'shim.cmd')
111+
const shim = `@echo off\nnode -p process.argv[1] -- %*`
112+
writeFile(shimFile, shim)
113+
const script = `@echo off\n.\\\\shim.cmd ${escape.cmd(input, double)}`
114+
writeFile(filename, script)
115+
} else {
116+
const script = `@echo off\nnode -p process.argv[1] -- ${escape.cmd(input)}`
117+
writeFile(filename, script)
118+
}
119+
const p = await promiseSpawn('cmd', ['/d', '/s', '/c', filename], { stdioString: true })
120+
const stdout = p.stdout.trim()
121+
t.equal(input, stdout, 'actual output matches input')
122+
unlink(filename)
123+
}
124+
125+
t.end()
126+
})
127+
128+
t.end()
80129
})

0 commit comments

Comments
 (0)