-
Notifications
You must be signed in to change notification settings - Fork 438
1126 bin console task sf #1138
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
Merged
Merged
1126 bin console task sf #1138
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
179c793
#1126 update contributing docs
bortefi 0f5673f
#1126 symfony console: update docs
bortefi f6c7385
#1126 Add SymfonyConsole Task, Test, service config
bortefi d7a2a28
#1126 update non-static method call
bortefi ba7cbf1
#1126 prevent symfony console: empty 'command' array config
bortefi 6e54735
#1126 Add composer suggest 'symfony/console'
bortefi 42f89ca
#1126 update docs: include default path symfony console
bortefi 4d34896
#1226 Add options: ignore_patterns, whitelist_patterns, triggered_by,…
bortefi 5a700bd
#1226 update test cases
bortefi 250ca75
#1226 update symfony_console docs
bortefi 3900ffb
Update composer, docs, remove php excutable finder
bortefi 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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,51 @@ | ||
# Symfony Console | ||
|
||
Run a symfony console command. | ||
|
||
## Composer | ||
|
||
Requires the Symfony Console component: [Console Component](https://symfony.com/components/Console) | ||
|
||
```bash | ||
composer require symfony/console | ||
``` | ||
|
||
## Config | ||
|
||
The task lives under the `symfony_console` namespace and has following configurable parameters: | ||
|
||
```yaml | ||
# grumphp.yml | ||
grumphp: | ||
tasks: | ||
symfony_console: | ||
veewee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
command: [ "lint:container", "-vvv" ] | ||
``` | ||
|
||
**Note:** GrumPHP will use the default Symfony console path `./bin/console` from the project root. | ||
|
||
### Parameters | ||
|
||
**command** | ||
|
||
veewee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Specify the symfony command with defined options and arguments. | ||
Verify the installed console component version for available commands `./bin/console list` | ||
|
||
## Multiple Console command tasks | ||
|
||
[Run the same task twice with different configuration](../tasks.md#run-the-same-task-twice-with-different-configuration) | ||
|
||
Specific running multiple symfony console commands: | ||
|
||
```yaml | ||
# grumphp.yml | ||
grumphp: | ||
lint-container: | ||
command: [ "lint:container", "-vvv"] | ||
metadata: | ||
task: symfony_console | ||
lint-yaml: | ||
command: [ "lint:yaml", "path/to/yaml"] | ||
metadata: | ||
task: symfony_console | ||
``` |
This file contains hidden or 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
This file contains hidden or 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,64 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace GrumPHP\Task; | ||
|
||
use GrumPHP\Formatter\ProcessFormatterInterface; | ||
use GrumPHP\Runner\TaskResult; | ||
use GrumPHP\Runner\TaskResultInterface; | ||
use GrumPHP\Task\Config\ConfigOptionsResolver; | ||
use GrumPHP\Task\Context\ContextInterface; | ||
use GrumPHP\Task\Context\GitPreCommitContext; | ||
use GrumPHP\Task\Context\RunContext; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
/** @extends AbstractExternalTask<ProcessFormatterInterface> */ | ||
class SymfonyConsole extends AbstractExternalTask | ||
{ | ||
public static function getConfigurableOptions(): ConfigOptionsResolver | ||
{ | ||
return ConfigOptionsResolver::fromOptionsResolver( | ||
(new OptionsResolver()) | ||
->setDefaults([ | ||
'bin' => './bin/console', | ||
'command' => [], | ||
]) | ||
->addAllowedTypes('command', ['string[]']) | ||
->setRequired('command') | ||
); | ||
} | ||
|
||
public function canRunInContext(ContextInterface $context): bool | ||
{ | ||
return ($context instanceof GitPreCommitContext || $context instanceof RunContext); | ||
} | ||
|
||
public function run(ContextInterface $context): TaskResultInterface | ||
{ | ||
$config = $this->getConfig()->getOptions(); | ||
veewee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (0 === \count($context->getFiles())) { | ||
return TaskResult::createSkipped($this, $context); | ||
} | ||
|
||
if ('' === \implode('', $config['command'])) { | ||
return TaskResult::createNonBlockingFailed( | ||
veewee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$this, | ||
$context, | ||
'Missing "command" configuration for task "symfony_console".' | ||
); | ||
} | ||
|
||
$arguments = $this->processBuilder->createArgumentsForCommand($config['bin']); | ||
bortefi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$arguments->addArgumentArray('%s', $config['command']); | ||
|
||
$process = $this->processBuilder->buildProcess($arguments); | ||
$process->run(); | ||
|
||
if (!$process->isSuccessful()) { | ||
return TaskResult::createFailed($this, $context, $this->formatter->format($process)); | ||
} | ||
|
||
return TaskResult::createPassed($this, $context); | ||
} | ||
} |
This file contains hidden or 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
This file contains hidden or 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,203 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace GrumPHPTest\Unit\Task; | ||
|
||
use GrumPHP\Runner\TaskResultInterface; | ||
use GrumPHP\Task\Context\ContextInterface; | ||
use GrumPHP\Task\Context\GitPreCommitContext; | ||
use GrumPHP\Task\Context\RunContext; | ||
use GrumPHP\Task\SymfonyConsole; | ||
use GrumPHP\Task\TaskInterface; | ||
use GrumPHP\Test\Task\AbstractExternalTaskTestCase; | ||
|
||
final class SymfonyConsoleTest extends AbstractExternalTaskTestCase | ||
{ | ||
protected function provideTask(): TaskInterface | ||
{ | ||
return new SymfonyConsole( | ||
$this->processBuilder->reveal(), | ||
$this->formatter->reveal() | ||
); | ||
} | ||
|
||
public function provideConfigurableOptions(): iterable | ||
{ | ||
yield 'default' => [ | ||
[], | ||
[ | ||
'bin' => './bin/console', | ||
'command' => [], | ||
] | ||
]; | ||
|
||
yield 'single-command' => [ | ||
[ | ||
'command' => ['task:run'], | ||
], | ||
[ | ||
'bin' => './bin/console', | ||
'command' => [ | ||
'task:run' | ||
], | ||
] | ||
]; | ||
|
||
yield 'array-command' => [ | ||
[ | ||
'command' => ['task:run', '--env', 'dev', '-vvv'], | ||
], | ||
[ | ||
'bin' => './bin/console', | ||
'command' => [ | ||
'task:run', | ||
'--env', | ||
'dev', | ||
'-vvv' | ||
], | ||
] | ||
]; | ||
} | ||
|
||
public function provideRunContexts(): iterable | ||
{ | ||
yield 'run-context' => [ | ||
true, | ||
$this->mockContext(RunContext::class) | ||
]; | ||
|
||
yield 'pre-commit-context' => [ | ||
true, | ||
$this->mockContext(GitPreCommitContext::class) | ||
]; | ||
|
||
yield 'other' => [ | ||
false, | ||
$this->mockContext() | ||
]; | ||
} | ||
|
||
public function provideFailsOnStuff(): iterable | ||
{ | ||
yield 'exitCode1' => [ | ||
[ | ||
'command' => ['--version'] | ||
], | ||
$this->mockContext(RunContext::class, ['hello.php', 'hello2.php']), | ||
function() { | ||
$process = $this->mockProcess(1); | ||
$this->mockProcessBuilder('./bin/console', $process); | ||
$this->formatter->format($process)->willReturn('nope'); | ||
}, | ||
'nope' | ||
]; | ||
} | ||
|
||
public function providePassesOnStuff(): iterable | ||
{ | ||
yield 'exitCode0' => [ | ||
[ | ||
'command' => ['--version'] | ||
], | ||
$this->mockContext(RunContext::class, ['hello.php', 'hello2.php']), | ||
function() { | ||
$this->mockProcessBuilder('./bin/console', $this->mockProcess()); | ||
} | ||
]; | ||
} | ||
|
||
public function provideSkipsOnStuff(): iterable | ||
{ | ||
yield 'no-files' => [ | ||
[], | ||
$this->mockContext(RunContext::class), | ||
function() { | ||
} | ||
]; | ||
} | ||
|
||
public function provideExternalTaskRuns(): iterable | ||
{ | ||
yield 'single-command' => [ | ||
[ | ||
'command' => ['lint:container'] | ||
], | ||
$this->mockContext(RunContext::class, ['hello.php', 'hello2.php']), | ||
'./bin/console', | ||
[ | ||
'lint:container', | ||
] | ||
]; | ||
|
||
yield 'array-command' => [ | ||
[ | ||
'command' => [ | ||
'task:run', | ||
'--env', | ||
'dev', | ||
'-vvv' | ||
] | ||
], | ||
$this->mockContext(RunContext::class, ['hello.php', 'hello2.php']), | ||
'./bin/console', | ||
[ | ||
'task:run', | ||
'--env', | ||
'dev', | ||
'-vvv' | ||
] | ||
]; | ||
} | ||
|
||
/** | ||
* @test | ||
* @dataProvider provideFailsNonBlockingOnStuff | ||
*/ | ||
public function it_fails_non_blocking_on_stuff( | ||
array $config, | ||
ContextInterface $context, | ||
callable $configurator, | ||
string $expectedErrorMessage, | ||
): void { | ||
$task = $this->configureTask($config); | ||
\Closure::bind($configurator, $this)($task->getConfig()->getOptions(), $context); | ||
$result = $task->run($context); | ||
|
||
self::assertInstanceOf(TaskResultInterface::class, $result); | ||
self::assertSame(TaskResultInterface::NONBLOCKING_FAILED, $result->getResultCode()); | ||
self::assertSame($task, $result->getTask()); | ||
self::assertSame($context, $result->getContext()); | ||
self::assertSame($expectedErrorMessage, $result->getMessage()); | ||
} | ||
|
||
public function provideFailsNonBlockingOnStuff(): iterable | ||
{ | ||
yield 'missing-command' => [ | ||
[ | ||
// missing command | ||
], | ||
$this->mockContext(RunContext::class, ['hello.php', 'hello2.php']), | ||
function() {}, | ||
'Missing "command" configuration for task "symfony_console".' | ||
]; | ||
|
||
yield 'empty-command-array' => [ | ||
[ | ||
'command' => [], // missing command config | ||
], | ||
$this->mockContext(RunContext::class, ['hello.php', 'hello2.php']), | ||
function() {}, | ||
'Missing "command" configuration for task "symfony_console".' | ||
]; | ||
|
||
yield 'empty-command-data' => [ | ||
[ | ||
'command' => [""], // empty command config | ||
], | ||
$this->mockContext(RunContext::class, ['hello.php', 'hello2.php']), | ||
function() {}, | ||
'Missing "command" configuration for task "symfony_console".' | ||
]; | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.