Skip to content

Commit

Permalink
fix: Ensure the debug flag is not forwarded (#43)
Browse files Browse the repository at this point in the history
If the PHPUnit test is executed via a Makefile command with the `--debug` flag, for example for this library:

```
make phpunit --debug
```

Then the `MAKEFLAGS` environment variable is updated and forwarded to
the processes in which the test make commands are executed. This result
in a different output which can easily break tests.
  • Loading branch information
theofidry authored Oct 6, 2023
1 parent 2982140 commit cef335a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,31 @@ jobs:
- name: "Run tests"
run: "make phpunit"

debug-test:
runs-on: "ubuntu-latest"
name: "Tests with PHP ${{ matrix.php }} and the make `--debug` flag"
strategy:
fail-fast: true
matrix:
php:
- "8.1"

steps:
- name: "Check out repository code"
uses: "actions/checkout@v4"

- name: "Setup PHP"
uses: "shivammathur/setup-php@v2"
with:
php-version: "${{ matrix.php }}"
tools: "composer"

- name: "Install Composer dependencies"
uses: "ramsey/composer-install@v2"

- name: "Run tests"
run: "make phpunit --debug"

infection:
runs-on: "ubuntu-latest"
name: "Infection with PHP ${{ matrix.php }}"
Expand Down Expand Up @@ -70,6 +95,7 @@ jobs:
runs-on: ubuntu-latest
needs:
- tests
- debug-test
- infection
if: always()
steps:
Expand Down
10 changes: 10 additions & 0 deletions infection.json.dist → infection.json5.dist
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,19 @@
"mutators": {
"@default": true,
"MBString": false,
"global-ignore": [
"Fidry\\Makefile\\Test\\BaseMakefileTestCase::getNonDebugMakeFlags"
],

"CastString": {
"ignore": [
// Testing that getenv() does not return a string is difficult there.
"Fidry\\Makefile\\Test\\BaseMakefileTestCase::getNonDebugMakeFlags"
]
},
"FunctionCallRemoval": {
"ignore": [
"Fidry\\Makefile\\Test\\BaseMakefileTestCase::executeCommand",
"Fidry\\Makefile\\Test\\BaseMakefileTestCase::executeInDirectory"
]
},
Expand Down
22 changes: 22 additions & 0 deletions src/Test/BaseMakefileTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,13 @@
use Fidry\Makefile\Rule;
use PHPUnit\Framework\TestCase;
use Safe\Exceptions\ExecException;
use function array_filter;
use function dirname;
use function error_clear_last;
use function explode;
use function function_exists;
use function getenv;
use function implode;
use function Safe\chdir;
use function Safe\file_get_contents;
use function Safe\getcwd;
Expand Down Expand Up @@ -129,6 +133,12 @@ final protected static function executeMakeCommand(string $commandName): string
// TODO: remove this as we remove support for PHP 7.4 and Safe v1
final protected static function executeCommand(string $command): string
{
$command = sprintf(
'MAKEFLAGS="%s" %s',
self::getNonDebugMakeFlags(),
$command,
);

if (function_exists('Safe\shell_exec')) {
return shell_exec($command);
}
Expand All @@ -144,6 +154,18 @@ final protected static function executeCommand(string $command): string
return $safeResult;
}

final protected static function getNonDebugMakeFlags(): string
{
$makeFlags = (string) getenv('MAKEFLAGS');

$nonDebugFlags = array_filter(
explode(' ', $makeFlags),
static fn (string $flag) => !str_starts_with($flag, '--debug='),
);

return implode(' ', $nonDebugFlags);
}

private static function getTimeout(): string
{
try {
Expand Down

0 comments on commit cef335a

Please sign in to comment.