Skip to content

Commit

Permalink
fix: Ensure the debug flag is not forwarded
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 committed Oct 6, 2023
1 parent 2982140 commit 340c1f5
Show file tree
Hide file tree
Showing 2 changed files with 48 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
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');

Check warning on line 159 in src/Test/BaseMakefileTestCase.php

View workflow job for this annotation

GitHub Actions / Infection with PHP 8.2

Escaped Mutant for Mutator "CastString": --- Original +++ New @@ @@ } protected static final function getNonDebugMakeFlags() : string { - $makeFlags = (string) getenv('MAKEFLAGS'); + $makeFlags = getenv('MAKEFLAGS'); $nonDebugFlags = array_filter(explode(' ', $makeFlags), static fn(string $flag) => str_starts_with('--debug=', $flag)); return implode(' ', $nonDebugFlags); }

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

return implode(' ', $nonDebugFlags);
}

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

0 comments on commit 340c1f5

Please sign in to comment.