Skip to content

Add safer regex that takes into account more possible whitespace #36

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

Merged
merged 1 commit into from
Mar 9, 2025
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
2 changes: 1 addition & 1 deletion src/EnvironmentSetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function setEnvVariable(string $envFileContent, string $key, string $valu
public function readKeyValuePair(string $envFileContent, string $key): ?string
{
// Match the given key at the beginning of a line
if (preg_match("#^ *{$key} *= *[^\r\n]*$#uimU", $envFileContent, $matches)) {
if (preg_match("#^\s*{$key}\s*=\s*[^\r\n]*$#uimU", $envFileContent, $matches)) {
return $matches[0];
}

Expand Down
27 changes: 14 additions & 13 deletions tests/Unit/EnvironmentSetCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ public function setUp(): void
/**
* @covers EnvironmentSetCommand::setEnvVariable
* @dataProvider setEnvVariableDataProvider
*
* @param string $originalEnvFileContent
* @param string $key
* @param string $value
* @param string $expectedNewEnvFile
*/
public function testSetEnvVariable(
string $originalEnvFileContent,
Expand Down Expand Up @@ -62,15 +57,24 @@ public function testSetEnvVariableTestOfNestedKeys(): void
$this->assertEquals($expectedEnv, $newEnv);
}

/**
* @covers EnvironmentSetCommand::setEnvVariable
*/
public function testWhitespaceAsValueDoesntCreateNewEntry(): void
{
$env = 'APP_KEY = \t' . "\n";

$expectedEnv = 'APP_KEY=test' . "\n";

[$newEnv, $_] = $this->command->setEnvVariable($env, 'APP_KEY', 'test');
$this->assertEquals($expectedEnv, $newEnv);
}

/**
* @covers EnvironmentSetCommand::readKeyValuePair
* @dataProvider readKeyValuePairDataProvider
*
* @param string $envFileContent
* @param string $key
* @param string|null $expectedKeyValuePair
*/
public function testReadKeyValuePair(string $envFileContent, string $key, ?string $expectedKeyValuePair): void
public function testReadKeyValuePair(string $envFileContent, string $key, ?string $expectedKeyValuePair = null): void
{
$realPair = $this->command->readKeyValuePair($envFileContent, $key);
$this->assertEquals($expectedKeyValuePair, $realPair);
Expand All @@ -92,9 +96,6 @@ public function testParseCommandArguments(array $params, array $expectedResult):
/**
* @covers EnvironmentSetCommand::assertKeyIsValid
* @dataProvider assertKeyIsValidDataProvider
*
* @param string $key
* @param bool $isGood
*/
public function testAssertKeyIsValid(string $key, bool $isGood): void
{
Expand Down