|
| 1 | +import { describe, jest, it, beforeEach } from '@jest/globals' |
| 2 | +import * as core from '../../test/mocks/core' |
| 3 | +import * as toolCache from '../../test/mocks/tool-cache' |
| 4 | +import * as fs from '../../test/mocks/fs' |
| 5 | +import { trdlCli } from '../../test/mocks/trdl-cli' |
| 6 | +import { gpgCli } from '../../test/mocks/gpg-cli' |
| 7 | + |
| 8 | +// Mocks should be declared before the module being tested is imported. |
| 9 | +jest.unstable_mockModule('@actions/core', () => core) |
| 10 | +jest.unstable_mockModule('@actions/tool-cache', () => toolCache) |
| 11 | +jest.unstable_mockModule('node:fs', () => fs) |
| 12 | + |
| 13 | +// The module being tested should be imported dynamically. This ensures that the |
| 14 | +// mocks are used in place of any actual dependencies. |
| 15 | +const { parseInputs, getOptions, formatDownloadUrls, Do } = await import('./action') |
| 16 | + |
| 17 | +describe('install/action.ts', function () { |
| 18 | + describe('parseInputs', function () { |
| 19 | + it('should work w/o required fields', function () { |
| 20 | + core.getInput.mockReturnValue('') |
| 21 | + expect(parseInputs()).toEqual({}) |
| 22 | + }) |
| 23 | + it('should work with all fields', function () { |
| 24 | + const channel = 'some channel' |
| 25 | + const version = 'some version' |
| 26 | + core.getInput.mockReturnValueOnce(channel) |
| 27 | + core.getInput.mockReturnValueOnce(version) |
| 28 | + expect(parseInputs()).toEqual({ channel, version }) |
| 29 | + }) |
| 30 | + }) |
| 31 | + describe('getOptions', function () { |
| 32 | + const defaults = { |
| 33 | + repo: 'trdl', |
| 34 | + group: '0', |
| 35 | + channel: 'stable' |
| 36 | + } |
| 37 | + it('should work w/o required inputs', async function () { |
| 38 | + const opts = await getOptions({}, defaults) |
| 39 | + expect(opts).toHaveProperty('channel', defaults.channel) |
| 40 | + expect(opts).toHaveProperty('version') |
| 41 | + expect(opts.version).toMatch(/[0-9.]+/) |
| 42 | + }) |
| 43 | + it('should work with all inputs', async function () { |
| 44 | + const inputs = { |
| 45 | + channel: 'some channel', |
| 46 | + version: 'some version' |
| 47 | + } |
| 48 | + const opts = await getOptions(inputs, defaults) |
| 49 | + expect(opts).toEqual(inputs) |
| 50 | + }) |
| 51 | + }) |
| 52 | + describe('formatDownloadUrls', function () { |
| 53 | + it('should work for platform=linux and arch=x64', function () { |
| 54 | + const version = '1.2.3' |
| 55 | + const plat = 'linux' |
| 56 | + const arch = 'amd64' |
| 57 | + const result = formatDownloadUrls(version) |
| 58 | + expect(result).toEqual([ |
| 59 | + `https://tuf.trdl.dev/targets/releases/${version}/${plat}-${arch}/bin/trdl`, |
| 60 | + `https://tuf.trdl.dev/targets/signatures/${version}/${plat}-${arch}/bin/trdl.sig`, |
| 61 | + `https://trdl.dev/trdl-client.asc` |
| 62 | + ]) |
| 63 | + }) |
| 64 | + }) |
| 65 | + describe('Do', function () { |
| 66 | + const inputs = { |
| 67 | + channel: 'test-channel', |
| 68 | + version: 'test-version' |
| 69 | + } |
| 70 | + const defaults = { |
| 71 | + repo: 'trdl', |
| 72 | + group: '0', |
| 73 | + channel: 'stable' |
| 74 | + } |
| 75 | + beforeEach(function () { |
| 76 | + trdlCli.defaults.mockReturnValue(defaults) |
| 77 | + }) |
| 78 | + it('should not install trdl if tool cache is found', async function () { |
| 79 | + const someCache = '/path/to/tool' |
| 80 | + toolCache.find.mockReturnValueOnce(someCache) |
| 81 | + |
| 82 | + await Do(trdlCli, gpgCli, inputs) |
| 83 | + |
| 84 | + expect(toolCache.find).toHaveBeenCalledWith(defaults.repo, inputs.version) |
| 85 | + expect(trdlCli.mustExist).toHaveBeenCalled() |
| 86 | + expect(trdlCli.update).toHaveBeenCalledWith(defaults) |
| 87 | + }) |
| 88 | + it('should install trdl if tool cache is not found', async function () { |
| 89 | + const binPath = 'bin path' |
| 90 | + const sigPath = 'sig path' |
| 91 | + const ascPath = 'asc path' |
| 92 | + |
| 93 | + toolCache.downloadTool.mockResolvedValueOnce(binPath) |
| 94 | + toolCache.downloadTool.mockResolvedValueOnce(sigPath) |
| 95 | + toolCache.downloadTool.mockResolvedValueOnce(ascPath) |
| 96 | + |
| 97 | + const installedPath = '/tmp/installed/path' |
| 98 | + toolCache.cacheFile.mockResolvedValueOnce(installedPath) |
| 99 | + |
| 100 | + await Do(trdlCli, gpgCli, inputs) |
| 101 | + |
| 102 | + expect(toolCache.find).toHaveBeenCalledWith(defaults.repo, inputs.version) |
| 103 | + expect(gpgCli.mustGnuGP).toHaveBeenCalled() |
| 104 | + expect(gpgCli.import).toHaveBeenCalledWith(ascPath) |
| 105 | + expect(gpgCli.verify).toHaveBeenCalledWith(sigPath, binPath) |
| 106 | + expect(toolCache.cacheFile).toHaveBeenCalledWith(binPath, defaults.repo, defaults.repo, inputs.version) |
| 107 | + expect(fs.chmodSync).toHaveBeenCalledWith(installedPath, 0o755) |
| 108 | + expect(core.addPath).toHaveBeenCalledWith(installedPath) |
| 109 | + }) |
| 110 | + }) |
| 111 | +}) |
0 commit comments