diff --git a/src/EnvironmentSetCommand.php b/src/EnvironmentSetCommand.php index e266d19..4a04fca 100644 --- a/src/EnvironmentSetCommand.php +++ b/src/EnvironmentSetCommand.php @@ -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) { diff --git a/tests/Unit/EnvironmentSetCommandTest.php b/tests/Unit/EnvironmentSetCommandTest.php index 7c2c14b..864d5ef 100644 --- a/tests/Unit/EnvironmentSetCommandTest.php +++ b/tests/Unit/EnvironmentSetCommandTest.php @@ -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), ], ]; } @@ -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"; } }