setup-clojure/__tests__/leiningen.test.ts

141 lines
4.1 KiB
TypeScript
Raw Normal View History

2022-02-21 23:49:37 +03:00
import * as _core from '@actions/core'
import * as _exec from '@actions/exec'
import * as _io from '@actions/io'
2022-03-20 14:17:06 +03:00
import * as _fs from '../src/fs'
2022-02-21 23:49:37 +03:00
import * as _tc from '@actions/tool-cache'
import * as _os from 'os'
import {join} from 'path'
2022-07-19 12:37:55 +02:00
import {VERSION} from '../src/version'
2019-10-07 12:25:25 +02:00
2022-02-21 23:49:37 +03:00
const toolPath = join(__dirname, 'runner', 'tools', 'leiningen')
const tempPath = join(__dirname, 'runner', 'temp', 'leiningen')
const downloadPath = join(__dirname, 'runner', 'download')
const cachePath = join(__dirname, 'runner', 'cache')
2019-10-07 12:25:25 +02:00
2020-08-20 11:39:44 +02:00
import * as leiningen from '../src/leiningen'
2022-02-21 23:49:37 +03:00
jest.mock('@actions/core')
const core: jest.Mocked<typeof _core> = _core as never
jest.mock('@actions/exec')
const exec: jest.Mocked<typeof _exec> = _exec as never
jest.mock('@actions/io')
const io: jest.Mocked<typeof _io> = _io as never
jest.mock('@actions/tool-cache')
const tc: jest.Mocked<typeof _tc> = _tc as never
jest.mock('os')
const os: jest.Mocked<typeof _os> = _os as never
2019-10-07 12:25:25 +02:00
2022-03-20 14:17:06 +03:00
jest.mock('../src/fs')
const fs: jest.Mocked<typeof _fs> = _fs as never
2019-10-07 12:25:25 +02:00
describe('leiningen tests', () => {
2022-02-21 23:49:37 +03:00
beforeEach(async () => {
process.env['RUNNER_TOOL_CACHE'] = toolPath
process.env['RUNNER_TEMP'] = tempPath
os.arch.mockReturnValue('x64')
os.platform.mockReturnValue('linux')
jest.spyOn(global.Math, 'random').mockReturnValue(1)
})
afterEach(async () => {
jest.spyOn(global.Math, 'random').mockRestore()
jest.resetAllMocks()
delete process.env['RUNNER_TOOL_CACHE']
delete process.env['RUNNER_TEMP']
})
2019-10-07 12:25:25 +02:00
2020-08-20 11:39:44 +02:00
it('Throws if invalid version', async () => {
2022-02-21 23:49:37 +03:00
const msg = 'Unexpected HTTP response: 403'
tc.downloadTool.mockRejectedValueOnce(new Error(msg))
await expect(leiningen.setup('1000')).rejects.toThrow(msg)
2020-08-20 11:39:44 +02:00
})
2019-10-07 12:25:25 +02:00
2020-08-20 11:39:44 +02:00
it('Install leiningen with normal version', async () => {
2022-02-21 23:49:37 +03:00
tc.downloadTool.mockResolvedValueOnce(downloadPath)
2022-03-20 14:17:06 +03:00
fs.stat.mockResolvedValueOnce({isFile: () => true} as never)
2022-02-21 23:49:37 +03:00
tc.cacheDir.mockResolvedValueOnce(cachePath)
2020-08-20 11:39:44 +02:00
await leiningen.setup('2.9.1')
2022-02-21 23:49:37 +03:00
expect(io.mkdirP).toHaveBeenNthCalledWith(
1,
join(tempPath, 'temp_2000000000')
)
expect(io.mkdirP).toHaveBeenNthCalledWith(
2,
join(tempPath, 'temp_2000000000', 'leiningen', 'bin')
)
expect(exec.exec.mock.calls[0]).toMatchObject([
'./lein version',
[],
{
cwd: join(tempPath, 'temp_2000000000', 'leiningen', 'bin'),
env: {
LEIN_HOME: join(tempPath, 'temp_2000000000', 'leiningen')
}
}
])
expect(tc.cacheDir).toHaveBeenCalledWith(
join(tempPath, 'temp_2000000000', 'leiningen'),
2021-12-01 17:52:09 +01:00
'Leiningen',
2022-07-19 12:37:55 +02:00
`2.9.1-${VERSION}`
2021-12-01 17:52:09 +01:00
)
2022-02-21 23:49:37 +03:00
expect(core.exportVariable).toHaveBeenCalledWith(
'LEIN_HOME',
join(cachePath)
)
expect(core.addPath).toHaveBeenCalledWith(join(cachePath, 'bin'))
})
2019-10-08 12:46:58 +02:00
2020-08-20 11:39:44 +02:00
it('Install latest leiningen', async () => {
2022-02-21 23:49:37 +03:00
tc.downloadTool.mockResolvedValueOnce(downloadPath)
2022-03-20 14:17:06 +03:00
fs.stat.mockResolvedValueOnce({isFile: () => true} as never)
2022-02-21 23:49:37 +03:00
tc.cacheDir.mockResolvedValueOnce(cachePath)
2019-10-08 12:46:58 +02:00
2022-02-21 23:49:37 +03:00
await leiningen.setup('latest')
2019-10-07 13:57:39 +02:00
2022-02-21 23:49:37 +03:00
expect(io.mkdirP).toHaveBeenNthCalledWith(
1,
join(tempPath, 'temp_2000000000')
)
expect(io.mkdirP).toHaveBeenNthCalledWith(
2,
join(tempPath, 'temp_2000000000', 'leiningen', 'bin')
)
expect(exec.exec.mock.calls[0]).toMatchObject([
'./lein version',
[],
{
cwd: join(tempPath, 'temp_2000000000', 'leiningen', 'bin'),
env: {
LEIN_HOME: join(tempPath, 'temp_2000000000', 'leiningen')
}
}
])
expect(tc.cacheDir).toHaveBeenCalledWith(
join(tempPath, 'temp_2000000000', 'leiningen'),
2020-08-20 11:39:44 +02:00
'Leiningen',
2022-07-19 12:37:55 +02:00
`latest.0.0-${VERSION}`
2020-08-20 11:39:44 +02:00
)
2022-02-21 23:49:37 +03:00
expect(core.exportVariable).toHaveBeenCalledWith(
'LEIN_HOME',
join(cachePath)
)
expect(core.addPath).toHaveBeenCalledWith(join(cachePath, 'bin'))
2020-08-20 11:39:44 +02:00
})
2019-10-07 13:57:39 +02:00
2022-02-21 23:49:37 +03:00
it('Uses version of leiningen installed in cache', async () => {
tc.find.mockReturnValue(cachePath)
await leiningen.setup('2.9.1')
expect(core.exportVariable).toHaveBeenCalledWith(
'LEIN_HOME',
join(cachePath)
2020-08-20 11:39:44 +02:00
)
2022-02-21 23:49:37 +03:00
expect(core.addPath).toHaveBeenCalledWith(join(cachePath, 'bin'))
2020-08-20 11:39:44 +02:00
})
})