Add unit tests for entrypoint function

This commit is contained in:
Sergei Zharinov 2022-05-12 18:57:55 +03:00
parent f1c081311f
commit 956a521fe1
3 changed files with 190 additions and 65 deletions

View File

@ -0,0 +1,123 @@
import * as _core from '@actions/core'
import * as _lein from '../src/leiningen'
import * as _boot from '../src/boot'
import * as _cli from '../src/cli'
import * as _bb from '../src/babashka'
import * as _utils from '../src/utils'
import {run} from '../src/entrypoint'
jest.mock('@actions/core')
const core: jest.Mocked<typeof _core> = _core as never
jest.mock('../src/leiningen')
const lein: jest.Mocked<typeof _lein> = _lein as never
jest.mock('../src/boot')
const boot: jest.Mocked<typeof _boot> = _boot as never
jest.mock('../src/cli')
const cli: jest.Mocked<typeof _cli> = _cli as never
jest.mock('../src/babashka')
const bb: jest.Mocked<typeof _bb> = _bb as never
jest.mock('../src/utils')
const utils: jest.Mocked<typeof _utils> = _utils as never
describe('setup-clojure', () => {
let inputs: Record<string, string> = {}
beforeEach(() => {
jest.resetAllMocks()
inputs = {}
core.getInput.mockImplementation(key => inputs[key])
})
it('sets up Leiningen', async () => {
inputs['lein'] = '1.2.3'
inputs['github-token'] = 'abc'
await run()
expect(lein.setup).toHaveBeenCalledWith('1.2.3', 'token abc')
})
it('sets up Boot', async () => {
inputs['boot'] = '1.2.3'
inputs['github-token'] = 'abc'
await run()
expect(boot.setup).toHaveBeenCalledWith('1.2.3', 'token abc')
})
it('throws on Boot setup in Windows', async () => {
inputs['boot'] = '1.2.3'
inputs['github-token'] = 'abc'
utils.isWindows.mockReturnValue(true)
await run()
expect(core.setFailed).toHaveBeenCalledWith(
'Boot on windows is not supported yet.'
)
})
it('sets up Clojure CLI tools from deprecated `tools-deps` option', async () => {
inputs['tools-deps'] = '1.2.3'
await run()
expect(cli.setup).toHaveBeenCalledWith('1.2.3')
})
it('sets up Clojure CLI tools for Windows from deprecated `tools-deps` option', async () => {
inputs['tools-deps'] = '1.2.3'
utils.isWindows.mockReturnValue(true)
await run()
expect(cli.setupWindows).toHaveBeenCalledWith('1.2.3')
})
it('sets up Clojure CLI tools', async () => {
inputs['cli'] = '1.2.3'
await run()
expect(cli.setup).toHaveBeenCalledWith('1.2.3')
})
it('sets up Clojure CLI tools for Windows', async () => {
inputs['cli'] = '1.2.3'
utils.isWindows.mockReturnValue(true)
await run()
expect(cli.setupWindows).toHaveBeenCalledWith('1.2.3')
})
it('sets up Babashka', async () => {
inputs['bb'] = '1.2.3'
inputs['github-token'] = 'abc'
await run()
expect(bb.setup).toHaveBeenCalledWith('1.2.3', 'token abc')
})
it('throws if none of Clojure tools is specified', async () => {
await run()
expect(core.setFailed).toHaveBeenCalledWith(
'You must specify at least one clojure tool.'
)
})
it('coerce non-Error exception to string', async () => {
inputs['bb'] = '1.2.3'
bb.setup.mockRejectedValueOnce('Unknown failure')
await run()
expect(core.setFailed).toHaveBeenCalledWith('Unknown failure')
})
})

66
src/entrypoint.ts Normal file
View File

@ -0,0 +1,66 @@
import * as core from '@actions/core'
import * as lein from './leiningen'
import * as boot from './boot'
import * as cli from './cli'
import * as bb from './babashka'
import * as utils from './utils'
export async function run(): Promise<void> {
try {
const LEIN_VERSION = core.getInput('lein')
const BOOT_VERSION = core.getInput('boot')
const TDEPS_VERSION = core.getInput('tools-deps')
const CLI_VERSION = core.getInput('cli')
const BB_VERSION = core.getInput('bb')
const githubToken = core.getInput('github-token')
const githubAuth = githubToken ? `token ${githubToken}` : undefined
if (LEIN_VERSION) {
lein.setup(LEIN_VERSION, githubAuth)
}
const IS_WINDOWS = utils.isWindows()
if (BOOT_VERSION) {
if (IS_WINDOWS) {
throw new Error('Boot on windows is not supported yet.')
}
boot.setup(BOOT_VERSION, githubAuth)
}
if (CLI_VERSION) {
if (IS_WINDOWS) {
cli.setupWindows(CLI_VERSION)
} else {
cli.setup(CLI_VERSION)
}
}
if (TDEPS_VERSION && !CLI_VERSION) {
if (IS_WINDOWS) {
cli.setupWindows(TDEPS_VERSION)
}
cli.setup(TDEPS_VERSION)
}
if (BB_VERSION) {
await bb.setup(BB_VERSION, githubAuth)
}
if (
!(
BOOT_VERSION ||
LEIN_VERSION ||
TDEPS_VERSION ||
CLI_VERSION ||
BB_VERSION
)
) {
throw new Error('You must specify at least one clojure tool.')
}
} catch (err) {
const error = err instanceof Error ? err.message : String(err)
core.setFailed(error)
}
}

View File

@ -1,67 +1,3 @@
import * as core from '@actions/core'
import * as lein from './leiningen'
import * as boot from './boot'
import * as cli from './cli'
import * as bb from './babashka'
import * as utils from './utils'
const IS_WINDOWS = utils.isWindows()
async function run(): Promise<void> {
try {
const LEIN_VERSION = core.getInput('lein')
const BOOT_VERSION = core.getInput('boot')
const TDEPS_VERSION = core.getInput('tools-deps')
const CLI_VERSION = core.getInput('cli')
const BB_VERSION = core.getInput('bb')
const githubToken = core.getInput('github-token')
const githubAuth = githubToken ? `token ${githubToken}` : undefined
if (LEIN_VERSION) {
lein.setup(LEIN_VERSION, githubAuth)
}
if (BOOT_VERSION) {
if (IS_WINDOWS) {
throw new Error('Boot on windows is not supported yet.')
}
boot.setup(BOOT_VERSION, githubAuth)
}
if (CLI_VERSION) {
if (IS_WINDOWS) {
cli.setupWindows(CLI_VERSION)
} else {
cli.setup(CLI_VERSION)
}
}
if (TDEPS_VERSION && !CLI_VERSION) {
if (IS_WINDOWS) {
cli.setupWindows(TDEPS_VERSION)
}
cli.setup(TDEPS_VERSION)
}
if (BB_VERSION) {
await bb.setup(BB_VERSION, githubAuth)
}
if (
!(
BOOT_VERSION ||
LEIN_VERSION ||
TDEPS_VERSION ||
CLI_VERSION ||
BB_VERSION
)
) {
throw new Error('You must specify at least one clojure tool.')
}
} catch (error) {
core.setFailed((error as Error).message)
}
}
import {run} from './entrypoint'
run()