Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix validate sub-command after normalizer failure #352

Closed
wants to merge 5 commits into from
Closed
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
1 change: 1 addition & 0 deletions composer-require-checker.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"Composer\\Plugin\\Capability\\CommandProvider",
"Composer\\Plugin\\Capable",
"Composer\\Plugin\\PluginInterface",
"Symfony\\Component\\Console\\Application",
"Symfony\\Component\\Console\\Input\\ArrayInput",
"Symfony\\Component\\Console\\Input\\InputArgument",
"Symfony\\Component\\Console\\Input\\InputInterface",
Expand Down
5 changes: 5 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ parameters:
count: 1
path: src/Command/NormalizeCommand.php

-
message: "#^Call to function method_exists\\(\\) with Symfony\\\\Component\\\\Console\\\\Application and 'isAutoExitEnabled' will always evaluate to true\\.$#"
count: 1
path: src/Command/NormalizeCommand.php

49 changes: 38 additions & 11 deletions src/Command/NormalizeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,12 @@ protected function execute(Console\Input\InputInterface $input, Console\Output\O
$exception->getMessage()
));

return $this->validateComposerFile(
$this->validateComposerFile(
$output,
$composerFile
);

return 1;
} catch (\RuntimeException $exception) {
$io->writeError(\sprintf(
'<error>%s</error>',
Expand Down Expand Up @@ -360,17 +362,14 @@ private function diff(string $before, string $after): string
*/
private function validateComposerFile(Console\Output\OutputInterface $output, string $composerFile): int
{
/** @var Console\Application $application */
$application = $this->getApplication();

return $application->run(
return $this->applicationRun(
new Console\Input\ArrayInput([
'command' => 'validate',
'file' => $composerFile,
'--no-check-all' => true,
'--no-check-lock' => true,
'--no-check-publish' => true,
'--strict' => true,
//'--no-check-publish' => true,
//'--strict' => true,
]),
$output
);
Expand All @@ -388,10 +387,7 @@ private function validateComposerFile(Console\Output\OutputInterface $output, st
*/
private function updateLockerInWorkingDirectory(Console\Output\OutputInterface $output, string $workingDirectory): int
{
/** @var Console\Application $application */
$application = $this->getApplication();

return $application->run(
return $this->applicationRun(
new Console\Input\ArrayInput([
'command' => 'update',
'--lock' => true,
Expand All @@ -404,4 +400,35 @@ private function updateLockerInWorkingDirectory(Console\Output\OutputInterface $
$output
);
}

/**
* @param Console\Input\InputInterface $input
* @param Console\Output\OutputInterface $output
*
* @throws \Exception
*
* @return int
*/
private function applicationRun(Console\Input\InputInterface $input, Console\Output\OutputInterface $output): int
{
/** @var Console\Application $application */
$application = $this->getApplication();

if (\method_exists($application, 'isAutoExitEnabled')) {
$savedAutoExit = $application->isAutoExitEnabled();
} else {
// BC layer for symfony/console < 3.1
$autoExitProperty = new \ReflectionProperty(Console\Application::class, 'autoExit');
$autoExitProperty->setAccessible(true);
$savedAutoExit = (bool) $autoExitProperty->getValue($application);
}

$application->setAutoExit(false);

try {
return $application->run($input, $output);
} finally {
$application->setAutoExit($savedAutoExit);
}
}
}
1 change: 1 addition & 0 deletions test/Integration/Command/NormalizeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ public function testFailsWhenComposerJsonIsPresentButNotValid(CommandInvocation
self::assertRegExp('/Original JSON is not valid according to schema ".*"/', $display);
self::assertContains('See https://getcomposer.org/doc/04-schema.md for details on the schema', $display);
self::assertContains('No license specified, it is recommended to do so. For closed-source software you may use "proprietary" as license.', $display);
self::assertContains('name : The property name is required', $display);
self::assertEquals($initialState, $scenario->currentState());
}

Expand Down