|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import fs from 'node:fs'; |
| 3 | +import path from 'node:path'; |
| 4 | +import { afterEach, beforeEach, test, vi } from 'vitest'; |
| 5 | +import { resetAllProcessMemosForTests } from '@agent-device/kernel/ttl-memo'; |
| 6 | +import { appleRunnerTestHost } from '../test-host.ts'; |
| 7 | +import type { ExecResult } from '@agent-device/host-kit/command'; |
| 8 | +import { ensureXctestrunArtifact } from '../runner-xctestrun.ts'; |
| 9 | +import { appleToolchainProbeResult } from './apple-toolchain-fixtures.ts'; |
| 10 | +import { IOS_SIMULATOR } from './device-fixtures.ts'; |
| 11 | +import { seedRunnerProductBundle } from './runner-xctestrun.fixtures.ts'; |
| 12 | +import { mkdtempForTestSync } from './tmp-dir.ts'; |
| 13 | + |
| 14 | +const XCTESTRUN_NAME = |
| 15 | + 'AgentDeviceRunner_AgentDeviceRunnerUITests_iphonesimulator27.0-arm64.xctestrun'; |
| 16 | + |
| 17 | +const runCmdStreaming = vi.fn(); |
| 18 | +let projectRoot: string; |
| 19 | +let derived: string; |
| 20 | +let customBuildLocation: string; |
| 21 | + |
| 22 | +beforeEach(() => { |
| 23 | + resetAllProcessMemosForTests(); |
| 24 | + projectRoot = mkdtempForTestSync('agent-device-runner-location-root-'); |
| 25 | + fs.mkdirSync( |
| 26 | + path.join(projectRoot, 'apple', 'runner', 'AgentDeviceRunner', 'AgentDeviceRunner.xcodeproj'), |
| 27 | + { recursive: true }, |
| 28 | + ); |
| 29 | + derived = mkdtempForTestSync('agent-device-runner-location-derived-'); |
| 30 | + customBuildLocation = mkdtempForTestSync('agent-device-runner-location-custom-'); |
| 31 | + process.env.AGENT_DEVICE_IOS_RUNNER_DERIVED_PATH = derived; |
| 32 | + runCmdStreaming.mockReset().mockImplementation(xcodebuildHonoringCustomBuildLocation); |
| 33 | + appleRunnerTestHost.update({ |
| 34 | + runCmdSync: vi.fn().mockImplementation(appleToolchainProbeResult), |
| 35 | + runCmdStreaming, |
| 36 | + findProjectRoot: () => projectRoot, |
| 37 | + readVersion: () => '0.0.0-test', |
| 38 | + }); |
| 39 | +}); |
| 40 | + |
| 41 | +afterEach(() => { |
| 42 | + delete process.env.AGENT_DEVICE_IOS_RUNNER_DERIVED_PATH; |
| 43 | +}); |
| 44 | + |
| 45 | +test('a custom Xcode build location cannot move the built xctestrun out of the runner cache', async () => { |
| 46 | + const built = await ensureXctestrunArtifact(IOS_SIMULATOR, {}); |
| 47 | + |
| 48 | + assert.equal(built.artifact, 'rebuilt'); |
| 49 | + assert.equal(built.xctestrunPath, path.join(derived, 'Build', 'Products', XCTESTRUN_NAME)); |
| 50 | + assert.deepEqual(fs.readdirSync(customBuildLocation), []); |
| 51 | + |
| 52 | + const reused = await ensureXctestrunArtifact(IOS_SIMULATOR, {}); |
| 53 | + |
| 54 | + assert.equal(reused.artifact, 'valid'); |
| 55 | + assert.equal(reused.xctestrunPath, built.xctestrunPath); |
| 56 | + assert.equal(runCmdStreaming.mock.calls.length, 1); |
| 57 | +}); |
| 58 | + |
| 59 | +/** |
| 60 | + * Stands in for `xcodebuild build-for-testing` on a host whose Xcode settings set a custom build |
| 61 | + * location: products land there unless the command line pins `SYMROOT`. |
| 62 | + */ |
| 63 | +async function xcodebuildHonoringCustomBuildLocation( |
| 64 | + _command: string, |
| 65 | + args: string[], |
| 66 | +): Promise<ExecResult> { |
| 67 | + const symroot = args.find((arg) => arg.startsWith('SYMROOT='))?.slice('SYMROOT='.length); |
| 68 | + const productsRoot = symroot ?? customBuildLocation; |
| 69 | + await seedRunnerProductBundle( |
| 70 | + path.join(productsRoot, 'Debug-iphonesimulator', 'AgentDeviceRunner.app'), |
| 71 | + ); |
| 72 | + fs.writeFileSync( |
| 73 | + path.join(productsRoot, XCTESTRUN_NAME), |
| 74 | + `<?xml version="1.0" encoding="UTF-8"?> |
| 75 | +<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 76 | +<plist version="1.0"> |
| 77 | +<dict> |
| 78 | + <key>ProjectRootHint</key> |
| 79 | + <string>${projectRoot}</string> |
| 80 | + <key>ProductPaths</key> |
| 81 | + <array> |
| 82 | + <string>__TESTROOT__/Debug-iphonesimulator/AgentDeviceRunner.app</string> |
| 83 | + </array> |
| 84 | +</dict> |
| 85 | +</plist>`, |
| 86 | + ); |
| 87 | + return { exitCode: 0, stdout: '', stderr: '' }; |
| 88 | +} |
0 commit comments