Skip to content
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

[3.5] Handle additional quoted escape sequences such as "\n" #368

Merged
merged 4 commits into from
Aug 27, 2019
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: 2 additions & 0 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ private static function parseValue($value)
case self::ESCAPE_STATE:
if ($char === $value[0] || $char === '\\') {
return [$data[0].$char, self::QUOTED_STATE];
} elseif (in_array($char, ['f', 'n', 'r', 't', 'v'], true)) {
return [$data[0].stripcslashes('\\' . $char), self::QUOTED_STATE];
} else {
throw new InvalidFileException(
self::getErrorMessage('an unexpected escape sequence', $value)
Expand Down
3 changes: 3 additions & 0 deletions tests/Dotenv/DotenvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,9 @@ public function testMutlilineLoading()
$dotenv = Dotenv::create($this->fixturesFolder, 'multiline.env');
$dotenv->load();
$this->assertSame("test\n test\"test\"\n test", getenv('TEST'));
$this->assertSame("test\ntest", getenv('TEST_ND'));
$this->assertSame("test\ntest", getenv('TEST_NS'));

$this->assertSame('https://vision.googleapis.com/v1/images:annotate?key=', getenv('TEST_EQD'));
$this->assertSame('https://vision.googleapis.com/v1/images:annotate?key=', getenv('TEST_EQS'));
}
Expand Down
2 changes: 2 additions & 0 deletions tests/Dotenv/LinesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public function testProcessQuotes()

$expected = [
"TEST=\"test\n test\\\"test\\\"\n test\"",
"TEST_ND=\"test\\ntest\"",
'TEST_NS=\'test\\ntest\'',
'TEST_EQD="https://vision.googleapis.com/v1/images:annotate?key="',
'TEST_EQS=\'https://vision.googleapis.com/v1/images:annotate?key=\'',
];
Expand Down
37 changes: 33 additions & 4 deletions tests/Dotenv/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,35 @@ public function testQuotesParse()
$this->assertSame(['FOO', "BAR \n"], Parser::parse("FOO=\"BAR \n\""));
}

public function testNewlineParse()
{
$this->assertSame(['FOO', "\n"], Parser::parse('FOO="\n"'));
}

public function testTabParse()
{
$this->assertSame(['FOO', "\t"], Parser::parse('FOO=\'\t\''));
}

public function testNonEscapeParse1()
{
$this->assertSame(['FOO', '\n\v'], Parser::parse('FOO=\n\v'));
}

public function testNonEscapeParse2()
{
$this->assertSame(['FOO', '\q'], Parser::parse('FOO=\q'));
}

/**
* @expectedException \Dotenv\Exception\InvalidFileException
* @expectedExceptionMessage Failed to parse dotenv file due to an unexpected escape sequence. Failed at ["\q"].
*/
public function testBadEscapeParse()
{
Parser::parse('FOO="\q"');
}

public function testWhitespaceParse()
{
$this->assertSame(['FOO', "\n"], Parser::parse("FOO=\"\n\""));
Expand Down Expand Up @@ -62,19 +91,19 @@ public function testParseInvalidName()

/**
* @expectedException \Dotenv\Exception\InvalidFileException
* @expectedExceptionMessage Failed to parse dotenv file due to an unexpected escape sequence. Failed at ["iiiiviiiixiiiiviiii\n"].
* @expectedExceptionMessage Failed to parse dotenv file due to an unexpected escape sequence. Failed at ["iiiiviiiixiiiiviiii\a"].
*/
public function testParserEscapingDouble()
{
Parser::parse('FOO_BAD="iiiiviiiixiiiiviiii\\n"');
Parser::parse('FOO_BAD="iiiiviiiixiiiiviiii\\a"');
}

/**
* @expectedException \Dotenv\Exception\InvalidFileException
* @expectedExceptionMessage Failed to parse dotenv file due to an unexpected escape sequence. Failed at ['iiiiviiiixiiiiviiii\n'].
* @expectedExceptionMessage Failed to parse dotenv file due to an unexpected escape sequence. Failed at ['iiiiviiiixiiiiviiii\a'].
*/
public function testParserEscapingSingle()
{
Parser::parse('FOO_BAD=\'iiiiviiiixiiiiviiii\\n\'');
Parser::parse('FOO_BAD=\'iiiiviiiixiiiiviiii\\a\'');
}
}
3 changes: 3 additions & 0 deletions tests/fixtures/env/multiline.env
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@ TEST="test
test\"test\"
test"

TEST_ND="test\ntest"
TEST_NS='test\ntest'

TEST_EQD="https://vision.googleapis.com/v1/images:annotate?key="
TEST_EQS='https://vision.googleapis.com/v1/images:annotate?key='