diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts new file mode 100644 index 0000000..28e1d66 --- /dev/null +++ b/__tests__/main.test.ts @@ -0,0 +1,82 @@ +/** + * Unit tests for the action's main functionality, src/main.ts + * + * These should be run as if the action was called from a workflow. + * Specifically, the inputs listed in `action.yml` should be set as environment + * variables following the pattern `INPUT_`. + */ + +import * as core from '@actions/core' +import * as tc from '@actions/tool-cache' +import os from 'os' +import * as main from '../src/main' + +// // Mock the action's main function +const runMock = jest.spyOn(main, 'run') + +jest.mock('os') + +let errorMock: jest.SpyInstance +let getInputMock: jest.SpyInstance +let addPathMock: jest.SpyInstance +let downloadToolMock: jest.SpyInstance +let extractTarMock: jest.SpyInstance + +describe('action', () => { + beforeEach(() => { + jest.clearAllMocks() + + getInputMock = jest.spyOn(core, 'getInput').mockImplementation() + addPathMock = jest.spyOn(core, 'addPath').mockImplementation() + errorMock = jest.spyOn(core, 'setFailed').mockImplementation() + downloadToolMock = jest.spyOn(tc, 'downloadTool').mockImplementation() + extractTarMock = jest.spyOn(tc, 'extractTar').mockImplementation() + + os.platform = jest.fn().mockReturnValue('linux') + os.arch = jest.fn().mockReturnValue('amd64') + }) + + it('sets the version output', async () => { + // Set the action's inputs as return values from core.getInput() + getInputMock.mockImplementation((name: string): string => { + switch (name) { + case 'version': + return 'v2.7.2' + default: + return '' + } + }) + + downloadToolMock.mockImplementation((dlUrl: string): string => { + switch (dlUrl) { + case 'https://github.com/dbsnapper/dbsnapper/releases/download/v2.7.2/dbsnapper_2.7.2_linux_amd64.tar.gz': + return 'fake-tar-path' + default: + return '' + } + }) + extractTarMock.mockImplementation((tarPath: string): string => { + switch (tarPath) { + case 'fake-tar-path': + return '/path/to/tarball' + default: + return '' + } + }) + addPathMock.mockImplementation((cliPath: string): void => { + switch (cliPath) { + case '/path/to/tarball': + return + default: + throw new Error('test: invalid cli path') + } + }) + + await main.run() + expect(runMock).toHaveReturned() + expect(errorMock).not.toHaveBeenCalled() + expect(downloadToolMock).toHaveBeenCalled() + expect(extractTarMock).toHaveBeenCalled() + expect(addPathMock).toHaveBeenCalled() + }) +}) diff --git a/__tests__/util.test.ts b/__tests__/util.test.ts new file mode 100644 index 0000000..4a64ce7 --- /dev/null +++ b/__tests__/util.test.ts @@ -0,0 +1,30 @@ +import { expect, test } from '@jest/globals' +import { getDownloadUrl } from '../src/util' + +import os from 'os' + +jest.mock('os') + +describe('util.ts', () => { + test('get latest download url', async () => { + os.platform = jest.fn().mockReturnValue('linux') + os.arch = jest.fn().mockReturnValue('amd64') + + await expect(getDownloadUrl('latest')).resolves.toBeTruthy() + }) + + test('get specific version download url', async () => { + const version = '2.7.2' + const ops = 'linux' + const arch = 'amd64' + const filename = `dbsnapper_${version}_${ops}_${arch}` + + os.platform = jest.fn().mockReturnValue(ops) + os.arch = jest.fn().mockReturnValue(arch) + + const url = await getDownloadUrl(version) + expect(url).toEqual( + `https://github.com/dbsnapper/dbsnapper/releases/download/v${version}/${filename}.tar.gz` + ) + }) +}) diff --git a/badges/coverage.svg b/badges/coverage.svg index 102895f..45ce1d4 100644 --- a/badges/coverage.svg +++ b/badges/coverage.svg @@ -1 +1 @@ -Coverage: 20.83%Coverage20.83% \ No newline at end of file +Coverage: 83.33%Coverage83.33% \ No newline at end of file