Skip to content

Commit 9134867

Browse files
committed
Add coverage support, improve logs and fix bugs
1 parent 36104f0 commit 9134867

26 files changed

+762
-284
lines changed

Diff for: README.md

+23
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,34 @@ Setup PHP with required extensions, php.ini configuration and composer in [GitHu
4646
- Extensions which are installed along with PHP if specified are enabled.
4747
- Extensions which cannot be installed gracefully leave an error message in the logs, the action is not interruped.
4848

49+
## :signal_strength: Coverage support
50+
- Specify `coverage: xdebug` to use `Xdebug`.
51+
- Runs on all [PHP versions supported](#tada-php-support)
52+
```
53+
uses: shivammathur/setup-php@master
54+
with:
55+
php-version: 7.3
56+
coverage: xdebug
57+
```
58+
- Specify `coverage: pcov` to use `PCOV`. `PCOV` is way faster than `Xdebug`
59+
- For `pcov.directory` to be other than `src`, `lib` or, `app`, specify it using the `ini-values-csv` input.
60+
- `PCOV` needs `PHPUnit >= 8.0` and `PHP >= 7.1`, `PHPUnit` needs `PHP >= 7.2`. So use `PHP >= 7.2` with `PCOV`
61+
```
62+
uses: shivammathur/setup-php@master
63+
with:
64+
php-version: 7.3
65+
ini-values-csv: 'pcov.directory=api' #optional, see above for usage.
66+
coverage: pcov
67+
```
68+
4969
## :memo: Usage
5070

5171
Inputs supported by this GitHub Action.
5272

5373
- php-version
5474
- extension-csv (optional)
5575
- ini-values-csv (optional)
76+
- coverage (optional)
5677

5778
See [action.yml](action.yml) for more info
5879

@@ -68,6 +89,7 @@ steps:
6889
php-version: 7.3
6990
extension-csv: mbstring, xdebug #optional
7091
ini-values-csv: "post_max_size=256M, short_open_tag=On" #optional
92+
coverage: xdebug #optional
7193
- name: Check PHP Version
7294
run: php -v
7395
- name: Check Composer Version
@@ -97,6 +119,7 @@ jobs:
97119
php-version: ${{ matrix.php-versions }}
98120
extension-csv: mbstring, xdebug #optional
99121
ini-values-csv: "post_max_size=256M, short_open_tag=On" #optional
122+
coverage: xdebug #optional
100123
- name: Check PHP Version
101124
run: php -v
102125
- name: Check Composer Version

Diff for: __tests__/features.test.ts

+110-6
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@ describe('Features tests', () => {
2828
'Install-PhpExtension pcov -MinimumStability alpha'
2929
);
3030

31-
win32 = await features.addExtension('DoesNotExist', '7.2', 'win32');
32-
expect(win32).not.toContain(
33-
'Install-PhpExtension DoesNotExist -MinimumStability stable'
34-
);
31+
win32 = await features.addExtension('does_not_exist', '7.2', 'win32');
32+
expect(win32).toContain('Could not find extension: does_not_exist');
33+
34+
win32 = await features.addExtension('xdebug', '7.2', 'fedora');
35+
expect(win32).toContain('Platform fedora is not supported');
3536
});
37+
3638
it('checking addExtensionOnLinux', async () => {
3739
let linux: string = await features.addExtension(
3840
'xdebug, pcov',
@@ -45,7 +47,11 @@ describe('Features tests', () => {
4547
expect(linux).toContain(
4648
'sudo DEBIAN_FRONTEND=noninteractive apt install -y php7.2-pcov'
4749
);
50+
51+
linux = await features.addExtension('xdebug', '7.2', 'fedora');
52+
expect(linux).toContain('Platform fedora is not supported');
4853
});
54+
4955
it('checking addExtensionOnDarwin', async () => {
5056
let darwin: string = await features.addExtension(
5157
'xdebug, pcov',
@@ -55,8 +61,14 @@ describe('Features tests', () => {
5561
expect(darwin).toContain('sudo pecl install xdebug');
5662
expect(darwin).toContain('sudo pecl install pcov');
5763

58-
darwin = await features.addExtension('DoesNotExist', '7.2', 'darwin');
59-
expect(darwin).not.toContain('sudo pecl install DoesNotExist');
64+
darwin = await features.addExtension('xdebug', '5.6', 'darwin');
65+
expect(darwin).toContain('sudo pecl install xdebug-2.5.5');
66+
67+
darwin = await features.addExtension('does_not_exist', '7.2', 'darwin');
68+
expect(darwin).toContain('Could not find extension: does_not_exist');
69+
70+
darwin = await features.addExtension('xdebug', '7.2', 'fedora');
71+
expect(darwin).toContain('Platform fedora is not supported');
6072
});
6173

6274
it('checking addINIValuesOnWindows', async () => {
@@ -73,6 +85,12 @@ describe('Features tests', () => {
7385
expect(win32).toContain(
7486
'Add-Content C:\\tools\\php\\php.ini "date.timezone=Asia/Kolkata"'
7587
);
88+
89+
win32 = await features.addINIValues(
90+
'post_max_size=256M, short_open_tag=On, date.timezone=Asia/Kolkata',
91+
'fedora'
92+
);
93+
expect(win32).toContain('Platform fedora is not supported');
7694
});
7795

7896
it('checking addINIValuesOnLinux', async () => {
@@ -83,6 +101,12 @@ describe('Features tests', () => {
83101
expect(linux).toContain('echo "post_max_size=256M" >> $ini_file');
84102
expect(linux).toContain('echo "short_open_tag=On" >> $ini_file');
85103
expect(linux).toContain('echo "date.timezone=Asia/Kolkata" >> $ini_file');
104+
105+
linux = await features.addINIValues(
106+
'post_max_size=256M, short_open_tag=On, date.timezone=Asia/Kolkata',
107+
'fedora'
108+
);
109+
expect(linux).toContain('Platform fedora is not supported');
86110
});
87111

88112
it('checking addINIValuesOnDarwin', async () => {
@@ -93,5 +117,85 @@ describe('Features tests', () => {
93117
expect(darwin).toContain('echo "post_max_size=256M" >> $ini_file');
94118
expect(darwin).toContain('echo "short_open_tag=On" >> $ini_file');
95119
expect(darwin).toContain('echo "date.timezone=Asia/Kolkata" >> $ini_file');
120+
121+
darwin = await features.addINIValues(
122+
'post_max_size=256M, short_open_tag=On, date.timezone=Asia/Kolkata',
123+
'fedora'
124+
);
125+
expect(darwin).toContain('Platform fedora is not supported');
126+
});
127+
128+
it('checking addCoverage on windows', async () => {
129+
let win32: string = await features.addCoverage('xdebug', '7.4', 'win32');
130+
expect(win32).toContain(
131+
'Install-PhpExtension xdebug -MinimumStability alpha'
132+
);
133+
134+
win32 = await features.addCoverage('xdebug', '7.3', 'win32');
135+
expect(win32).toContain(
136+
'Install-PhpExtension xdebug -MinimumStability stable'
137+
);
138+
139+
win32 = await features.addCoverage('pcov', '7.4', 'win32');
140+
expect(win32).toContain(
141+
'Install-PhpExtension pcov -MinimumStability alpha'
142+
);
143+
expect(win32).toContain(
144+
'if(php -m | findstr -i xdebug) { Disable-PhpExtension xdebug C:\\tools\\php'
145+
);
146+
147+
win32 = await features.addCoverage('pcov', '7.3', 'win32');
148+
expect(win32).toContain(
149+
'Install-PhpExtension pcov -MinimumStability stable'
150+
);
151+
expect(win32).toContain(
152+
'if(php -m | findstr -i xdebug) { Disable-PhpExtension xdebug C:\\tools\\php'
153+
);
154+
155+
win32 = await features.addCoverage('nocov', '7.3', 'win32');
156+
expect(win32).toContain('');
157+
158+
win32 = await features.addCoverage('pcov', '7.0', 'win32');
159+
expect(win32).toContain('pcov requires php 7.1 or newer');
160+
161+
win32 = await features.addCoverage('pcov', '5.6', 'win32');
162+
expect(win32).toContain('pcov requires php 7.1 or newer');
163+
164+
win32 = await features.addCoverage('', '7.4', 'win32');
165+
expect(win32).toEqual('');
166+
});
167+
168+
it('checking addCoverage on linux', async () => {
169+
let linux: string = await features.addCoverage('xdebug', '7.4', 'linux');
170+
expect(linux).toContain(
171+
'sudo DEBIAN_FRONTEND=noninteractive apt install -y php7.4-xdebug'
172+
);
173+
174+
linux = await features.addCoverage('pcov', '7.4', 'linux');
175+
expect(linux).toContain(
176+
'sudo DEBIAN_FRONTEND=noninteractive apt install -y php7.4-pcov'
177+
);
178+
expect(linux).toContain(
179+
"sudo phpdismod xdebug || echo 'xdebug not installed'"
180+
);
181+
expect(linux).toContain("sudo phpenmod pcov || echo 'pcov not installed'");
182+
183+
linux = await features.addCoverage('', '7.4', 'linux');
184+
expect(linux).toEqual('');
185+
});
186+
187+
it('checking addCoverage on darwin', async () => {
188+
let darwin: string = await features.addCoverage('xdebug', '7.4', 'darwin');
189+
expect(darwin).toContain('sudo pecl install xdebug');
190+
191+
darwin = await features.addCoverage('xdebug', '5.6', 'darwin');
192+
expect(darwin).toContain('sudo pecl install xdebug-2.5.5');
193+
194+
darwin = await features.addCoverage('pcov', '7.4', 'darwin');
195+
expect(darwin).toContain('sudo pecl install pcov');
196+
expect(darwin).toContain('sudo sed -i \'\' "/xdebug/d" $ini_file\n');
197+
198+
darwin = await features.addCoverage('', '7.4', 'win32');
199+
expect(darwin).toEqual('');
96200
});
97201
});

Diff for: __tests__/utils.test.ts

+47-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ jest.mock('../src/pecl', () => ({
1010
})
1111
}));
1212

13+
jest.mock('@actions/core', () => ({
14+
getInput: jest.fn().mockImplementation(key => {
15+
return ['setup-php'].indexOf(key) !== -1 ? key : '';
16+
})
17+
}));
18+
1319
async function cleanup(path: string): Promise<void> {
1420
fs.unlink(path, error => {
1521
if (error) {
@@ -21,7 +27,10 @@ async function cleanup(path: string): Promise<void> {
2127
describe('Utils tests', () => {
2228
it('checking getInput', async () => {
2329
process.env['test'] = 'setup-php';
30+
process.env['undefined'] = '';
2431
expect(await utils.getInput('test', false)).toBe('setup-php');
32+
expect(await utils.getInput('undefined', false)).toBe('');
33+
expect(await utils.getInput('setup-php', false)).toBe('setup-php');
2534
expect(await utils.getInput('DoesNotExist', false)).toBe('');
2635
});
2736

@@ -51,10 +60,13 @@ describe('Utils tests', () => {
5160
path.join(__dirname, '../src/win32.ps1'),
5261
'utf8'
5362
);
54-
expect(rc).toBe(await utils.readScript('darwin.sh', '7.4', 'darwin'));
55-
expect(darwin).toBe(await utils.readScript('darwin.sh', '7.3', 'darwin'));
56-
expect(linux).toBe(await utils.readScript('linux.sh', '7.3', 'linux'));
57-
expect(win32).toBe(await utils.readScript('win32.ps1', '7.3', 'win32'));
63+
expect(await utils.readScript('darwin.sh', '7.4', 'darwin')).toBe(rc);
64+
expect(await utils.readScript('darwin.sh', '7.3', 'darwin')).toBe(darwin);
65+
expect(await utils.readScript('linux.sh', '7.4', 'linux')).toBe(linux);
66+
expect(await utils.readScript('win32.ps1', '7.3', 'win32')).toBe(win32);
67+
expect(await utils.readScript('fedora.sh', '7.3', 'fedora')).toContain(
68+
'Platform fedora is not supported'
69+
);
5870
});
5971

6072
it('checking writeScripts', async () => {
@@ -86,6 +98,37 @@ describe('Utils tests', () => {
8698
]);
8799
});
88100

101+
it('checking log', async () => {
102+
let message: string = 'Test message';
103+
104+
let warning_log: string = await utils.log(message, 'win32', 'warning');
105+
// expect(warning_log).toEqual(
106+
// "Write-Host '" + message + "' -ForegroundColor yellow"
107+
// );
108+
warning_log = await utils.log(message, 'linux', 'warning');
109+
expect(warning_log).toEqual('echo -e "\\033[33;1m' + message + '\\033[0m"');
110+
warning_log = await utils.log(message, 'darwin', 'warning');
111+
expect(warning_log).toEqual('echo -e "\\033[33;1m' + message + '\\033[0m"');
112+
113+
let error_log: string = await utils.log(message, 'win32', 'error');
114+
// expect(error_log).toEqual(
115+
// "Write-Host '" + message + "' -ForegroundColor red"
116+
// );
117+
error_log = await utils.log(message, 'linux', 'error');
118+
expect(error_log).toEqual('echo -e "\\033[31;1m' + message + '\\033[0m"');
119+
error_log = await utils.log(message, 'darwin', 'error');
120+
expect(error_log).toEqual('echo -e "\\033[31;1m' + message + '\\033[0m"');
121+
122+
let success_log: string = await utils.log(message, 'win32', 'success');
123+
// expect(success_log).toEqual(
124+
// "Write-Host '" + message + "' -ForegroundColor green"
125+
// );
126+
success_log = await utils.log(message, 'linux', 'success');
127+
expect(success_log).toEqual('echo -e "\\033[32;1m' + message + '\\033[0m"');
128+
success_log = await utils.log(message, 'darwin', 'success');
129+
expect(success_log).toEqual('echo -e "\\033[32;1m' + message + '\\033[0m"');
130+
});
131+
89132
it('checking checkPECLExtension', async () => {
90133
expect(await pecl.checkPECLExtension('extensionDoesNotExist')).toBe(false);
91134
expect(await pecl.checkPECLExtension('xdebug')).toBe(true);

Diff for: action.yml

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ inputs:
1414
ini-values-csv:
1515
description: '(Optional) Custom values you want to set in php.ini'
1616
required: false
17+
coverage:
18+
description: '(Optional) Driver to calculate code coverage (Accepts: xdebug and pcov)'
19+
required: false
1720
runs:
1821
using: 'node12'
1922
main: 'lib/install.js'

0 commit comments

Comments
 (0)