Refactor to use setup-php

This commit is contained in:
Shivam Mathur
2021-09-19 06:06:31 +05:30
parent 3572ca5db9
commit b52930e590
7 changed files with 2811 additions and 2074 deletions

View File

@@ -1,4 +1,5 @@
import path from 'path';
import * as spu from 'setup-php/lib/utils';
import * as cache from '../src/cache';
import * as utils from '../src/utils';
@@ -7,7 +8,7 @@ import * as utils from '../src/utils';
*/
jest.mock('../src/cache', () => ({
run: jest.fn().mockImplementation(async (): Promise<string> => {
const version: string = await utils.parseVersion(
const version: string = await spu.parseVersion(
process.env['php-version'] || ''
);
const extensions = await utils.filterExtensions(

View File

@@ -2,12 +2,6 @@ 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 ['cache-extensions'].indexOf(key) !== -1 ? key : '';
})
}));
async function cleanup(path: string): Promise<void> {
fs.unlink(path, error => {
if (error) {
@@ -17,24 +11,6 @@ async function cleanup(path: string): Promise<void> {
}
describe('Utils tests', () => {
it('checking readEnv', async () => {
process.env['test'] = 'setup-php';
expect(await utils.readEnv('test')).toBe('setup-php');
expect(await utils.readEnv('undefined')).toBe('');
});
it('checking getInput', async () => {
process.env['test'] = 'setup-php';
expect(await utils.getInput('test', false)).toBe('setup-php');
expect(await utils.getInput('cache-extensions', false)).toBe(
'cache-extensions'
);
expect(await utils.getInput('DoesNotExist', false)).toBe('');
await expect(async () => {
await utils.getInput('DoesNotExist', true);
}).rejects.toThrow('Input required and not supplied: DoesNotExist');
});
it('checking getOutput', async () => {
const temp_dir: string = process.env['RUNNER_TEMP'] || '';
const file_path: string = path.join(temp_dir, 'test');
@@ -43,14 +19,15 @@ describe('Utils tests', () => {
await cleanup(file_path);
});
it('checking parseVersion', async () => {
expect(await utils.parseVersion('7')).toBe('7.0');
expect(await utils.parseVersion('7.4')).toBe('7.4');
expect(await utils.parseVersion('latest')).toBe('7.4');
});
it('checking filterExtensions', async () => {
expect(await utils.filterExtensions('a,:b,c')).toBe('"a,c"');
expect(await utils.filterExtensions('a, :b, c')).toBe('"a, c"');
});
it('checking scriptCall', async () => {
const script: string = path.join(__dirname, '../src/scripts/cache.sh');
expect(await utils.scriptCall('test a b')).toBe(
['bash', script, 'test', 'a', 'b'].join(' ')
);
});
});

4719
dist/index.js vendored

File diff suppressed because it is too large Load Diff

48
package-lock.json generated
View File

@@ -5,12 +5,14 @@
"requires": true,
"packages": {
"": {
"name": "cache-extensions",
"version": "1.6.1",
"license": "MIT",
"dependencies": {
"@actions/cache": "^1.0.7",
"@actions/core": "^1.4.0",
"@actions/exec": "^1.1.0"
"@actions/exec": "^1.1.0",
"setup-php": "^2.14.0"
},
"devDependencies": {
"@types/jest": "^26.0.24",
@@ -3288,6 +3290,11 @@
"node": ">= 0.12"
}
},
"node_modules/fs": {
"version": "0.0.1-security",
"resolved": "https://registry.npmjs.org/fs/-/fs-0.0.1-security.tgz",
"integrity": "sha1-invTcYa23d84E/I4WLV+yq9eQdQ="
},
"node_modules/fs.realpath": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz",
@@ -6204,6 +6211,22 @@
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/setup-php": {
"version": "2.14.0",
"resolved": "https://registry.npmjs.org/setup-php/-/setup-php-2.14.0.tgz",
"integrity": "sha512-/diujbeAO3ZlfAvz4TQg+co2SbfeArHozM1cCLJ6BEgwMQQidVqpzlm/c8fDwEFtFO3S4U+UFR0SxJMia8Pk8Q==",
"dependencies": {
"@actions/core": "^1.5.0",
"@actions/exec": "^1.1.0",
"@actions/io": "^1.1.1",
"fs": "0.0.1-security"
}
},
"node_modules/setup-php/node_modules/@actions/core": {
"version": "1.5.0",
"resolved": "https://registry.npmjs.org/@actions/core/-/core-1.5.0.tgz",
"integrity": "sha512-eDOLH1Nq9zh+PJlYLqEMkS/jLQxhksPNmUGNBHfa4G+tQmnIhzpctxmchETtVGyBOvXgOVVpYuE40+eS4cUnwQ=="
},
"node_modules/shebang-command": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-2.0.0.tgz",
@@ -9628,6 +9651,11 @@
"mime-types": "^2.1.12"
}
},
"fs": {
"version": "0.0.1-security",
"resolved": "https://registry.npmjs.org/fs/-/fs-0.0.1-security.tgz",
"integrity": "sha1-invTcYa23d84E/I4WLV+yq9eQdQ="
},
"fs.realpath": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz",
@@ -11783,6 +11811,24 @@
"integrity": "sha512-bXWyL6EAKOJa81XG1OZ/Yyuq+oT0b2YLlxx7c+mrdYPaPbnj6WgVULXhinMIeZGufuUBu/eVRqXEhiv4imfwxA==",
"dev": true
},
"setup-php": {
"version": "2.14.0",
"resolved": "https://registry.npmjs.org/setup-php/-/setup-php-2.14.0.tgz",
"integrity": "sha512-/diujbeAO3ZlfAvz4TQg+co2SbfeArHozM1cCLJ6BEgwMQQidVqpzlm/c8fDwEFtFO3S4U+UFR0SxJMia8Pk8Q==",
"requires": {
"@actions/core": "^1.5.0",
"@actions/exec": "^1.1.0",
"@actions/io": "^1.1.1",
"fs": "0.0.1-security"
},
"dependencies": {
"@actions/core": {
"version": "1.5.0",
"resolved": "https://registry.npmjs.org/@actions/core/-/core-1.5.0.tgz",
"integrity": "sha512-eDOLH1Nq9zh+PJlYLqEMkS/jLQxhksPNmUGNBHfa4G+tQmnIhzpctxmchETtVGyBOvXgOVVpYuE40+eS4cUnwQ=="
}
}
},
"shebang-command": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-2.0.0.tgz",

View File

@@ -27,7 +27,8 @@
"dependencies": {
"@actions/cache": "^1.0.7",
"@actions/core": "^1.4.0",
"@actions/exec": "^1.1.0"
"@actions/exec": "^1.1.0",
"setup-php": "^2.14.0"
},
"devDependencies": {
"@types/jest": "^26.0.24",

View File

@@ -1,6 +1,7 @@
import {exec} from '@actions/exec';
import * as cache from '@actions/cache';
import * as core from '@actions/core';
import * as spu from 'setup-php/lib/utils';
import * as path from 'path';
import * as utils from './utils';
@@ -17,7 +18,7 @@ export async function handleDependencies(
if (!/^5.[3-5]$/.test(version) && /linux|darwin/.test(process.platform)) {
const cache_key: string = (await utils.getOutput('key')) + '-deps';
const cache_dir: string = path.join(
await utils.readEnv('RUNNER_TOOL_CACHE'),
await spu.readEnv('RUNNER_TOOL_CACHE'),
'deps'
);
const cache_hit: string | undefined = await cache.restoreCache(
@@ -37,17 +38,17 @@ export async function handleDependencies(
*/
export async function run(): Promise<void> {
try {
const version: string = await utils.parseVersion(
await utils.getInput('php-version', true)
const version: string = await spu.parseVersion(
await spu.getInput('php-version', true)
);
const extensions: string = await utils.filterExtensions(
await utils.getInput('extensions', true)
await spu.getInput('extensions', true)
);
const key: string = await utils.getInput('key', true);
const key: string = await spu.getInput('key', true);
await exec(await utils.scriptCall('data', extensions, version, key));
await handleDependencies(extensions, version);
} catch (error) {
core.setFailed(error.message);
core.setFailed((error as Error).message);
}
}

View File

@@ -1,75 +1,17 @@
import * as fs from 'fs';
import * as path from 'path';
import * as core from '@actions/core';
/**
* Function to read environment variable and return a string value.
*
* @param property
*/
export async function readEnv(property: string): Promise<string> {
const value = process.env[property];
switch (value) {
case undefined:
return '';
default:
return value;
}
}
/**
* Function to get inputs from both with and env annotations.
*
* @param name
* @param mandatory
*/
export async function getInput(
name: string,
mandatory: boolean
): Promise<string> {
const input = core.getInput(name);
const env_input = await readEnv(name);
switch (true) {
case input != '':
return input;
case input == '' && env_input != '':
return env_input;
case input == '' && env_input == '' && mandatory:
throw new Error(`Input required and not supplied: ${name}`);
default:
return '';
}
}
import * as spu from 'setup-php/lib/utils';
/**
* Function to get outputs
*/
export async function getOutput(output: string): Promise<string> {
return fs.readFileSync(
path.join(await readEnv('RUNNER_TEMP'), output),
path.join(await spu.readEnv('RUNNER_TEMP'), output),
'utf8'
);
}
/**
* Function to parse PHP version.
*
* @param version
*/
export async function parseVersion(version: string): Promise<string> {
switch (version) {
case 'latest':
return '7.4';
default:
switch (true) {
case version.length > 1:
return version.slice(0, 3);
default:
return version + '.0';
}
}
}
/**
* Function to filter extensions
*