mirror of
https://github.com/robinraju/release-downloader.git
synced 2026-10-10 09:14:15 +03:00
* Update action.yml * Update download-settings.ts * Update input-helper.ts * Update main.ts * Update action.yml * Add files via upload * Update main.test.ts * Update README.md * Consolidate extract path and output path inputs, add tests * Remove dist --------- Co-authored-by: Robin Raju <robinraju@users.noreply.github.com>
157 lines
4.4 KiB
TypeScript
157 lines
4.4 KiB
TypeScript
import { jest } from '@jest/globals'
|
|
import * as os from 'os'
|
|
import * as path from 'path'
|
|
|
|
import * as core from '../__fixtures__/core.js'
|
|
import { ConfigError } from '../src/errors.js'
|
|
|
|
jest.unstable_mockModule('@actions/core', () => core)
|
|
|
|
const { getInputs } = await import('../src/input-helper.js')
|
|
|
|
describe('getInputs', () => {
|
|
const workspacePath = path.join(os.tmpdir(), 'release-downloader-workspace')
|
|
|
|
let originalWorkspacePath: string | undefined
|
|
let stringInputs: Record<string, string>
|
|
let booleanInputs: Record<string, boolean>
|
|
|
|
beforeEach(() => {
|
|
originalWorkspacePath = process.env['GITHUB_WORKSPACE']
|
|
process.env['GITHUB_WORKSPACE'] = workspacePath
|
|
|
|
stringInputs = {
|
|
repository: 'robinraju/probable-potato',
|
|
tag: '',
|
|
releaseId: '',
|
|
fileName: 'test-1.txt',
|
|
'out-file-path': 'downloads/output'
|
|
}
|
|
|
|
booleanInputs = {
|
|
latest: true,
|
|
preRelease: false,
|
|
tarBall: false,
|
|
zipBall: false,
|
|
extract: false
|
|
}
|
|
|
|
core.getInput.mockImplementation((name: string): string => {
|
|
return stringInputs[name] ?? ''
|
|
})
|
|
|
|
core.getBooleanInput.mockImplementation((name: string): boolean => {
|
|
return booleanInputs[name] ?? false
|
|
})
|
|
})
|
|
|
|
afterEach(() => {
|
|
jest.clearAllMocks()
|
|
|
|
if (originalWorkspacePath) {
|
|
process.env['GITHUB_WORKSPACE'] = originalWorkspacePath
|
|
} else {
|
|
delete process.env['GITHUB_WORKSPACE']
|
|
}
|
|
})
|
|
|
|
test('returns resolved settings relative to GITHUB_WORKSPACE', () => {
|
|
const settings = getInputs()
|
|
|
|
expect(settings).toEqual({
|
|
sourceRepoPath: 'robinraju/probable-potato',
|
|
isLatest: true,
|
|
preRelease: false,
|
|
tag: '',
|
|
id: '',
|
|
fileName: 'test-1.txt',
|
|
tarBall: false,
|
|
zipBall: false,
|
|
extractAssets: false,
|
|
outFilePath: path.resolve(workspacePath, 'downloads/output'),
|
|
extractPath: path.resolve(workspacePath, 'downloads/output')
|
|
})
|
|
})
|
|
|
|
test('uses the workspace root when out-file-path is empty', () => {
|
|
stringInputs['out-file-path'] = ''
|
|
|
|
const settings = getInputs()
|
|
|
|
expect(settings.outFilePath).toBe(workspacePath)
|
|
})
|
|
|
|
test('uses the workspace root for extractPath when both inputs are empty', () => {
|
|
stringInputs['out-file-path'] = ''
|
|
stringInputs['extract-path'] = ''
|
|
|
|
const settings = getInputs()
|
|
|
|
expect(settings.extractPath).toBe(workspacePath)
|
|
expect(settings.outFilePath).toBe(workspacePath)
|
|
})
|
|
|
|
test('honors an explicit extract-path independently of out-file-path', () => {
|
|
stringInputs['extract-path'] = 'extracted/assets'
|
|
|
|
const settings = getInputs()
|
|
|
|
expect(settings.outFilePath).toBe(
|
|
path.resolve(workspacePath, 'downloads/output')
|
|
)
|
|
expect(settings.extractPath).toBe(
|
|
path.resolve(workspacePath, 'extracted/assets')
|
|
)
|
|
})
|
|
|
|
test('extract-path overrides out-file-path when both are set', () => {
|
|
stringInputs['out-file-path'] = 'downloads/output'
|
|
stringInputs['extract-path'] = 'somewhere/else'
|
|
|
|
const settings = getInputs()
|
|
|
|
expect(settings.extractPath).toBe(
|
|
path.resolve(workspacePath, 'somewhere/else')
|
|
)
|
|
expect(settings.extractPath).not.toBe(settings.outFilePath)
|
|
})
|
|
|
|
test('throws a config error when GITHUB_WORKSPACE is missing', () => {
|
|
delete process.env['GITHUB_WORKSPACE']
|
|
|
|
expect(() => getInputs()).toThrow(ConfigError)
|
|
expect(() => getInputs()).toThrow('$GITHUB_WORKSPACE not defined')
|
|
})
|
|
|
|
test('throws a config error for an invalid repository path', () => {
|
|
stringInputs['repository'] = 'downloader'
|
|
|
|
expect(() => getInputs()).toThrow(ConfigError)
|
|
expect(() => getInputs()).toThrow(
|
|
"Invalid repository 'downloader'. Expected format {owner}/{repo}."
|
|
)
|
|
})
|
|
|
|
test('throws a config error when tag and releaseId coexist', () => {
|
|
booleanInputs['latest'] = false
|
|
stringInputs['tag'] = 'v1.0.0'
|
|
stringInputs['releaseId'] = '123'
|
|
|
|
expect(() => getInputs()).toThrow(ConfigError)
|
|
expect(() => getInputs()).toThrow(
|
|
"Invalid inputs. latest=false, tag=v1.0.0, and releaseId=123 can't coexist"
|
|
)
|
|
})
|
|
|
|
test('throws a config error when latest, tag, and releaseId coexist', () => {
|
|
booleanInputs['latest'] = true
|
|
stringInputs['tag'] = 'v1.0.0'
|
|
stringInputs['releaseId'] = '123'
|
|
|
|
expect(() => getInputs()).toThrow(ConfigError)
|
|
expect(() => getInputs()).toThrow(
|
|
"Invalid inputs. latest=true, tag=v1.0.0, and releaseId=123 can't coexist"
|
|
)
|
|
})
|
|
})
|