From 5acd00623274d67afa51eaa4e5ff9f70c9715881 Mon Sep 17 00:00:00 2001 From: Shivam Mathur Date: Sun, 30 Jan 2022 08:45:11 +0530 Subject: [PATCH] Fix second run in the same job Refactor install.ts and its tests --- __tests__/install.test.ts | 117 +++++++++++++------------------------- __tests__/utils.test.ts | 6 +- dist/index.js | 47 ++++++--------- src/install.ts | 58 +++++++------------ src/utils.ts | 4 +- 5 files changed, 82 insertions(+), 150 deletions(-) diff --git a/__tests__/install.test.ts b/__tests__/install.test.ts index 2ab4cb3e..eef91409 100644 --- a/__tests__/install.test.ts +++ b/__tests__/install.test.ts @@ -5,81 +5,49 @@ import * as utils from '../src/utils'; * Mock install.ts */ jest.mock('../src/install', () => ({ - getScript: jest.fn().mockImplementation(async (): Promise => { - 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 => { + 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 => { 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); } ); diff --git a/__tests__/utils.test.ts b/__tests__/utils.test.ts index 15ff336c..f2405012 100644 --- a/__tests__/utils.test.ts +++ b/__tests__/utils.test.ts @@ -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' ); diff --git a/dist/index.js b/dist/index.js index 0446df38..1fca2f32 100644 --- a/dist/index.js +++ b/dist/index.js @@ -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'); } diff --git a/src/install.ts b/src/install.ts index 69e59f00..778b037e 100644 --- a/src/install.ts +++ b/src/install.ts @@ -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 { +export async function getScript(os: string): Promise { 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 { - 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 diff --git a/src/utils.ts b/src/utils.ts index bd5fc9b6..ba10afcf 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -401,10 +401,10 @@ export async function scriptExtension(os: string): Promise { export async function scriptTool(os: string): Promise { 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'); }