|
| 1 | +import * as fs from 'fs' |
| 2 | +import * as git from '../git' |
| 3 | +import * as spawn from '../spawn' |
| 4 | +import * as core from '@actions/core' |
| 5 | + |
| 6 | +// We want to mock only the rmSync method on the fs module, and leave everything |
| 7 | +// else untouched. |
| 8 | +jest.mock('fs', () => ({ |
| 9 | + ...jest.requireActual('fs'), |
| 10 | + rmSync: jest.fn() |
| 11 | +})) |
| 12 | + |
| 13 | +describe('git', () => { |
| 14 | + // We don't want to _actually_ spawn external commands, so we mock the function |
| 15 | + let spawnSpy: jest.SpyInstance |
| 16 | + // Capture the startGroup calls |
| 17 | + let coreSpy: jest.SpyInstance |
| 18 | + // The script calls fs.rmSync, so let's mock it and verify it was called |
| 19 | + let rmSyncSpy: jest.SpyInstance |
| 20 | + |
| 21 | + beforeEach(() => { |
| 22 | + coreSpy = jest.spyOn(core, 'startGroup') |
| 23 | + spawnSpy = jest.spyOn(spawn, 'spawnAndWaitForExitCode').mockResolvedValue({ |
| 24 | + // 0 is the exit code for success |
| 25 | + exitCode: 0 |
| 26 | + }) |
| 27 | + // We don't want to _actually_ clone the repo, so we mock the function |
| 28 | + jest.spyOn(git, 'clone').mockResolvedValue() |
| 29 | + rmSyncSpy = fs.rmSync as jest.Mocked<typeof fs>['rmSync'] |
| 30 | + }) |
| 31 | + |
| 32 | + test('getViaGit build-installers x86_64', async () => { |
| 33 | + const flavor = 'build-installers' |
| 34 | + const architecture = 'x86_64' |
| 35 | + const outputDirectory = 'outputDirectory' |
| 36 | + const {artifactName, download} = await git.getViaGit(flavor, architecture) |
| 37 | + |
| 38 | + expect(artifactName).toEqual('git-sdk-64-build-installers') |
| 39 | + |
| 40 | + await download(outputDirectory, true) |
| 41 | + |
| 42 | + expect(coreSpy).toHaveBeenCalledWith(`Creating ${flavor} artifact`) |
| 43 | + expect(spawnSpy).toHaveBeenCalledWith( |
| 44 | + expect.stringContaining('/bash.exe'), |
| 45 | + expect.arrayContaining([ |
| 46 | + '.tmp/build-extra/please.sh', |
| 47 | + 'create-sdk-artifact', |
| 48 | + `--architecture=${architecture}`, |
| 49 | + `--out=${outputDirectory}` |
| 50 | + ]), |
| 51 | + expect.objectContaining({ |
| 52 | + env: expect.objectContaining({ |
| 53 | + // We want to ensure that the correct /bin folders are in the PATH, |
| 54 | + // so that please.sh can find git.exe |
| 55 | + // https://github.com/git-for-windows/setup-git-for-windows-sdk/issues/951 |
| 56 | + PATH: |
| 57 | + expect.stringContaining('/clangarm64/bin') && |
| 58 | + expect.stringContaining('/mingw64/bin') |
| 59 | + }) |
| 60 | + }) |
| 61 | + ) |
| 62 | + expect(rmSyncSpy).toHaveBeenCalledWith('.tmp', {recursive: true}) |
| 63 | + }) |
| 64 | + |
| 65 | + test('getViaGit full x86_64', async () => { |
| 66 | + const flavor = 'full' |
| 67 | + const architecture = 'x86_64' |
| 68 | + const outputDirectory = 'outputDirectory' |
| 69 | + const {artifactName, download} = await git.getViaGit(flavor, architecture) |
| 70 | + |
| 71 | + expect(artifactName).toEqual('git-sdk-64-full') |
| 72 | + |
| 73 | + await download(outputDirectory, true) |
| 74 | + |
| 75 | + expect(coreSpy).toHaveBeenCalledWith(`Checking out git-sdk-64`) |
| 76 | + expect(spawnSpy).toHaveBeenCalledWith( |
| 77 | + expect.stringContaining('/git.exe'), |
| 78 | + expect.arrayContaining([ |
| 79 | + '--git-dir=.tmp', |
| 80 | + 'worktree', |
| 81 | + 'add', |
| 82 | + outputDirectory |
| 83 | + ]), |
| 84 | + expect.any(Object) |
| 85 | + ) |
| 86 | + expect(rmSyncSpy).toHaveBeenCalledWith('.tmp', {recursive: true}) |
| 87 | + }) |
| 88 | +}) |
0 commit comments