| 1 | import { spawn } from 'node:child_process'; |
| 2 | import { readFileSync } |
| 3 | import path from 'node:path |
| 4 | |
| 5 | import { capturing } |
| 6 | |
| 7 | /** The hostname the gh suite serves on port 443; set in CI, where /etc/hosts maps it. The suite is skipped without it. */ |
| 8 | export const GH_E2E_HOST = |
| 9 | |
| 10 | const certDir = path |
| 11 | const certPath = path |
| 12 | |
| 13 | const ghEnv = ( |
| 14 | PATH: process.env |
| 15 | HOME: configDir, |
| 16 | GH_CONFIG_DIR: configDir, |
| 17 | GH_HOST: GH_E2E_HOST ?? |
| 18 | GH_PROMPT_DISABLED: '1', |
| 19 | GH_NO_UPDATE_NOTIFIER: '1', |
| 20 | NO_COLOR: '1', |
| 21 | GH_BROWSER: 'true', |
| 22 | // gh and git trust the suite's self-signed certificate. |
| 23 | SSL_CERT_FILE: certPath, |
| 24 | GIT_SSL_CAINFO: certPath, |
| 25 | ...(token && |
| 26 | }); |
| 27 | |
| 28 | export function tlsFiles() |
| 29 | return { |
| 30 | key: readFileSync(path |
| 31 | cert: readFileSync(certPath |
| 32 | certPath, |
| 33 | }; |
| 34 | } |
| 35 | |
| 36 | /** Runs gh against the e2e host with its own config dir. Async because the server runs in this process. */ |
| 37 | export function runGh( |
| 38 | args: string[] |
| 39 | { |
| 40 | configDir, |
| 41 | token, |
| 42 | input, |
| 43 | cwd, |
| 44 | }: { configDir |
| 45 | ) { |
| 46 | const stopCapturing = |
| 47 | const child = |
| 48 | child.on('close |
| 49 | if (input !== |
| 50 | else child.stdin. |
| 51 | |
| 52 | let stdout = |
| 53 | let stderr = |
| 54 | child.stdout.on |
| 55 | child.stderr.on |
| 56 | return new Promise |
| 57 | (resolve) => |
| 58 | child.on('close |
| 59 | resolve({ stdout, |
| 60 | ), |
| 61 | ); |
| 62 | } |
| 63 | |
| 64 | export function startGh( |
| 65 | args: string[] |
| 66 | { configDir, token |
| 67 | ) { |
| 68 | const stopCapturing = |
| 69 | const child = |
| 70 | child.on('close |
| 71 | child.stdin.end |
| 72 | let stdout = |
| 73 | let stderr = |
| 74 | const waiters: |
| 75 | pattern: RegExp |
| 76 | resolve: (match |
| 77 | reject: (error |
| 78 | }> = [] |
| 79 | child.stdout.on |
| 80 | child.stderr.on |
| 81 | stderr += chunk; |
| 82 | for (const waiter |
| 83 | const match = |
| 84 | if (match) { |
| 85 | waiters.splice(waiters |
| 86 | waiter.resolve(match |
| 87 | } |
| 88 | } |
| 89 | }); |
| 90 | const done = |
| 91 | (resolve) => |
| 92 | child.on('close |
| 93 | // gh exited before printing what a waiter expects: fail with its output instead of timing out. |
| 94 | for (const waiter |
| 95 | waiter.reject( |
| 96 | new Error(`gh exited |
| 97 | ); |
| 98 | resolve({ stdout, |
| 99 | }), |
| 100 | ); |
| 101 | return { |
| 102 | stderrUntil: (pattern: |
| 103 | new Promise<RegExpMatchArray |
| 104 | const match = |
| 105 | if (match) resolve |
| 106 | else if (child. |
| 107 | reject( |
| 108 | new Error( |
| 109 | `gh exited ${child.exitCode |
| 110 | ), |
| 111 | ); |
| 112 | else waiters.push( |
| 113 | }), |
| 114 | done, |
| 115 | }; |
| 116 | } |