From 5901e82e8a54c1792f6a79f6a89383270fd5d622 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franz=20Guzm=C3=A1n?= Date: Mon, 10 Jun 2024 21:45:35 -0500 Subject: [PATCH 1/3] test: Add tests for correctly escaping new lines characters `\n` --- spec/fixtures/.dotenv | 5 +++++ src/Configuration/Dotenv/Parse.hs | 5 +++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/spec/fixtures/.dotenv b/spec/fixtures/.dotenv index 1aeaf37d..b04e046c 100644 --- a/spec/fixtures/.dotenv +++ b/spec/fixtures/.dotenv @@ -4,3 +4,8 @@ ENVIRONMENT="$HOME" PREVIOUS="$DOTENV" ME="$(whoami)" BLANK= +NEWLINE_TEST="Hello\nWorld" +MULTILINE_TEST="Roses are red +Violets are blue +Code is my art +And bugs are my glue" diff --git a/src/Configuration/Dotenv/Parse.hs b/src/Configuration/Dotenv/Parse.hs index f1b2c46d..5a975a5a 100644 --- a/src/Configuration/Dotenv/Parse.hs +++ b/src/Configuration/Dotenv/Parse.hs @@ -25,7 +25,7 @@ import Text.Megaparsec (Parsec, anySingle, between, eof, noneOf, oneOf, sepEndBy, ()) import Text.Megaparsec.Char (char, digitChar, eol, - letterChar, spaceChar) + letterChar, spaceChar, string) import qualified Text.Megaparsec.Char.Lexer as L type Parser = Parsec Void String @@ -85,8 +85,9 @@ interpolatedValueCommandInterpolation = do symbol = L.symbol sc literalValueFragment :: String -> Parser VarFragment -literalValueFragment charsToEscape = VarLiteral <$> some (escapedChar <|> normalChar) +literalValueFragment charsToEscape = VarLiteral <$> some (newlineChar <|> escapedChar <|> normalChar) where + newlineChar = string "\\n" >> return '\n' escapedChar = (char '\\' *> anySingle) "escaped character" normalChar = noneOf charsToEscape "unescaped character" From c1ea77b390d36544878524fcac409beebcaedc28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franz=20Guzm=C3=A1n?= Date: Mon, 10 Jun 2024 21:45:46 -0500 Subject: [PATCH 2/3] fix: Correctly parse newline and multiline strings in dotenv files --- spec/Configuration/Dotenv/TextSpec.hs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/spec/Configuration/Dotenv/TextSpec.hs b/spec/Configuration/Dotenv/TextSpec.hs index 373b2709..b2e886ce 100644 --- a/spec/Configuration/Dotenv/TextSpec.hs +++ b/spec/Configuration/Dotenv/TextSpec.hs @@ -27,3 +27,11 @@ spec = it "recognizes unicode characters" $ liftM (!! 1) (parseFile "spec/fixtures/.dotenv") `shouldReturn` (T.pack "UNICODE_TEST", T.pack "ManabĂ­") + + it "handles newline characters correctly" $ do + liftM (!! 6) (parseFile "spec/fixtures/.dotenv") `shouldReturn` + (T.pack "NEWLINE_TEST", T.pack "Hello\nWorld") + + it "handles manual line breaks correctly" $ do + liftM (!! 7) (parseFile "spec/fixtures/.dotenv") `shouldReturn` + (T.pack "MULTILINE_TEST", T.pack "Roses are red\nViolets are blue\nCode is my art\nAnd bugs are my glue") From 5dff2e975da4a14e2c8f95c02c6186f499450716 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franz=20Guzm=C3=A1n?= Date: Mon, 10 Jun 2024 22:11:21 -0500 Subject: [PATCH 3/3] test: Enhance test cases for more robust `\n` --- spec/Configuration/Dotenv/TextSpec.hs | 4 ++-- spec/fixtures/.dotenv | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/spec/Configuration/Dotenv/TextSpec.hs b/spec/Configuration/Dotenv/TextSpec.hs index b2e886ce..ac503348 100644 --- a/spec/Configuration/Dotenv/TextSpec.hs +++ b/spec/Configuration/Dotenv/TextSpec.hs @@ -30,8 +30,8 @@ spec = it "handles newline characters correctly" $ do liftM (!! 6) (parseFile "spec/fixtures/.dotenv") `shouldReturn` - (T.pack "NEWLINE_TEST", T.pack "Hello\nWorld") + (T.pack "NEWLINE_TEST", T.pack "Hello\nWorld\nThis is a test\n") it "handles manual line breaks correctly" $ do liftM (!! 7) (parseFile "spec/fixtures/.dotenv") `shouldReturn` - (T.pack "MULTILINE_TEST", T.pack "Roses are red\nViolets are blue\nCode is my art\nAnd bugs are my glue") + (T.pack "MULTILINE_TEST", T.pack "Roses are red\nViolets are blue\nCode is my art\nAnd bugs are my glue\n") diff --git a/spec/fixtures/.dotenv b/spec/fixtures/.dotenv index b04e046c..eeeb3d6c 100644 --- a/spec/fixtures/.dotenv +++ b/spec/fixtures/.dotenv @@ -4,8 +4,9 @@ ENVIRONMENT="$HOME" PREVIOUS="$DOTENV" ME="$(whoami)" BLANK= -NEWLINE_TEST="Hello\nWorld" +NEWLINE_TEST="Hello\nWorld\nThis is a test\n" MULTILINE_TEST="Roses are red Violets are blue Code is my art -And bugs are my glue" +And bugs are my glue +"