From d28c7b444e04cd54d0e40cf3e5358895973dbbe7 Mon Sep 17 00:00:00 2001 From: Yassen Bantchev Date: Tue, 11 Oct 2022 14:13:35 +0300 Subject: [PATCH] Support trailing comments The original Ruby dotenv supports trailing comments as it is valid shell syntax. In general it would be good to conform to the regular expression as defined here: https://github.com/bkeepers/dotenv/blob/master/lib/dotenv/parser.rb#L14-L30 --- .../java/io/github/cdimascio/dotenv/internal/DotenvParser.java | 3 ++- src/test/java/tests/BasicTests.java | 1 + src/test/resources/.env | 1 + src/test/resources/env | 1 + 4 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/github/cdimascio/dotenv/internal/DotenvParser.java b/src/main/java/io/github/cdimascio/dotenv/internal/DotenvParser.java index 6288cbd..8c19592 100644 --- a/src/main/java/io/github/cdimascio/dotenv/internal/DotenvParser.java +++ b/src/main/java/io/github/cdimascio/dotenv/internal/DotenvParser.java @@ -23,7 +23,8 @@ public class DotenvParser { private final Function isWhiteSpace = s -> matches("^\\s*$", s); // ^\s*${'$'} private final Function isComment = s -> s.startsWith("#") || s.startsWith("////"); private final Function isQuoted = s -> s.startsWith("\"") && s.endsWith("\""); - private final Function parseLine = s -> matchEntry("^\\s*([\\w.\\-]+)\\s*(=)\\s*(.*)?\\s*$", s); // ^\s*([\w.\-]+)\s*(=)\s*(.*)?\s*$ + private final Function parseLine = + s -> matchEntry("^\\s*([\\w.\\-]+)\\s*(=)\\s*([^#]*)?\\s*(#.*)?$", s); /** * Creates a dotenv parser diff --git a/src/test/java/tests/BasicTests.java b/src/test/java/tests/BasicTests.java index b4850ac..c15cfd7 100644 --- a/src/test/java/tests/BasicTests.java +++ b/src/test/java/tests/BasicTests.java @@ -15,6 +15,7 @@ public class BasicTests { put("MY_TEST_EV2", "my test ev 2"); put("WITHOUT_VALUE", ""); put("MULTI_LINE", "hello\\nworld"); + put("TRAILING_COMMENT", "value"); }}; @Test(expected = DotenvException.class) diff --git a/src/test/resources/.env b/src/test/resources/.env index f2f18a6..2c940e0 100644 --- a/src/test/resources/.env +++ b/src/test/resources/.env @@ -3,6 +3,7 @@ MY_TEST_EV1=my test ev 1 MY_TEST_EV2=my test ev 2 WITHOUT_VALUE= MULTI_LINE=hello\nworld +TRAILING_COMMENT=value # comment ## Malformed EV! MY_TEST_EV3 diff --git a/src/test/resources/env b/src/test/resources/env index 8894abb..8aaf95f 100644 --- a/src/test/resources/env +++ b/src/test/resources/env @@ -3,6 +3,7 @@ MY_TEST_EV1=my test ev 1 MY_TEST_EV2=my test ev 2 WITHOUT_VALUE= MULTI_LINE=hello\nworld +TRAILING_COMMENT=value # comment ## Malformed EV! MY_TEST_EV3