-
Notifications
You must be signed in to change notification settings - Fork 10.9k
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
[9.x] Return non-zero exit code for uncaught exceptions #46541
Merged
Merged
Changes from 6 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
5da78b1
wip
bert-w 35fcee6
Update MakesArtisanScript.php
bert-w 0cb217a
artisanScript function
bert-w ba87d3d
Update MakesArtisanScript.php
bert-w 38fece5
styleci
bert-w 448e08f
change to PhpExecutableFinder
bert-w 3dc765b
move `exit(1)` call up to only console applications
bert-w 8c070d2
styleci
bert-w 8f1a29d
wip
crynobone c23dc7b
Merge pull request #1 from crynobone/return-non-zero-exit-codes
bert-w 77dada9
imports
bert-w 70acaad
formatting
taylorotwell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
src/Illuminate/Foundation/Testing/Concerns/MakesArtisanScript.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
<?php | ||
|
||
namespace Illuminate\Foundation\Testing\Concerns; | ||
|
||
use Illuminate\Filesystem\Filesystem; | ||
use Illuminate\Support\Arr; | ||
use Illuminate\Support\Str; | ||
use Symfony\Component\Process\PhpExecutableFinder; | ||
|
||
trait MakesArtisanScript | ||
{ | ||
/** | ||
* The file system. | ||
* | ||
* @var Filesystem|null | ||
*/ | ||
protected $fileSystem; | ||
|
||
/** | ||
* The contents of the original Artisan file if it exists. | ||
* | ||
* @var string|null | ||
*/ | ||
protected $originalArtisanFile; | ||
|
||
/** | ||
* Create an Artisan script in the Laravel testbench core for external testing. | ||
* | ||
* @param array<string, callable>|callable $slots | ||
* @return string | ||
*/ | ||
public function setUpArtisanScript($slots = []): string | ||
{ | ||
$this->fileSystem = new Filesystem; | ||
$path = base_path('artisan'); | ||
|
||
$uuid = Str::random(32); | ||
|
||
// Save existing artisan script if there is one. | ||
if ($this->fileSystem->exists($path)) { | ||
$this->originalArtisanFile = $this->fileSystem->get($path); | ||
} | ||
|
||
$this->fileSystem->put( | ||
base_path('artisan'), | ||
$this->buildArtisanScript($uuid, $slots) | ||
); | ||
|
||
return $uuid; | ||
} | ||
|
||
/** | ||
* Delete an Artisan script and revert it to the cached original. | ||
* | ||
* @return void | ||
*/ | ||
public function tearDownArtisanScript(): void | ||
{ | ||
$this->fileSystem->delete(base_path('artisan')); | ||
if (! is_null($this->originalArtisanFile)) { | ||
$this->fileSystem->put(base_path('artisan'), $this->originalArtisanFile); | ||
} | ||
} | ||
|
||
/** | ||
* Execute the Artisan script in a separate process and return the output and exit code. | ||
* | ||
* @param string $command | ||
* @return array | ||
*/ | ||
public function artisanScript($command): array | ||
{ | ||
$output = $exitCode = null; | ||
exec((new PhpExecutableFinder)->find().' '.base_path('artisan').' '.$command, $output, $exitCode); | ||
|
||
return [$output, $exitCode]; | ||
} | ||
|
||
/** | ||
* Build a custom Artisan script containing specific scripts. | ||
* | ||
* @param string $uuid | ||
* @param array<string, callable>|callable $slots | ||
* @return string | ||
*/ | ||
protected function buildArtisanScript($uuid, $slots = []): string | ||
{ | ||
// If no array is passed, the default "preHandle" slot is assumed. | ||
$slots = ! is_array($slots) ? ['preHandle' => $slots] : $slots; | ||
|
||
$thisFile = __FILE__; | ||
|
||
$slots = array_merge([ | ||
'preBootstrap' => '', 'preKernel' => '', 'preHandle' => '', 'preTerminate' => '', 'preExit' => '', | ||
], Arr::map($slots, fn ($part) => value($part, $uuid))); | ||
|
||
return <<<PHP | ||
#!/usr/bin/env php | ||
<?php | ||
|
||
define('LARAVEL_START', microtime(true)); | ||
|
||
// This is a custom artisan testing script made specifically for: | ||
// File: {$thisFile} | ||
// Uuid: {$uuid} | ||
|
||
require __DIR__.'/../../../autoload.php'; | ||
|
||
{$slots['preBootstrap']} | ||
|
||
\$app = require_once __DIR__.'/bootstrap/app.php'; | ||
|
||
{$slots['preKernel']} | ||
|
||
\$kernel = \$app->make(Illuminate\Contracts\Console\Kernel::class); | ||
|
||
{$slots['preHandle']} | ||
|
||
\$status = \$kernel->handle( | ||
\$input = new Symfony\Component\Console\Input\ArgvInput, | ||
new Symfony\Component\Console\Output\ConsoleOutput | ||
); | ||
|
||
{$slots['preTerminate']} | ||
|
||
\$kernel->terminate(\$input, \$status); | ||
|
||
{$slots['preExit']} | ||
|
||
exit(\$status); | ||
|
||
PHP; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't this cause Octane to exit the worker on every uncaught exception, which is not desired?
laravel/octane#654
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good question. That could be the case yes.
Anyhow, there is no way to say to php "exit with exitCode=1 on your own accord (when its time to exit)". AFAIK you must call
exit(1)
yourself which will then still call any remaining__destruct()
listeners and shutdown handlers.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved the
exit(1)
call a few lines up now (inside the$app->runningInConsole()
since this is a command line related issue)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Octane workers still will have$app->runningInConsole() === true
since it is an artisan command.Octane worker emits
APP_RUNNING_IN_CONSOLE=false
so should be okay.framework/src/Illuminate/Foundation/Application.php
Line 702 in 6316655