From 040e8a1bd755de86d6578e56a3471781d4eb4f7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dan=20Michael=20O=2E=20Hegg=C3=B8?= Date: Wed, 31 Jul 2019 15:38:54 +0200 Subject: [PATCH 1/4] Add failing test for \n newline handling merge into test --- tests/Dotenv/DotenvTest.php | 3 +++ tests/Dotenv/LinesTest.php | 2 ++ tests/fixtures/env/multiline.env | 3 +++ 3 files changed, 8 insertions(+) diff --git a/tests/Dotenv/DotenvTest.php b/tests/Dotenv/DotenvTest.php index 53eeb5d7..63184f4c 100644 --- a/tests/Dotenv/DotenvTest.php +++ b/tests/Dotenv/DotenvTest.php @@ -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')); } diff --git a/tests/Dotenv/LinesTest.php b/tests/Dotenv/LinesTest.php index 12a8e1fa..5a0fb349 100644 --- a/tests/Dotenv/LinesTest.php +++ b/tests/Dotenv/LinesTest.php @@ -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=\'', ]; diff --git a/tests/fixtures/env/multiline.env b/tests/fixtures/env/multiline.env index 4d95ac81..5ea54b09 100644 --- a/tests/fixtures/env/multiline.env +++ b/tests/fixtures/env/multiline.env @@ -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=' From 2dc1cbae069315c00301db23e92dab05a1933979 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dan=20Michael=20O=2E=20Hegg=C3=B8?= Date: Wed, 31 Jul 2019 15:40:01 +0200 Subject: [PATCH 2/4] Fix for \n newline handling Fix parser escaping tests --- src/Parser.php | 2 ++ tests/Dotenv/ParserTest.php | 8 ++++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Parser.php b/src/Parser.php index 62ec5559..c975e49d 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -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, ['n', 'r'], true)) { + return [$data[0].stripcslashes('\\' . $char), self::QUOTED_STATE]; } else { throw new InvalidFileException( self::getErrorMessage('an unexpected escape sequence', $value) diff --git a/tests/Dotenv/ParserTest.php b/tests/Dotenv/ParserTest.php index d3a0ba03..680ff601 100644 --- a/tests/Dotenv/ParserTest.php +++ b/tests/Dotenv/ParserTest.php @@ -62,19 +62,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\''); } } From 8e75dadbb892631f7a5cb47fc6b7c9d5ca8667fc Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Tue, 27 Aug 2019 17:55:39 +0100 Subject: [PATCH 3/4] Support the other usual sequences --- src/Parser.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Parser.php b/src/Parser.php index c975e49d..0d8d46a5 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -134,7 +134,7 @@ 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, ['n', 'r'], true)) { + } elseif (in_array($char, ['f', 'n', 'r', 't', 'v'], true)) { return [$data[0].stripcslashes('\\' . $char), self::QUOTED_STATE]; } else { throw new InvalidFileException( From 92ba1f6e3a37b6af52dd03a7b90560c662af6d50 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Tue, 27 Aug 2019 17:56:08 +0100 Subject: [PATCH 4/4] Added more parser escape sequence tests --- tests/Dotenv/ParserTest.php | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/Dotenv/ParserTest.php b/tests/Dotenv/ParserTest.php index 680ff601..2543b150 100644 --- a/tests/Dotenv/ParserTest.php +++ b/tests/Dotenv/ParserTest.php @@ -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\""));