setup-php/__tests__/utils.test.ts

175 lines
6.8 KiB
TypeScript
Raw Normal View History

2019-09-20 09:41:20 +07:00
import * as fs from 'fs';
import * as path from 'path';
import * as utils from '../src/utils';
jest.mock('@actions/core', () => ({
getInput: jest.fn().mockImplementation(key => {
return ['setup-php'].indexOf(key) !== -1 ? key : '';
})
}));
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', () => {
it('checking getInput', async () => {
process.env['test'] = 'setup-php';
process.env['undefined'] = '';
2019-09-20 09:41:20 +07:00
expect(await utils.getInput('test', false)).toBe('setup-php');
expect(await utils.getInput('undefined', false)).toBe('');
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('');
});
it('checking asyncForEach', async () => {
2019-11-24 03:34:12 +07:00
const array: Array<string> = ['a', 'b', 'c'];
let concat = '';
await utils.asyncForEach(array, async function(str: string): Promise<void> {
concat += str;
2019-09-20 09:41:20 +07:00
});
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(
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(
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(
path.join(__dirname, '../src/scripts/win32.ps1'),
2019-09-20 09:41:20 +07:00
'utf8'
);
2019-11-30 04:59:00 +07:00
expect(await utils.readScript('darwin.sh', '7.4', 'darwin')).toBe(darwin);
expect(await utils.readScript('darwin.sh', '7.3', 'darwin')).toBe(darwin);
expect(await utils.readScript('linux.sh', '7.4', 'linux')).toBe(linux);
expect(await utils.readScript('linux.sh', '7.3', 'linux')).toBe(linux);
expect(await utils.readScript('win32.ps1', '7.4', 'win32')).toBe(win32);
expect(await utils.readScript('win32.ps1', '7.3', 'win32')).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);
2019-11-24 03:34:12 +07:00
await fs.readFile(script_path, function(error: Error | null, data: Buffer) {
2019-09-20 09:41:20 +07:00
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
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'
]);
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
});
it('checking log', async () => {
2019-11-24 03:34:12 +07:00
const message = 'Test message';
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"');
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"');
warning_log = await utils.log(message, 'darwin', 'warning');
expect(warning_log).toEqual('echo "\\033[33;1m' + message + '\\033[0m"');
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"');
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"');
error_log = await utils.log(message, 'darwin', 'error');
expect(error_log).toEqual('echo "\\033[31;1m' + message + '\\033[0m"');
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"');
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"');
success_log = await utils.log(message, 'darwin', 'success');
expect(success_log).toEqual('echo "\\033[32;1m' + message + '\\033[0m"');
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"');
step_log = await utils.stepLog(message, 'fedora');
expect(step_log).toContain('Platform fedora is not supported');
let add_log: string = await utils.addLog(
'tick',
'xdebug',
'enabled',
'win32'
);
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"');
add_log = await utils.addLog('tick', 'xdebug', 'enabled', 'fedora');
expect(add_log).toContain('Platform fedora is not supported');
});
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 () => {
expect(await utils.suppressOutput('win32')).toEqual(' >$null 2>&1');
expect(await utils.suppressOutput('linux')).toEqual(' >/dev/null 2>&1');
expect(await utils.suppressOutput('darwin')).toEqual(' >/dev/null 2>&1');
expect(await utils.suppressOutput('fedora')).toContain(
'Platform fedora is not supported'
);
});
2020-02-27 18:55:54 +07:00
it('checking getBlackfireVersion', async () => {
expect(await utils.getBlackfireVersion('')).toEqual('1.31.0');
expect(await utils.getBlackfireVersion(undefined)).toEqual('1.31.0');
expect(await utils.getBlackfireVersion(null)).toEqual('1.31.0');
expect(await utils.getBlackfireVersion('1.32.0')).toEqual('1.32.0');
});
it('checking getBlackfireAgentVersion', async () => {
expect(await utils.getBlackfireAgentVersion()).toEqual('1.32.0');
});
2019-09-28 10:19:11 +07:00
});