Skip to content

Commit 35f5213

Browse files
Don't trim values anymore (#302)
I've rebased the original PR, and made a couple of fixes during the rebase so that everything really is not trimmed. --- Closes #273.
1 parent 4f26ad7 commit 35f5213

File tree

3 files changed

+13
-31
lines changed

3 files changed

+13
-31
lines changed

src/Loader.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -294,12 +294,7 @@ protected function splitCompoundStringIntoParts($name, $value)
294294
*/
295295
protected function sanitiseVariableValue($name, $value)
296296
{
297-
if ($value === null) {
298-
return [$name, $value];
299-
}
300-
301-
$value = trim($value);
302-
if ($value === '') {
297+
if ($value === null || trim($value) === '') {
303298
return [$name, $value];
304299
}
305300

@@ -325,7 +320,7 @@ protected function sanitiseVariableValue($name, $value)
325320
$value = str_replace('\\\\', '\\', $value);
326321
} else {
327322
$parts = explode(' #', $value, 2);
328-
$value = trim($parts[0]);
323+
$value = $parts[0];
329324

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

341-
return [$name, trim($value)];
336+
return [$name, $value];
342337
}
343338

344339
/**

tests/Dotenv/DotenvTest.php

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -260,12 +260,12 @@ public function testDotenvAssertions()
260260
$dotenv->load();
261261
$this->assertSame('val1', getenv('ASSERTVAR1'));
262262
$this->assertEmpty(getenv('ASSERTVAR2'));
263-
$this->assertEmpty(getenv('ASSERTVAR3'));
263+
$this->assertSame('val3 ', getenv('ASSERTVAR3'));
264264
$this->assertSame('0', getenv('ASSERTVAR4'));
265265
$this->assertSame('#foo', getenv('ASSERTVAR5'));
266266
$this->assertSame("val1\nval2", getenv('ASSERTVAR6'));
267-
$this->assertSame("val3", getenv('ASSERTVAR7'));
268-
$this->assertSame("val3", getenv('ASSERTVAR8'));
267+
$this->assertSame("\nval3", getenv('ASSERTVAR7'));
268+
$this->assertSame("val3\n", getenv('ASSERTVAR8'));
269269

270270
$dotenv->required([
271271
'ASSERTVAR1',
@@ -281,6 +281,7 @@ public function testDotenvAssertions()
281281

282282
$dotenv->required([
283283
'ASSERTVAR1',
284+
'ASSERTVAR3',
284285
'ASSERTVAR4',
285286
'ASSERTVAR5',
286287
'ASSERTVAR6',
@@ -292,9 +293,7 @@ public function testDotenvAssertions()
292293
'ASSERTVAR1',
293294
'ASSERTVAR4',
294295
'ASSERTVAR5',
295-
'ASSERTVAR7',
296-
'ASSERTVAR8',
297-
])->notEmpty()->allowedValues(['0', 'val1', 'val3', '#foo']);
296+
])->notEmpty()->allowedValues(['0', 'val1', '#foo']);
298297

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

315314
/**
316315
* @expectedException \Dotenv\Exception\ValidationException
317-
* @expectedExceptionMessage One or more environment variables failed assertions: ASSERTVAR3 is empty.
316+
* @expectedExceptionMessage One or more environment variables failed assertions: ASSERTVAR9 is empty.
318317
*/
319318
public function testDotenvStringOfSpacesConsideredEmpty()
320319
{
321320
$dotenv = Dotenv::create($this->fixturesFolder, 'assertions.env');
322321
$dotenv->load();
323-
$this->assertEmpty(getenv('ASSERTVAR3'));
324-
325-
$dotenv->required('ASSERTVAR3')->notEmpty();
326-
}
327-
328-
/**
329-
* @expectedException \Dotenv\Exception\ValidationException
330-
* @expectedExceptionMessage One or more environment variables failed assertions: ASSERTVAR3 is empty.
331-
*/
332-
public function testDotenvHitsLastChain()
333-
{
334-
$dotenv = Dotenv::create($this->fixturesFolder, 'assertions.env');
335-
$dotenv->load();
336-
$dotenv->required('ASSERTVAR3')->notEmpty();
322+
$dotenv->required('ASSERTVAR9')->notEmpty();
337323
}
338324

339325
/**

tests/fixtures/env/assertions.env

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
ASSERTVAR1="val1"
1+
ASSERTVAR1=val1
2+
23
ASSERTVAR2=""
3-
ASSERTVAR3=" "
4+
ASSERTVAR3="val3 "
45
ASSERTVAR4="0" # empty looking value
56
ASSERTVAR5=#foo
67
ASSERTVAR6="val1

0 commit comments

Comments
 (0)