|
| 1 | +import * as core from '@actions/core' |
| 2 | +import * as exec from '@actions/exec' |
| 3 | +import {SdkToolchainInstaller} from '../../src/installer/sdk' |
| 4 | +import {describe, expect, it, jest, beforeEach, afterEach} from '@jest/globals' |
| 5 | + |
| 6 | +describe('SDK toolchain installation', () => { |
| 7 | + const sdkSnapshot = { |
| 8 | + name: 'Swift SDK for Android', |
| 9 | + date: new Date('2024-03-30 10:28:49.000000000 -05:00'), |
| 10 | + download: 'swift-6.0-android-sdk.tar.gz', |
| 11 | + checksum: 'abc123', |
| 12 | + dir: 'swift-6.0-RELEASE', |
| 13 | + platform: 'android', |
| 14 | + branch: 'swift-6.0-release', |
| 15 | + preventCaching: false |
| 16 | + } |
| 17 | + |
| 18 | + beforeEach(() => { |
| 19 | + jest.useFakeTimers() |
| 20 | + }) |
| 21 | + |
| 22 | + afterEach(() => { |
| 23 | + jest.restoreAllMocks() |
| 24 | + jest.useRealTimers() |
| 25 | + }) |
| 26 | + |
| 27 | + it('tests install succeeds on first attempt', async () => { |
| 28 | + const installer = new SdkToolchainInstaller(sdkSnapshot) |
| 29 | + const execSpy = jest.spyOn(exec, 'exec').mockResolvedValue(0) |
| 30 | + |
| 31 | + await installer.install('x86_64', false) |
| 32 | + |
| 33 | + expect(execSpy).toHaveBeenCalledTimes(1) |
| 34 | + expect(execSpy).toHaveBeenCalledWith('swift', [ |
| 35 | + 'sdk', |
| 36 | + 'install', |
| 37 | + 'https://download.swift.org/swift-6.0-release/android/swift-6.0-RELEASE/swift-6.0-android-sdk.tar.gz', |
| 38 | + '--checksum', |
| 39 | + 'abc123' |
| 40 | + ]) |
| 41 | + }) |
| 42 | + |
| 43 | + it('tests install without checksum', async () => { |
| 44 | + const snapshotWithoutChecksum = {...sdkSnapshot, checksum: undefined} |
| 45 | + const installer = new SdkToolchainInstaller(snapshotWithoutChecksum) |
| 46 | + const execSpy = jest.spyOn(exec, 'exec').mockResolvedValue(0) |
| 47 | + |
| 48 | + await installer.install('aarch64', false) |
| 49 | + |
| 50 | + expect(execSpy).toHaveBeenCalledTimes(1) |
| 51 | + expect(execSpy).toHaveBeenCalledWith('swift', [ |
| 52 | + 'sdk', |
| 53 | + 'install', |
| 54 | + 'https://download.swift.org/swift-6.0-release/android/swift-6.0-RELEASE/swift-6.0-android-sdk.tar.gz' |
| 55 | + ]) |
| 56 | + }) |
| 57 | + |
| 58 | + it('tests install retries on failure and succeeds on second attempt', async () => { |
| 59 | + const installer = new SdkToolchainInstaller(sdkSnapshot) |
| 60 | + const execSpy = jest |
| 61 | + .spyOn(exec, 'exec') |
| 62 | + .mockRejectedValueOnce(new Error('Network error')) |
| 63 | + .mockResolvedValueOnce(0) |
| 64 | + const infoSpy = jest.spyOn(core, 'info').mockReturnValue() |
| 65 | + |
| 66 | + const installPromise = installer.install('x86_64', false) |
| 67 | + |
| 68 | + // Fast-forward through first retry delay (1000ms) |
| 69 | + await jest.advanceTimersByTimeAsync(1000) |
| 70 | + |
| 71 | + await installPromise |
| 72 | + |
| 73 | + expect(execSpy).toHaveBeenCalledTimes(2) |
| 74 | + expect(infoSpy).toHaveBeenCalledWith('Waiting 1000ms before retrying') |
| 75 | + }) |
| 76 | + |
| 77 | + it('tests install retries on failure and succeeds on third attempt', async () => { |
| 78 | + const installer = new SdkToolchainInstaller(sdkSnapshot) |
| 79 | + const execSpy = jest |
| 80 | + .spyOn(exec, 'exec') |
| 81 | + .mockRejectedValueOnce(new Error('Network error')) |
| 82 | + .mockRejectedValueOnce(new Error('Timeout')) |
| 83 | + .mockResolvedValueOnce(0) |
| 84 | + const infoSpy = jest.spyOn(core, 'info').mockReturnValue() |
| 85 | + |
| 86 | + const installPromise = installer.install('x86_64', false) |
| 87 | + |
| 88 | + // Fast-forward through first retry delay (1000ms) |
| 89 | + await jest.advanceTimersByTimeAsync(1000) |
| 90 | + // Fast-forward through second retry delay (2000ms) |
| 91 | + await jest.advanceTimersByTimeAsync(2000) |
| 92 | + |
| 93 | + await installPromise |
| 94 | + |
| 95 | + expect(execSpy).toHaveBeenCalledTimes(3) |
| 96 | + expect(infoSpy).toHaveBeenCalledWith('Waiting 1000ms before retrying') |
| 97 | + expect(infoSpy).toHaveBeenCalledWith('Waiting 2000ms before retrying') |
| 98 | + }) |
| 99 | + |
| 100 | + it('tests install throws after three failed attempts', async () => { |
| 101 | + jest.useRealTimers() |
| 102 | + const installer = new SdkToolchainInstaller(sdkSnapshot) |
| 103 | + const error = new Error('Persistent network error') |
| 104 | + const execSpy = jest.spyOn(exec, 'exec').mockRejectedValue(error) |
| 105 | + // Mock setTimeout to resolve immediately for faster test execution |
| 106 | + jest.spyOn(global, 'setTimeout').mockImplementation(callback => { |
| 107 | + callback() |
| 108 | + return 0 as unknown as NodeJS.Timeout |
| 109 | + }) |
| 110 | + |
| 111 | + await expect(installer.install('x86_64', false)).rejects.toThrow( |
| 112 | + 'Persistent network error' |
| 113 | + ) |
| 114 | + expect(execSpy).toHaveBeenCalledTimes(3) |
| 115 | + }) |
| 116 | + |
| 117 | + it('tests install with custom base URL', async () => { |
| 118 | + const customSnapshot = { |
| 119 | + ...sdkSnapshot, |
| 120 | + baseUrl: new URL('https://custom.swift.org/downloads/') |
| 121 | + } |
| 122 | + const installer = new SdkToolchainInstaller(customSnapshot) |
| 123 | + const execSpy = jest.spyOn(exec, 'exec').mockResolvedValue(0) |
| 124 | + |
| 125 | + await installer.install('x86_64', false) |
| 126 | + |
| 127 | + expect(execSpy).toHaveBeenCalledTimes(1) |
| 128 | + expect(execSpy).toHaveBeenCalledWith('swift', [ |
| 129 | + 'sdk', |
| 130 | + 'install', |
| 131 | + 'https://custom.swift.org/downloads/swift-6.0-android-sdk.tar.gz', |
| 132 | + '--checksum', |
| 133 | + 'abc123' |
| 134 | + ]) |
| 135 | + }) |
| 136 | +}) |
0 commit comments