|
1 | 1 | import assert from 'node:assert/strict'; |
2 | 2 | import fs from 'node:fs'; |
3 | 3 | import path from 'node:path'; |
4 | | -import { test } from 'vitest'; |
| 4 | +import { afterEach, beforeEach, test, vi } from 'vitest'; |
| 5 | +import { resetAllProcessMemosForTests } from '@agent-device/kernel/ttl-memo'; |
5 | 6 | import { IOS_DEVICE, IOS_SIMULATOR, TVOS_SIMULATOR } from './device-fixtures.ts'; |
6 | 7 | import { mkdtempForTestSync } from './tmp-dir.ts'; |
| 8 | +import { STUBBED_APPLE_TOOLCHAIN, stubAppleToolchainProbes } from './apple-toolchain-fixtures.ts'; |
7 | 9 | import { |
| 10 | + isSameRunnerSimulator, |
8 | 11 | restoreLegacyXctestDeviceSetRedirect, |
9 | 12 | runnerSimulatorSetFailureDetails, |
10 | 13 | xcodebuildDestinationArgs, |
11 | 14 | } from '../runner-device-set.ts'; |
12 | 15 |
|
| 16 | +const toolchainProbe = stubAppleToolchainProbes(); |
| 17 | +beforeEach(resetAllProcessMemosForTests); |
| 18 | +afterEach(() => { |
| 19 | + vi.restoreAllMocks(); |
| 20 | +}); |
| 21 | + |
13 | 22 | const DESTINATION = 'platform=iOS Simulator,id=sim-1'; |
14 | 23 |
|
15 | 24 | test('a scoped-set simulator names its set to xcodebuild beside the destination', () => { |
@@ -42,14 +51,38 @@ test('the default set, a blank set path and a physical device leave the destinat |
42 | 51 | } |
43 | 52 | }); |
44 | 53 |
|
45 | | -test('a failure reports the scoped set and the Xcode only for a scoped-set simulator', () => { |
| 54 | +test('a failure reports the scoped set and the selected Xcode only for a scoped-set simulator', () => { |
46 | 55 | const scoped = { ...IOS_SIMULATOR, simulatorSetPath: '/tmp/tenant-a/simulators' }; |
47 | 56 |
|
48 | | - assert.deepEqual(runnerSimulatorSetFailureDetails(scoped, '26.2'), { |
| 57 | + assert.deepEqual(runnerSimulatorSetFailureDetails(scoped), { |
49 | 58 | simulatorSetPath: '/tmp/tenant-a/simulators', |
50 | | - xcodeVersion: '26.2', |
| 59 | + xcodeVersion: STUBBED_APPLE_TOOLCHAIN.xcodeVersion, |
51 | 60 | }); |
52 | | - assert.deepEqual(runnerSimulatorSetFailureDetails(IOS_SIMULATOR, '26.2'), {}); |
| 61 | + assert.deepEqual(runnerSimulatorSetFailureDetails(IOS_SIMULATOR), {}); |
| 62 | +}); |
| 63 | + |
| 64 | +test('a failure whose Xcode cannot be read still names the scoped set, and no Xcode key', () => { |
| 65 | + toolchainProbe.mockReturnValue({ exitCode: 1, stdout: '', stderr: 'xcode-select: error' }); |
| 66 | + |
| 67 | + assert.deepEqual( |
| 68 | + runnerSimulatorSetFailureDetails({ ...IOS_SIMULATOR, simulatorSetPath: '/tmp/tenant-a/sims' }), |
| 69 | + { simulatorSetPath: '/tmp/tenant-a/sims' }, |
| 70 | + ); |
| 71 | +}); |
| 72 | + |
| 73 | +test('one udid in two simulator sets names two simulators', () => { |
| 74 | + const tenantA = { ...IOS_SIMULATOR, simulatorSetPath: '/tmp/tenant-a/simulators' }; |
| 75 | + |
| 76 | + assert.equal(isSameRunnerSimulator(tenantA, { ...tenantA }), true); |
| 77 | + assert.equal( |
| 78 | + isSameRunnerSimulator(IOS_SIMULATOR, { ...IOS_SIMULATOR, simulatorSetPath: ' ' }), |
| 79 | + true, |
| 80 | + ); |
| 81 | + assert.equal(isSameRunnerSimulator(tenantA, IOS_SIMULATOR), false); |
| 82 | + assert.equal( |
| 83 | + isSameRunnerSimulator(tenantA, { ...tenantA, simulatorSetPath: '/tmp/tenant-b/simulators' }), |
| 84 | + false, |
| 85 | + ); |
53 | 86 | }); |
54 | 87 |
|
55 | 88 | type LegacyRedirect = { |
@@ -140,3 +173,58 @@ test('a host with no leftovers is left untouched', () => { |
140 | 173 | assert.equal(fs.existsSync(paths.xctestDeviceSetPath), false); |
141 | 174 | assert.equal(fs.existsSync(paths.backupPath), false); |
142 | 175 | }); |
| 176 | + |
| 177 | +/** Runs `step` with the other daemon's whole restore landing just before this daemon's `call`. */ |
| 178 | +function withOtherDaemonFinishingBefore( |
| 179 | + call: 'readlinkSync' | 'unlinkSync' | 'renameSync', |
| 180 | + paths: LegacyRedirect, |
| 181 | + device: Parameters<typeof restoreLegacyXctestDeviceSetRedirect>[0], |
| 182 | +): void { |
| 183 | + const original = fs[call] as (...args: unknown[]) => unknown; |
| 184 | + vi.spyOn(fs, call).mockImplementationOnce(((...args: unknown[]) => { |
| 185 | + restoreLegacyXctestDeviceSetRedirect(device, paths.xctestDeviceSetPath); |
| 186 | + return original.apply(fs, args); |
| 187 | + }) as never); |
| 188 | + restoreLegacyXctestDeviceSetRedirect(device, paths.xctestDeviceSetPath); |
| 189 | +} |
| 190 | + |
| 191 | +test('a daemon whose unlink or rename the other daemon already made finishes the restore', () => { |
| 192 | + for (const call of ['unlinkSync', 'renameSync'] as const) { |
| 193 | + const paths = makeLegacyPaths(); |
| 194 | + fs.mkdirSync(path.join(paths.backupPath, 'HOST-UDID'), { recursive: true }); |
| 195 | + fs.symlinkSync(paths.scopedSetPath, paths.xctestDeviceSetPath, 'dir'); |
| 196 | + |
| 197 | + withOtherDaemonFinishingBefore(call, paths, IOS_SIMULATOR); |
| 198 | + |
| 199 | + assert.equal(fs.lstatSync(paths.xctestDeviceSetPath).isSymbolicLink(), false, call); |
| 200 | + assert.ok(fs.existsSync(path.join(paths.xctestDeviceSetPath, 'HOST-UDID')), call); |
| 201 | + assert.equal(fs.existsSync(paths.backupPath), false, call); |
| 202 | + assert.ok(fs.existsSync(path.join(paths.scopedSetPath, 'SCOPED-UDID')), call); |
| 203 | + vi.restoreAllMocks(); |
| 204 | + } |
| 205 | +}); |
| 206 | + |
| 207 | +test('a daemon whose link the other daemon removed before it was read leaves the host alone', () => { |
| 208 | + const paths = makeLegacyPaths(); |
| 209 | + const scoped = { ...IOS_SIMULATOR, simulatorSetPath: paths.scopedSetPath }; |
| 210 | + fs.symlinkSync(paths.scopedSetPath, paths.xctestDeviceSetPath, 'dir'); |
| 211 | + |
| 212 | + withOtherDaemonFinishingBefore('readlinkSync', paths, scoped); |
| 213 | + |
| 214 | + assert.equal(fs.lstatSync(paths.xctestDeviceSetPath, { throwIfNoEntry: false }), undefined); |
| 215 | + assert.ok(fs.existsSync(path.join(paths.scopedSetPath, 'SCOPED-UDID'))); |
| 216 | +}); |
| 217 | + |
| 218 | +test('an unlink that fails while the link is still there is reported', () => { |
| 219 | + const paths = makeLegacyPaths(); |
| 220 | + fs.mkdirSync(paths.backupPath); |
| 221 | + fs.symlinkSync(paths.scopedSetPath, paths.xctestDeviceSetPath, 'dir'); |
| 222 | + vi.spyOn(fs, 'unlinkSync').mockImplementationOnce(() => { |
| 223 | + throw Object.assign(new Error('EACCES: permission denied'), { code: 'EACCES' }); |
| 224 | + }); |
| 225 | + |
| 226 | + assert.throws( |
| 227 | + () => restoreLegacyXctestDeviceSetRedirect(IOS_SIMULATOR, paths.xctestDeviceSetPath), |
| 228 | + /EACCES/, |
| 229 | + ); |
| 230 | +}); |
0 commit comments