diff --git a/src/Loader.php b/src/Loader.php index 327302db..cb3508fa 100644 --- a/src/Loader.php +++ b/src/Loader.php @@ -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.'); + } } } diff --git a/tests/Dotenv/DotenvTest.php b/tests/Dotenv/DotenvTest.php index 2e972945..b44b4983 100644 --- a/tests/Dotenv/DotenvTest.php +++ b/tests/Dotenv/DotenvTest.php @@ -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() @@ -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 } diff --git a/tests/fixtures/env/assertions.env b/tests/fixtures/env/assertions.env index 2a4f4b01..6361d178 100644 --- a/tests/fixtures/env/assertions.env +++ b/tests/fixtures/env/assertions.env @@ -2,3 +2,4 @@ ASSERTVAR1="val1" ASSERTVAR2="" ASSERTVAR3=" " ASSERTVAR4="0" # empty looking value +ASSERTVAR5=#foo \ No newline at end of file