Skip to content

Commit

Permalink
Set XDEBUG_MODE for processes with coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
sanmai committed May 4, 2021
1 parent e60c464 commit 5390f63
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
64 changes: 64 additions & 0 deletions src/Process/CoveredPhpProcess.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* This code is licensed under the BSD 3-Clause License.
*
* Copyright (c) 2017, Maks Rafalko
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

declare(strict_types=1);

namespace Infection\Process;

use function array_merge;
use Symfony\Component\Process\Process;

/**
* @internal
*
* Process which setups necessary environment variables to make coverage reporting happen
* without any extra user interaction.
*
* As of now we only cover Xdebug, adding XDEBUG_MODE environment variable to ensure it
* is properly activated. Since we can't know if Xdebug was offloaded, we add this env
* variable at all times.
*/
final class CoveredPhpProcess extends Process
{
/**
* @param array<string|bool>|null $env
*/
public function start(?callable $callback = null, ?array $env = null): void
{
$env = array_merge($env ?? [], [
'XDEBUG_MODE' => 'debug',
]);

parent::start($callback, $env);
}
}
3 changes: 2 additions & 1 deletion src/Process/Factory/InitialTestsRunProcessFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
namespace Infection\Process\Factory;

use Infection\AbstractTestFramework\TestFrameworkAdapter;
use Infection\Process\CoveredPhpProcess;
use Infection\Process\OriginalPhpProcess;
use function method_exists;
use Symfony\Component\Process\Process;
Expand Down Expand Up @@ -64,7 +65,7 @@ public function createProcess(
bool $skipCoverage
): Process {
// If we're expecting to receive a code coverage, test process must run in a vanilla environment
$processClass = $skipCoverage ? Process::class : OriginalPhpProcess::class;
$processClass = $skipCoverage ? CoveredPhpProcess::class : OriginalPhpProcess::class;

/** @var Process $process */
$process = new $processClass(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
namespace Infection\Tests\Process\Factory;

use Infection\AbstractTestFramework\TestFrameworkAdapter;
use Infection\Process\CoveredPhpProcess;
use Infection\Process\Factory\InitialTestsRunProcessFactory;
use Infection\Process\OriginalPhpProcess;
use const PHP_OS_FAMILY;
Expand Down Expand Up @@ -86,6 +87,7 @@ public function test_it_creates_a_process_with_coverage_skipped(): void

$this->assertNull($process->getTimeout());
$this->assertNotInstanceOf(OriginalPhpProcess::class, $process);
$this->assertInstanceOf(CoveredPhpProcess::class, $process);
}

public function test_it_creates_a_process_with_coverage(): void
Expand Down

0 comments on commit 5390f63

Please sign in to comment.