Skip to content

Commit

Permalink
Don't trim values anymore (#302)
Browse files Browse the repository at this point in the history
I've rebased the original PR, and made a couple of fixes during the rebase so that everything really is not trimmed.

---

Closes #273.
  • Loading branch information
GrahamCampbell authored Jan 2, 2019
1 parent 4f26ad7 commit 35f5213
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 31 deletions.
11 changes: 3 additions & 8 deletions src/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,12 +294,7 @@ protected function splitCompoundStringIntoParts($name, $value)
*/
protected function sanitiseVariableValue($name, $value)
{
if ($value === null) {
return [$name, $value];
}

$value = trim($value);
if ($value === '') {
if ($value === null || trim($value) === '') {
return [$name, $value];
}

Expand All @@ -325,7 +320,7 @@ protected function sanitiseVariableValue($name, $value)
$value = str_replace('\\\\', '\\', $value);
} else {
$parts = explode(' #', $value, 2);
$value = trim($parts[0]);
$value = $parts[0];

// Unquoted values cannot contain whitespace
if (preg_match('/\s+/', $value) > 0) {
Expand All @@ -338,7 +333,7 @@ protected function sanitiseVariableValue($name, $value)
}
}

return [$name, trim($value)];
return [$name, $value];
}

/**
Expand Down
28 changes: 7 additions & 21 deletions tests/Dotenv/DotenvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,12 +260,12 @@ public function testDotenvAssertions()
$dotenv->load();
$this->assertSame('val1', getenv('ASSERTVAR1'));
$this->assertEmpty(getenv('ASSERTVAR2'));
$this->assertEmpty(getenv('ASSERTVAR3'));
$this->assertSame('val3 ', getenv('ASSERTVAR3'));
$this->assertSame('0', getenv('ASSERTVAR4'));
$this->assertSame('#foo', getenv('ASSERTVAR5'));
$this->assertSame("val1\nval2", getenv('ASSERTVAR6'));
$this->assertSame("val3", getenv('ASSERTVAR7'));
$this->assertSame("val3", getenv('ASSERTVAR8'));
$this->assertSame("\nval3", getenv('ASSERTVAR7'));
$this->assertSame("val3\n", getenv('ASSERTVAR8'));

$dotenv->required([
'ASSERTVAR1',
Expand All @@ -281,6 +281,7 @@ public function testDotenvAssertions()

$dotenv->required([
'ASSERTVAR1',
'ASSERTVAR3',
'ASSERTVAR4',
'ASSERTVAR5',
'ASSERTVAR6',
Expand All @@ -292,9 +293,7 @@ public function testDotenvAssertions()
'ASSERTVAR1',
'ASSERTVAR4',
'ASSERTVAR5',
'ASSERTVAR7',
'ASSERTVAR8',
])->notEmpty()->allowedValues(['0', 'val1', 'val3', '#foo']);
])->notEmpty()->allowedValues(['0', 'val1', '#foo']);

$this->assertTrue(true); // anything wrong an an exception will be thrown
}
Expand All @@ -314,26 +313,13 @@ public function testDotenvEmptyThrowsRuntimeException()

/**
* @expectedException \Dotenv\Exception\ValidationException
* @expectedExceptionMessage One or more environment variables failed assertions: ASSERTVAR3 is empty.
* @expectedExceptionMessage One or more environment variables failed assertions: ASSERTVAR9 is empty.
*/
public function testDotenvStringOfSpacesConsideredEmpty()
{
$dotenv = Dotenv::create($this->fixturesFolder, 'assertions.env');
$dotenv->load();
$this->assertEmpty(getenv('ASSERTVAR3'));

$dotenv->required('ASSERTVAR3')->notEmpty();
}

/**
* @expectedException \Dotenv\Exception\ValidationException
* @expectedExceptionMessage One or more environment variables failed assertions: ASSERTVAR3 is empty.
*/
public function testDotenvHitsLastChain()
{
$dotenv = Dotenv::create($this->fixturesFolder, 'assertions.env');
$dotenv->load();
$dotenv->required('ASSERTVAR3')->notEmpty();
$dotenv->required('ASSERTVAR9')->notEmpty();
}

/**
Expand Down
5 changes: 3 additions & 2 deletions tests/fixtures/env/assertions.env
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
ASSERTVAR1="val1"
ASSERTVAR1=val1

ASSERTVAR2=""
ASSERTVAR3=" "
ASSERTVAR3="val3 "
ASSERTVAR4="0" # empty looking value
ASSERTVAR5=#foo
ASSERTVAR6="val1
Expand Down

0 comments on commit 35f5213

Please sign in to comment.