Path: blob/main/src/vs/base/test/common/envfile.test.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { parseEnvFile } from '../../common/envfile.js';6import { ensureNoDisposablesAreLeakedInTestSuite } from './utils.js';7import * as assert from 'assert';89/*10Test cases from https://github.com/motdotla/dotenv/blob/master/tests/.env1112Copyright (c) 2015, Scott Motte13All rights reserved.1415Redistribution and use in source and binary forms, with or without16modification, are permitted provided that the following conditions are met:1718* Redistributions of source code must retain the above copyright notice, this19list of conditions and the following disclaimer.2021* Redistributions in binary form must reproduce the above copyright notice,22this list of conditions and the following disclaimer in the documentation23and/or other materials provided with the distribution.2425THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"26AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE27IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE28DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE29FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL30DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR31SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER32CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,33OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE34OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.35*/3637const example = `38BASIC=basic3940# previous line intentionally left blank41AFTER_LINE=after_line42EMPTY=43EMPTY_SINGLE_QUOTES=''44EMPTY_DOUBLE_QUOTES=""45EMPTY_BACKTICKS=\`\`46SINGLE_QUOTES='single_quotes'47SINGLE_QUOTES_SPACED=' single quotes '48DOUBLE_QUOTES="double_quotes"49DOUBLE_QUOTES_SPACED=" double quotes "50DOUBLE_QUOTES_INSIDE_SINGLE='double "quotes" work inside single quotes'51DOUBLE_QUOTES_WITH_NO_SPACE_BRACKET="{ port: $MONGOLAB_PORT}"52SINGLE_QUOTES_INSIDE_DOUBLE="single 'quotes' work inside double quotes"53BACKTICKS_INSIDE_SINGLE='\`backticks\` work inside single quotes'54BACKTICKS_INSIDE_DOUBLE="\`backticks\` work inside double quotes"55BACKTICKS=\`backticks\`56BACKTICKS_SPACED=\` backticks \`57DOUBLE_QUOTES_INSIDE_BACKTICKS=\`double "quotes" work inside backticks\`58SINGLE_QUOTES_INSIDE_BACKTICKS=\`single 'quotes' work inside backticks\`59DOUBLE_AND_SINGLE_QUOTES_INSIDE_BACKTICKS=\`double "quotes" and single 'quotes' work inside backticks\`60EXPAND_NEWLINES="expand\\nnew\\nlines"61DONT_EXPAND_UNQUOTED=dontexpand\\nnewlines62DONT_EXPAND_SQUOTED='dontexpand\\nnewlines'63# COMMENTS=work64INLINE_COMMENTS=inline comments # work #very #well65INLINE_COMMENTS_SINGLE_QUOTES='inline comments outside of #singlequotes' # work66INLINE_COMMENTS_DOUBLE_QUOTES="inline comments outside of #doublequotes" # work67INLINE_COMMENTS_BACKTICKS=\`inline comments outside of #backticks\` # work68INLINE_COMMENTS_SPACE=inline comments start with a#number sign. no space required.69EQUAL_SIGNS=equals==70RETAIN_INNER_QUOTES={"foo": "bar"}71RETAIN_INNER_QUOTES_AS_STRING='{"foo": "bar"}'72RETAIN_INNER_QUOTES_AS_BACKTICKS=\`{"foo": "bar's"}\`73TRIM_SPACE_FROM_UNQUOTED= some spaced out string74[email protected]75SPACED_KEY = parsed76`;7778suite('parseEnvFile', () => {79ensureNoDisposablesAreLeakedInTestSuite();8081test('parses', () => {82const parsed = parseEnvFile(example);83assert.strictEqual(parsed.get('BASIC'), 'basic');84assert.strictEqual(parsed.get('AFTER_LINE'), 'after_line');85assert.strictEqual(parsed.get('EMPTY'), '');86assert.strictEqual(parsed.get('EMPTY_SINGLE_QUOTES'), '');87assert.strictEqual(parsed.get('EMPTY_DOUBLE_QUOTES'), '');88assert.strictEqual(parsed.get('EMPTY_BACKTICKS'), '');89assert.strictEqual(parsed.get('SINGLE_QUOTES'), 'single_quotes');90assert.strictEqual(parsed.get('SINGLE_QUOTES_SPACED'), ' single quotes ');91assert.strictEqual(parsed.get('DOUBLE_QUOTES'), 'double_quotes');92assert.strictEqual(parsed.get('DOUBLE_QUOTES_SPACED'), ' double quotes ');93assert.strictEqual(parsed.get('DOUBLE_QUOTES_INSIDE_SINGLE'), 'double "quotes" work inside single quotes');94assert.strictEqual(parsed.get('DOUBLE_QUOTES_WITH_NO_SPACE_BRACKET'), '{ port: $MONGOLAB_PORT}');95assert.strictEqual(parsed.get('SINGLE_QUOTES_INSIDE_DOUBLE'), "single 'quotes' work inside double quotes");96assert.strictEqual(parsed.get('BACKTICKS_INSIDE_SINGLE'), '`backticks` work inside single quotes');97assert.strictEqual(parsed.get('BACKTICKS_INSIDE_DOUBLE'), '`backticks` work inside double quotes');98assert.strictEqual(parsed.get('BACKTICKS'), 'backticks');99assert.strictEqual(parsed.get('BACKTICKS_SPACED'), ' backticks ');100assert.strictEqual(parsed.get('DOUBLE_QUOTES_INSIDE_BACKTICKS'), 'double "quotes" work inside backticks');101assert.strictEqual(parsed.get('SINGLE_QUOTES_INSIDE_BACKTICKS'), "single 'quotes' work inside backticks");102assert.strictEqual(parsed.get('DOUBLE_AND_SINGLE_QUOTES_INSIDE_BACKTICKS'), "double \"quotes\" and single 'quotes' work inside backticks");103assert.strictEqual(parsed.get('EXPAND_NEWLINES'), 'expand\nnew\nlines');104assert.strictEqual(parsed.get('DONT_EXPAND_UNQUOTED'), 'dontexpand\\nnewlines');105assert.strictEqual(parsed.get('DONT_EXPAND_SQUOTED'), 'dontexpand\\nnewlines');106assert.strictEqual(parsed.get('COMMENTS'), undefined);107assert.strictEqual(parsed.get('INLINE_COMMENTS'), 'inline comments');108assert.strictEqual(parsed.get('INLINE_COMMENTS_SINGLE_QUOTES'), 'inline comments outside of #singlequotes');109assert.strictEqual(parsed.get('INLINE_COMMENTS_DOUBLE_QUOTES'), 'inline comments outside of #doublequotes');110assert.strictEqual(parsed.get('INLINE_COMMENTS_BACKTICKS'), 'inline comments outside of #backticks');111assert.strictEqual(parsed.get('INLINE_COMMENTS_SPACE'), 'inline comments start with a');112assert.strictEqual(parsed.get('EQUAL_SIGNS'), 'equals==');113assert.strictEqual(parsed.get('RETAIN_INNER_QUOTES'), '{"foo": "bar"}');114assert.strictEqual(parsed.get('RETAIN_INNER_QUOTES_AS_STRING'), '{"foo": "bar"}');115assert.strictEqual(parsed.get('RETAIN_INNER_QUOTES_AS_BACKTICKS'), '{"foo": "bar\'s"}');116assert.strictEqual(parsed.get('TRIM_SPACE_FROM_UNQUOTED'), 'some spaced out string');117assert.strictEqual(parsed.get('USERNAME'), '[email protected]');118assert.strictEqual(parsed.get('SPACED_KEY'), 'parsed');119const payload = parseEnvFile('BUFFER=true');120assert.strictEqual(payload.get('BUFFER'), 'true');121const expectedPayload = Object.entries({ SERVER: 'localhost', PASSWORD: 'password', DB: 'tests' });122const RPayload = parseEnvFile('SERVER=localhost\rPASSWORD=password\rDB=tests\r');123assert.deepStrictEqual([...RPayload], expectedPayload);124const NPayload = parseEnvFile('SERVER=localhost\nPASSWORD=password\nDB=tests\n');125assert.deepStrictEqual([...NPayload], expectedPayload);126const RNPayload = parseEnvFile('SERVER=localhost\r\nPASSWORD=password\r\nDB=tests\r\n');127assert.deepStrictEqual([...RNPayload], expectedPayload);128});129});130131132