-
-
Notifications
You must be signed in to change notification settings - Fork 861
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add option to allow line breaks in values #486
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0a9332f
feat: add option to allow line breaks in values
andreialecu f652c44
add test for unended multiline values
andreialecu 5b3aaa4
use plain for
andreialecu ac20ff8
fix tests on node 12
andreialecu 9b1d338
Merge branch 'master' into feat-multiline
andreialecu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,36 @@ | ||
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 | ||
[email protected] | ||
SPACED_KEY = parsed | ||
|
||
MULTI_DOUBLE_QUOTED="THIS | ||
IS | ||
A | ||
MULTILINE | ||
STRING" | ||
|
||
MULTI_SINGLE_QUOTED='THIS | ||
IS | ||
A | ||
MULTILINE | ||
STRING' | ||
|
||
MULTI_UNENDED="THIS | ||
LINE HAS | ||
NO END QUOTE |
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,74 @@ | ||
/* @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(26) | ||
|
||
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, '[email protected]', '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') | ||
|
||
t.equal(parsed.MULTI_UNENDED, 'THIS\nLINE HAS\nNO END QUOTE', '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() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See https://stackoverflow.com/questions/46124087/sinon-stub-spy-on-local-functions-in-unit-testing for an explanation of why this was necessary