Skip to content

Commit

Permalink
Add coverage support, improve logs and fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
shivammathur committed Sep 26, 2019
1 parent 36104f0 commit 9134867
Show file tree
Hide file tree
Showing 26 changed files with 762 additions and 284 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,34 @@ Setup PHP with required extensions, php.ini configuration and composer in [GitHu
- Extensions which are installed along with PHP if specified are enabled.
- Extensions which cannot be installed gracefully leave an error message in the logs, the action is not interruped.

## :signal_strength: Coverage support
- Specify `coverage: xdebug` to use `Xdebug`.
- Runs on all [PHP versions supported](#tada-php-support)
```
uses: shivammathur/setup-php@master
with:
php-version: 7.3
coverage: xdebug
```
- Specify `coverage: pcov` to use `PCOV`. `PCOV` is way faster than `Xdebug`
- For `pcov.directory` to be other than `src`, `lib` or, `app`, specify it using the `ini-values-csv` input.
- `PCOV` needs `PHPUnit >= 8.0` and `PHP >= 7.1`, `PHPUnit` needs `PHP >= 7.2`. So use `PHP >= 7.2` with `PCOV`
```
uses: shivammathur/setup-php@master
with:
php-version: 7.3
ini-values-csv: 'pcov.directory=api' #optional, see above for usage.
coverage: pcov
```

## :memo: Usage

Inputs supported by this GitHub Action.

- php-version
- extension-csv (optional)
- ini-values-csv (optional)
- coverage (optional)

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

Expand All @@ -68,6 +89,7 @@ steps:
php-version: 7.3
extension-csv: mbstring, xdebug #optional
ini-values-csv: "post_max_size=256M, short_open_tag=On" #optional
coverage: xdebug #optional
- name: Check PHP Version
run: php -v
- name: Check Composer Version
Expand Down Expand Up @@ -97,6 +119,7 @@ jobs:
php-version: ${{ matrix.php-versions }}
extension-csv: mbstring, xdebug #optional
ini-values-csv: "post_max_size=256M, short_open_tag=On" #optional
coverage: xdebug #optional
- name: Check PHP Version
run: php -v
- name: Check Composer Version
Expand Down
116 changes: 110 additions & 6 deletions __tests__/features.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ describe('Features tests', () => {
'Install-PhpExtension pcov -MinimumStability alpha'
);

win32 = await features.addExtension('DoesNotExist', '7.2', 'win32');
expect(win32).not.toContain(
'Install-PhpExtension DoesNotExist -MinimumStability stable'
);
win32 = await features.addExtension('does_not_exist', '7.2', 'win32');
expect(win32).toContain('Could not find extension: does_not_exist');

win32 = await features.addExtension('xdebug', '7.2', 'fedora');
expect(win32).toContain('Platform fedora is not supported');
});

it('checking addExtensionOnLinux', async () => {
let linux: string = await features.addExtension(
'xdebug, pcov',
Expand All @@ -45,7 +47,11 @@ describe('Features tests', () => {
expect(linux).toContain(
'sudo DEBIAN_FRONTEND=noninteractive apt install -y php7.2-pcov'
);

linux = await features.addExtension('xdebug', '7.2', 'fedora');
expect(linux).toContain('Platform fedora is not supported');
});

it('checking addExtensionOnDarwin', async () => {
let darwin: string = await features.addExtension(
'xdebug, pcov',
Expand All @@ -55,8 +61,14 @@ describe('Features tests', () => {
expect(darwin).toContain('sudo pecl install xdebug');
expect(darwin).toContain('sudo pecl install pcov');

darwin = await features.addExtension('DoesNotExist', '7.2', 'darwin');
expect(darwin).not.toContain('sudo pecl install DoesNotExist');
darwin = await features.addExtension('xdebug', '5.6', 'darwin');
expect(darwin).toContain('sudo pecl install xdebug-2.5.5');

darwin = await features.addExtension('does_not_exist', '7.2', 'darwin');
expect(darwin).toContain('Could not find extension: does_not_exist');

darwin = await features.addExtension('xdebug', '7.2', 'fedora');
expect(darwin).toContain('Platform fedora is not supported');
});

it('checking addINIValuesOnWindows', async () => {
Expand All @@ -73,6 +85,12 @@ describe('Features tests', () => {
expect(win32).toContain(
'Add-Content C:\\tools\\php\\php.ini "date.timezone=Asia/Kolkata"'
);

win32 = await features.addINIValues(
'post_max_size=256M, short_open_tag=On, date.timezone=Asia/Kolkata',
'fedora'
);
expect(win32).toContain('Platform fedora is not supported');
});

it('checking addINIValuesOnLinux', async () => {
Expand All @@ -83,6 +101,12 @@ describe('Features tests', () => {
expect(linux).toContain('echo "post_max_size=256M" >> $ini_file');
expect(linux).toContain('echo "short_open_tag=On" >> $ini_file');
expect(linux).toContain('echo "date.timezone=Asia/Kolkata" >> $ini_file');

linux = await features.addINIValues(
'post_max_size=256M, short_open_tag=On, date.timezone=Asia/Kolkata',
'fedora'
);
expect(linux).toContain('Platform fedora is not supported');
});

it('checking addINIValuesOnDarwin', async () => {
Expand All @@ -93,5 +117,85 @@ describe('Features tests', () => {
expect(darwin).toContain('echo "post_max_size=256M" >> $ini_file');
expect(darwin).toContain('echo "short_open_tag=On" >> $ini_file');
expect(darwin).toContain('echo "date.timezone=Asia/Kolkata" >> $ini_file');

darwin = await features.addINIValues(
'post_max_size=256M, short_open_tag=On, date.timezone=Asia/Kolkata',
'fedora'
);
expect(darwin).toContain('Platform fedora is not supported');
});

it('checking addCoverage on windows', async () => {
let win32: string = await features.addCoverage('xdebug', '7.4', 'win32');
expect(win32).toContain(
'Install-PhpExtension xdebug -MinimumStability alpha'
);

win32 = await features.addCoverage('xdebug', '7.3', 'win32');
expect(win32).toContain(
'Install-PhpExtension xdebug -MinimumStability stable'
);

win32 = await features.addCoverage('pcov', '7.4', 'win32');
expect(win32).toContain(
'Install-PhpExtension pcov -MinimumStability alpha'
);
expect(win32).toContain(
'if(php -m | findstr -i xdebug) { Disable-PhpExtension xdebug C:\\tools\\php'
);

win32 = await features.addCoverage('pcov', '7.3', 'win32');
expect(win32).toContain(
'Install-PhpExtension pcov -MinimumStability stable'
);
expect(win32).toContain(
'if(php -m | findstr -i xdebug) { Disable-PhpExtension xdebug C:\\tools\\php'
);

win32 = await features.addCoverage('nocov', '7.3', 'win32');
expect(win32).toContain('');

win32 = await features.addCoverage('pcov', '7.0', 'win32');
expect(win32).toContain('pcov requires php 7.1 or newer');

win32 = await features.addCoverage('pcov', '5.6', 'win32');
expect(win32).toContain('pcov requires php 7.1 or newer');

win32 = await features.addCoverage('', '7.4', 'win32');
expect(win32).toEqual('');
});

it('checking addCoverage on linux', async () => {
let linux: string = await features.addCoverage('xdebug', '7.4', 'linux');
expect(linux).toContain(
'sudo DEBIAN_FRONTEND=noninteractive apt install -y php7.4-xdebug'
);

linux = await features.addCoverage('pcov', '7.4', 'linux');
expect(linux).toContain(
'sudo DEBIAN_FRONTEND=noninteractive apt install -y php7.4-pcov'
);
expect(linux).toContain(
"sudo phpdismod xdebug || echo 'xdebug not installed'"
);
expect(linux).toContain("sudo phpenmod pcov || echo 'pcov not installed'");

linux = await features.addCoverage('', '7.4', 'linux');
expect(linux).toEqual('');
});

it('checking addCoverage on darwin', async () => {
let darwin: string = await features.addCoverage('xdebug', '7.4', 'darwin');
expect(darwin).toContain('sudo pecl install xdebug');

darwin = await features.addCoverage('xdebug', '5.6', 'darwin');
expect(darwin).toContain('sudo pecl install xdebug-2.5.5');

darwin = await features.addCoverage('pcov', '7.4', 'darwin');
expect(darwin).toContain('sudo pecl install pcov');
expect(darwin).toContain('sudo sed -i \'\' "/xdebug/d" $ini_file\n');

darwin = await features.addCoverage('', '7.4', 'win32');
expect(darwin).toEqual('');
});
});
51 changes: 47 additions & 4 deletions __tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ jest.mock('../src/pecl', () => ({
})
}));

jest.mock('@actions/core', () => ({
getInput: jest.fn().mockImplementation(key => {
return ['setup-php'].indexOf(key) !== -1 ? key : '';
})
}));

async function cleanup(path: string): Promise<void> {
fs.unlink(path, error => {
if (error) {
Expand All @@ -21,7 +27,10 @@ async function cleanup(path: string): Promise<void> {
describe('Utils tests', () => {
it('checking getInput', async () => {
process.env['test'] = 'setup-php';
process.env['undefined'] = '';
expect(await utils.getInput('test', false)).toBe('setup-php');
expect(await utils.getInput('undefined', false)).toBe('');
expect(await utils.getInput('setup-php', false)).toBe('setup-php');
expect(await utils.getInput('DoesNotExist', false)).toBe('');
});

Expand Down Expand Up @@ -51,10 +60,13 @@ describe('Utils tests', () => {
path.join(__dirname, '../src/win32.ps1'),
'utf8'
);
expect(rc).toBe(await utils.readScript('darwin.sh', '7.4', 'darwin'));
expect(darwin).toBe(await utils.readScript('darwin.sh', '7.3', 'darwin'));
expect(linux).toBe(await utils.readScript('linux.sh', '7.3', 'linux'));
expect(win32).toBe(await utils.readScript('win32.ps1', '7.3', 'win32'));
expect(await utils.readScript('darwin.sh', '7.4', 'darwin')).toBe(rc);
expect(await utils.readScript('darwin.sh', '7.3', 'darwin')).toBe(darwin);
expect(await utils.readScript('linux.sh', '7.4', 'linux')).toBe(linux);
expect(await utils.readScript('win32.ps1', '7.3', 'win32')).toBe(win32);
expect(await utils.readScript('fedora.sh', '7.3', 'fedora')).toContain(
'Platform fedora is not supported'
);
});

it('checking writeScripts', async () => {
Expand Down Expand Up @@ -86,6 +98,37 @@ describe('Utils tests', () => {
]);
});

it('checking log', async () => {
let message: string = 'Test message';

let warning_log: string = await utils.log(message, 'win32', 'warning');
// expect(warning_log).toEqual(
// "Write-Host '" + message + "' -ForegroundColor yellow"
// );
warning_log = await utils.log(message, 'linux', 'warning');
expect(warning_log).toEqual('echo -e "\\033[33;1m' + message + '\\033[0m"');
warning_log = await utils.log(message, 'darwin', 'warning');
expect(warning_log).toEqual('echo -e "\\033[33;1m' + message + '\\033[0m"');

let error_log: string = await utils.log(message, 'win32', 'error');
// expect(error_log).toEqual(
// "Write-Host '" + message + "' -ForegroundColor red"
// );
error_log = await utils.log(message, 'linux', 'error');
expect(error_log).toEqual('echo -e "\\033[31;1m' + message + '\\033[0m"');
error_log = await utils.log(message, 'darwin', 'error');
expect(error_log).toEqual('echo -e "\\033[31;1m' + message + '\\033[0m"');

let success_log: string = await utils.log(message, 'win32', 'success');
// expect(success_log).toEqual(
// "Write-Host '" + message + "' -ForegroundColor green"
// );
success_log = await utils.log(message, 'linux', 'success');
expect(success_log).toEqual('echo -e "\\033[32;1m' + message + '\\033[0m"');
success_log = await utils.log(message, 'darwin', 'success');
expect(success_log).toEqual('echo -e "\\033[32;1m' + message + '\\033[0m"');
});

it('checking checkPECLExtension', async () => {
expect(await pecl.checkPECLExtension('extensionDoesNotExist')).toBe(false);
expect(await pecl.checkPECLExtension('xdebug')).toBe(true);
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ inputs:
ini-values-csv:
description: '(Optional) Custom values you want to set in php.ini'
required: false
coverage:
description: '(Optional) Driver to calculate code coverage (Accepts: xdebug and pcov)'
required: false
runs:
using: 'node12'
main: 'lib/install.js'
Loading

0 comments on commit 9134867

Please sign in to comment.