|
| 1 | +import execa from 'execa'; |
| 2 | +import {execaPod, findPodRunner} from '../tools/pods'; |
| 3 | + |
| 4 | +jest.mock('execa', () => jest.fn()); |
| 5 | + |
| 6 | +const mockExeca = execa as unknown as jest.Mock; |
| 7 | +const OPTIONS = {cwd: '/project'}; |
| 8 | + |
| 9 | +function failWhen(predicate: (command: string, args: string[]) => boolean) { |
| 10 | + mockExeca.mockImplementation(async (command: string, args: string[]) => { |
| 11 | + if (predicate(command, args)) { |
| 12 | + throw new Error(`${command} ${args.join(' ')} failed`); |
| 13 | + } |
| 14 | + return {exitCode: 0}; |
| 15 | + }); |
| 16 | +} |
| 17 | + |
| 18 | +const isBundledPod = (command: string, args: string[]) => |
| 19 | + command === 'bundle' && args[0] === 'exec'; |
| 20 | +const isBundleInstall = (command: string, args: string[]) => |
| 21 | + command === 'bundle' && args[0] === 'install'; |
| 22 | + |
| 23 | +beforeEach(() => { |
| 24 | + mockExeca.mockReset(); |
| 25 | +}); |
| 26 | + |
| 27 | +describe('findPodRunner', () => { |
| 28 | + it('prefers the bundled CocoaPods', async () => { |
| 29 | + failWhen(() => false); |
| 30 | + |
| 31 | + await expect(findPodRunner(OPTIONS)).resolves.toBe('bundle'); |
| 32 | + expect(mockExeca).toHaveBeenCalledTimes(1); |
| 33 | + expect(mockExeca).toHaveBeenCalledWith( |
| 34 | + 'bundle', |
| 35 | + ['exec', 'pod', '--version'], |
| 36 | + OPTIONS, |
| 37 | + ); |
| 38 | + }); |
| 39 | + |
| 40 | + it('runs bundle install with the given options when the bundled CocoaPods is missing', async () => { |
| 41 | + let installed = false; |
| 42 | + mockExeca.mockImplementation(async (command: string, args: string[]) => { |
| 43 | + if (isBundleInstall(command, args)) { |
| 44 | + installed = true; |
| 45 | + } else if (isBundledPod(command, args) && !installed) { |
| 46 | + throw new Error('not installed'); |
| 47 | + } |
| 48 | + return {exitCode: 0}; |
| 49 | + }); |
| 50 | + |
| 51 | + await expect(findPodRunner(OPTIONS)).resolves.toBe('bundle'); |
| 52 | + expect(mockExeca).toHaveBeenCalledWith('bundle', ['install'], OPTIONS); |
| 53 | + }); |
| 54 | + |
| 55 | + it('falls back to the system CocoaPods', async () => { |
| 56 | + failWhen((command) => command === 'bundle'); |
| 57 | + |
| 58 | + await expect(findPodRunner(OPTIONS)).resolves.toBe('system'); |
| 59 | + expect(mockExeca).toHaveBeenLastCalledWith('pod', ['--version'], OPTIONS); |
| 60 | + }); |
| 61 | + |
| 62 | + it('throws when CocoaPods is not installed', async () => { |
| 63 | + failWhen(() => true); |
| 64 | + |
| 65 | + await expect(findPodRunner(OPTIONS)).rejects.toThrow( |
| 66 | + 'cocoapods not installed', |
| 67 | + ); |
| 68 | + }); |
| 69 | +}); |
| 70 | + |
| 71 | +describe('execaPod', () => { |
| 72 | + it('runs pod through bundle exec when the bundled CocoaPods is available', async () => { |
| 73 | + failWhen(() => false); |
| 74 | + |
| 75 | + await execaPod(['install'], OPTIONS); |
| 76 | + |
| 77 | + expect(mockExeca).toHaveBeenLastCalledWith( |
| 78 | + 'bundle', |
| 79 | + ['exec', 'pod', 'install'], |
| 80 | + OPTIONS, |
| 81 | + ); |
| 82 | + }); |
| 83 | + |
| 84 | + it('runs the system pod otherwise', async () => { |
| 85 | + failWhen((command) => command === 'bundle'); |
| 86 | + |
| 87 | + await execaPod(['install'], OPTIONS); |
| 88 | + |
| 89 | + expect(mockExeca).toHaveBeenLastCalledWith('pod', ['install'], OPTIONS); |
| 90 | + }); |
| 91 | +}); |
0 commit comments