Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/Symfony/Component/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ public function run(?InputInterface $input = null, ?OutputInterface $output = nu
}
}

$prevShellVerbosity = getenv('SHELL_VERBOSITY');

try {
$this->configureIO($input, $output);

Expand Down Expand Up @@ -223,6 +225,22 @@ public function run(?InputInterface $input = null, ?OutputInterface $output = nu
$phpHandler[0]->setExceptionHandler($finalHandler);
}
}

// SHELL_VERBOSITY is set by Application::configureIO so we need to unset/reset it
// to its previous value to avoid one command verbosity to spread to other commands
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;
}
}

if ($this->autoExit) {
Expand Down
38 changes: 9 additions & 29 deletions src/Symfony/Component/Console/Tester/ApplicationTester.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,37 +47,17 @@ public function __construct(
*/
public function run(array $input, array $options = []): int
{
$prevShellVerbosity = getenv('SHELL_VERBOSITY');

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

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

$this->initOutput($options);
$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;
}
}
return $this->statusCode = $this->application->run($this->input, $this->output);
}
}
99 changes: 98 additions & 1 deletion src/Symfony/Component/Console/Tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\Output;
Expand Down Expand Up @@ -831,7 +832,7 @@ public function testSetCatchErrors(bool $catchExceptions)

try {
$tester->run(['command' => 'boom']);
$this->fail('The exception is not catched.');
$this->fail('The exception is not caught.');
} catch (\Throwable $e) {
$this->assertInstanceOf(\Error::class, $e);
$this->assertSame('This is an error.', $e->getMessage());
Expand Down Expand Up @@ -2463,6 +2464,102 @@ private function createSignalableApplication(Command $command, ?EventDispatcherI

return $application;
}

public function testShellVerbosityIsRestoredAfterCommandExecutionWithInitialValue()
{
// Set initial SHELL_VERBOSITY
putenv('SHELL_VERBOSITY=-2');
$_ENV['SHELL_VERBOSITY'] = '-2';
$_SERVER['SHELL_VERBOSITY'] = '-2';

$application = new Application();
$application->setAutoExit(false);
$application->register('foo')
->setCode(function (InputInterface $input, OutputInterface $output): int {
$output->write('SHELL_VERBOSITY: '.$_SERVER['SHELL_VERBOSITY']);

return 0;
});

$input = new ArrayInput(['command' => 'foo', '--verbose' => 3]);
$output = new BufferedOutput();

$application->run($input, $output);

$this->assertSame('SHELL_VERBOSITY: 3', $output->fetch());
$this->assertSame('-2', getenv('SHELL_VERBOSITY'));
$this->assertSame('-2', $_ENV['SHELL_VERBOSITY']);
$this->assertSame('-2', $_SERVER['SHELL_VERBOSITY']);

// Clean up for other tests
putenv('SHELL_VERBOSITY');
unset($_ENV['SHELL_VERBOSITY']);
unset($_SERVER['SHELL_VERBOSITY']);
}

public function testShellVerbosityIsRemovedAfterCommandExecutionWhenNotSetInitially()
{
// Ensure SHELL_VERBOSITY is not set initially
putenv('SHELL_VERBOSITY');
unset($_ENV['SHELL_VERBOSITY']);
unset($_SERVER['SHELL_VERBOSITY']);

$application = new Application();
$application->setAutoExit(false);
$application->register('foo')
->setCode(function (InputInterface $input, OutputInterface $output): int {
$output->write('SHELL_VERBOSITY: '.$_SERVER['SHELL_VERBOSITY']);

return 0;
});

$input = new ArrayInput(['command' => 'foo', '--verbose' => 3]);
$output = new BufferedOutput();

$application->run($input, $output);

$this->assertSame('SHELL_VERBOSITY: 3', $output->fetch());
$this->assertFalse(getenv('SHELL_VERBOSITY'));
$this->assertArrayNotHasKey('SHELL_VERBOSITY', $_ENV);
$this->assertArrayNotHasKey('SHELL_VERBOSITY', $_SERVER);
}

public function testShellVerbosityDoesNotLeakBetweenCommandExecutions()
{
// Ensure no initial SHELL_VERBOSITY
putenv('SHELL_VERBOSITY');
unset($_ENV['SHELL_VERBOSITY']);
unset($_SERVER['SHELL_VERBOSITY']);

$application = new Application();
$application->setAutoExit(false);
$application->register('verbose-cmd')
->setCode(function (InputInterface $input, OutputInterface $output): int {
$output->write('SHELL_VERBOSITY: '.$_SERVER['SHELL_VERBOSITY']);

return 0;
});
$application->register('normal-cmd')
->setCode(function (InputInterface $input, OutputInterface $output): int {
$output->write('SHELL_VERBOSITY: '.$_SERVER['SHELL_VERBOSITY']);

return 0;
});

$output = new BufferedOutput();

$application->run(new ArrayInput(['command' => 'verbose-cmd', '--verbose' => true]), $output);

$this->assertSame('SHELL_VERBOSITY: 1', $output->fetch(), 'SHELL_VERBOSITY should be set to 1 for verbose command');
$this->assertFalse(getenv('SHELL_VERBOSITY'), 'SHELL_VERBOSITY should not be set after first command');

$application->run(new ArrayInput(['command' => 'normal-cmd']), $output);

$this->assertSame('SHELL_VERBOSITY: 0', $output->fetch(), 'SHELL_VERBOSITY should not leak to second command');
$this->assertFalse(getenv('SHELL_VERBOSITY'), 'SHELL_VERBOSITY should not leak to second command');
$this->assertArrayNotHasKey('SHELL_VERBOSITY', $_ENV);
$this->assertArrayNotHasKey('SHELL_VERBOSITY', $_SERVER);
}
}

class CustomApplication extends Application
Expand Down