Skip to content

Commit

Permalink
Add tests for ShellExec (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored Dec 7, 2022
1 parent b52bb04 commit 864d201
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/.yamllint.yaml export-ignore
/composer-require-checker.json export-ignore
/composer.lock export-ignore
/infection.json export-ignore
/infection.json5 export-ignore
/Makefile export-ignore
/phpstan.src.neon export-ignore
/phpstan.tests.neon export-ignore
Expand Down
12 changes: 12 additions & 0 deletions infection.json → infection.json5
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@
"Fidry\\CpuCoreCounter\\CpuCoreCounter::getDefaultFinders"
]
},
"FalseValue": {
"ignore": [
// This is from thecodingmachine/safe – leave it alone
"Fidry\\CpuCoreCounter\\Exec\\ShellExec::execute"
]
},
"FunctionCallRemoval": {
"ignore": [
// This is from thecodingmachine/safe – leave it alone
"Fidry\\CpuCoreCounter\\Exec\\ShellExec::execute"
]
},
"PublicVisibility": false
}
}
4 changes: 4 additions & 0 deletions phpstan.tests.neon
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ parameters:
# This is a sanity check
- path: tests/CpuCoreCounterTest.php
message: '#assertInstanceOf\(\) with .Exception. and Exception will always evaluate to true.#'

# Skipped test
- path: tests/Exec/ShellExecTest.php
message: '#Unreachable statement \- code above always terminates\.#'
11 changes: 11 additions & 0 deletions src/Exec/ExecException.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,21 @@

use ErrorException;
use function error_get_last;
use function sprintf;

// see https://github.com/thecodingmachine/safe/blob/master/generated/Exceptions/ExecException.php
final class ExecException extends ErrorException
{
public static function createFromStderr(string $commmand): self
{
return new self(
sprintf(
'The command "%s" exited without writing to the STDOUT.',
$commmand
)
);
}

public static function createFromPhpError(): self
{
$error = error_get_last();
Expand Down
6 changes: 5 additions & 1 deletion src/Exec/ShellExec.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ public static function execute(string $command): string

$safeResult = shell_exec($command);

if (null === $safeResult || false === $safeResult) {
if (null === $safeResult) {
throw ExecException::createFromStderr($command);
}

if (false === $safeResult) {
throw ExecException::createFromPhpError();
}

Expand Down
59 changes: 59 additions & 0 deletions tests/Exec/ShellExecTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

/*
* This file is part of the Fidry CPUCounter Config package.
*
* (c) Théo FIDRY <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Fidry\CpuCoreCounter\Test\Exec;

use Fidry\CpuCoreCounter\Exec\ExecException;
use Fidry\CpuCoreCounter\Exec\ShellExec;
use PHPUnit\Framework\TestCase;
use const PHP_EOL;

/**
* @covers \Fidry\CpuCoreCounter\Exec\ExecException
* @covers \Fidry\CpuCoreCounter\Exec\ShellExec
*
* @internal
*/
final class ShellExecTest extends TestCase
{
public function test_it_can_execute_a_command_writing_output_to_the_stdout(): void
{
$command = 'echo "Hello world!"';

$expected = 'Hello world!'.PHP_EOL;
$actual = ShellExec::execute($command);

self::assertSame($expected, $actual);
}

public function test_it_can_execute_a_command_writing_output_to_the_stderr_instead_of_the_stdout(): void
{
self::markTestSkipped('This is messing up with the output.');
$command = 'echo "Hello world!" 1>&2';

$this->expectException(ExecException::class);
$this->expectExceptionMessage('The command "echo "Hello world!" 1>&2" exited without writing to the STDOUT.');

ShellExec::execute($command);
}

public function test_it_can_execute_a_command_writing_output_to_the_stdout_instead_of_the_stderr(): void
{
$command = 'echoerr() { echo "$@" 1>&2; }; echoerr "Hello world!" 2>&1';

$expected = 'Hello world!'.PHP_EOL;
$actual = ShellExec::execute($command);

self::assertSame($expected, $actual);
}
}

0 comments on commit 864d201

Please sign in to comment.