Skip to content

Commit d6cf27a

Browse files
committed
feat: add default output flags to artisan and wp commands
1 parent 37c9ae2 commit d6cf27a

File tree

5 files changed

+74
-10
lines changed

5 files changed

+74
-10
lines changed

src/Command/Laravel/ArtisanCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ protected function perform()
9494

9595
$this->output->info(sprintf('Running "<comment>php artisan %s</comment>" %s "<comment>%s</comment>" environment', $command, $async ? 'asynchronously on' : 'on', $environment->getName()));
9696

97-
$result = $this->invokeArtisanCommand($this->getProject(), $command, $environment, $async ? 0 : null);
97+
$result = $this->invokeArtisanCommand($this->getProject(), $this->appendCommandOptionIfMissing($command, '--no-ansi', ['--ansi', '--no-ansi']), $environment, $async ? 0 : null);
9898

9999
if (!$async) {
100100
$this->output->newLine();

src/Command/ParsesConsoleCommandTrait.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,20 @@
1515

1616
trait ParsesConsoleCommandTrait
1717
{
18+
/**
19+
* Appends a command option if no matching option is present.
20+
*/
21+
protected function appendCommandOptionIfMissing(string $command, string $option, array $matchingOptions): string
22+
{
23+
$hasMatchingOption = collect($this->parseCommand($command))->contains(function (string $commandPart) use ($matchingOptions): bool {
24+
return collect($matchingOptions)->contains(function (string $matchingOption) use ($commandPart): bool {
25+
return $matchingOption === $commandPart;
26+
});
27+
});
28+
29+
return $hasMatchingOption ? $command : sprintf('%s %s', $command, $option);
30+
}
31+
1832
/**
1933
* Parses a command string into command parts.
2034
*/

src/Command/WordPress/WpCliCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ protected function perform()
8383

8484
$this->output->info(sprintf('Running "<comment>wp %s</comment>" %s "<comment>%s</comment>" environment', $command, $async ? 'asynchronously on' : 'on', $environment->getName()));
8585

86-
$result = $this->invokeWpCliCommand($this->getProject(), $command, $environment, $async ? 0 : null);
86+
$result = $this->invokeWpCliCommand($this->getProject(), $this->appendCommandOptionIfMissing($command, '--no-color', ['--color', '--no-color']), $environment, $async ? 0 : null);
8787

8888
if (!$async) {
8989
$this->output->newLine();

tests/Integration/Command/Laravel/ArtisanCommandTest.php

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,39 @@
2626

2727
class ArtisanCommandTest extends TestCase
2828
{
29+
public function testPerformDoesNotAddNoAnsiOptionWhenAnsiOptionIsPresent(): void
30+
{
31+
$this->setupActiveTeam();
32+
$project = $this->setupValidProject(1, 'project', ['production' => []], 'laravel', LaravelProjectType::class);
33+
$environment = EnvironmentFactory::create(['name' => 'production']);
34+
35+
$this->apiClient->shouldReceive('getEnvironments')->with(\Mockery::type(Project::class))->andReturn(new ResourceCollection([$environment]));
36+
$this->apiClient->shouldReceive('createInvocation')->with(\Mockery::type(Project::class), $environment, ['php' => 'artisan migrate --ansi'])->andReturn(collect(['id' => 123]));
37+
$this->apiClient->shouldReceive('getInvocation')->with(123)->andReturn(collect([
38+
'status' => 'completed',
39+
'result' => [
40+
'exitCode' => 0,
41+
'output' => 'migrate output',
42+
],
43+
]));
44+
45+
$this->bootApplication([new ArtisanCommand($this->apiClient, $this->createExecutionContextFactory([
46+
Environment::class => function () { return new EnvironmentDefinition(); },
47+
]))]);
48+
49+
$tester = $this->executeCommand(ArtisanCommand::NAME, ['artisan-command' => ['migrate', '--ansi'], '--environment' => 'production']);
50+
51+
$this->assertStringContainsString('Running "php artisan migrate --ansi" on "production" environment', $tester->getDisplay());
52+
}
53+
2954
public function testPerformExecutesArtisanCommand(): void
3055
{
3156
$this->setupActiveTeam();
3257
$project = $this->setupValidProject(1, 'project', ['production' => []], 'laravel', LaravelProjectType::class);
3358
$environment = EnvironmentFactory::create(['name' => 'production']);
3459

3560
$this->apiClient->shouldReceive('getEnvironments')->with(\Mockery::type(Project::class))->andReturn(new ResourceCollection([$environment]));
36-
$this->apiClient->shouldReceive('createInvocation')->with(\Mockery::type(Project::class), $environment, ['php' => 'artisan migrate:status'])->andReturn(collect(['id' => 123]));
61+
$this->apiClient->shouldReceive('createInvocation')->with(\Mockery::type(Project::class), $environment, ['php' => 'artisan migrate:status --no-ansi'])->andReturn(collect(['id' => 123]));
3762
$this->apiClient->shouldReceive('getInvocation')->with(123)->andReturn(collect([
3863
'status' => 'completed',
3964
'result' => [
@@ -59,7 +84,7 @@ public function testPerformExecutesArtisanCommandAsynchronously(): void
5984
$environment = EnvironmentFactory::create(['name' => 'production']);
6085

6186
$this->apiClient->shouldReceive('getEnvironments')->with(\Mockery::type(Project::class))->andReturn(new ResourceCollection([$environment]));
62-
$this->apiClient->shouldReceive('createInvocation')->with(\Mockery::type(Project::class), $environment, ['php' => 'artisan cache:clear'])->andReturn(collect(['id' => 123]));
87+
$this->apiClient->shouldReceive('createInvocation')->with(\Mockery::type(Project::class), $environment, ['php' => 'artisan cache:clear --no-ansi'])->andReturn(collect(['id' => 123]));
6388

6489
$this->bootApplication([new ArtisanCommand($this->apiClient, $this->createExecutionContextFactory([
6590
Environment::class => function () { return new EnvironmentDefinition(); },
@@ -77,7 +102,7 @@ public function testPerformExecutesArtisanCommandInteractively(): void
77102
$environment = EnvironmentFactory::create(['name' => 'production']);
78103

79104
$this->apiClient->shouldReceive('getEnvironments')->with(\Mockery::type(Project::class))->andReturn(new ResourceCollection([$environment]));
80-
$this->apiClient->shouldReceive('createInvocation')->with(\Mockery::type(Project::class), $environment, ['php' => 'artisan route:list'])->andReturn(collect(['id' => 123]));
105+
$this->apiClient->shouldReceive('createInvocation')->with(\Mockery::type(Project::class), $environment, ['php' => 'artisan route:list --no-ansi'])->andReturn(collect(['id' => 123]));
81106
$this->apiClient->shouldReceive('getInvocation')->with(123)->andReturn(collect([
82107
'status' => 'completed',
83108
'result' => [
@@ -103,7 +128,7 @@ public function testPerformRemovesPhpArtisanPrefixFromCommand(): void
103128
$environment = EnvironmentFactory::create(['name' => 'production']);
104129

105130
$this->apiClient->shouldReceive('getEnvironments')->with(\Mockery::type(Project::class))->andReturn(new ResourceCollection([$environment]));
106-
$this->apiClient->shouldReceive('createInvocation')->with(\Mockery::type(Project::class), $environment, ['php' => 'artisan migrate'])->andReturn(collect(['id' => 123]));
131+
$this->apiClient->shouldReceive('createInvocation')->with(\Mockery::type(Project::class), $environment, ['php' => 'artisan migrate --no-ansi'])->andReturn(collect(['id' => 123]));
107132
$this->apiClient->shouldReceive('getInvocation')->with(123)->andReturn(collect([
108133
'status' => 'completed',
109134
'result' => [

tests/Integration/Command/WordPress/WpCliCommandTest.php

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,39 @@
2626

2727
class WpCliCommandTest extends TestCase
2828
{
29+
public function testPerformDoesNotAddNoColorOptionWhenColorOptionIsPresent(): void
30+
{
31+
$this->setupActiveTeam();
32+
$project = $this->setupValidProject(1, 'project', ['production' => []], 'wordpress', WordPressProjectType::class);
33+
$environment = EnvironmentFactory::create(['name' => 'production']);
34+
35+
$this->apiClient->shouldReceive('getEnvironments')->with(\Mockery::type(Project::class))->andReturn(new ResourceCollection([$environment]));
36+
$this->apiClient->shouldReceive('createInvocation')->with(\Mockery::type(Project::class), $environment, ['php' => 'bin/wp plugin list --color'])->andReturn(collect(['id' => 123]));
37+
$this->apiClient->shouldReceive('getInvocation')->with(123)->andReturn(collect([
38+
'status' => 'completed',
39+
'result' => [
40+
'exitCode' => 0,
41+
'output' => 'plugin list output',
42+
],
43+
]));
44+
45+
$this->bootApplication([new WpCliCommand($this->apiClient, $this->createExecutionContextFactory([
46+
Environment::class => function () { return new EnvironmentDefinition(); },
47+
]))]);
48+
49+
$tester = $this->executeCommand(WpCliCommand::NAME, ['wp-command' => ['plugin', 'list', '--color'], '--environment' => 'production']);
50+
51+
$this->assertStringContainsString('Running "wp plugin list --color" on "production" environment', $tester->getDisplay());
52+
}
53+
2954
public function testPerformExecutesWpCliCommand(): void
3055
{
3156
$this->setupActiveTeam();
3257
$project = $this->setupValidProject(1, 'project', ['production' => []], 'wordpress', WordPressProjectType::class);
3358
$environment = EnvironmentFactory::create(['name' => 'production']);
3459

3560
$this->apiClient->shouldReceive('getEnvironments')->with(\Mockery::type(Project::class))->andReturn(new ResourceCollection([$environment]));
36-
$this->apiClient->shouldReceive('createInvocation')->with(\Mockery::type(Project::class), $environment, ['php' => 'bin/wp plugin list'])->andReturn(collect(['id' => 123]));
61+
$this->apiClient->shouldReceive('createInvocation')->with(\Mockery::type(Project::class), $environment, ['php' => 'bin/wp plugin list --no-color'])->andReturn(collect(['id' => 123]));
3762
$this->apiClient->shouldReceive('getInvocation')->with(123)->andReturn(collect([
3863
'status' => 'completed',
3964
'result' => [
@@ -59,7 +84,7 @@ public function testPerformExecutesWpCliCommandAsynchronously(): void
5984
$environment = EnvironmentFactory::create(['name' => 'production']);
6085

6186
$this->apiClient->shouldReceive('getEnvironments')->with(\Mockery::type(Project::class))->andReturn(new ResourceCollection([$environment]));
62-
$this->apiClient->shouldReceive('createInvocation')->with(\Mockery::type(Project::class), $environment, ['php' => 'bin/wp cache flush'])->andReturn(collect(['id' => 123]));
87+
$this->apiClient->shouldReceive('createInvocation')->with(\Mockery::type(Project::class), $environment, ['php' => 'bin/wp cache flush --no-color'])->andReturn(collect(['id' => 123]));
6388

6489
$this->bootApplication([new WpCliCommand($this->apiClient, $this->createExecutionContextFactory([
6590
Environment::class => function () { return new EnvironmentDefinition(); },
@@ -77,7 +102,7 @@ public function testPerformExecutesWpCliCommandInteractively(): void
77102
$environment = EnvironmentFactory::create(['name' => 'production']);
78103

79104
$this->apiClient->shouldReceive('getEnvironments')->with(\Mockery::type(Project::class))->andReturn(new ResourceCollection([$environment]));
80-
$this->apiClient->shouldReceive('createInvocation')->with(\Mockery::type(Project::class), $environment, ['php' => 'bin/wp post list'])->andReturn(collect(['id' => 123]));
105+
$this->apiClient->shouldReceive('createInvocation')->with(\Mockery::type(Project::class), $environment, ['php' => 'bin/wp post list --no-color'])->andReturn(collect(['id' => 123]));
81106
$this->apiClient->shouldReceive('getInvocation')->with(123)->andReturn(collect([
82107
'status' => 'completed',
83108
'result' => [
@@ -103,7 +128,7 @@ public function testPerformRemovesWpPrefixFromCommand(): void
103128
$environment = EnvironmentFactory::create(['name' => 'production']);
104129

105130
$this->apiClient->shouldReceive('getEnvironments')->with(\Mockery::type(Project::class))->andReturn(new ResourceCollection([$environment]));
106-
$this->apiClient->shouldReceive('createInvocation')->with(\Mockery::type(Project::class), $environment, ['php' => 'bin/wp plugin list'])->andReturn(collect(['id' => 123]));
131+
$this->apiClient->shouldReceive('createInvocation')->with(\Mockery::type(Project::class), $environment, ['php' => 'bin/wp plugin list --no-color'])->andReturn(collect(['id' => 123]));
107132
$this->apiClient->shouldReceive('getInvocation')->with(123)->andReturn(collect([
108133
'status' => 'completed',
109134
'result' => [

0 commit comments

Comments
 (0)