|
| 1 | +import fs from 'node:fs'; |
| 2 | +import os from 'node:os'; |
| 3 | +import path from 'node:path'; |
| 4 | +import { execSync } from 'node:child_process'; |
| 5 | + |
| 6 | +import * as rockTools from '@rock-js/tools'; |
| 7 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 8 | + |
| 9 | +import { stripFrameworkBinary } from '../stripFrameworkBinary.js'; |
| 10 | + |
| 11 | +vi.mock('@rock-js/tools', async (importOriginal) => { |
| 12 | + const actual = await importOriginal<typeof rockTools>(); |
| 13 | + return { |
| 14 | + ...actual, |
| 15 | + logger: { |
| 16 | + ...actual.logger, |
| 17 | + error: vi.fn(), |
| 18 | + warn: vi.fn(), |
| 19 | + info: vi.fn(), |
| 20 | + success: vi.fn(), |
| 21 | + debug: vi.fn(), |
| 22 | + }, |
| 23 | + }; |
| 24 | +}); |
| 25 | + |
| 26 | +const mockLoggerWarn = rockTools.logger.warn as ReturnType<typeof vi.fn>; |
| 27 | +const mockLoggerSuccess = rockTools.logger.success as ReturnType<typeof vi.fn>; |
| 28 | + |
| 29 | +function createMockXcframework( |
| 30 | + baseDir: string, |
| 31 | + name: string, |
| 32 | + slices: string[] |
| 33 | +): string { |
| 34 | + const xcframeworkPath = path.join(baseDir, `${name}.xcframework`); |
| 35 | + fs.mkdirSync(xcframeworkPath, { recursive: true }); |
| 36 | + |
| 37 | + for (const slice of slices) { |
| 38 | + const frameworkDir = path.join(xcframeworkPath, slice, `${name}.framework`); |
| 39 | + fs.mkdirSync(frameworkDir, { recursive: true }); |
| 40 | + |
| 41 | + const binaryPath = path.join(frameworkDir, name); |
| 42 | + fs.writeFileSync(binaryPath, 'fake binary content for testing'); |
| 43 | + } |
| 44 | + |
| 45 | + return xcframeworkPath; |
| 46 | +} |
| 47 | + |
| 48 | +describe('stripFrameworkBinary', () => { |
| 49 | + let tempDir: string; |
| 50 | + |
| 51 | + beforeEach(() => { |
| 52 | + tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'strip-framework-test-')); |
| 53 | + vi.clearAllMocks(); |
| 54 | + }); |
| 55 | + |
| 56 | + afterEach(() => { |
| 57 | + fs.rmSync(tempDir, { recursive: true, force: true }); |
| 58 | + }); |
| 59 | + |
| 60 | + it('throws when xcframework does not exist', () => { |
| 61 | + const nonExistentPath = path.join(tempDir, 'NonExistent.xcframework'); |
| 62 | + |
| 63 | + expect(() => stripFrameworkBinary(nonExistentPath)).toThrow( |
| 64 | + 'XCFramework not found at:' |
| 65 | + ); |
| 66 | + }); |
| 67 | + |
| 68 | + it('strips binary from ios-arm64 slice', () => { |
| 69 | + const xcframeworkPath = createMockXcframework(tempDir, 'TestFramework', [ |
| 70 | + 'ios-arm64', |
| 71 | + ]); |
| 72 | + const binaryPath = path.join( |
| 73 | + xcframeworkPath, |
| 74 | + 'ios-arm64', |
| 75 | + 'TestFramework.framework', |
| 76 | + 'TestFramework' |
| 77 | + ); |
| 78 | + const originalContent = fs.readFileSync(binaryPath, 'utf-8'); |
| 79 | + |
| 80 | + stripFrameworkBinary(xcframeworkPath); |
| 81 | + |
| 82 | + const newContent = fs.readFileSync(binaryPath); |
| 83 | + expect(newContent.toString()).not.toBe(originalContent); |
| 84 | + expect(fs.existsSync(binaryPath)).toBe(true); |
| 85 | + expect(mockLoggerSuccess).toHaveBeenCalledWith( |
| 86 | + 'TestFramework.xcframework is now interface-only' |
| 87 | + ); |
| 88 | + }); |
| 89 | + |
| 90 | + it('strips binary from simulator slice with fat binary', () => { |
| 91 | + const xcframeworkPath = createMockXcframework(tempDir, 'TestFramework', [ |
| 92 | + 'ios-arm64_x86_64-simulator', |
| 93 | + ]); |
| 94 | + const binaryPath = path.join( |
| 95 | + xcframeworkPath, |
| 96 | + 'ios-arm64_x86_64-simulator', |
| 97 | + 'TestFramework.framework', |
| 98 | + 'TestFramework' |
| 99 | + ); |
| 100 | + const originalContent = fs.readFileSync(binaryPath, 'utf-8'); |
| 101 | + |
| 102 | + stripFrameworkBinary(xcframeworkPath); |
| 103 | + |
| 104 | + const newContent = fs.readFileSync(binaryPath); |
| 105 | + expect(newContent.toString()).not.toBe(originalContent); |
| 106 | + |
| 107 | + const archInfo = execSync(`xcrun lipo -info "${binaryPath}"`, { |
| 108 | + encoding: 'utf-8', |
| 109 | + }); |
| 110 | + expect(archInfo).toContain('arm64'); |
| 111 | + expect(archInfo).toContain('x86_64'); |
| 112 | + }); |
| 113 | + |
| 114 | + it('handles multiple slices', () => { |
| 115 | + const xcframeworkPath = createMockXcframework(tempDir, 'TestFramework', [ |
| 116 | + 'ios-arm64', |
| 117 | + 'ios-arm64_x86_64-simulator', |
| 118 | + ]); |
| 119 | + |
| 120 | + stripFrameworkBinary(xcframeworkPath); |
| 121 | + |
| 122 | + const deviceBinary = path.join( |
| 123 | + xcframeworkPath, |
| 124 | + 'ios-arm64', |
| 125 | + 'TestFramework.framework', |
| 126 | + 'TestFramework' |
| 127 | + ); |
| 128 | + const simBinary = path.join( |
| 129 | + xcframeworkPath, |
| 130 | + 'ios-arm64_x86_64-simulator', |
| 131 | + 'TestFramework.framework', |
| 132 | + 'TestFramework' |
| 133 | + ); |
| 134 | + |
| 135 | + expect(fs.existsSync(deviceBinary)).toBe(true); |
| 136 | + expect(fs.existsSync(simBinary)).toBe(true); |
| 137 | + expect(mockLoggerSuccess).toHaveBeenCalledOnce(); |
| 138 | + }); |
| 139 | + |
| 140 | + it('warns and skips unknown slice types', () => { |
| 141 | + const xcframeworkPath = createMockXcframework(tempDir, 'TestFramework', [ |
| 142 | + 'ios-unknown-slice', |
| 143 | + ]); |
| 144 | + |
| 145 | + stripFrameworkBinary(xcframeworkPath); |
| 146 | + |
| 147 | + expect(mockLoggerWarn).toHaveBeenCalledWith( |
| 148 | + 'Unknown slice type: ios-unknown-slice, skipping' |
| 149 | + ); |
| 150 | + }); |
| 151 | + |
| 152 | + it('warns and skips slices without binary', () => { |
| 153 | + const xcframeworkPath = path.join(tempDir, 'TestFramework.xcframework'); |
| 154 | + const frameworkDir = path.join( |
| 155 | + xcframeworkPath, |
| 156 | + 'ios-arm64', |
| 157 | + 'TestFramework.framework' |
| 158 | + ); |
| 159 | + fs.mkdirSync(frameworkDir, { recursive: true }); |
| 160 | + |
| 161 | + stripFrameworkBinary(xcframeworkPath); |
| 162 | + |
| 163 | + expect(mockLoggerWarn).toHaveBeenCalledWith( |
| 164 | + expect.stringContaining('No binary found at') |
| 165 | + ); |
| 166 | + }); |
| 167 | + |
| 168 | + it('ignores non-ios directories', () => { |
| 169 | + const xcframeworkPath = createMockXcframework(tempDir, 'TestFramework', [ |
| 170 | + 'ios-arm64', |
| 171 | + ]); |
| 172 | + fs.mkdirSync(path.join(xcframeworkPath, 'macos-arm64'), { |
| 173 | + recursive: true, |
| 174 | + }); |
| 175 | + |
| 176 | + stripFrameworkBinary(xcframeworkPath); |
| 177 | + |
| 178 | + expect(mockLoggerSuccess).toHaveBeenCalledOnce(); |
| 179 | + }); |
| 180 | +}); |
0 commit comments