Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions src/lib/helpers/envfile.test.ts
Original file line number Diff line number Diff line change
@@ -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'
});
});
Comment thread
greptile-apps[bot] marked this conversation as resolved.

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: ''
});
});
});
Comment thread
greptile-apps[bot] marked this conversation as resolved.
2 changes: 1 addition & 1 deletion src/lib/helpers/envfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down