|
| 1 | +import {getBuildConfigurationFromXcScheme} from '../getBuildConfigurationFromXcScheme'; |
| 2 | +import fs from 'fs'; |
| 3 | +import path from 'path'; |
| 4 | +import {CLIError} from '@react-native-community/cli-tools'; |
| 5 | + |
| 6 | +jest.mock('fs', () => ({ |
| 7 | + readFileSync: jest.fn(), |
| 8 | + readdirSync: jest.fn(), |
| 9 | +})); |
| 10 | + |
| 11 | +describe('getBuildConfigurationFromXcScheme', () => { |
| 12 | + afterEach(() => { |
| 13 | + jest.resetAllMocks(); |
| 14 | + }); |
| 15 | + |
| 16 | + it('returns build configuration when the shared scheme file exists', () => { |
| 17 | + (fs.readdirSync as jest.Mock).mockReturnValue(['Test.xcodeproj']); |
| 18 | + |
| 19 | + const xmlContent = `<Scheme> |
| 20 | + <LaunchAction buildConfiguration="Debug"/> |
| 21 | +</Scheme>`; |
| 22 | + (fs.readFileSync as jest.Mock).mockReturnValue(xmlContent); |
| 23 | + |
| 24 | + const sourceDir = '/some/dir'; |
| 25 | + const scheme = 'Test'; |
| 26 | + const defaultConfig = 'Release'; |
| 27 | + const projectInfo = {name: 'Test', schemes: [scheme]}; |
| 28 | + |
| 29 | + const result = getBuildConfigurationFromXcScheme( |
| 30 | + scheme, |
| 31 | + defaultConfig, |
| 32 | + sourceDir, |
| 33 | + projectInfo, |
| 34 | + ); |
| 35 | + |
| 36 | + expect(result).toBe('Debug'); |
| 37 | + |
| 38 | + const expectedPath = path.join( |
| 39 | + sourceDir, |
| 40 | + 'Test.xcodeproj', |
| 41 | + 'xcshareddata', |
| 42 | + 'xcschemes', |
| 43 | + `${scheme}.xcscheme`, |
| 44 | + ); |
| 45 | + expect(fs.readFileSync).toHaveBeenCalledWith(expectedPath, { |
| 46 | + encoding: 'utf-8', |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + it('throws CLIError when reading the shared scheme file fails', () => { |
| 51 | + process.env.FORCE_COLOR = '0'; // To disable chalk |
| 52 | + (fs.readdirSync as jest.Mock).mockReturnValue(['Test.xcodeproj']); |
| 53 | + (fs.readFileSync as jest.Mock).mockImplementation(() => { |
| 54 | + throw new Error('File not found'); |
| 55 | + }); |
| 56 | + |
| 57 | + const sourceDir = '/some/dir'; |
| 58 | + const scheme = 'Test'; |
| 59 | + const defaultConfig = 'Release'; |
| 60 | + const projectInfo = {name: 'Test', schemes: [scheme]}; |
| 61 | + |
| 62 | + expect(() => { |
| 63 | + getBuildConfigurationFromXcScheme( |
| 64 | + scheme, |
| 65 | + defaultConfig, |
| 66 | + sourceDir, |
| 67 | + projectInfo, |
| 68 | + ); |
| 69 | + }).toThrow(CLIError); |
| 70 | + |
| 71 | + try { |
| 72 | + getBuildConfigurationFromXcScheme( |
| 73 | + scheme, |
| 74 | + defaultConfig, |
| 75 | + sourceDir, |
| 76 | + projectInfo, |
| 77 | + ); |
| 78 | + } catch (err) { |
| 79 | + const msg = (err as CLIError).message; |
| 80 | + expect(msg).toContain(`Could not load the shared scheme for ${scheme}`); |
| 81 | + expect(msg).toContain(`includes: ${projectInfo.schemes[0]}`); |
| 82 | + } |
| 83 | + }); |
| 84 | + |
| 85 | + it('returns the default configuration when no .xcodeproj folder is found', () => { |
| 86 | + (fs.readdirSync as jest.Mock).mockReturnValue([]); |
| 87 | + |
| 88 | + const sourceDir = '/some/dir'; |
| 89 | + const scheme = 'Test'; |
| 90 | + const defaultConfig = 'Release'; |
| 91 | + const result = getBuildConfigurationFromXcScheme( |
| 92 | + scheme, |
| 93 | + defaultConfig, |
| 94 | + sourceDir, |
| 95 | + undefined, |
| 96 | + ); |
| 97 | + |
| 98 | + expect(result).toBe(defaultConfig); |
| 99 | + }); |
| 100 | +}); |
0 commit comments