Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { afterEach, beforeEach, test, vi } from 'vitest';
import { resetAllProcessMemosForTests } from '@agent-device/kernel/ttl-memo';
import { appleRunnerTestHost } from '../test-host.ts';
import type { ExecResult } from '@agent-device/host-kit/command';
import { ensureXctestrunArtifact } from '../runner-xctestrun.ts';
import { appleToolchainProbeResult } from './apple-toolchain-fixtures.ts';
import { IOS_SIMULATOR } from './device-fixtures.ts';
import { seedRunnerProductBundle } from './runner-xctestrun.fixtures.ts';
import { mkdtempForTestSync } from './tmp-dir.ts';

const XCTESTRUN_NAME =
'AgentDeviceRunner_AgentDeviceRunnerUITests_iphonesimulator27.0-arm64.xctestrun';

const runCmdStreaming = vi.fn();
let projectRoot: string;
let derived: string;
let customBuildLocation: string;

beforeEach(() => {
resetAllProcessMemosForTests();
projectRoot = mkdtempForTestSync('agent-device-runner-location-root-');
fs.mkdirSync(
path.join(projectRoot, 'apple', 'runner', 'AgentDeviceRunner', 'AgentDeviceRunner.xcodeproj'),
{ recursive: true },
);
derived = mkdtempForTestSync('agent-device-runner-location-derived-');
customBuildLocation = mkdtempForTestSync('agent-device-runner-location-custom-');
process.env.AGENT_DEVICE_IOS_RUNNER_DERIVED_PATH = derived;
runCmdStreaming.mockReset().mockImplementation(xcodebuildHonoringCustomBuildLocation);
appleRunnerTestHost.update({
runCmdSync: vi.fn().mockImplementation(appleToolchainProbeResult),
runCmdStreaming,
findProjectRoot: () => projectRoot,
readVersion: () => '0.0.0-test',
});
});

afterEach(() => {
delete process.env.AGENT_DEVICE_IOS_RUNNER_DERIVED_PATH;
});

test('a custom Xcode build location cannot move the built xctestrun out of the runner cache', async () => {
const built = await ensureXctestrunArtifact(IOS_SIMULATOR, {});

assert.equal(built.artifact, 'rebuilt');
assert.equal(built.xctestrunPath, path.join(derived, 'Build', 'Products', XCTESTRUN_NAME));
assert.deepEqual(fs.readdirSync(customBuildLocation), []);

const reused = await ensureXctestrunArtifact(IOS_SIMULATOR, {});

assert.equal(reused.artifact, 'valid');
assert.equal(reused.xctestrunPath, built.xctestrunPath);
assert.equal(runCmdStreaming.mock.calls.length, 1);
});

/**
* Stands in for `xcodebuild build-for-testing` on a host whose Xcode settings set a custom build
* location: products land there unless the command line pins `SYMROOT`.
*/
async function xcodebuildHonoringCustomBuildLocation(
_command: string,
args: string[],
): Promise<ExecResult> {
const symroot = args.find((arg) => arg.startsWith('SYMROOT='))?.slice('SYMROOT='.length);
const productsRoot = symroot ?? customBuildLocation;
await seedRunnerProductBundle(
path.join(productsRoot, 'Debug-iphonesimulator', 'AgentDeviceRunner.app'),
);
fs.writeFileSync(
path.join(productsRoot, XCTESTRUN_NAME),
`<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>ProjectRootHint</key>
<string>${projectRoot}</string>
<key>ProductPaths</key>
<array>
<string>__TESTROOT__/Debug-iphonesimulator/AgentDeviceRunner.app</string>
</array>
</dict>
</plist>`,
);
return { exitCode: 0, stdout: '', stderr: '' };
}
2 changes: 2 additions & 0 deletions packages/platform-apple/src/runner/runner-artifact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
requireRunnerPhaseRemainingMs,
resolveExpectedRunnerCacheMetadata,
resolveRunnerArchBuildSettings,
resolveRunnerBuildLocationSettings,
resolveRunnerBundleBuildSettings,
resolveRunnerDerivedPath,
resolveRunnerMaxConcurrentDestinationsFlag,
Expand Down Expand Up @@ -470,6 +471,7 @@ async function buildRunnerXctestrun(
...xcodebuildDestinationArgs(device, resolveRunnerBuildDestination(device)),
'-derivedDataPath',
derived,
...resolveRunnerBuildLocationSettings(derived),
...performanceBuildSettings,
...archBuildSettings,
...sandboxBuildArgs,
Expand Down
14 changes: 14 additions & 0 deletions packages/platform-apple/src/runner/runner-cache-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,20 @@ export function resolveRunnerArchBuildSettings(env: NodeJS.ProcessEnv = process.
return archs ? [`ARCHS=${archs}`] : [];
}

/**
* Pins the build roots to the default layout under `derived`. `-derivedDataPath` alone does not:
* a custom or legacy build location in the user's Xcode settings still redirects products and
* intermediates, so the `.xctestrun` would land outside the cache directory.
*/
export function resolveRunnerBuildLocationSettings(derived: string): string[] {
const intermediates = path.join(derived, 'Build', 'Intermediates.noindex');
return [
`SYMROOT=${path.join(derived, 'Build', 'Products')}`,
`OBJROOT=${intermediates}`,
`SHARED_PRECOMPS_DIR=${path.join(intermediates, 'PrecompiledHeaders')}`,
];
}

export function resolveRunnerSandboxBuildArgs(): string[] {
return [
...RUNNER_SANDBOX_BUILD_ARGS,
Expand Down
1 change: 1 addition & 0 deletions packages/platform-apple/src/runner/runner-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export {
requireRunnerPhaseRemainingMs,
resolveExpectedRunnerCacheMetadata,
resolveRunnerArchBuildSettings,
resolveRunnerBuildLocationSettings,
resolveRunnerBundleBuildSettings,
resolveRunnerDerivedPath,
resolveRunnerMaxConcurrentDestinationsFlag,
Expand Down
15 changes: 15 additions & 0 deletions scripts/__tests__/runner-isolation-diagnostics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,18 @@ describe('scripts/build-xcuitest-apple.sh isolation scan', () => {
expect(result.stderr).not.toMatch(/runner isolation scan/);
});
});

describe('scripts/build-xcuitest-apple.sh build location', () => {
test('pins every build root under the derived path so a custom Xcode build location cannot move products', () => {
const result = runBuildScript(log(...CANARY_DIAGNOSTICS), 65);
const args = fs.readFileSync(result.xcodebuildArgs, 'utf8').split('\n');
const intermediates = path.join(result.derived, 'Build', 'Intermediates.noindex');
expect(args).toEqual(
expect.arrayContaining([
`SYMROOT=${path.join(result.derived, 'Build', 'Products')}`,
`OBJROOT=${intermediates}`,
`SHARED_PRECOMPS_DIR=${path.join(intermediates, 'PrecompiledHeaders')}`,
]),
);
});
});
3 changes: 3 additions & 0 deletions scripts/build-xcuitest-apple.sh
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ build_for_testing() {
-scheme "$SCHEME" \
-destination "$DESTINATION" \
-derivedDataPath "$DERIVED_PATH" \
SYMROOT="$DERIVED_PATH/Build/Products" \
OBJROOT="$DERIVED_PATH/Build/Intermediates.noindex" \
SHARED_PRECOMPS_DIR="$DERIVED_PATH/Build/Intermediates.noindex/PrecompiledHeaders" \
Comment on lines +147 to +149
"$@"
}

Expand Down
Loading