2019-09-20 09:41:20 +07:00
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as path from 'path';
|
|
|
|
import * as utils from '../src/utils';
|
|
|
|
|
2019-09-26 04:03:39 +07:00
|
|
|
jest.mock('@actions/core', () => ({
|
|
|
|
getInput: jest.fn().mockImplementation(key => {
|
|
|
|
return ['setup-php'].indexOf(key) !== -1 ? key : '';
|
|
|
|
})
|
|
|
|
}));
|
|
|
|
|
2021-05-31 03:59:23 +07:00
|
|
|
jest.spyOn(utils, 'fetch').mockImplementation(async (url): Promise<string> => {
|
2022-02-07 14:40:17 +07:00
|
|
|
return `{ "latest": "8.0", "5.x": "5.6", "url": "${url}" }`;
|
2021-05-31 03:59:23 +07:00
|
|
|
});
|
2021-02-22 12:52:59 +07:00
|
|
|
|
2019-09-20 09:41:20 +07:00
|
|
|
async function cleanup(path: string): Promise<void> {
|
|
|
|
fs.unlink(path, error => {
|
|
|
|
if (error) {
|
|
|
|
console.log(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('Utils tests', () => {
|
2020-10-05 14:33:30 +07:00
|
|
|
it('checking readEnv', async () => {
|
2019-09-20 09:41:20 +07:00
|
|
|
process.env['test'] = 'setup-php';
|
2021-07-23 17:49:00 +07:00
|
|
|
process.env['test-hyphen'] = 'setup-php';
|
2020-10-05 14:33:30 +07:00
|
|
|
expect(await utils.readEnv('test')).toBe('setup-php');
|
2021-07-23 17:49:00 +07:00
|
|
|
expect(await utils.readEnv('TEST')).toBe('setup-php');
|
|
|
|
expect(await utils.readEnv('test_hyphen')).toBe('setup-php');
|
|
|
|
expect(await utils.readEnv('TEST_HYPHEN')).toBe('setup-php');
|
2020-10-05 14:33:30 +07:00
|
|
|
expect(await utils.readEnv('undefined')).toBe('');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('checking getInput', async () => {
|
2019-09-20 09:41:20 +07:00
|
|
|
expect(await utils.getInput('test', false)).toBe('setup-php');
|
2019-09-26 04:03:39 +07:00
|
|
|
expect(await utils.getInput('setup-php', false)).toBe('setup-php');
|
2019-09-20 09:41:20 +07:00
|
|
|
expect(await utils.getInput('DoesNotExist', false)).toBe('');
|
2020-10-05 14:33:30 +07:00
|
|
|
expect(async () => {
|
|
|
|
await utils.getInput('DoesNotExist', true);
|
|
|
|
}).rejects.toThrow('Input required and not supplied: DoesNotExist');
|
2019-09-20 09:41:20 +07:00
|
|
|
});
|
|
|
|
|
2021-02-22 12:52:59 +07:00
|
|
|
it('checking fetch', async () => {
|
|
|
|
expect(await utils.fetch('test_url')).toBe(
|
2022-02-07 14:40:17 +07:00
|
|
|
'{ "latest": "8.0", "5.x": "5.6", "url": "test_url" }'
|
2021-02-22 12:52:59 +07:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('checking parseVersion', async () => {
|
2022-02-07 14:40:17 +07:00
|
|
|
expect(await utils.parseVersion('latest')).toBe('8.0');
|
2021-02-22 12:52:59 +07:00
|
|
|
expect(await utils.parseVersion('7')).toBe('7.0');
|
|
|
|
expect(await utils.parseVersion('7.4')).toBe('7.4');
|
|
|
|
expect(await utils.parseVersion('5.x')).toBe('5.6');
|
|
|
|
expect(await utils.parseVersion('4.x')).toBe(undefined);
|
|
|
|
});
|
|
|
|
|
2019-09-20 09:41:20 +07:00
|
|
|
it('checking asyncForEach', async () => {
|
2019-11-24 03:34:12 +07:00
|
|
|
const array: Array<string> = ['a', 'b', 'c'];
|
|
|
|
let concat = '';
|
2020-11-27 01:20:49 +07:00
|
|
|
await utils.asyncForEach(
|
|
|
|
array,
|
|
|
|
async function (str: string): Promise<void> {
|
|
|
|
concat += str;
|
|
|
|
}
|
|
|
|
);
|
2019-11-24 03:34:12 +07:00
|
|
|
expect(concat).toBe('abc');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('checking asyncForEach', async () => {
|
|
|
|
expect(await utils.color('error')).toBe('31');
|
|
|
|
expect(await utils.color('success')).toBe('32');
|
|
|
|
expect(await utils.color('any')).toBe('32');
|
|
|
|
expect(await utils.color('warning')).toBe('33');
|
2019-09-20 09:41:20 +07:00
|
|
|
});
|
|
|
|
|
|
|
|
it('checking readScripts', async () => {
|
2019-11-24 03:34:12 +07:00
|
|
|
const darwin: string = fs.readFileSync(
|
2019-09-28 21:39:05 +07:00
|
|
|
path.join(__dirname, '../src/scripts/darwin.sh'),
|
2019-09-20 09:41:20 +07:00
|
|
|
'utf8'
|
|
|
|
);
|
2019-11-24 03:34:12 +07:00
|
|
|
const linux: string = fs.readFileSync(
|
2019-09-28 21:39:05 +07:00
|
|
|
path.join(__dirname, '../src/scripts/linux.sh'),
|
2019-09-20 09:41:20 +07:00
|
|
|
'utf8'
|
|
|
|
);
|
2019-11-24 03:34:12 +07:00
|
|
|
const win32: string = fs.readFileSync(
|
2019-09-28 21:39:05 +07:00
|
|
|
path.join(__dirname, '../src/scripts/win32.ps1'),
|
2019-09-20 09:41:20 +07:00
|
|
|
'utf8'
|
|
|
|
);
|
2020-03-14 09:23:43 +07:00
|
|
|
expect(await utils.readScript('darwin.sh')).toBe(darwin);
|
|
|
|
expect(await utils.readScript('darwin.sh')).toBe(darwin);
|
|
|
|
expect(await utils.readScript('linux.sh')).toBe(linux);
|
|
|
|
expect(await utils.readScript('linux.sh')).toBe(linux);
|
|
|
|
expect(await utils.readScript('win32.ps1')).toBe(win32);
|
|
|
|
expect(await utils.readScript('win32.ps1')).toBe(win32);
|
2019-09-20 09:41:20 +07:00
|
|
|
});
|
|
|
|
|
|
|
|
it('checking writeScripts', async () => {
|
2019-11-24 03:34:12 +07:00
|
|
|
const testString = 'sudo apt-get install php';
|
|
|
|
const runner_dir: string = process.env['RUNNER_TOOL_CACHE'] || '';
|
|
|
|
const script_path: string = path.join(runner_dir, 'test.sh');
|
2019-10-27 07:12:49 +07:00
|
|
|
await utils.writeScript('test.sh', testString);
|
2021-02-22 13:14:10 +07:00
|
|
|
fs.readFile(script_path, function (error: Error | null, data: Buffer) {
|
|
|
|
expect(testString).toBe(data.toString());
|
|
|
|
});
|
2019-10-27 07:12:49 +07:00
|
|
|
await cleanup(script_path);
|
2019-09-20 09:41:20 +07:00
|
|
|
});
|
|
|
|
|
|
|
|
it('checking extensionArray', async () => {
|
|
|
|
expect(await utils.extensionArray('a, b, php_c, php-d')).toEqual([
|
|
|
|
'a',
|
|
|
|
'b',
|
|
|
|
'c',
|
|
|
|
'd'
|
|
|
|
]);
|
2019-10-08 19:42:54 +07:00
|
|
|
|
2019-09-28 21:39:05 +07:00
|
|
|
expect(await utils.extensionArray('')).toEqual([]);
|
|
|
|
expect(await utils.extensionArray(' ')).toEqual([]);
|
2019-09-20 09:41:20 +07:00
|
|
|
});
|
|
|
|
|
|
|
|
it('checking INIArray', async () => {
|
2019-12-27 08:26:49 +07:00
|
|
|
expect(await utils.CSVArray('a=1, b=2, c=3')).toEqual([
|
2019-09-20 09:41:20 +07:00
|
|
|
'a=1',
|
|
|
|
'b=2',
|
|
|
|
'c=3'
|
|
|
|
]);
|
2021-01-15 15:22:18 +07:00
|
|
|
expect(await utils.CSVArray('\'a=1,2\', "b=3, 4", c=5, d=~e~')).toEqual([
|
|
|
|
'a=1,2',
|
|
|
|
'b=3, 4',
|
|
|
|
'c=5',
|
|
|
|
"d='~e~'"
|
|
|
|
]);
|
|
|
|
expect(await utils.CSVArray('a=\'1,2\', b="3, 4", c=5')).toEqual([
|
|
|
|
'a=1,2',
|
|
|
|
'b=3, 4',
|
|
|
|
'c=5'
|
|
|
|
]);
|
2021-02-22 13:14:10 +07:00
|
|
|
expect(await utils.CSVArray('\'a=1,2\', "b=3, 4", c=5, d=~e~')).toEqual([
|
|
|
|
'a=1,2',
|
|
|
|
'b=3, 4',
|
|
|
|
'c=5',
|
|
|
|
"d='~e~'"
|
|
|
|
]);
|
|
|
|
expect(await utils.CSVArray('a=\'1,2\', b="3, 4", c=5')).toEqual([
|
|
|
|
'a=1,2',
|
|
|
|
'b=3, 4',
|
|
|
|
'c=5'
|
|
|
|
]);
|
2019-12-27 08:26:49 +07:00
|
|
|
expect(await utils.CSVArray('')).toEqual([]);
|
|
|
|
expect(await utils.CSVArray(' ')).toEqual([]);
|
2019-09-20 09:41:20 +07:00
|
|
|
});
|
|
|
|
|
2019-09-26 04:03:39 +07:00
|
|
|
it('checking log', async () => {
|
2019-11-24 03:34:12 +07:00
|
|
|
const message = 'Test message';
|
2019-09-26 04:03:39 +07:00
|
|
|
|
|
|
|
let warning_log: string = await utils.log(message, 'win32', 'warning');
|
2019-10-17 03:11:13 +07:00
|
|
|
expect(warning_log).toEqual('printf "\\033[33;1m' + message + ' \\033[0m"');
|
2019-09-26 04:03:39 +07:00
|
|
|
warning_log = await utils.log(message, 'linux', 'warning');
|
2019-09-28 04:36:41 +07:00
|
|
|
expect(warning_log).toEqual('echo "\\033[33;1m' + message + '\\033[0m"');
|
2019-09-26 04:03:39 +07:00
|
|
|
warning_log = await utils.log(message, 'darwin', 'warning');
|
2019-09-28 21:39:05 +07:00
|
|
|
expect(warning_log).toEqual('echo "\\033[33;1m' + message + '\\033[0m"');
|
2019-09-26 04:03:39 +07:00
|
|
|
|
|
|
|
let error_log: string = await utils.log(message, 'win32', 'error');
|
2019-10-17 03:11:13 +07:00
|
|
|
expect(error_log).toEqual('printf "\\033[31;1m' + message + ' \\033[0m"');
|
2019-09-26 04:03:39 +07:00
|
|
|
error_log = await utils.log(message, 'linux', 'error');
|
2019-09-28 04:36:41 +07:00
|
|
|
expect(error_log).toEqual('echo "\\033[31;1m' + message + '\\033[0m"');
|
2019-09-26 04:03:39 +07:00
|
|
|
error_log = await utils.log(message, 'darwin', 'error');
|
2019-09-28 21:39:05 +07:00
|
|
|
expect(error_log).toEqual('echo "\\033[31;1m' + message + '\\033[0m"');
|
2019-09-26 04:03:39 +07:00
|
|
|
|
|
|
|
let success_log: string = await utils.log(message, 'win32', 'success');
|
2019-10-17 03:11:13 +07:00
|
|
|
expect(success_log).toEqual('printf "\\033[32;1m' + message + ' \\033[0m"');
|
2019-09-26 04:03:39 +07:00
|
|
|
success_log = await utils.log(message, 'linux', 'success');
|
2019-09-28 04:36:41 +07:00
|
|
|
expect(success_log).toEqual('echo "\\033[32;1m' + message + '\\033[0m"');
|
2019-09-26 04:03:39 +07:00
|
|
|
success_log = await utils.log(message, 'darwin', 'success');
|
2019-09-28 21:39:05 +07:00
|
|
|
expect(success_log).toEqual('echo "\\033[32;1m' + message + '\\033[0m"');
|
2019-10-04 21:04:05 +07:00
|
|
|
|
2019-10-17 03:11:13 +07:00
|
|
|
let step_log: string = await utils.stepLog(message, 'win32');
|
|
|
|
expect(step_log).toEqual('Step-Log "Test message"');
|
|
|
|
step_log = await utils.stepLog(message, 'linux');
|
|
|
|
expect(step_log).toEqual('step_log "Test message"');
|
|
|
|
step_log = await utils.stepLog(message, 'darwin');
|
|
|
|
expect(step_log).toEqual('step_log "Test message"');
|
2020-09-07 03:46:49 +07:00
|
|
|
step_log = await utils.stepLog(message, 'openbsd');
|
|
|
|
expect(step_log).toContain('Platform openbsd is not supported');
|
2019-10-17 03:11:13 +07:00
|
|
|
|
|
|
|
let add_log: string = await utils.addLog(
|
|
|
|
'tick',
|
|
|
|
'xdebug',
|
|
|
|
'enabled',
|
|
|
|
'win32'
|
2019-10-04 21:04:05 +07:00
|
|
|
);
|
2019-10-17 03:11:13 +07:00
|
|
|
expect(add_log).toEqual('Add-Log "tick" "xdebug" "enabled"');
|
|
|
|
add_log = await utils.addLog('tick', 'xdebug', 'enabled', 'linux');
|
|
|
|
expect(add_log).toEqual('add_log "tick" "xdebug" "enabled"');
|
|
|
|
add_log = await utils.addLog('tick', 'xdebug', 'enabled', 'darwin');
|
|
|
|
expect(add_log).toEqual('add_log "tick" "xdebug" "enabled"');
|
2020-09-07 03:46:49 +07:00
|
|
|
add_log = await utils.addLog('tick', 'xdebug', 'enabled', 'openbsd');
|
|
|
|
expect(add_log).toContain('Platform openbsd is not supported');
|
2019-09-26 04:03:39 +07:00
|
|
|
});
|
|
|
|
|
2019-09-28 10:19:11 +07:00
|
|
|
it('checking getExtensionPrefix', async () => {
|
|
|
|
expect(await utils.getExtensionPrefix('extensionDoesNotExist')).toEqual(
|
|
|
|
'extension'
|
|
|
|
);
|
|
|
|
expect(await utils.getExtensionPrefix('xsl')).toEqual('extension');
|
|
|
|
expect(await utils.getExtensionPrefix('xdebug')).toEqual('zend_extension');
|
|
|
|
expect(await utils.getExtensionPrefix('opcache')).toEqual('zend_extension');
|
|
|
|
});
|
2019-10-17 03:11:13 +07:00
|
|
|
|
|
|
|
it('checking suppressOutput', async () => {
|
2022-04-10 04:40:28 +07:00
|
|
|
expect(await utils.suppressOutput('win32')).toEqual(' ');
|
|
|
|
expect(await utils.suppressOutput('linux')).toEqual(' ');
|
|
|
|
expect(await utils.suppressOutput('darwin')).toEqual(' ');
|
2020-09-07 03:46:49 +07:00
|
|
|
expect(await utils.suppressOutput('openbsd')).toContain(
|
|
|
|
'Platform openbsd is not supported'
|
2019-10-17 03:11:13 +07:00
|
|
|
);
|
|
|
|
});
|
2019-09-28 10:19:11 +07:00
|
|
|
});
|