|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +import { afterEach, describe, expect, it, vi } from 'vitest' |
| 4 | +import fs from 'node:fs' |
| 5 | +import path from 'node:path' |
| 6 | +import os from 'node:os' |
| 7 | +import { execAsync } from './exec' |
| 8 | + |
| 9 | +const { traceInfoMock } = vi.hoisted(() => ({ traceInfoMock: vi.fn() })) |
| 10 | + |
| 11 | +vi.mock('./common/log', () => ({ traceInfo: traceInfoMock })) |
| 12 | + |
| 13 | +/** Create a temporary directory for a test executable. */ |
| 14 | +function makeTmpDir(): string { |
| 15 | + return fs.mkdtempSync(path.join(os.tmpdir(), 'exec-test-')) |
| 16 | +} |
| 17 | + |
| 18 | +describe('execAsync', () => { |
| 19 | + const tmpdirs: string[] = [] |
| 20 | + |
| 21 | + afterEach(() => { |
| 22 | + traceInfoMock.mockClear() |
| 23 | + for (const dir of tmpdirs.splice(0)) { |
| 24 | + fs.rmSync(dir, { recursive: true, force: true }) |
| 25 | + } |
| 26 | + }) |
| 27 | + |
| 28 | + it('executes an executable whose path contains a space', async () => { |
| 29 | + // Create a temp directory whose name contains a space. |
| 30 | + const baseDir = makeTmpDir() |
| 31 | + tmpdirs.push(baseDir) |
| 32 | + const spacedDir = path.join(baseDir, 'dir with spaces') |
| 33 | + fs.mkdirSync(spacedDir) |
| 34 | + |
| 35 | + // Copy the current Node.js binary into the spaced directory. |
| 36 | + const isWindows = process.platform === 'win32' |
| 37 | + const ext = isWindows ? '.exe' : '' |
| 38 | + const destExe = path.join(spacedDir, `node${ext}`) |
| 39 | + fs.copyFileSync(process.execPath, destExe) |
| 40 | + |
| 41 | + // Ensure the copied binary is executable on POSIX. |
| 42 | + if (!isWindows) { |
| 43 | + fs.chmodSync(destExe, 0o755) |
| 44 | + } |
| 45 | + |
| 46 | + // The path must contain a space for this test to be meaningful. |
| 47 | + expect(destExe).toContain(' ') |
| 48 | + |
| 49 | + const result = await execAsync(destExe, ['--version']) |
| 50 | + |
| 51 | + expect(result.exitCode).toBe(0) |
| 52 | + expect(result.stderr).toBe('') |
| 53 | + expect(result.stdout.trim()).toBe(process.version) |
| 54 | + }) |
| 55 | + |
| 56 | + it('passes arguments with spaces and shell metacharacters literally', async () => { |
| 57 | + const argument = 'value with spaces; $(not-a-command) & more' |
| 58 | + const result = await execAsync(process.execPath, [ |
| 59 | + '-e', |
| 60 | + 'process.stdout.write(process.argv[1])', |
| 61 | + argument, |
| 62 | + ]) |
| 63 | + |
| 64 | + expect(result.exitCode).toBe(0) |
| 65 | + expect(result.stdout).toBe(argument) |
| 66 | + expect(result.stderr).toBe('') |
| 67 | + }) |
| 68 | + |
| 69 | + it('logs an unambiguous argv representation', async () => { |
| 70 | + const argument = 'value with spaces\nand "quotes"' |
| 71 | + |
| 72 | + await execAsync(process.execPath, ['-e', '', argument]) |
| 73 | + |
| 74 | + expect(traceInfoMock).toHaveBeenCalledWith( |
| 75 | + `Executing command: ${JSON.stringify([ |
| 76 | + process.execPath, |
| 77 | + '-e', |
| 78 | + '', |
| 79 | + argument, |
| 80 | + ])} in undefined`, |
| 81 | + ) |
| 82 | + }) |
| 83 | + |
| 84 | + it('resolves with a nonzero ExecResult instead of rejecting', async () => { |
| 85 | + // Run a Node.js one-liner that writes to stdout/stderr and exits nonzero. |
| 86 | + const result = await execAsync(process.execPath, [ |
| 87 | + '-e', |
| 88 | + "process.stdout.write('out'); process.stderr.write('err'); process.exit(42)", |
| 89 | + ]) |
| 90 | + |
| 91 | + expect(result.exitCode).toBe(42) |
| 92 | + expect(result.stdout).toBe('out') |
| 93 | + expect(result.stderr).toBe('err') |
| 94 | + }) |
| 95 | + |
| 96 | + it('preserves empty stderr for a nonzero exit with no child output', async () => { |
| 97 | + const result = await execAsync(process.execPath, ['-e', 'process.exit(7)']) |
| 98 | + |
| 99 | + expect(result.exitCode).toBe(7) |
| 100 | + expect(result.stdout).toBe('') |
| 101 | + expect(result.stderr).toBe('') |
| 102 | + }) |
| 103 | + |
| 104 | + it('resolves spawn failures with a useful nonzero ExecResult', async () => { |
| 105 | + const missingExecutable = path.join( |
| 106 | + os.tmpdir(), |
| 107 | + `missing-executable-${process.pid}-${Date.now()}`, |
| 108 | + ) |
| 109 | + expect(fs.existsSync(missingExecutable)).toBe(false) |
| 110 | + |
| 111 | + const result = await execAsync(missingExecutable) |
| 112 | + |
| 113 | + expect(result.exitCode).not.toBe(0) |
| 114 | + expect(result.stdout).toBe('') |
| 115 | + expect(result.stderr).toContain(path.basename(missingExecutable)) |
| 116 | + }) |
| 117 | + |
| 118 | + it('rejects with AbortError when the signal is aborted', async () => { |
| 119 | + const controller = new AbortController() |
| 120 | + |
| 121 | + // Start a long-running child process. |
| 122 | + const promise = execAsync( |
| 123 | + process.execPath, |
| 124 | + ['-e', 'setTimeout(() => {}, 60_000)'], |
| 125 | + { signal: controller.signal }, |
| 126 | + ) |
| 127 | + |
| 128 | + // Abort immediately. |
| 129 | + controller.abort() |
| 130 | + |
| 131 | + await expect(promise).rejects.toMatchObject({ name: 'AbortError' }) |
| 132 | + }) |
| 133 | +}) |
0 commit comments