Skip to content

Commit

Permalink
Support trailing comments (#38)
Browse files Browse the repository at this point in the history
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

Co-authored-by: Carmine DiMascio <cdimasci@amazon.com>
  • Loading branch information
yassenb and cdimascio authored Nov 23, 2022
1 parent e52b498 commit 45f25e2
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
public class DotenvParser {

private static final Pattern WHITE_SPACE_REGEX = Pattern.compile("^\\s*$"); // ^\s*${'$'}
private static final Pattern DOTENV_ENTRY_REGEX = Pattern.compile("^\\s*([\\w.\\-]+)\\s*(=)\\s*(.*)?\\s*$"); // ^\s*([\w.\-]+)\s*(=)\s*(.*)?\s*$
private static final Pattern DOTENV_ENTRY_REGEX = Pattern.compile("^\\s*([\\w.\\-]+)\\s*(=)\\s*([^#]*)?\\s*(#.*)?$"); // ^\s*([\w.\-]+)\s*(=)\s*([^#]*)?\s*(#.*)?$

private final DotenvReader reader;
private final boolean throwIfMissing;
Expand Down
1 change: 1 addition & 0 deletions src/test/java/tests/BasicTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/.env
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/env
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 45f25e2

Please sign in to comment.