mirror of
https://github.com/shivammathur/setup-php.git
synced 2025-09-08 05:44:07 +07:00
Improve error handling in utils.fetch and its uses
Minor fixes in tests Revert to ES2019
This commit is contained in:
@ -32,15 +32,29 @@ function getData(data: IData): Record<string, string> {
|
||||
|
||||
jest
|
||||
.spyOn(utils, 'fetch')
|
||||
.mockImplementation(async (url: string): Promise<string> => {
|
||||
return `[{"ref": "refs/tags/1.2.3", "url": "${url}"}]`;
|
||||
.mockImplementation(async (url: string, token?: string): Promise<
|
||||
Record<string, string>
|
||||
> => {
|
||||
if (!token || token === 'valid_token') {
|
||||
return {data: `[{"ref": "refs/tags/1.2.3", "url": "${url}"}]`};
|
||||
} else if (token === 'no_data') {
|
||||
return {data: '[]'};
|
||||
} else {
|
||||
return {error: 'Invalid token'};
|
||||
}
|
||||
});
|
||||
|
||||
describe('Tools tests', () => {
|
||||
it('checking getToolSemver', async () => {
|
||||
it.each`
|
||||
token | version
|
||||
${'invalid_token'} | ${'1.2'}
|
||||
${'valid_token'} | ${'1.2.3'}
|
||||
${''} | ${'1.2.3'}
|
||||
`('checking getToolSemver: $token', async ({token, version}) => {
|
||||
process.env['COMPOSER_TOKEN'] = token;
|
||||
expect(
|
||||
await tools.getToolSemver(getData({tool: 'tool', version: 'latest'}))
|
||||
).toBe('1.2.3');
|
||||
await tools.getToolSemver(getData({tool: 'tool', version: '1.2'}))
|
||||
).toBe(version);
|
||||
});
|
||||
|
||||
it.each`
|
||||
@ -90,49 +104,22 @@ describe('Tools tests', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it.each([
|
||||
[
|
||||
['a', 'b'],
|
||||
['composer', 'a', 'b']
|
||||
],
|
||||
[
|
||||
['a', 'b', 'composer'],
|
||||
['composer', 'a', 'b']
|
||||
],
|
||||
[
|
||||
['a', 'b', 'composer:1.2.3'],
|
||||
['composer:1.2.3', 'a', 'b']
|
||||
],
|
||||
[
|
||||
['a', 'b', 'composer:v1.2.3'],
|
||||
['composer:1.2.3', 'a', 'b']
|
||||
],
|
||||
[
|
||||
['a', 'b', 'composer:snapshot'],
|
||||
['composer:snapshot', 'a', 'b']
|
||||
],
|
||||
[
|
||||
['a', 'b', 'composer:preview'],
|
||||
['composer:preview', 'a', 'b']
|
||||
],
|
||||
[
|
||||
['a', 'b', 'composer:1'],
|
||||
['composer:1', 'a', 'b']
|
||||
],
|
||||
[
|
||||
['a', 'b', 'composer:2'],
|
||||
['composer:2', 'a', 'b']
|
||||
],
|
||||
[
|
||||
['a', 'b', 'composer:v1'],
|
||||
['composer:1', 'a', 'b']
|
||||
],
|
||||
[
|
||||
['a', 'b', 'composer:v2'],
|
||||
['composer:2', 'a', 'b']
|
||||
]
|
||||
])('checking filterList', async (input_list, filtered_list) => {
|
||||
expect(await tools.filterList(input_list)).toStrictEqual(filtered_list);
|
||||
it.each`
|
||||
input_list | filtered_list
|
||||
${'a, b'} | ${'composer, a, b'}
|
||||
${'a, b, composer'} | ${'composer, a, b'}
|
||||
${'a, b, composer:1.2.3'} | ${'composer:1.2.3, a, b'}
|
||||
${'a, b, composer:v1.2.3'} | ${'composer:1.2.3, a, b'}
|
||||
${'a, b, composer:snapshot'} | ${'composer:snapshot, a, b'}
|
||||
${'a, b, composer:preview'} | ${'composer:preview, a, b'}
|
||||
${'a, b, composer:1'} | ${'composer:1, a, b'}
|
||||
${'a, b, composer:2'} | ${'composer:2, a, b'}
|
||||
${'a, b, composer:v1'} | ${'composer:1, a, b'}
|
||||
${'a, b, composer:v2'} | ${'composer:2, a, b'}
|
||||
`('checking filterList $input_list', async ({input_list, filtered_list}) => {
|
||||
expect(await tools.filterList(input_list.split(', '))).toStrictEqual(
|
||||
filtered_list.split(', ')
|
||||
);
|
||||
});
|
||||
|
||||
it.each`
|
||||
@ -174,7 +161,7 @@ describe('Tools tests', () => {
|
||||
${'darwin'} | ${'add_tool https://example.com/tool.phar tool "-v"'}
|
||||
${'win32'} | ${'Add-Tool https://example.com/tool.phar tool "-v"'}
|
||||
${'openbsd'} | ${'Platform openbsd is not supported'}
|
||||
`('checking addPackage: $tool, $os_version', async ({os_version, script}) => {
|
||||
`('checking addPackage: $os_version', async ({os_version, script}) => {
|
||||
const data = getData({
|
||||
tool: 'tool',
|
||||
version: 'latest',
|
||||
@ -474,4 +461,14 @@ describe('Tools tests', () => {
|
||||
`('checking composer setup: $tools_csv', async ({tools_csv, script}) => {
|
||||
expect(await tools.addTools(tools_csv, '7.4', 'linux')).toContain(script);
|
||||
});
|
||||
|
||||
it.each`
|
||||
tools_csv | token | script
|
||||
${'cs2pr:1.2'} | ${'invalid_token'} | ${'add_log "$cross" "cs2pr" "Invalid token"'}
|
||||
${'phpunit:1.2'} | ${'invalid_token'} | ${'add_log "$cross" "phpunit" "Invalid token"'}
|
||||
${'phpunit:0.1'} | ${'no_data'} | ${'add_log "$cross" "phpunit" "No version found with prefix 0.1."'}
|
||||
`('checking error: $tools_csv', async ({tools_csv, token, script}) => {
|
||||
process.env['COMPOSER_TOKEN'] = token;
|
||||
expect(await tools.addTools(tools_csv, '7.4', 'linux')).toContain(script);
|
||||
});
|
||||
});
|
||||
|
@ -8,10 +8,6 @@ jest.mock('@actions/core', () => ({
|
||||
})
|
||||
}));
|
||||
|
||||
jest.spyOn(utils, 'fetch').mockImplementation(async (url): Promise<string> => {
|
||||
return `{ "latest": "8.0", "5.x": "5.6", "url": "${url}" }`;
|
||||
});
|
||||
|
||||
async function cleanup(path: string): Promise<void> {
|
||||
fs.unlink(path, error => {
|
||||
if (error) {
|
||||
@ -38,16 +34,29 @@ describe('Utils tests', () => {
|
||||
});
|
||||
|
||||
it('checking fetch', async () => {
|
||||
expect(await utils.fetch('test_url')).toBe(
|
||||
'{ "latest": "8.0", "5.x": "5.6", "url": "test_url" }'
|
||||
);
|
||||
process.env['COMPOSER_TOKEN'] = 'GITHUB_TOKEN';
|
||||
expect(await utils.fetch('test_url')).toBe(
|
||||
'{ "latest": "8.0", "5.x": "5.6", "url": "test_url" }'
|
||||
);
|
||||
const manifest = await utils.getManifestURL();
|
||||
let response: Record<string, string> = await utils.fetch(manifest);
|
||||
expect(response.error).toBe(undefined);
|
||||
expect(response.data).toContain('latest');
|
||||
|
||||
response = await utils.fetch(manifest, 'invalid_token');
|
||||
expect(response.error).toBe('404: Not Found');
|
||||
});
|
||||
|
||||
it('checking getManifestURL', async () => {
|
||||
expect(await utils.getManifestURL()).toContain('php-versions.json');
|
||||
});
|
||||
|
||||
it('checking parseVersion', async () => {
|
||||
jest.spyOn(utils, 'fetch').mockImplementation(async (url, token?): Promise<
|
||||
Record<string, string>
|
||||
> => {
|
||||
if (!token || token === 'valid_token') {
|
||||
return {data: `{ "latest": "8.0", "5.x": "5.6", "url": "${url}" }`};
|
||||
} else {
|
||||
return {error: 'Invalid token'};
|
||||
}
|
||||
});
|
||||
expect(await utils.parseVersion('latest')).toBe('8.0');
|
||||
expect(await utils.parseVersion('7')).toBe('7.0');
|
||||
expect(await utils.parseVersion('7.4')).toBe('7.4');
|
||||
|
Reference in New Issue
Block a user