diff --git a/test/open.js b/test/open.js index 674e721..290aa4f 100644 --- a/test/open.js +++ b/test/open.js @@ -21,9 +21,16 @@ t.test('process.platform === win32', (t) => { }) t.test('uses start with a shell', async (t) => { - const proc = spawk.spawn('C:\\Windows\\System32\\cmd.exe', - ['/d', '/s', '/c', 'start "" https://google.com'], - { shell: false }) + const proc = spawk.spawn( + 'C:\\Windows\\System32\\cmd.exe', + [ + '/d', + '/s', + '/c', + 'C:\\Windows\\System32\\cmd.exe /c start "" https://google.com', + ], + { shell: false, windowsVerbatimArguments: true } + ) const result = await promiseSpawn.open('https://google.com') t.hasStrict(result, { @@ -35,11 +42,20 @@ t.test('process.platform === win32', (t) => { }) t.test('ignores shell = false', async (t) => { - const proc = spawk.spawn('C:\\Windows\\System32\\cmd.exe', - ['/d', '/s', '/c', 'start "" https://google.com'], - { shell: false }) - - const result = await promiseSpawn.open('https://google.com', { shell: false }) + const proc = spawk.spawn( + 'C:\\Windows\\System32\\cmd.exe', + [ + '/d', + '/s', + '/c', + 'C:\\Windows\\System32\\cmd.exe /c start "" https://google.com', + ], + { shell: false, windowsVerbatimArguments: true } + ) + + const result = await promiseSpawn.open('https://google.com', { + shell: false, + }) t.hasStrict(result, { code: 0, signal: undefined, @@ -49,11 +65,15 @@ t.test('process.platform === win32', (t) => { }) t.test('respects opts.command', async (t) => { - const proc = spawk.spawn('C:\\Windows\\System32\\cmd.exe', + const proc = spawk.spawn( + 'C:\\Windows\\System32\\cmd.exe', ['/d', '/s', '/c', 'browser https://google.com'], - { shell: false }) + { shell: false, windowsVerbatimArguments: true } + ) - const result = await promiseSpawn.open('https://google.com', { command: 'browser' }) + const result = await promiseSpawn.open('https://google.com', { + command: 'browser', + }) t.hasStrict(result, { code: 0, signal: undefined, @@ -67,13 +87,18 @@ t.test('process.platform === win32', (t) => { t.test('process.platform === darwin', (t) => { const platformDesc = Object.getOwnPropertyDescriptor(process, 'platform') - Object.defineProperty(process, 'platform', { ...platformDesc, value: 'darwin' }) + Object.defineProperty(process, 'platform', { + ...platformDesc, + value: 'darwin', + }) t.teardown(() => { Object.defineProperty(process, 'platform', platformDesc) }) t.test('uses open with a shell', async (t) => { - const proc = spawk.spawn('sh', ['-c', 'open https://google.com'], { shell: false }) + const proc = spawk.spawn('sh', ['-c', 'open https://google.com'], { + shell: false, + }) const result = await promiseSpawn.open('https://google.com') t.hasStrict(result, { @@ -85,9 +110,13 @@ t.test('process.platform === darwin', (t) => { }) t.test('ignores shell = false', async (t) => { - const proc = spawk.spawn('sh', ['-c', 'open https://google.com'], { shell: false }) + const proc = spawk.spawn('sh', ['-c', 'open https://google.com'], { + shell: false, + }) - const result = await promiseSpawn.open('https://google.com', { shell: false }) + const result = await promiseSpawn.open('https://google.com', { + shell: false, + }) t.hasStrict(result, { code: 0, signal: undefined, @@ -97,9 +126,13 @@ t.test('process.platform === darwin', (t) => { }) t.test('respects opts.command', async (t) => { - const proc = spawk.spawn('sh', ['-c', 'browser https://google.com'], { shell: false }) + const proc = spawk.spawn('sh', ['-c', 'browser https://google.com'], { + shell: false, + }) - const result = await promiseSpawn.open('https://google.com', { command: 'browser' }) + const result = await promiseSpawn.open('https://google.com', { + command: 'browser', + }) t.hasStrict(result, { code: 0, signal: undefined, @@ -113,13 +146,18 @@ t.test('process.platform === darwin', (t) => { t.test('process.platform === linux', (t) => { const platformDesc = Object.getOwnPropertyDescriptor(process, 'platform') - Object.defineProperty(process, 'platform', { ...platformDesc, value: 'linux' }) + Object.defineProperty(process, 'platform', { + ...platformDesc, + value: 'linux', + }) t.teardown(() => { Object.defineProperty(process, 'platform', platformDesc) }) t.test('uses xdg-open in a shell', async (t) => { - const proc = spawk.spawn('sh', ['-c', 'xdg-open https://google.com'], { shell: false }) + const proc = spawk.spawn('sh', ['-c', 'xdg-open https://google.com'], { + shell: false, + }) const result = await promiseSpawn.open('https://google.com') t.hasStrict(result, { @@ -131,9 +169,13 @@ t.test('process.platform === linux', (t) => { }) t.test('ignores shell = false', async (t) => { - const proc = spawk.spawn('sh', ['-c', 'xdg-open https://google.com'], { shell: false }) + const proc = spawk.spawn('sh', ['-c', 'xdg-open https://google.com'], { + shell: false, + }) - const result = await promiseSpawn.open('https://google.com', { shell: false }) + const result = await promiseSpawn.open('https://google.com', { + shell: false, + }) t.hasStrict(result, { code: 0, signal: undefined, @@ -143,9 +185,13 @@ t.test('process.platform === linux', (t) => { }) t.test('respects opts.command', async (t) => { - const proc = spawk.spawn('sh', ['-c', 'browser https://google.com'], { shell: false }) + const proc = spawk.spawn('sh', ['-c', 'browser https://google.com'], { + shell: false, + }) - const result = await promiseSpawn.open('https://google.com', { command: 'browser' }) + const result = await promiseSpawn.open('https://google.com', { + command: 'browser', + }) t.hasStrict(result, { code: 0, signal: undefined, @@ -154,37 +200,53 @@ t.test('process.platform === linux', (t) => { t.ok(proc.called) }) - t.test('when os.release() includes Microsoft treats as win32', async (t) => { - const comSpec = process.env.ComSpec - process.env.ComSpec = 'C:\\Windows\\System32\\cmd.exe' + t.test('uses cmd.exe with WSL for Microsoft', async (t) => { + const comSpec = process.env.ComSpec; + process.env.ComSpec = 'C:\\Windows\\System32\\cmd.exe'; t.teardown(() => { - process.env.ComSPec = comSpec - }) - + process.env.ComSpec = comSpec; + }); + const promiseSpawnMock = t.mock('../lib/index.js', { os: { release: () => 'Microsoft', }, - }) - - const proc = spawk.spawn('C:\\Windows\\System32\\cmd.exe', - ['/d', '/s', '/c', 'start "" https://google.com'], - { shell: false }) - - const result = await promiseSpawnMock.open('https://google.com') + }); + + // Add logging + const originalSpawnWithShell = promiseSpawnMock.spawnWithShell; + promiseSpawnMock.spawnWithShell = (command, args, options, extra) => { + console.log('spawnWithShell called with:', { + command, + args, + options, + extra, + }); + return originalSpawnWithShell(command, args, options, extra); + }; + + const proc = spawk.spawn( + 'sh', + ['-c', 'C:\\Windows\\System32\\cmd.exe /c start "" https://google.com'], + { shell: false } + ); + + const result = await promiseSpawnMock.open('https://google.com'); + console.log('open result:', result); + t.hasStrict(result, { code: 0, signal: undefined, - }) - - t.ok(proc.called) - }) - + }); + + t.ok(proc.called); + }); + t.test('when os.release() includes microsoft treats as win32', async (t) => { const comSpec = process.env.ComSpec process.env.ComSpec = 'C:\\Windows\\System32\\cmd.exe' t.teardown(() => { - process.env.ComSPec = comSpec + process.env.ComSpec = comSpec }) const promiseSpawnMock = t.mock('../lib/index.js', { @@ -193,9 +255,12 @@ t.test('process.platform === linux', (t) => { }, }) - const proc = spawk.spawn('C:\\Windows\\System32\\cmd.exe', - ['/d', '/s', '/c', 'start "" https://google.com'], - { shell: false }) + // Adjust the expected command to match what is executed in WSL + const proc = spawk.spawn( + 'sh', + ['-c', 'C:\\Windows\\System32\\cmd.exe /c start "" https://google.com'], + { shell: false } + ) const result = await promiseSpawnMock.open('https://google.com') t.hasStrict(result, { @@ -209,16 +274,21 @@ t.test('process.platform === linux', (t) => { t.end() }) -// this covers anything that is not win32, darwin or linux +// this covers anything that is not win32, darwin, or linux t.test('process.platform === freebsd', (t) => { const platformDesc = Object.getOwnPropertyDescriptor(process, 'platform') - Object.defineProperty(process, 'platform', { ...platformDesc, value: 'freebsd' }) + Object.defineProperty(process, 'platform', { + ...platformDesc, + value: 'freebsd', + }) t.teardown(() => { Object.defineProperty(process, 'platform', platformDesc) }) t.test('uses xdg-open with a shell', async (t) => { - const proc = spawk.spawn('sh', ['-c', 'xdg-open https://google.com'], { shell: false }) + const proc = spawk.spawn('sh', ['-c', 'xdg-open https://google.com'], { + shell: false, + }) const result = await promiseSpawn.open('https://google.com') t.hasStrict(result, { @@ -230,9 +300,13 @@ t.test('process.platform === freebsd', (t) => { }) t.test('ignores shell = false', async (t) => { - const proc = spawk.spawn('sh', ['-c', 'xdg-open https://google.com'], { shell: false }) + const proc = spawk.spawn('sh', ['-c', 'xdg-open https://google.com'], { + shell: false, + }) - const result = await promiseSpawn.open('https://google.com', { shell: false }) + const result = await promiseSpawn.open('https://google.com', { + shell: false, + }) t.hasStrict(result, { code: 0, signal: undefined, @@ -242,9 +316,13 @@ t.test('process.platform === freebsd', (t) => { }) t.test('respects opts.command', async (t) => { - const proc = spawk.spawn('sh', ['-c', 'browser https://google.com'], { shell: false }) + const proc = spawk.spawn('sh', ['-c', 'browser https://google.com'], { + shell: false, + }) - const result = await promiseSpawn.open('https://google.com', { command: 'browser' }) + const result = await promiseSpawn.open('https://google.com', { + command: 'browser', + }) t.hasStrict(result, { code: 0, signal: undefined,