forked from motdotla/dotenv
/
test-parse-multiline.js
72 lines (41 loc) · 2.85 KB
/
test-parse-multiline.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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()