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

Properly handle input options #1793

Merged
Merged
Show file tree
Hide file tree
Changes from 8 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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# Changelog


## master
[v6.4.2...master](https://github.com/deployphp/deployer/compare/v6.4.2...master)

### Fixed
- Input option handling [#1793]


## v6.4.2
[v6.4.1...v6.4.2](https://github.com/deployphp/deployer/compare/v6.4.1...v6.4.2)

Expand Down Expand Up @@ -442,6 +449,7 @@
- Fixed remove of shared dir on first deploy


[#1793]: https://github.com/deployphp/deployer/pull/1793
[#1792]: https://github.com/deployphp/deployer/pull/1792
[#1790]: https://github.com/deployphp/deployer/pull/1790
[#1778]: https://github.com/deployphp/deployer/issues/1778
Expand Down
55 changes: 13 additions & 42 deletions src/Console/Input/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,64 +17,35 @@ public static function toString(
InputOption $option
): string {
$name = $option->getName();
$values = $input->getOption($name);

if (!$option->acceptValue()) {
return \sprintf(
'--%s',
$name
);
return true === $values
? \sprintf('--%s', $name)
: '';
}

if (!$option->isArray()) {
return self::generatePartialOption(
$option,
$name,
$input->getOption($name)
);
$values = [$values];
}

$isValueRequired = $option->isValueRequired();
/** @var string[] $outputs */
$outputs = [];
foreach ($input->getOption($name) as $value) {
$value = self::generatePartialOption(
$option,
$name,
$value
);

if ($value === '') {
foreach ($values as $value) {
if ($isValueRequired && \null === $value) {
continue;
}

$outputs[] = $value;
}

return \implode(' ', $outputs);
}

/**
* @param null|string $value
*/
private static function generatePartialOption(
InputOption $option,
string $name,
$value
): string {
if (\null !== $value && \strlen($value) !== 0) {
return \sprintf(
'--%s=%s',
$value = sprintf(
'--%s%s%s',
$name,
\null === $value ? '' : '=',
$value
);
}

if ($option->isValueOptional()) {
return \sprintf(
'--%s',
$name
);
$outputs[] = $value;
}

return '';
return \implode(' ', $outputs);
}
}
5 changes: 3 additions & 2 deletions src/Executor/ParallelExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use Deployer\Task\Context;
use Deployer\Task\Task;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;

Expand Down Expand Up @@ -269,7 +268,9 @@ private function generateOptions(): string
$inputs[] = Option::toString($this->input, $option);
}

return implode(' ', $inputs);
return implode(' ', array_filter($inputs, function (string $item): bool {
kick-the-bucket marked this conversation as resolved.
Show resolved Hide resolved
return $item !== '';
}));
}

private function generateArguments(): string
Expand Down
107 changes: 56 additions & 51 deletions test/src/Console/Input/OptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ public function toStringProvider(): \Generator
{
// InputOption::VALUE_NONE
foreach ([
['--fooBar', 'fooBar'],
['--0', '0'],
['--1', '1'],
['--foo\-%&Bar', 'foo\-%&Bar'],
['--ù+ì', 'ù+ì'],
] as list($expectedValue, $optionName)) {
'VALUE_NONE 1' => ['--fooBar', 'fooBar', true],
'VALUE_NONE 2' => ['--0', '0', true],
'VALUE_NONE 3' => ['--1', '1', true],
'VALUE_NONE 4' => ['--foo\-%&Bar', 'foo\-%&Bar', true],
'VALUE_NONE 5' => ['--ù+ì', 'ù+ì', true],
'VALUE_NONE 6' => ['', 'value-none-unset', false],
] as $key => list($expectedValue, $optionName, $optionValue)) {
$input = $this->createMock(InputInterface::class);
$input->expects($this->once())
->method('getOption')
->willReturn($optionValue);

$option = $this->createMock(InputOption::class);
$option->expects($this->once())
Expand All @@ -34,7 +38,7 @@ public function toStringProvider(): \Generator
->method('acceptValue')
->willReturn(\false);

yield [
yield $key => [
$expectedValue,
$input,
$option,
Expand All @@ -43,13 +47,13 @@ public function toStringProvider(): \Generator

// InputOption::VALUE_REQUIRED
foreach ([
['--fooBar=ciao', 'fooBar', 'ciao'],
['', 'fooBar', \null],
['', 'fooBar', ''],
['--fooBar=0', 'fooBar', '0'],
['--foo\-%&Bar=test', 'foo\-%&Bar', 'test'],
['--ù+ì=omg', 'ù+ì', 'omg'],
] as list($expectedValue, $optionName, $optionValue)) {
'VALUE_REQUIRED 1' => ['--fooBar=ciao', 'fooBar', 'ciao'],
'VALUE_REQUIRED 2' => ['', 'fooBar', \null],
'VALUE_REQUIRED 3' => ['--fooBar=', 'fooBar', ''],
'VALUE_REQUIRED 4' => ['--fooBar=0', 'fooBar', '0'],
'VALUE_REQUIRED 5' => ['--foo\-%&Bar=test', 'foo\-%&Bar', 'test'],
'VALUE_REQUIRED 6' => ['--ù+ì=omg', 'ù+ì', 'omg'],
] as $key => list($expectedValue, $optionName, $optionValue)) {
$input = $this->createMock(InputInterface::class);
$input->expects($this->once())
->method('getOption')
Expand All @@ -68,11 +72,11 @@ public function toStringProvider(): \Generator
->method('isArray')
->willReturn(\false);

$option->expects($this->any())
->method('isValueOptional')
->willReturn(\false);
$option->expects($this->once())
->method('isValueRequired')
->willReturn(\true);

yield [
yield $key => [
$expectedValue,
$input,
$option,
Expand All @@ -81,13 +85,13 @@ public function toStringProvider(): \Generator

// InputOption::VALUE_OPTIONAL
foreach ([
['--fooBar=ciao', 'fooBar', 'ciao'],
['--fooBar', 'fooBar', \null],
['--fooBar', 'fooBar', ''],
['--fooBar=0', 'fooBar', '0'],
['--foo\-%&Bar=test', 'foo\-%&Bar', 'test'],
['--ù+ì=omg', 'ù+ì', 'omg'],
] as list($expectedValue, $optionName, $optionValue)) {
'VALUE_OPTIONAL 1' => ['--fooBar=ciao', 'fooBar', 'ciao'],
'VALUE_OPTIONAL 2' => ['--fooBar', 'fooBar', \null],
'VALUE_OPTIONAL 3' => ['--fooBar=', 'fooBar', ''],
'VALUE_OPTIONAL 4' => ['--fooBar=0', 'fooBar', '0'],
'VALUE_OPTIONAL 5' => ['--foo\-%&Bar=test', 'foo\-%&Bar', 'test'],
'VALUE_OPTIONAL 6' => ['--ù+ì=omg', 'ù+ì', 'omg'],
] as $key => list($expectedValue, $optionName, $optionValue)) {
$input = $this->createMock(InputInterface::class);
$input->expects($this->once())
->method('getOption')
Expand All @@ -106,11 +110,11 @@ public function toStringProvider(): \Generator
->method('isArray')
->willReturn(\false);

$option->expects($this->any())
->method('isValueOptional')
->willReturn(\true);
$option->expects($this->once())
->method('isValueRequired')
->willReturn(\false);

yield [
yield $key => [
$expectedValue,
$input,
$option,
Expand All @@ -119,13 +123,14 @@ public function toStringProvider(): \Generator

// InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY
foreach ([
['--fooBar=ciao --fooBar=Привет', 'fooBar', ['ciao', 'Привет']],
['--fooBar=ciao --fooBar=Привет', 'fooBar', ['ciao', \null, 'Привет']],
['', 'fooBar', [\null, '']],
['', 'fooBar', [\null]],
['', 'fooBar', ['']],
['--fooBar=0 --fooBar=1 --fooBar=2 --fooBar=...', 'fooBar', ['0', '1', '2', '...']],
] as list($expectedValue, $optionName, $optionValue)) {
'VALUE_ARRAY_REQUIRED 1' => ['--fooBar=ciao --fooBar=Привет', 'fooBar', ['ciao', 'Привет']],
'VALUE_ARRAY_REQUIRED 2' => ['--fooBar=ciao --fooBar=Привет', 'fooBar', ['ciao', \null, 'Привет']],
kick-the-bucket marked this conversation as resolved.
Show resolved Hide resolved
'VALUE_ARRAY_REQUIRED 3' => ['--fooBar=', 'fooBar', [\null, '']],
'VALUE_ARRAY_REQUIRED 4' => ['', 'fooBar', [\null]],
'VALUE_ARRAY_REQUIRED 5' => ['--fooBar=', 'fooBar', ['']],
'VALUE_ARRAY_REQUIRED 6' => ['--fooBar=0 --fooBar=1 --fooBar=2 --fooBar=...', 'fooBar', ['0', '1', '2', '...']],
'VALUE_ARRAY_REQUIRED 7' => ['--fooBar=ciao --fooBar= --fooBar=Привет', 'fooBar', ['ciao', '', 'Привет']],
] as $key => list($expectedValue, $optionName, $optionValue)) {
$input = $this->createMock(InputInterface::class);
$input->expects($this->once())
->method('getOption')
Expand All @@ -144,11 +149,11 @@ public function toStringProvider(): \Generator
->method('isArray')
->willReturn(\true);

$option->expects($this->any())
->method('isValueOptional')
->willReturn(\false);
$option->expects($this->once())
->method('isValueRequired')
->willReturn(\true);

yield [
yield $key => [
$expectedValue,
$input,
$option,
Expand All @@ -157,13 +162,13 @@ public function toStringProvider(): \Generator

// InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY
foreach ([
['--fooBar=ciao --fooBar=Привет', 'fooBar', ['ciao', 'Привет']],
['--fooBar=ciao --fooBar --fooBar=Привет', 'fooBar', ['ciao', \null, 'Привет']],
['--fooBar --fooBar', 'fooBar', [\null, '']],
['--fooBar', 'fooBar', [\null]],
['--fooBar', 'fooBar', ['']],
['--fooBar=0 --fooBar=1 --fooBar=2 --fooBar=...', 'fooBar', ['0', '1', '2', '...']],
] as list($expectedValue, $optionName, $optionValue)) {
'VALUE_ARRAY_OPTIONAL 1' => ['--fooBar=ciao --fooBar=Привет', 'fooBar', ['ciao', 'Привет']],
'VALUE_ARRAY_OPTIONAL 2' => ['--fooBar=ciao --fooBar --fooBar=Привет', 'fooBar', ['ciao', \null, 'Привет']],
'VALUE_ARRAY_OPTIONAL 3' => ['--fooBar --fooBar=', 'fooBar', [\null, '']],
'VALUE_ARRAY_OPTIONAL 4' => ['--fooBar', 'fooBar', [\null]],
'VALUE_ARRAY_OPTIONAL 5' => ['--fooBar=', 'fooBar', ['']],
'VALUE_ARRAY_OPTIONAL 6' => ['--fooBar=0 --fooBar=1 --fooBar=2 --fooBar=...', 'fooBar', ['0', '1', '2', '...']],
] as $key => list($expectedValue, $optionName, $optionValue)) {
$input = $this->createMock(InputInterface::class);
$input->expects($this->once())
->method('getOption')
Expand All @@ -182,11 +187,11 @@ public function toStringProvider(): \Generator
->method('isArray')
->willReturn(\true);

$option->expects($this->any())
->method('isValueOptional')
->willReturn(\true);
$option->expects($this->once())
->method('isValueRequired')
->willReturn(\false);

yield [
yield $key => [
$expectedValue,
$input,
$option,
Expand Down