Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions src/EnvironmentSetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 13 additions & 0 deletions tests/Unit/EnvironmentSetCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading