Skip to content

Commit

Permalink
Add validator
Browse files Browse the repository at this point in the history
  • Loading branch information
crazywhalecc committed Oct 8, 2024
1 parent 9da9e98 commit 92ebc74
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .prettierrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"printWidth": 80,
"printWidth": 100,
"tabWidth": 2,
"useTabs": false,
"semi": true,
Expand Down
40 changes: 31 additions & 9 deletions __tests__/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const setOutputMock = jest.spyOn(core, 'setOutput').mockImplementation();
const runMock = jest.spyOn(main, 'run');

// Other utilities
const timeRegex = /^\d{2}:\d{2}:\d{2}/;
// const timeRegex = /^\d{2}:\d{2}:\d{2}/;

describe('action', () => {
beforeEach(() => {
Expand Down Expand Up @@ -58,9 +58,11 @@ describe('action', () => {
getInputMock.mockImplementation(name => {
switch (name) {
case 'sapi':
throw new Error(
'Need to input a valid sapi: cli, fpm, micro, embed'
);
return 'cl';
case 'php-version':
return '8.2';
case 'extensions':
return 'mbstring';
default:
return '';
}
Expand All @@ -69,10 +71,30 @@ describe('action', () => {
await main.run();
expect(runMock).toHaveReturned();

// Verify that all of the core library functions were called correctly
expect(setFailedMock).toHaveBeenNthCalledWith(
1,
'Need to input a valid sapi: cli, fpm, micro, embed'
);
// Verify that all the core library functions were called correctly
expect(setFailedMock).toHaveBeenNthCalledWith(1, 'Invalid sapi');
});

it.each`
sapi | output
${'clvvi,micro'} | ${'Invalid sapi: clvvi'}
${'micro,clhhi'} | ${'Invalid sapi: clhhi'}
${'mieecro'} | ${'Invalid sapi: mieecro'}
`(
'tests the action with sapi=$sapi, php_version=$php_version, and extensions=$extensions',
async ({ sapi, output }) => {
// Set the action's inputs as return values from core.getInput()
getInputMock.mockImplementation(name => {
switch (name) {
case 'sapi':
return sapi;
default:
return '';
}
});

await main.run();
expect(setFailedMock).toHaveBeenNthCalledWith(1, output);
}
);
});
10 changes: 5 additions & 5 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ author: 'crazywhalecc'

# Define your inputs here.
inputs:
sapi:
description: 'PHP SAPI'
required: true
default: 'cli,micro'
php-version:
description: 'Setup PHP Version'
required: false
default: '8.2'
extensions:
description: 'PHP Extensions'
required: false
default: '待确定'
default: 'mbstring,mbregex'
ini-values:
description: 'PHP INI Values'
required: false
Expand All @@ -24,10 +28,6 @@ inputs:
description: 'Enable ZTS'
required: false
default: 'false'
sapi:
description: 'PHP SAPI'
required: true
default: 'cli,micro'

# Define your outputs here.
outputs:
Expand Down
6 changes: 2 additions & 4 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
const core = require('@actions/core');
const validator = require('./validator');

/**
* The main function for the action.
* @returns {Promise<void>} Resolves when the action is complete.
*/
async function run() {
try {
await validator.validateInputs();
const ms = core.getInput('sapi', { required: true });
// only accepts cli,fpm,micro,embed for now
if (!['cli', 'fpm', 'micro', 'embed'].includes(ms)) {
core.setFailed('Invalid sapi');
}

core.setOutput('sapi', ms);
core.debug(`Sapi: ${ms}`);
Expand Down
34 changes: 34 additions & 0 deletions src/validator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const core = require('@actions/core');

async function validateInputs() {
return new Promise(resolve => {
// sapi, must be a valid PHP SAPI name, accepts cli,fpm,micro,embed
const sapi = core.getInput('sapi', { required: true });
core.debug(`SAPI: ${sapi}`);
// split comma separated values
const sapisArray = sapi.split(',').map(s => s.trim());
// check if all values are valid, if some value is invalid, throw an error
for (const s of sapisArray) {
if (!['cli', 'fpm', 'micro', 'embed'].includes(s)) {
throw new Error(`Invalid sapi: ${s}`);
}
}

// php-version, must be a valid PHP version, accepts 8.0-8.3, and also patch versions, like 8.3.10
const phpVersion = core.getInput('php-version', { required: false });
core.debug(`PHP_VERSION: ${phpVersion}`);
if (!phpVersion.match(/^8\.[0-3](\.\d+)?$/)) {
throw new Error('Invalid php-version');
}

// extensions, comma separated list of PHP extensions
const extensions = core.getInput('extensions', { required: false });
if (!extensions.match(/^(\w+,)*\w+$/)) {
throw new Error('Invalid extensions');
}

resolve();
});
}

module.exports = { validateInputs };

0 comments on commit 92ebc74

Please sign in to comment.