Skip to content

Commit 92ebc74

Browse files
committed
Add validator
1 parent 9da9e98 commit 92ebc74

File tree

5 files changed

+73
-19
lines changed

5 files changed

+73
-19
lines changed

.prettierrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"printWidth": 80,
2+
"printWidth": 100,
33
"tabWidth": 2,
44
"useTabs": false,
55
"semi": true,

__tests__/main.test.js

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const setOutputMock = jest.spyOn(core, 'setOutput').mockImplementation();
1414
const runMock = jest.spyOn(main, 'run');
1515

1616
// Other utilities
17-
const timeRegex = /^\d{2}:\d{2}:\d{2}/;
17+
// const timeRegex = /^\d{2}:\d{2}:\d{2}/;
1818

1919
describe('action', () => {
2020
beforeEach(() => {
@@ -58,9 +58,11 @@ describe('action', () => {
5858
getInputMock.mockImplementation(name => {
5959
switch (name) {
6060
case 'sapi':
61-
throw new Error(
62-
'Need to input a valid sapi: cli, fpm, micro, embed'
63-
);
61+
return 'cl';
62+
case 'php-version':
63+
return '8.2';
64+
case 'extensions':
65+
return 'mbstring';
6466
default:
6567
return '';
6668
}
@@ -69,10 +71,30 @@ describe('action', () => {
6971
await main.run();
7072
expect(runMock).toHaveReturned();
7173

72-
// Verify that all of the core library functions were called correctly
73-
expect(setFailedMock).toHaveBeenNthCalledWith(
74-
1,
75-
'Need to input a valid sapi: cli, fpm, micro, embed'
76-
);
74+
// Verify that all the core library functions were called correctly
75+
expect(setFailedMock).toHaveBeenNthCalledWith(1, 'Invalid sapi');
7776
});
77+
78+
it.each`
79+
sapi | output
80+
${'clvvi,micro'} | ${'Invalid sapi: clvvi'}
81+
${'micro,clhhi'} | ${'Invalid sapi: clhhi'}
82+
${'mieecro'} | ${'Invalid sapi: mieecro'}
83+
`(
84+
'tests the action with sapi=$sapi, php_version=$php_version, and extensions=$extensions',
85+
async ({ sapi, output }) => {
86+
// Set the action's inputs as return values from core.getInput()
87+
getInputMock.mockImplementation(name => {
88+
switch (name) {
89+
case 'sapi':
90+
return sapi;
91+
default:
92+
return '';
93+
}
94+
});
95+
96+
await main.run();
97+
expect(setFailedMock).toHaveBeenNthCalledWith(1, output);
98+
}
99+
);
78100
});

action.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@ author: 'crazywhalecc'
44

55
# Define your inputs here.
66
inputs:
7+
sapi:
8+
description: 'PHP SAPI'
9+
required: true
10+
default: 'cli,micro'
711
php-version:
812
description: 'Setup PHP Version'
913
required: false
1014
default: '8.2'
1115
extensions:
1216
description: 'PHP Extensions'
1317
required: false
14-
default: '待确定'
18+
default: 'mbstring,mbregex'
1519
ini-values:
1620
description: 'PHP INI Values'
1721
required: false
@@ -24,10 +28,6 @@ inputs:
2428
description: 'Enable ZTS'
2529
required: false
2630
default: 'false'
27-
sapi:
28-
description: 'PHP SAPI'
29-
required: true
30-
default: 'cli,micro'
3131

3232
# Define your outputs here.
3333
outputs:

src/main.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
const core = require('@actions/core');
2+
const validator = require('./validator');
23

34
/**
45
* The main function for the action.
56
* @returns {Promise<void>} Resolves when the action is complete.
67
*/
78
async function run() {
89
try {
10+
await validator.validateInputs();
911
const ms = core.getInput('sapi', { required: true });
10-
// only accepts cli,fpm,micro,embed for now
11-
if (!['cli', 'fpm', 'micro', 'embed'].includes(ms)) {
12-
core.setFailed('Invalid sapi');
13-
}
1412

1513
core.setOutput('sapi', ms);
1614
core.debug(`Sapi: ${ms}`);

src/validator.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const core = require('@actions/core');
2+
3+
async function validateInputs() {
4+
return new Promise(resolve => {
5+
// sapi, must be a valid PHP SAPI name, accepts cli,fpm,micro,embed
6+
const sapi = core.getInput('sapi', { required: true });
7+
core.debug(`SAPI: ${sapi}`);
8+
// split comma separated values
9+
const sapisArray = sapi.split(',').map(s => s.trim());
10+
// check if all values are valid, if some value is invalid, throw an error
11+
for (const s of sapisArray) {
12+
if (!['cli', 'fpm', 'micro', 'embed'].includes(s)) {
13+
throw new Error(`Invalid sapi: ${s}`);
14+
}
15+
}
16+
17+
// php-version, must be a valid PHP version, accepts 8.0-8.3, and also patch versions, like 8.3.10
18+
const phpVersion = core.getInput('php-version', { required: false });
19+
core.debug(`PHP_VERSION: ${phpVersion}`);
20+
if (!phpVersion.match(/^8\.[0-3](\.\d+)?$/)) {
21+
throw new Error('Invalid php-version');
22+
}
23+
24+
// extensions, comma separated list of PHP extensions
25+
const extensions = core.getInput('extensions', { required: false });
26+
if (!extensions.match(/^(\w+,)*\w+$/)) {
27+
throw new Error('Invalid extensions');
28+
}
29+
30+
resolve();
31+
});
32+
}
33+
34+
module.exports = { validateInputs };

0 commit comments

Comments
 (0)