-
-
Notifications
You must be signed in to change notification settings - Fork 861
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add option to allow line breaks in values
This adds an option `{ multiline: "line-breaks" }` that allow values to contain new lines. Closes #458
- Loading branch information
1 parent
7301ac9
commit 0a9332f
Showing
11 changed files
with
241 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
BASIC=basic | ||
|
||
# previous line intentionally left blank | ||
AFTER_LINE=after_line | ||
EMPTY= | ||
SINGLE_QUOTES='single_quotes' | ||
SINGLE_QUOTES_SPACED=' single quotes ' | ||
DOUBLE_QUOTES="double_quotes" | ||
DOUBLE_QUOTES_SPACED=" double quotes " | ||
EXPAND_NEWLINES="expand\nnew\nlines" | ||
DONT_EXPAND_UNQUOTED=dontexpand\nnewlines | ||
DONT_EXPAND_SQUOTED='dontexpand\nnewlines' | ||
# COMMENTS=work | ||
EQUAL_SIGNS=equals== | ||
RETAIN_INNER_QUOTES={"foo": "bar"} | ||
|
||
RETAIN_INNER_QUOTES_AS_STRING='{"foo": "bar"}' | ||
TRIM_SPACE_FROM_UNQUOTED= some spaced out string | ||
USERNAME=therealnerdybeast@example.tld | ||
SPACED_KEY = parsed | ||
|
||
MULTI_DOUBLE_QUOTED="THIS | ||
IS | ||
A | ||
MULTILINE | ||
STRING" | ||
|
||
MULTI_SINGLE_QUOTED='THIS | ||
IS | ||
A | ||
MULTILINE | ||
STRING' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* @flow */ | ||
|
||
const fs = require('fs') | ||
|
||
const sinon = require('sinon') | ||
const t = require('tap') | ||
|
||
const dotenv = require('../lib/main') | ||
|
||
const parsed = dotenv.parse(fs.readFileSync('tests/.env-multiline', { encoding: 'utf8' }), { multiline: 'line-breaks' }) | ||
|
||
t.plan(25) | ||
|
||
t.type(parsed, Object, 'should return an object') | ||
|
||
t.equal(parsed.BASIC, 'basic', 'sets basic environment variable') | ||
|
||
t.equal(parsed.AFTER_LINE, 'after_line', 'reads after a skipped line') | ||
|
||
t.equal(parsed.EMPTY, '', 'defaults empty values to empty string') | ||
|
||
t.equal(parsed.SINGLE_QUOTES, 'single_quotes', 'escapes single quoted values') | ||
|
||
t.equal(parsed.SINGLE_QUOTES_SPACED, ' single quotes ', 'respects surrounding spaces in single quotes') | ||
|
||
t.equal(parsed.DOUBLE_QUOTES, 'double_quotes', 'escapes double quoted values') | ||
|
||
t.equal(parsed.DOUBLE_QUOTES_SPACED, ' double quotes ', 'respects surrounding spaces in double quotes') | ||
|
||
t.equal(parsed.EXPAND_NEWLINES, 'expand\nnew\nlines', 'expands newlines but only if double quoted') | ||
|
||
t.equal(parsed.DONT_EXPAND_UNQUOTED, 'dontexpand\\nnewlines', 'expands newlines but only if double quoted') | ||
|
||
t.equal(parsed.DONT_EXPAND_SQUOTED, 'dontexpand\\nnewlines', 'expands newlines but only if double quoted') | ||
|
||
t.notOk(parsed.COMMENTS, 'ignores commented lines') | ||
|
||
t.equal(parsed.EQUAL_SIGNS, 'equals==', 'respects equals signs in values') | ||
|
||
t.equal(parsed.RETAIN_INNER_QUOTES, '{"foo": "bar"}', 'retains inner quotes') | ||
|
||
t.equal(parsed.RETAIN_INNER_QUOTES_AS_STRING, '{"foo": "bar"}', 'retains inner quotes') | ||
|
||
t.equal(parsed.TRIM_SPACE_FROM_UNQUOTED, 'some spaced out string', 'retains spaces in string') | ||
|
||
t.equal(parsed.USERNAME, 'therealnerdybeast@example.tld', 'parses email addresses completely') | ||
|
||
t.equal(parsed.SPACED_KEY, 'parsed', 'parses keys and values surrounded by spaces') | ||
|
||
t.equal(parsed.MULTI_DOUBLE_QUOTED, 'THIS\nIS\nA\nMULTILINE\nSTRING', 'parses multi-line strings when using double quotes') | ||
|
||
t.equal(parsed.MULTI_SINGLE_QUOTED, 'THIS\nIS\nA\nMULTILINE\nSTRING', 'parses multi-line strings when using single quotes') | ||
|
||
const payload = dotenv.parse(Buffer.from('BUFFER=true')) | ||
t.equal(payload.BUFFER, 'true', 'should parse a buffer into an object') | ||
|
||
const expectedPayload = { SERVER: 'localhost', PASSWORD: 'password', DB: 'tests' } | ||
|
||
const RPayload = dotenv.parse(Buffer.from('SERVER=localhost\rPASSWORD=password\rDB=tests\r')) | ||
t.same(RPayload, expectedPayload, 'can parse (\\r) line endings') | ||
|
||
const NPayload = dotenv.parse(Buffer.from('SERVER=localhost\nPASSWORD=password\nDB=tests\n')) | ||
t.same(NPayload, expectedPayload, 'can parse (\\n) line endings') | ||
|
||
const RNPayload = dotenv.parse(Buffer.from('SERVER=localhost\r\nPASSWORD=password\r\nDB=tests\r\n')) | ||
t.same(RNPayload, expectedPayload, 'can parse (\\r\\n) line endings') | ||
|
||
// test debug path | ||
const logStub = sinon.stub(console, 'log') | ||
dotenv.parse(Buffer.from('what is this'), { debug: true }) | ||
t.ok(logStub.called) | ||
logStub.restore() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.