Skip to content

Commit

Permalink
fix(dotenv): handle multiline variables on Windows (#6216)
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorM867 authored Nov 29, 2024
1 parent 09e426b commit 9f294ee
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
2 changes: 1 addition & 1 deletion dotenv/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type LineParseResult = {
type CharactersMap = { [key: string]: string };

const RE_KEY_VALUE =
/^\s*(?:export\s+)?(?<key>[^\s=#]+?)\s*=[\ \t]*('\n?(?<notInterpolated>(.|\n)*?)\n?'|"\n?(?<interpolated>(.|\n)*?)\n?"|(?<unquoted>[^\n#]*)) *#*.*$/gm;
/^\s*(?:export\s+)?(?<key>[^\s=#]+?)\s*=[\ \t]*('\r?\n?(?<notInterpolated>(.|\r\n|\n)*?)\r?\n?'|"\r?\n?(?<interpolated>(.|\r\n|\n)*?)\r?\n?"|(?<unquoted>[^\r\n#]*)) *#*.*$/gm;

const RE_VALID_KEY = /^[a-zA-Z_][a-zA-Z0-9_]*$/;

Expand Down
16 changes: 13 additions & 3 deletions dotenv/parse_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Deno.test("parse()", () => {
);

const load = parse(testDotenv);
assertEquals(Object.keys(load).length, 24, "parses 24 keys");
assertEquals(Object.keys(load).length, 26, "parses 26 keys");
assertEquals(load.BASIC, "basic", "parses a basic variable");
assertEquals(load.AFTER_EMPTY, "empty", "skips empty lines");
assertEquals(load["#COMMENT"], undefined, "skips lines with comments");
Expand Down Expand Up @@ -47,10 +47,15 @@ Deno.test("parse()", () => {
);

assertEquals(
load.MULTILINE,
load.MULTILINE1,
"hello\nworld",
"new lines are expanded in double quotes",
);
assertEquals(
load.MULTILINE2,
"hello\r\nworld",
"new lines are expanded in double quotes",
);

assertEquals(
JSON.parse(load.JSON || JSON.stringify({})).foo,
Expand All @@ -71,10 +76,15 @@ Deno.test("parse()", () => {
);

assertEquals(
load.MULTILINE_SINGLE_QUOTE,
load.MULTILINE_SINGLE_QUOTE1,
"hello\\nworld",
"new lines are escaped in single quotes",
);
assertEquals(
load.MULTILINE_SINGLE_QUOTE2,
"hello\\r\\nworld",
"new lines are escaped in single quotes",
);

assertEquals(load.EQUALS, "equ==als", "handles equals inside string");

Expand Down
6 changes: 4 additions & 2 deletions dotenv/testdata/.env.test
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ QUOTED_DOUBLE="double quoted"
EMPTY_SINGLE=''
EMPTY_DOUBLE=""

MULTILINE="hello\nworld"
MULTILINE1="hello\nworld"
MULTILINE2="hello\r\nworld"
JSON='{"foo": "bar"}'
WHITESPACE=' whitespace '
WHITESPACE_DOUBLE=" whitespace "
MULTILINE_SINGLE_QUOTE='hello\nworld'
MULTILINE_SINGLE_QUOTE1='hello\nworld'
MULTILINE_SINGLE_QUOTE2='hello\r\nworld'
EQUALS='equ==als'
THE_ANSWER=42

Expand Down

0 comments on commit 9f294ee

Please sign in to comment.