Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/base/test/common/envfile.test.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { parseEnvFile } from '../../common/envfile.js';
7
import { ensureNoDisposablesAreLeakedInTestSuite } from './utils.js';
8
import * as assert from 'assert';
9
10
/*
11
Test cases from https://github.com/motdotla/dotenv/blob/master/tests/.env
12
13
Copyright (c) 2015, Scott Motte
14
All rights reserved.
15
16
Redistribution and use in source and binary forms, with or without
17
modification, are permitted provided that the following conditions are met:
18
19
* Redistributions of source code must retain the above copyright notice, this
20
list of conditions and the following disclaimer.
21
22
* Redistributions in binary form must reproduce the above copyright notice,
23
this list of conditions and the following disclaimer in the documentation
24
and/or other materials provided with the distribution.
25
26
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
30
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
34
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36
*/
37
38
const example = `
39
BASIC=basic
40
41
# previous line intentionally left blank
42
AFTER_LINE=after_line
43
EMPTY=
44
EMPTY_SINGLE_QUOTES=''
45
EMPTY_DOUBLE_QUOTES=""
46
EMPTY_BACKTICKS=\`\`
47
SINGLE_QUOTES='single_quotes'
48
SINGLE_QUOTES_SPACED=' single quotes '
49
DOUBLE_QUOTES="double_quotes"
50
DOUBLE_QUOTES_SPACED=" double quotes "
51
DOUBLE_QUOTES_INSIDE_SINGLE='double "quotes" work inside single quotes'
52
DOUBLE_QUOTES_WITH_NO_SPACE_BRACKET="{ port: $MONGOLAB_PORT}"
53
SINGLE_QUOTES_INSIDE_DOUBLE="single 'quotes' work inside double quotes"
54
BACKTICKS_INSIDE_SINGLE='\`backticks\` work inside single quotes'
55
BACKTICKS_INSIDE_DOUBLE="\`backticks\` work inside double quotes"
56
BACKTICKS=\`backticks\`
57
BACKTICKS_SPACED=\` backticks \`
58
DOUBLE_QUOTES_INSIDE_BACKTICKS=\`double "quotes" work inside backticks\`
59
SINGLE_QUOTES_INSIDE_BACKTICKS=\`single 'quotes' work inside backticks\`
60
DOUBLE_AND_SINGLE_QUOTES_INSIDE_BACKTICKS=\`double "quotes" and single 'quotes' work inside backticks\`
61
EXPAND_NEWLINES="expand\\nnew\\nlines"
62
DONT_EXPAND_UNQUOTED=dontexpand\\nnewlines
63
DONT_EXPAND_SQUOTED='dontexpand\\nnewlines'
64
# COMMENTS=work
65
INLINE_COMMENTS=inline comments # work #very #well
66
INLINE_COMMENTS_SINGLE_QUOTES='inline comments outside of #singlequotes' # work
67
INLINE_COMMENTS_DOUBLE_QUOTES="inline comments outside of #doublequotes" # work
68
INLINE_COMMENTS_BACKTICKS=\`inline comments outside of #backticks\` # work
69
INLINE_COMMENTS_SPACE=inline comments start with a#number sign. no space required.
70
EQUAL_SIGNS=equals==
71
RETAIN_INNER_QUOTES={"foo": "bar"}
72
RETAIN_INNER_QUOTES_AS_STRING='{"foo": "bar"}'
73
RETAIN_INNER_QUOTES_AS_BACKTICKS=\`{"foo": "bar's"}\`
74
TRIM_SPACE_FROM_UNQUOTED= some spaced out string
75
[email protected]
76
SPACED_KEY = parsed
77
`;
78
79
suite('parseEnvFile', () => {
80
ensureNoDisposablesAreLeakedInTestSuite();
81
82
test('parses', () => {
83
const parsed = parseEnvFile(example);
84
assert.strictEqual(parsed.get('BASIC'), 'basic');
85
assert.strictEqual(parsed.get('AFTER_LINE'), 'after_line');
86
assert.strictEqual(parsed.get('EMPTY'), '');
87
assert.strictEqual(parsed.get('EMPTY_SINGLE_QUOTES'), '');
88
assert.strictEqual(parsed.get('EMPTY_DOUBLE_QUOTES'), '');
89
assert.strictEqual(parsed.get('EMPTY_BACKTICKS'), '');
90
assert.strictEqual(parsed.get('SINGLE_QUOTES'), 'single_quotes');
91
assert.strictEqual(parsed.get('SINGLE_QUOTES_SPACED'), ' single quotes ');
92
assert.strictEqual(parsed.get('DOUBLE_QUOTES'), 'double_quotes');
93
assert.strictEqual(parsed.get('DOUBLE_QUOTES_SPACED'), ' double quotes ');
94
assert.strictEqual(parsed.get('DOUBLE_QUOTES_INSIDE_SINGLE'), 'double "quotes" work inside single quotes');
95
assert.strictEqual(parsed.get('DOUBLE_QUOTES_WITH_NO_SPACE_BRACKET'), '{ port: $MONGOLAB_PORT}');
96
assert.strictEqual(parsed.get('SINGLE_QUOTES_INSIDE_DOUBLE'), "single 'quotes' work inside double quotes");
97
assert.strictEqual(parsed.get('BACKTICKS_INSIDE_SINGLE'), '`backticks` work inside single quotes');
98
assert.strictEqual(parsed.get('BACKTICKS_INSIDE_DOUBLE'), '`backticks` work inside double quotes');
99
assert.strictEqual(parsed.get('BACKTICKS'), 'backticks');
100
assert.strictEqual(parsed.get('BACKTICKS_SPACED'), ' backticks ');
101
assert.strictEqual(parsed.get('DOUBLE_QUOTES_INSIDE_BACKTICKS'), 'double "quotes" work inside backticks');
102
assert.strictEqual(parsed.get('SINGLE_QUOTES_INSIDE_BACKTICKS'), "single 'quotes' work inside backticks");
103
assert.strictEqual(parsed.get('DOUBLE_AND_SINGLE_QUOTES_INSIDE_BACKTICKS'), "double \"quotes\" and single 'quotes' work inside backticks");
104
assert.strictEqual(parsed.get('EXPAND_NEWLINES'), 'expand\nnew\nlines');
105
assert.strictEqual(parsed.get('DONT_EXPAND_UNQUOTED'), 'dontexpand\\nnewlines');
106
assert.strictEqual(parsed.get('DONT_EXPAND_SQUOTED'), 'dontexpand\\nnewlines');
107
assert.strictEqual(parsed.get('COMMENTS'), undefined);
108
assert.strictEqual(parsed.get('INLINE_COMMENTS'), 'inline comments');
109
assert.strictEqual(parsed.get('INLINE_COMMENTS_SINGLE_QUOTES'), 'inline comments outside of #singlequotes');
110
assert.strictEqual(parsed.get('INLINE_COMMENTS_DOUBLE_QUOTES'), 'inline comments outside of #doublequotes');
111
assert.strictEqual(parsed.get('INLINE_COMMENTS_BACKTICKS'), 'inline comments outside of #backticks');
112
assert.strictEqual(parsed.get('INLINE_COMMENTS_SPACE'), 'inline comments start with a');
113
assert.strictEqual(parsed.get('EQUAL_SIGNS'), 'equals==');
114
assert.strictEqual(parsed.get('RETAIN_INNER_QUOTES'), '{"foo": "bar"}');
115
assert.strictEqual(parsed.get('RETAIN_INNER_QUOTES_AS_STRING'), '{"foo": "bar"}');
116
assert.strictEqual(parsed.get('RETAIN_INNER_QUOTES_AS_BACKTICKS'), '{"foo": "bar\'s"}');
117
assert.strictEqual(parsed.get('TRIM_SPACE_FROM_UNQUOTED'), 'some spaced out string');
118
assert.strictEqual(parsed.get('USERNAME'), '[email protected]');
119
assert.strictEqual(parsed.get('SPACED_KEY'), 'parsed');
120
const payload = parseEnvFile('BUFFER=true');
121
assert.strictEqual(payload.get('BUFFER'), 'true');
122
const expectedPayload = Object.entries({ SERVER: 'localhost', PASSWORD: 'password', DB: 'tests' });
123
const RPayload = parseEnvFile('SERVER=localhost\rPASSWORD=password\rDB=tests\r');
124
assert.deepStrictEqual([...RPayload], expectedPayload);
125
const NPayload = parseEnvFile('SERVER=localhost\nPASSWORD=password\nDB=tests\n');
126
assert.deepStrictEqual([...NPayload], expectedPayload);
127
const RNPayload = parseEnvFile('SERVER=localhost\r\nPASSWORD=password\r\nDB=tests\r\n');
128
assert.deepStrictEqual([...RNPayload], expectedPayload);
129
});
130
});
131
132