Skip to content

Commit

Permalink
Only wrap necessary values in quotes, and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Liam Hammett committed Jul 18, 2020
1 parent 6ea4ffd commit 5d6820a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/EnvironmentSetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,13 @@ public function handle(): void
public function setEnvVariable(string $envFileContent, string $key, string $value): array
{
$oldPair = $this->readKeyValuePair($envFileContent, $key);
$newPair = $key . '="' . $value . '"';

// Wrap values that have a space or equals in quotes to escape them
if (preg_match('/\s/',$value) || strpos($value, '=') !== false) {
$value = '"' . $value . '"';
}

$newPair = $key . '=' . $value;

// For existed key.
if ($oldPair !== null) {
Expand Down
12 changes: 10 additions & 2 deletions tests/Unit/EnvironmentSetCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,14 @@ public function setEnvVariableDataProvider(): array
'some_key',
'=========',
str_replace('some_key=some_value',
'some_key==========', $envFileContent),
'some_key="========="', $envFileContent),
],
[
&$envFileContent,
'value_with_spaces',
'this is a value',
str_replace('value_with_spaces=',
'value_with_spaces="this is a value"', $envFileContent),
],
];
}
Expand Down Expand Up @@ -353,6 +360,7 @@ protected function getTestEnvFile(): string
. ' spaces_in_the_quotes = " " ' . "\n"
. 'a_lot_of_equals_signs_one=======' . "\n"
. 'a_lot_of_equals_signs_two = ====== ' . "\n"
. 'a_lot_of_equals_signs_three = "======" ' . "\n";
. 'a_lot_of_equals_signs_three = "======" ' . "\n"
. 'value_with_spaces=' . "\n";
}
}

0 comments on commit 5d6820a

Please sign in to comment.