|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import fs from 'node:fs'; |
| 3 | +import os from 'node:os'; |
| 4 | +import path from 'node:path'; |
| 5 | +import { test } from 'node:test'; |
| 6 | +import { fileURLToPath } from 'node:url'; |
| 7 | +import { runCmd, runCmdBackground } from '../src/utils/exec.ts'; |
| 8 | + |
| 9 | +const REPOSITORY_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..'); |
| 10 | +const WRAPPER = path.join(REPOSITORY_ROOT, 'scripts', 'swift-toolchain-tmpdir.ts'); |
| 11 | + |
| 12 | +async function runProbe(exitCode: number): Promise<{ |
| 13 | + childTmpDir: string; |
| 14 | + resultExitCode: number; |
| 15 | +}> { |
| 16 | + const evidenceRoot = fs.mkdtempSync( |
| 17 | + path.join(os.tmpdir(), 'swift-toolchain-tmpdir-lifecycle-test-'), |
| 18 | + ); |
| 19 | + const evidencePath = path.join(evidenceRoot, 'child-tmpdir.txt'); |
| 20 | + let childTmpDir: string | undefined; |
| 21 | + |
| 22 | + try { |
| 23 | + const probe = ` |
| 24 | +const fs = require('node:fs'); |
| 25 | +const path = require('node:path'); |
| 26 | +fs.writeFileSync(${JSON.stringify(evidencePath)}, process.env.TMPDIR); |
| 27 | +const leaked = path.join(process.env.TMPDIR, 'TemporaryDirectory.probe'); |
| 28 | +fs.mkdirSync(leaked); |
| 29 | +fs.writeFileSync(path.join(leaked, '.keep-directory'), ''); |
| 30 | +process.exit(${exitCode}); |
| 31 | +`; |
| 32 | + const result = await runCmd( |
| 33 | + process.execPath, |
| 34 | + ['--experimental-strip-types', WRAPPER, process.execPath, '-e', probe], |
| 35 | + { |
| 36 | + cwd: REPOSITORY_ROOT, |
| 37 | + timeoutMs: 30_000, |
| 38 | + allowFailure: true, |
| 39 | + }, |
| 40 | + ); |
| 41 | + |
| 42 | + childTmpDir = fs.readFileSync(evidencePath, 'utf8'); |
| 43 | + assert.match(path.basename(childTmpDir), /^agent-device-swift-toolchain-/); |
| 44 | + assert.equal(fs.existsSync(childTmpDir), false, `wrapper left behind: ${childTmpDir}`); |
| 45 | + return { childTmpDir, resultExitCode: result.exitCode }; |
| 46 | + } finally { |
| 47 | + if (childTmpDir) fs.rmSync(childTmpDir, { recursive: true, force: true }); |
| 48 | + fs.rmSync(evidenceRoot, { recursive: true, force: true }); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +test('the Swift toolchain wrapper removes its TMPDIR after success', async () => { |
| 53 | + const result = await runProbe(0); |
| 54 | + assert.equal(result.resultExitCode, 0); |
| 55 | +}); |
| 56 | + |
| 57 | +test('the Swift toolchain wrapper cleans up and forwards a failure', async () => { |
| 58 | + const result = await runProbe(17); |
| 59 | + assert.equal(result.resultExitCode, 17); |
| 60 | +}); |
| 61 | + |
| 62 | +test('the Swift toolchain wrapper keeps TMPDIR until a signaled child exits', async () => { |
| 63 | + const evidenceRoot = fs.mkdtempSync( |
| 64 | + path.join(os.tmpdir(), 'swift-toolchain-tmpdir-signal-test-'), |
| 65 | + ); |
| 66 | + const readyPath = path.join(evidenceRoot, 'ready.json'); |
| 67 | + const shutdownPath = path.join(evidenceRoot, 'shutdown.json'); |
| 68 | + let childTmpDir: string | undefined; |
| 69 | + |
| 70 | + try { |
| 71 | + const probe = ` |
| 72 | +const fs = require('node:fs'); |
| 73 | +fs.writeFileSync(${JSON.stringify(readyPath)}, JSON.stringify({ tmpdir: process.env.TMPDIR })); |
| 74 | +process.on('SIGTERM', () => { |
| 75 | + setTimeout(() => { |
| 76 | + fs.writeFileSync( |
| 77 | + ${JSON.stringify(shutdownPath)}, |
| 78 | + JSON.stringify({ tmpdirExisted: fs.existsSync(process.env.TMPDIR) }), |
| 79 | + ); |
| 80 | + process.exit(0); |
| 81 | + }, 200); |
| 82 | +}); |
| 83 | +setInterval(() => {}, 1_000); |
| 84 | +`; |
| 85 | + const background = runCmdBackground( |
| 86 | + process.execPath, |
| 87 | + ['--experimental-strip-types', WRAPPER, process.execPath, '-e', probe], |
| 88 | + { |
| 89 | + cwd: REPOSITORY_ROOT, |
| 90 | + allowFailure: true, |
| 91 | + }, |
| 92 | + ); |
| 93 | + |
| 94 | + await waitForFile(readyPath); |
| 95 | + childTmpDir = (JSON.parse(fs.readFileSync(readyPath, 'utf8')) as { tmpdir: string }).tmpdir; |
| 96 | + background.child.kill('SIGTERM'); |
| 97 | + |
| 98 | + const result = await background.wait; |
| 99 | + assert.equal(result.exitCode, 143); |
| 100 | + assert.equal( |
| 101 | + (JSON.parse(fs.readFileSync(shutdownPath, 'utf8')) as { tmpdirExisted: boolean }) |
| 102 | + .tmpdirExisted, |
| 103 | + true, |
| 104 | + 'the child must retain TMPDIR until its delayed shutdown completes', |
| 105 | + ); |
| 106 | + assert.equal(fs.existsSync(childTmpDir), false, `wrapper left behind: ${childTmpDir}`); |
| 107 | + } finally { |
| 108 | + if (childTmpDir) fs.rmSync(childTmpDir, { recursive: true, force: true }); |
| 109 | + fs.rmSync(evidenceRoot, { recursive: true, force: true }); |
| 110 | + } |
| 111 | +}); |
| 112 | + |
| 113 | +test( |
| 114 | + 'the Swift toolchain wrapper keeps TMPDIR until signaled descendants exit', |
| 115 | + { skip: process.platform === 'win32' }, |
| 116 | + async () => { |
| 117 | + const evidenceRoot = fs.mkdtempSync( |
| 118 | + path.join(os.tmpdir(), 'swift-toolchain-tmpdir-descendant-test-'), |
| 119 | + ); |
| 120 | + const readyPath = path.join(evidenceRoot, 'ready.json'); |
| 121 | + const shutdownPath = path.join(evidenceRoot, 'shutdown.json'); |
| 122 | + let childTmpDir: string | undefined; |
| 123 | + let descendantPid: number | undefined; |
| 124 | + |
| 125 | + try { |
| 126 | + const descendantProbe = ` |
| 127 | +const fs = require('node:fs'); |
| 128 | +fs.writeFileSync( |
| 129 | + ${JSON.stringify(readyPath)}, |
| 130 | + JSON.stringify({ pid: process.pid, tmpdir: process.env.TMPDIR }), |
| 131 | +); |
| 132 | +process.on('SIGTERM', () => { |
| 133 | + setTimeout(() => { |
| 134 | + fs.writeFileSync( |
| 135 | + ${JSON.stringify(shutdownPath)}, |
| 136 | + JSON.stringify({ tmpdirExisted: fs.existsSync(process.env.TMPDIR) }), |
| 137 | + ); |
| 138 | + process.exit(0); |
| 139 | + }, 200); |
| 140 | +}); |
| 141 | +setInterval(() => {}, 1_000); |
| 142 | +`; |
| 143 | + const directChildProbe = ` |
| 144 | +const { spawn } = require('node:child_process'); |
| 145 | +process.on('SIGTERM', () => process.exit(0)); |
| 146 | +const descendant = spawn(process.execPath, ['-e', ${JSON.stringify(descendantProbe)}], { |
| 147 | + env: process.env, |
| 148 | + stdio: 'ignore', |
| 149 | +}); |
| 150 | +descendant.unref(); |
| 151 | +setInterval(() => {}, 1_000); |
| 152 | +`; |
| 153 | + const background = runCmdBackground( |
| 154 | + process.execPath, |
| 155 | + ['--experimental-strip-types', WRAPPER, process.execPath, '-e', directChildProbe], |
| 156 | + { |
| 157 | + cwd: REPOSITORY_ROOT, |
| 158 | + allowFailure: true, |
| 159 | + }, |
| 160 | + ); |
| 161 | + |
| 162 | + await waitForFile(readyPath); |
| 163 | + const ready = JSON.parse(fs.readFileSync(readyPath, 'utf8')) as { |
| 164 | + pid: number; |
| 165 | + tmpdir: string; |
| 166 | + }; |
| 167 | + descendantPid = ready.pid; |
| 168 | + childTmpDir = ready.tmpdir; |
| 169 | + background.child.kill('SIGTERM'); |
| 170 | + |
| 171 | + const result = await background.wait; |
| 172 | + assert.equal(result.exitCode, 143); |
| 173 | + if (!fs.existsSync(shutdownPath)) process.kill(descendantPid, 'SIGTERM'); |
| 174 | + await waitForFile(shutdownPath); |
| 175 | + assert.equal( |
| 176 | + (JSON.parse(fs.readFileSync(shutdownPath, 'utf8')) as { tmpdirExisted: boolean }) |
| 177 | + .tmpdirExisted, |
| 178 | + true, |
| 179 | + 'a delayed descendant must retain TMPDIR until its shutdown completes', |
| 180 | + ); |
| 181 | + assert.equal(fs.existsSync(childTmpDir), false, `wrapper left behind: ${childTmpDir}`); |
| 182 | + } finally { |
| 183 | + if (descendantPid) { |
| 184 | + try { |
| 185 | + process.kill(descendantPid, 'SIGKILL'); |
| 186 | + } catch {} |
| 187 | + } |
| 188 | + if (childTmpDir) fs.rmSync(childTmpDir, { recursive: true, force: true }); |
| 189 | + fs.rmSync(evidenceRoot, { recursive: true, force: true }); |
| 190 | + } |
| 191 | + }, |
| 192 | +); |
| 193 | + |
| 194 | +test('Apple build lanes route toolchain commands through the cleanup wrapper', () => { |
| 195 | + const manifest = JSON.parse( |
| 196 | + fs.readFileSync(path.join(REPOSITORY_ROOT, 'package.json'), 'utf8'), |
| 197 | + ) as { scripts?: Record<string, string> }; |
| 198 | + const scripts = manifest.scripts ?? {}; |
| 199 | + assert.match(scripts['build:macos-helper'] ?? '', /swift-toolchain-tmpdir\.ts.*swift build/); |
| 200 | + assert.match( |
| 201 | + scripts['build:macos-helper:clean'] ?? '', |
| 202 | + /swift-toolchain-tmpdir\.ts.*swift package/, |
| 203 | + ); |
| 204 | + |
| 205 | + const xcodeBuildScript = fs.readFileSync( |
| 206 | + path.join(REPOSITORY_ROOT, 'scripts', 'build-xcuitest-apple.sh'), |
| 207 | + 'utf8', |
| 208 | + ); |
| 209 | + assert.match(xcodeBuildScript, /swift-toolchain-tmpdir\.ts xcodebuild build-for-testing/); |
| 210 | +}); |
| 211 | + |
| 212 | +async function waitForFile(filePath: string): Promise<void> { |
| 213 | + for (let attempt = 0; attempt < 100; attempt += 1) { |
| 214 | + if (fs.existsSync(filePath)) return; |
| 215 | + await new Promise((resolve) => setTimeout(resolve, 10)); |
| 216 | + } |
| 217 | + throw new Error(`Timed out waiting for ${filePath}`); |
| 218 | +} |
0 commit comments