Skip to content

Commit

Permalink
minor updates
Browse files Browse the repository at this point in the history
  • Loading branch information
joescharf committed Jun 19, 2024
1 parent 84899c6 commit d6aba68
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 1 deletion.
82 changes: 82 additions & 0 deletions __tests__/main.test.ts
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()
})
})
30 changes: 30 additions & 0 deletions __tests__/util.test.ts
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`
)
})
})
2 changes: 1 addition & 1 deletion badges/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d6aba68

Please sign in to comment.