Fix second run in the same job

Refactor install.ts and its tests
This commit is contained in:
Shivam Mathur 2022-01-30 08:45:11 +05:30
parent 0022878947
commit 5acd006232
No known key found for this signature in database
GPG Key ID: 3E13E4C8591ACC2A
5 changed files with 82 additions and 150 deletions

View File

@ -5,81 +5,49 @@ import * as utils from '../src/utils';
* Mock install.ts
*/
jest.mock('../src/install', () => ({
getScript: jest.fn().mockImplementation(async (): Promise<string> => {
const extension_csv: string = process.env['extensions'] || '';
const ini_values_csv: string = process.env['ini-values'] || '';
const coverage_driver: string = process.env['coverage'] || '';
const tools_csv: string = process.env['tools'] || '';
let script = 'initial script';
script += extension_csv ? ' install extensions' : '';
script += tools_csv ? ' add_tool' : '';
script += coverage_driver ? ' set coverage driver' : '';
script += ini_values_csv ? ' edit php.ini' : '';
return script;
}),
getScript: jest
.fn()
.mockImplementation(async (os: string): Promise<string> => {
const filename = os + (await utils.scriptExtension(os));
const version: string = await utils.parseVersion(
await utils.getInput('php-version', true)
);
const ini_file: string = await utils.parseIniFile(
await utils.getInput('ini-file', false)
);
const extension_csv: string = process.env['extensions'] || '';
const ini_values_csv: string = process.env['ini-values'] || '';
const coverage_driver: string = process.env['coverage'] || '';
const tools_csv: string = process.env['tools'] || '';
let script = await utils.joins(filename, version, ini_file);
script += extension_csv ? ' install extensions' : '';
script += tools_csv ? ' add_tool' : '';
script += coverage_driver ? ' set coverage driver' : '';
script += ini_values_csv ? ' edit php.ini' : '';
return script;
}),
run: jest.fn().mockImplementation(async (): Promise<string> => {
const os: string = process.env['RUNNER_OS'] || '';
const version: string = await utils.parseVersion(
await utils.getInput('php-version', true)
);
const ini_file: string = await utils.parseIniFile(
await utils.getInput('ini-file', false)
);
const tool = await utils.scriptTool(os);
const filename = os + (await utils.scriptExtension(os));
return [
await install.getScript(filename, version, os),
tool,
filename,
version,
ini_file
].join(' ');
return tool + (await install.getScript(os));
})
}));
/**
* Function to set the process.env
*
* @param version
* @param os
* @param extension_csv
* @param ini_values_csv
* @param coverage_driver
* @param tools
*/
function setEnv(
version: string | number,
os: string,
extension_csv: string,
ini_file: string,
ini_values_csv: string,
coverage_driver: string,
tools: string
): void {
process.env['php-version'] = version.toString();
process.env['RUNNER_OS'] = os;
process.env['extensions'] = extension_csv;
process.env['ini-file'] = ini_file;
process.env['ini-values'] = ini_values_csv;
process.env['coverage'] = coverage_driver;
process.env['tools'] = tools;
}
describe('Install', () => {
it.each`
version | os | extension_csv | ini_file | ini_values_csv | coverage_driver | tools | output
${'7.3'} | ${'darwin'} | ${''} | ${'production'} | ${''} | ${''} | ${''} | ${'initial script bash darwin.sh 7.3 production'}
${'7.3'} | ${'darwin'} | ${'a, b'} | ${'development'} | ${'a=b'} | ${'x'} | ${''} | ${'initial script install extensions set coverage driver edit php.ini bash darwin.sh 7.3 development'}
${'7.4.1'} | ${'darwin'} | ${''} | ${'none'} | ${''} | ${''} | ${''} | ${'initial script bash darwin.sh 7.4 none'}
${'8'} | ${'darwin'} | ${''} | ${''} | ${''} | ${''} | ${''} | ${'initial script bash darwin.sh 8.0 production'}
${'8.0'} | ${'darwin'} | ${''} | ${'development'} | ${''} | ${''} | ${''} | ${'initial script bash darwin.sh 8.0 development'}
${'8.1'} | ${'darwin'} | ${''} | ${'none'} | ${''} | ${''} | ${''} | ${'initial script bash darwin.sh 8.1 none'}
${'7.3'} | ${'linux'} | ${''} | ${'invalid'} | ${''} | ${''} | ${''} | ${'initial script bash linux.sh 7.3 production'}
${'7.3'} | ${'linux'} | ${'a, b'} | ${'development'} | ${'a=b'} | ${'x'} | ${'phpunit'} | ${'initial script install extensions add_tool set coverage driver edit php.ini bash linux.sh 7.3 development'}
${'latest'} | ${'linux'} | ${''} | ${'none'} | ${''} | ${''} | ${''} | ${'initial script bash linux.sh 8.1 none'}
${'7.0'} | ${'win32'} | ${''} | ${'production'} | ${''} | ${''} | ${''} | ${'initial script pwsh win32.ps1 7.0 production'}
${'7.3'} | ${'win32'} | ${''} | ${'development'} | ${''} | ${''} | ${''} | ${'initial script pwsh win32.ps1 7.3 development'}
${'7.3'} | ${'win32'} | ${'a, b'} | ${'none'} | ${'a=b'} | ${'x'} | ${''} | ${'initial script install extensions set coverage driver edit php.ini pwsh win32.ps1 7.3 none'}
${'7.3'} | ${'darwin'} | ${''} | ${'production'} | ${''} | ${''} | ${''} | ${'bash darwin.sh 7.3 production'}
${'7.3'} | ${'darwin'} | ${'a, b'} | ${'development'} | ${'a=b'} | ${'x'} | ${''} | ${'bash darwin.sh 7.3 development install extensions set coverage driver edit php.ini'}
${'7.4.1'} | ${'darwin'} | ${''} | ${'none'} | ${''} | ${''} | ${''} | ${'bash darwin.sh 7.4 none'}
${'8'} | ${'darwin'} | ${''} | ${''} | ${''} | ${''} | ${''} | ${'bash darwin.sh 8.0 production'}
${'8.0'} | ${'darwin'} | ${''} | ${'development'} | ${''} | ${''} | ${''} | ${'bash darwin.sh 8.0 development'}
${'8.1'} | ${'darwin'} | ${''} | ${'none'} | ${''} | ${''} | ${''} | ${'bash darwin.sh 8.1 none'}
${'7.3'} | ${'linux'} | ${''} | ${'invalid'} | ${''} | ${''} | ${''} | ${'bash linux.sh 7.3 production'}
${'7.3'} | ${'linux'} | ${'a, b'} | ${'development'} | ${'a=b'} | ${'x'} | ${'phpunit'} | ${'bash linux.sh 7.3 development install extensions add_tool set coverage driver edit php.ini'}
${'latest'} | ${'linux'} | ${''} | ${'none'} | ${''} | ${''} | ${''} | ${'bash linux.sh 8.1 none'}
${'7.0'} | ${'win32'} | ${''} | ${'production'} | ${''} | ${''} | ${''} | ${'pwsh win32.ps1 7.0 production'}
${'7.3'} | ${'win32'} | ${''} | ${'development'} | ${''} | ${''} | ${''} | ${'pwsh win32.ps1 7.3 development'}
${'7.3'} | ${'win32'} | ${'a, b'} | ${'none'} | ${'a=b'} | ${'x'} | ${''} | ${'pwsh win32.ps1 7.3 none install extensions set coverage driver edit php.ini'}
`(
'Test install on $os for $version with extensions=$extension_csv, ini_values=$ini_values_csv, coverage_driver=$coverage_driver, tools=$tools',
async ({
@ -92,16 +60,13 @@ describe('Install', () => {
tools,
output
}) => {
setEnv(
version,
os,
extension_csv,
ini_file,
ini_values_csv,
coverage_driver,
tools
);
process.env['php-version'] = version.toString();
process.env['RUNNER_OS'] = os;
process.env['extensions'] = extension_csv;
process.env['ini-file'] = ini_file;
process.env['ini-values'] = ini_values_csv;
process.env['coverage'] = coverage_driver;
process.env['tools'] = tools;
expect(await install.run()).toBe(output);
}
);

View File

@ -219,9 +219,9 @@ describe('Utils tests', () => {
});
it('checking scriptTool', async () => {
expect(await utils.scriptTool('linux')).toBe('bash');
expect(await utils.scriptTool('darwin')).toBe('bash');
expect(await utils.scriptTool('win32')).toBe('pwsh');
expect(await utils.scriptTool('linux')).toBe('bash ');
expect(await utils.scriptTool('darwin')).toBe('bash ');
expect(await utils.scriptTool('win32')).toBe('pwsh ');
expect(await utils.scriptTool('openbsd')).toContain(
'Platform openbsd is not supported'
);

47
dist/index.js vendored
View File

@ -434,6 +434,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.run = exports.getScript = void 0;
const path_1 = __importDefault(__nccwpck_require__(17));
const fs_1 = __importDefault(__nccwpck_require__(147));
const exec_1 = __nccwpck_require__(514);
const core = __importStar(__nccwpck_require__(186));
const config = __importStar(__nccwpck_require__(88));
@ -441,17 +443,19 @@ const coverage = __importStar(__nccwpck_require__(730));
const extensions = __importStar(__nccwpck_require__(390));
const tools = __importStar(__nccwpck_require__(740));
const utils = __importStar(__nccwpck_require__(918));
const path_1 = __importDefault(__nccwpck_require__(17));
const fs_1 = __importDefault(__nccwpck_require__(147));
async function getScript(filename, version, os) {
async function getScript(os) {
const url = 'https://setup-php.com/sponsor';
const filename = os + (await utils.scriptExtension(os));
const script_path = path_1.default.join(__dirname, '../src/scripts', filename);
const run_path = script_path.replace(os, 'run');
process.env['fail_fast'] = await utils.getInput('fail-fast', false);
const extension_csv = await utils.getInput('extensions', false);
const ini_values_csv = await utils.getInput('ini-values', false);
const coverage_driver = await utils.getInput('coverage', false);
const tools_csv = await utils.getInput('tools', false);
const script_path = path_1.default.join(__dirname, '../src/scripts', filename);
let script = '\n';
const version = await utils.parseVersion(await utils.getInput('php-version', true));
const ini_file = await utils.parseIniFile(await utils.getInput('ini-file', false));
let script = await utils.joins('.', script_path, version, ini_file);
if (extension_csv) {
script += await extensions.addExtension(extension_csv, version, os);
}
@ -464,32 +468,15 @@ async function getScript(filename, version, os) {
}
script += '\n' + (await utils.stepLog(`Sponsor setup-php`, os));
script += '\n' + (await utils.addLog('$tick', 'setup-php', url, os));
fs_1.default.appendFileSync(script_path, script, { mode: 0o755 });
return script_path;
fs_1.default.writeFileSync(run_path, script, { mode: 0o755 });
return run_path;
}
exports.getScript = getScript;
async function run() {
try {
if ((await utils.readEnv('ImageOS')) == 'ubuntu16') {
core.setFailed('setup-php is not supported on Ubuntu 16.04. Please upgrade to Ubuntu 18.04 or Ubuntu 20.04 - https://setup-php.com/i/452');
return;
}
const version = await utils.parseVersion(await utils.getInput('php-version', true));
const ini_file = await utils.parseIniFile(await utils.getInput('ini-file', false));
if (version) {
const os = process.platform;
const tool = await utils.scriptTool(os);
const script = os + (await utils.scriptExtension(os));
const location = await getScript(script, version, os);
await (0, exec_1.exec)(await utils.joins(tool, location, version, ini_file));
}
else {
core.setFailed('Unable to get the PHP version');
}
}
catch (error) {
core.setFailed(error.message);
}
const os = process.platform;
const tool = await utils.scriptTool(os);
const run_path = await getScript(os);
await (0, exec_1.exec)(tool + run_path);
}
exports.run = run;
(async () => {
@ -1147,10 +1134,10 @@ exports.scriptExtension = scriptExtension;
async function scriptTool(os) {
switch (os) {
case 'win32':
return 'pwsh';
return 'pwsh ';
case 'linux':
case 'darwin':
return 'bash';
return 'bash ';
default:
return await log('Platform ' + os + ' is not supported', os, 'error');
}

View File

@ -1,3 +1,5 @@
import path from 'path';
import fs from 'fs';
import {exec} from '@actions/exec';
import * as core from '@actions/core';
import * as config from './config';
@ -5,30 +7,29 @@ import * as coverage from './coverage';
import * as extensions from './extensions';
import * as tools from './tools';
import * as utils from './utils';
import path from 'path';
import fs from 'fs';
/**
* Build the script
*
* @param filename
* @param version
* @param os
*/
export async function getScript(
filename: string,
version: string,
os: string
): Promise<string> {
export async function getScript(os: string): Promise<string> {
const url = 'https://setup-php.com/sponsor';
// taking inputs
const filename = os + (await utils.scriptExtension(os));
const script_path = path.join(__dirname, '../src/scripts', filename);
const run_path = script_path.replace(os, 'run');
process.env['fail_fast'] = await utils.getInput('fail-fast', false);
const extension_csv: string = await utils.getInput('extensions', false);
const ini_values_csv: string = await utils.getInput('ini-values', false);
const coverage_driver: string = await utils.getInput('coverage', false);
const tools_csv: string = await utils.getInput('tools', false);
const script_path = path.join(__dirname, '../src/scripts', filename);
let script = '\n';
const version: string = await utils.parseVersion(
await utils.getInput('php-version', true)
);
const ini_file: string = await utils.parseIniFile(
await utils.getInput('ini-file', false)
);
let script = await utils.joins('.', script_path, version, ini_file);
if (extension_csv) {
script += await extensions.addExtension(extension_csv, version, os);
}
@ -42,40 +43,19 @@ export async function getScript(
script += '\n' + (await utils.stepLog(`Sponsor setup-php`, os));
script += '\n' + (await utils.addLog('$tick', 'setup-php', url, os));
fs.appendFileSync(script_path, script, {mode: 0o755});
fs.writeFileSync(run_path, script, {mode: 0o755});
return script_path;
return run_path;
}
/**
* Run the script
*/
export async function run(): Promise<void> {
try {
if ((await utils.readEnv('ImageOS')) == 'ubuntu16') {
core.setFailed(
'setup-php is not supported on Ubuntu 16.04. Please upgrade to Ubuntu 18.04 or Ubuntu 20.04 - https://setup-php.com/i/452'
);
return;
}
const version: string = await utils.parseVersion(
await utils.getInput('php-version', true)
);
const ini_file: string = await utils.parseIniFile(
await utils.getInput('ini-file', false)
);
if (version) {
const os: string = process.platform;
const tool = await utils.scriptTool(os);
const script = os + (await utils.scriptExtension(os));
const location = await getScript(script, version, os);
await exec(await utils.joins(tool, location, version, ini_file));
} else {
core.setFailed('Unable to get the PHP version');
}
} catch (error) {
core.setFailed((error as Error).message);
}
const os: string = process.platform;
const tool = await utils.scriptTool(os);
const run_path = await getScript(os);
await exec(tool + run_path);
}
// call the run function

View File

@ -401,10 +401,10 @@ export async function scriptExtension(os: string): Promise<string> {
export async function scriptTool(os: string): Promise<string> {
switch (os) {
case 'win32':
return 'pwsh';
return 'pwsh ';
case 'linux':
case 'darwin':
return 'bash';
return 'bash ';
default:
return await log('Platform ' + os + ' is not supported', os, 'error');
}