diff --git a/src/EnvironmentSetCommand.php b/src/EnvironmentSetCommand.php index ab89487..72b96af 100644 --- a/src/EnvironmentSetCommand.php +++ b/src/EnvironmentSetCommand.php @@ -80,8 +80,9 @@ public function setEnvVariable(string $envFileContent, string $key, string $valu $oldPair = $this->readKeyValuePair($envFileContent, $key); // Wrap values that have a space or equals in quotes to escape them - if (preg_match('/\s/',$value) || strpos($value, '=') !== false) { - $value = '"' . $value . '"'; + $trimmedValue = trim($value); + if (!str_starts_with($trimmedValue, '"') && !str_ends_with($trimmedValue, '"') && preg_match('/\s/',$value) || strpos($value, '=') !== false) { + $value = '"' . str_replace('"', '\"', $value) . '"'; } $newPair = $key . '=' . $value; diff --git a/tests/Unit/EnvironmentSetCommandTest.php b/tests/Unit/EnvironmentSetCommandTest.php index d0d3c28..ae95a36 100644 --- a/tests/Unit/EnvironmentSetCommandTest.php +++ b/tests/Unit/EnvironmentSetCommandTest.php @@ -70,6 +70,19 @@ public function testWhitespaceAsValueDoesntCreateNewEntry(): void $this->assertEquals($expectedEnv, $newEnv); } + /** + * @covers EnvironmentSetCommand::setEnvVariable + */ + public function testQuotedCharactersArePreserved(): void + { + $env = 'APP_NAME=' . "\n"; + + $expectedEnv = 'APP_NAME="MY.NAME & C."' . "\n"; + + [$newEnv, $_] = $this->command->setEnvVariable($env, 'APP_NAME', 'MY.NAME & C.'); + $this->assertEquals($expectedEnv, $newEnv); + } + /** * @covers EnvironmentSetCommand::readKeyValuePair * @dataProvider readKeyValuePairDataProvider