|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import fs from 'node:fs'; |
| 3 | +import path from 'node:path'; |
| 4 | +import { afterEach, test, vi } from 'vitest'; |
| 5 | +import { mkdtempForTestSync } from './tmp-dir.ts'; |
| 6 | +import { restoreLegacyXctestDeviceSetRedirect } from '../legacy-xctest-device-set.ts'; |
| 7 | + |
| 8 | +afterEach(() => { |
| 9 | + vi.restoreAllMocks(); |
| 10 | +}); |
| 11 | + |
| 12 | +type LegacyRedirect = { |
| 13 | + xctestDeviceSetPath: string; |
| 14 | + backupPath: string; |
| 15 | + scopedSetPath: string; |
| 16 | +}; |
| 17 | + |
| 18 | +function makeLegacyPaths(): LegacyRedirect { |
| 19 | + const root = mkdtempForTestSync('agent-device-legacy-xctest-set-'); |
| 20 | + const developer = path.join(root, 'Library', 'Developer'); |
| 21 | + fs.mkdirSync(developer, { recursive: true }); |
| 22 | + const scopedSetPath = path.join(root, 'tenant-set'); |
| 23 | + fs.mkdirSync(path.join(scopedSetPath, 'SCOPED-UDID'), { recursive: true }); |
| 24 | + return { |
| 25 | + xctestDeviceSetPath: path.join(developer, 'XCTestDevices'), |
| 26 | + backupPath: path.join(developer, 'XCTestDevices.agent-device-backup'), |
| 27 | + scopedSetPath, |
| 28 | + }; |
| 29 | +} |
| 30 | + |
| 31 | +test('an older redirect is undone: the symlink goes and the host set comes back', () => { |
| 32 | + const paths = makeLegacyPaths(); |
| 33 | + fs.mkdirSync(path.join(paths.backupPath, 'HOST-UDID'), { recursive: true }); |
| 34 | + fs.symlinkSync(paths.scopedSetPath, paths.xctestDeviceSetPath, 'dir'); |
| 35 | + |
| 36 | + const phases = restoreRecordingPhases(paths); |
| 37 | + |
| 38 | + assert.equal(fs.lstatSync(paths.xctestDeviceSetPath).isSymbolicLink(), false); |
| 39 | + assert.ok(fs.existsSync(path.join(paths.xctestDeviceSetPath, 'HOST-UDID'))); |
| 40 | + assert.equal(fs.existsSync(paths.backupPath), false); |
| 41 | + // Unlinking never follows the link: the scoped set it pointed at keeps its devices. |
| 42 | + assert.ok(fs.existsSync(path.join(paths.scopedSetPath, 'SCOPED-UDID'))); |
| 43 | + assert.deepEqual(phases, [ |
| 44 | + 'ios_runner_legacy_xctest_device_set_link_removed', |
| 45 | + 'ios_runner_legacy_xctest_device_set_backup_restored', |
| 46 | + ]); |
| 47 | +}); |
| 48 | + |
| 49 | +test('a symlink an older redirect left without a backup is removed, whatever it points at', () => { |
| 50 | + for (const target of ['scoped', 'external'] as const) { |
| 51 | + const paths = makeLegacyPaths(); |
| 52 | + const linkTarget = |
| 53 | + target === 'scoped' |
| 54 | + ? paths.scopedSetPath |
| 55 | + : path.join(path.dirname(paths.scopedSetPath), 'external-volume'); |
| 56 | + fs.mkdirSync(linkTarget, { recursive: true }); |
| 57 | + fs.symlinkSync(linkTarget, paths.xctestDeviceSetPath, 'dir'); |
| 58 | + const diagnostics: unknown[] = []; |
| 59 | + |
| 60 | + restoreLegacyXctestDeviceSetRedirect( |
| 61 | + (diagnostic) => diagnostics.push(diagnostic), |
| 62 | + paths.xctestDeviceSetPath, |
| 63 | + ); |
| 64 | + |
| 65 | + assert.equal(fs.lstatSync(paths.xctestDeviceSetPath, { throwIfNoEntry: false }), undefined); |
| 66 | + assert.ok(fs.existsSync(linkTarget), target); |
| 67 | + assert.deepEqual(diagnostics, [ |
| 68 | + { |
| 69 | + phase: 'ios_runner_legacy_xctest_device_set_link_removed', |
| 70 | + resourcePath: paths.xctestDeviceSetPath, |
| 71 | + data: { linkTarget }, |
| 72 | + }, |
| 73 | + ]); |
| 74 | + } |
| 75 | +}); |
| 76 | + |
| 77 | +test('a backup never replaces a host set that is already in place', () => { |
| 78 | + const paths = makeLegacyPaths(); |
| 79 | + fs.mkdirSync(path.join(paths.xctestDeviceSetPath, 'CURRENT-UDID'), { recursive: true }); |
| 80 | + fs.mkdirSync(path.join(paths.backupPath, 'OLD-UDID'), { recursive: true }); |
| 81 | + |
| 82 | + restoreRecordingPhases(paths); |
| 83 | + |
| 84 | + assert.ok(fs.existsSync(path.join(paths.xctestDeviceSetPath, 'CURRENT-UDID'))); |
| 85 | + assert.ok(fs.existsSync(path.join(paths.backupPath, 'OLD-UDID'))); |
| 86 | +}); |
| 87 | + |
| 88 | +test('a host with no leftovers is left untouched and reports nothing', () => { |
| 89 | + const paths = makeLegacyPaths(); |
| 90 | + |
| 91 | + const phases = restoreRecordingPhases(paths); |
| 92 | + |
| 93 | + assert.equal(fs.existsSync(paths.xctestDeviceSetPath), false); |
| 94 | + assert.equal(fs.existsSync(paths.backupPath), false); |
| 95 | + assert.deepEqual(phases, []); |
| 96 | +}); |
| 97 | + |
| 98 | +/** Runs the restore with the other daemon's whole restore landing just before this one's `call`. */ |
| 99 | +function withOtherDaemonFinishingBefore( |
| 100 | + call: 'unlinkSync' | 'renameSync', |
| 101 | + paths: LegacyRedirect, |
| 102 | +): string[] { |
| 103 | + const original = fs[call] as (...args: unknown[]) => unknown; |
| 104 | + vi.spyOn(fs, call).mockImplementationOnce(((...args: unknown[]) => { |
| 105 | + restoreRecordingPhases(paths); |
| 106 | + return original.apply(fs, args); |
| 107 | + }) as never); |
| 108 | + return restoreRecordingPhases(paths); |
| 109 | +} |
| 110 | + |
| 111 | +test('a daemon whose unlink or rename the other daemon already made finishes the restore', () => { |
| 112 | + for (const call of ['unlinkSync', 'renameSync'] as const) { |
| 113 | + const paths = makeLegacyPaths(); |
| 114 | + fs.mkdirSync(path.join(paths.backupPath, 'HOST-UDID'), { recursive: true }); |
| 115 | + fs.symlinkSync(paths.scopedSetPath, paths.xctestDeviceSetPath, 'dir'); |
| 116 | + |
| 117 | + const phases = withOtherDaemonFinishingBefore(call, paths); |
| 118 | + |
| 119 | + assert.equal( |
| 120 | + phases.includes('ios_runner_legacy_xctest_device_set_restore_failed'), |
| 121 | + false, |
| 122 | + call, |
| 123 | + ); |
| 124 | + assert.equal(fs.lstatSync(paths.xctestDeviceSetPath).isSymbolicLink(), false, call); |
| 125 | + assert.ok(fs.existsSync(path.join(paths.xctestDeviceSetPath, 'HOST-UDID')), call); |
| 126 | + assert.equal(fs.existsSync(paths.backupPath), false, call); |
| 127 | + assert.ok(fs.existsSync(path.join(paths.scopedSetPath, 'SCOPED-UDID')), call); |
| 128 | + vi.restoreAllMocks(); |
| 129 | + } |
| 130 | +}); |
| 131 | + |
| 132 | +test('an unlink that fails while the link is still there is reported, not thrown', () => { |
| 133 | + const paths = makeLegacyPaths(); |
| 134 | + fs.mkdirSync(paths.backupPath); |
| 135 | + fs.symlinkSync(paths.scopedSetPath, paths.xctestDeviceSetPath, 'dir'); |
| 136 | + vi.spyOn(fs, 'unlinkSync').mockImplementationOnce(() => { |
| 137 | + throw Object.assign(new Error('EACCES: permission denied'), { code: 'EACCES' }); |
| 138 | + }); |
| 139 | + const diagnostics: unknown[] = []; |
| 140 | + |
| 141 | + restoreLegacyXctestDeviceSetRedirect( |
| 142 | + (diagnostic) => diagnostics.push(diagnostic), |
| 143 | + paths.xctestDeviceSetPath, |
| 144 | + ); |
| 145 | + |
| 146 | + assert.deepEqual(diagnostics, [ |
| 147 | + { |
| 148 | + phase: 'ios_runner_legacy_xctest_device_set_restore_failed', |
| 149 | + resourcePath: paths.xctestDeviceSetPath, |
| 150 | + data: { error: 'EACCES: permission denied' }, |
| 151 | + }, |
| 152 | + ]); |
| 153 | + assert.equal(fs.lstatSync(paths.xctestDeviceSetPath).isSymbolicLink(), true); |
| 154 | + assert.ok(fs.existsSync(paths.backupPath)); |
| 155 | +}); |
| 156 | + |
| 157 | +function restoreRecordingPhases(paths: LegacyRedirect): string[] { |
| 158 | + const phases: string[] = []; |
| 159 | + restoreLegacyXctestDeviceSetRedirect( |
| 160 | + (diagnostic) => phases.push(diagnostic.phase), |
| 161 | + paths.xctestDeviceSetPath, |
| 162 | + ); |
| 163 | + return phases; |
| 164 | +} |
0 commit comments