generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/main' into dependabot/npm_and_ya…
…rn/npm-development-5070d215b2
- Loading branch information
Showing
3 changed files
with
113 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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_<INPUT_NAME>`. | ||
*/ | ||
|
||
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() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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` | ||
) | ||
}) | ||
}) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.