diff --git a/src/lib/helpers/envfile.test.ts b/src/lib/helpers/envfile.test.ts new file mode 100644 index 0000000000..2a629d12a5 --- /dev/null +++ b/src/lib/helpers/envfile.test.ts @@ -0,0 +1,66 @@ +import { parse } from '$lib/helpers/envfile'; +import { describe, expect, it } from 'vitest'; + +describe('parse', () => { + it('parses key-value pairs separated by equals signs', () => { + expect(parse('APPWRITE_ENDPOINT=http://localhost/v1\nAPPWRITE_PROJECT=console')).toEqual({ + APPWRITE_ENDPOINT: 'http://localhost/v1', + APPWRITE_PROJECT: 'console' + }); + }); + + it('parses key-value pairs separated by colons', () => { + expect(parse('HOST: localhost\nPORT: 3000')).toEqual({ + HOST: 'localhost', + PORT: '3000' + }); + }); + + it('trims whitespace around keys and values', () => { + expect(parse(' PUBLIC_KEY = value \n SECRET_KEY : secret ')).toEqual({ + PUBLIC_KEY: 'value', + SECRET_KEY: 'secret' + }); + }); + + it('removes single and double quotes from values', () => { + expect(parse("NAME='Appwrite'\nMESSAGE=\"Hello world\"")).toEqual({ + NAME: 'Appwrite', + MESSAGE: 'Hello world' + }); + }); + + it('preserves quotes inside values', () => { + expect(parse("AUTHOR=O'Brien\nMESSAGE=Say \"hello\"")).toEqual({ + AUTHOR: "O'Brien", + MESSAGE: 'Say "hello"' + }); + }); + + it('keeps equals signs and colons inside values', () => { + expect(parse('DATABASE_URL=mysql://user:pass@example.com/db\nTOKEN=abc=123')).toEqual({ + DATABASE_URL: 'mysql://user:pass@example.com/db', + TOKEN: 'abc=123' + }); + }); + + it('parses files with CRLF line endings', () => { + expect(parse('NAME=Appwrite\r\nMESSAGE=Hello world\r\n')).toEqual({ + NAME: 'Appwrite', + MESSAGE: 'Hello world' + }); + }); + + it('ignores blank lines, comments, and malformed lines', () => { + expect(parse('\n# comment\nKEY=value\nMALFORMED\n:missing-key\n')).toEqual({ + KEY: 'value' + }); + }); + + it('allows empty values', () => { + expect(parse('EMPTY=\nBLANK:')).toEqual({ + EMPTY: '', + BLANK: '' + }); + }); +}); diff --git a/src/lib/helpers/envfile.ts b/src/lib/helpers/envfile.ts index 637e852737..3b8891c506 100644 --- a/src/lib/helpers/envfile.ts +++ b/src/lib/helpers/envfile.ts @@ -9,7 +9,7 @@ export function parse(src: string): Data { const match = line.match(/^([^=:#]+?)[=:](.*)/); if (match) { const key = match[1].trim(); - const value = match[2].trim().replace(/['"]+/g, ''); + const value = match[2].trim().replace(/^(['"])(.*)\1$/, '$2'); result[key] = value; } }