Skip to content

Commit

Permalink
bug #46747 Fix global state pollution between tests run with Applicat…
Browse files Browse the repository at this point in the history
…ionTester (Seldaek)

This PR was merged into the 4.4 branch.

Discussion
----------

Fix global state pollution between tests run with ApplicationTester

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets       | Fix #... <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead -->
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!-- required for new features -->

If you run a test with ApplicationTester which includes -v or similar flag, then https://github.com/symfony/symfony/blob/4.4/src/Symfony/Component/Console/Application.php#L985-L989 leaves the env in a dirty state, and all tests following also use -v.

Commits
-------

f4c81f13ca Fix global state pollution between tests run with ApplicationTester
  • Loading branch information
fabpot committed Jun 23, 2022
2 parents b4a1d2d + 7797262 commit 8a2628d
Showing 1 changed file with 29 additions and 9 deletions.
38 changes: 29 additions & 9 deletions Tester/ApplicationTester.php
Expand Up @@ -54,17 +54,37 @@ public function __construct(Application $application)
*/
public function run(array $input, $options = [])
{
$this->input = new ArrayInput($input);
if (isset($options['interactive'])) {
$this->input->setInteractive($options['interactive']);
}
$prevShellVerbosity = getenv('SHELL_VERBOSITY');

if ($this->inputs) {
$this->input->setStream(self::createStream($this->inputs));
}
try {
$this->input = new ArrayInput($input);
if (isset($options['interactive'])) {
$this->input->setInteractive($options['interactive']);
}

$this->initOutput($options);
if ($this->inputs) {
$this->input->setStream(self::createStream($this->inputs));
}

return $this->statusCode = $this->application->run($this->input, $this->output);
$this->initOutput($options);

return $this->statusCode = $this->application->run($this->input, $this->output);
} finally {
// SHELL_VERBOSITY is set by Application::configureIO so we need to unset/reset it
// to its previous value to avoid one test's verbosity to spread to the following tests
if (false === $prevShellVerbosity) {
if (\function_exists('putenv')) {
@putenv('SHELL_VERBOSITY');
}
unset($_ENV['SHELL_VERBOSITY']);
unset($_SERVER['SHELL_VERBOSITY']);
} else {
if (\function_exists('putenv')) {
@putenv('SHELL_VERBOSITY='.$prevShellVerbosity);
}
$_ENV['SHELL_VERBOSITY'] = $prevShellVerbosity;
$_SERVER['SHELL_VERBOSITY'] = $prevShellVerbosity;
}
}
}
}

0 comments on commit 8a2628d

Please sign in to comment.