Skip to content

Commit

Permalink
Merge pull request #277 from Korbeil/276-variable-values-starting-wit…
Browse files Browse the repository at this point in the history
…h-are-being-converted-into-empty-strings

276 variable values starting with are being converted into empty strings
  • Loading branch information
GrahamCampbell authored Jul 29, 2018
2 parents 6ae3e2e + 5b31537 commit 8abb4f9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
12 changes: 6 additions & 6 deletions src/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,14 +253,14 @@ protected function sanitiseVariableValue($name, $value)
$parts = explode(' #', $value, 2);
$value = trim($parts[0]);

// Check if value is a comment (usually triggered when empty value with comment)
if (preg_match('/^#/', $value) > 0) {
$value = '';
}

// Unquoted values cannot contain whitespace
if (preg_match('/\s+/', $value) > 0) {
throw new InvalidFileException('Dotenv values containing spaces must be surrounded by quotes.');
// Check if value is a comment (usually triggered when empty value with comment)
if (preg_match('/^#/', $value) > 0) {
$value = '';
} else {
throw new InvalidFileException('Dotenv values containing spaces must be surrounded by quotes.');
}
}
}

Expand Down
7 changes: 6 additions & 1 deletion tests/Dotenv/DotenvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public function testCommentedDotenvLoadsEnvironmentVars()
$this->assertSame('a value with a # character', getenv('CQUOTES'));
$this->assertSame('a value with a # character & a quote " character inside quotes', getenv('CQUOTESWITHQUOTE'));
$this->assertEmpty(getenv('CNULL'));
$this->assertEmpty(getenv('EMPTY'));
}

public function testQuotedDotenvLoadsEnvironmentVars()
Expand Down Expand Up @@ -261,23 +262,27 @@ public function testDotenvAssertions()
$this->assertEmpty(getenv('ASSERTVAR2'));
$this->assertEmpty(getenv('ASSERTVAR3'));
$this->assertSame('0', getenv('ASSERTVAR4'));
$this->assertSame('#foo', getenv('ASSERTVAR5'));

$dotenv->required(array(
'ASSERTVAR1',
'ASSERTVAR2',
'ASSERTVAR3',
'ASSERTVAR4',
'ASSERTVAR5',
));

$dotenv->required(array(
'ASSERTVAR1',
'ASSERTVAR4',
'ASSERTVAR5',
))->notEmpty();

$dotenv->required(array(
'ASSERTVAR1',
'ASSERTVAR4',
))->notEmpty()->allowedValues(array('0', 'val1'));
'ASSERTVAR5',
))->notEmpty()->allowedValues(array('0', 'val1', '#foo'));

$this->assertTrue(true); // anything wrong an an exception will be thrown
}
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/env/assertions.env
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ ASSERTVAR1="val1"
ASSERTVAR2=""
ASSERTVAR3=" "
ASSERTVAR4="0" # empty looking value
ASSERTVAR5=#foo

0 comments on commit 8abb4f9

Please sign in to comment.