// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. // See LICENSE in the project root for license information. import { spawn, spawnSync } from 'node:child_process'; import { once } from 'node:events'; export const canRunPtyTests: boolean = process.platform === 'linux' && spawnSync('python3', ['-c', 'import pty'], { stdio: 'ignore' }).status === 0; interface IPtyResult { readonly exitCode: number; readonly output: string; readonly restored: boolean; } const PYTHON_DRIVER: string = ` import errno,fcntl,json,os,pty,select,signal,struct,subprocess,sys,termios,time master,slave=pty.openpty() fcntl.ioctl(slave,termios.TIOCSWINSZ,struct.pack('HHHH',24,123,0,0)) def attach_terminal(): os.setsid() fcntl.ioctl(1,termios.TIOCSCTTY,0) child=subprocess.Popen(sys.argv[2:]+['-q','tty'], stdin=slave if sys.argv[1]=='tty' else subprocess.DEVNULL,stdout=slave,stderr=slave, close_fds=True,preexec_fn=attach_terminal) output=b'' signaled=False try: deadline=time.monotonic()+15 while time.monotonic() { const child = spawn( 'python3', ['-c', PYTHON_DRIVER, stdinIsTTY ? 'tty' : 'pipe', process.execPath, entry], { cwd, env: environment, stdio: ['ignore', 'pipe', 'pipe'] } ); let stdout: string = ''; let stderr: string = ''; child.stdout.on('data', (bytes: Buffer) => { stdout += bytes.toString(); }); child.stderr.on('data', (bytes: Buffer) => { stderr += bytes.toString(); }); const [code] = await once(child, 'close'); if (code !== 0) throw new Error(`PTY driver failed (${code}): ${stderr}`); return JSON.parse(stdout); }