apply-ddl.test.mjs
1.81 KB
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
import { test } from 'node:test'
import assert from 'node:assert/strict'
import { parseEnv } from './apply-ddl.mjs'
test('parseEnv ignores comments, trims, keeps special chars literally', () => {
const env = parseEnv('# c\nDB_PASS=p@ss$word!\nDB_NAME = erp \n')
assert.equal(env.DB_PASS, 'p@ss$word!')
assert.equal(env.DB_NAME, 'erp')
})
test('parseEnv does NOT expand variables', () => {
const env = parseEnv('A=1\nB=${A}\nC=$A\nD=$(whoami)\nE=`id`\n')
assert.equal(env.B, '${A}')
assert.equal(env.C, '$A')
assert.equal(env.D, '$(whoami)')
assert.equal(env.E, '`id`')
})
test('parseEnv skips blank lines and comment lines', () => {
const env = parseEnv('\n \n# comment\nK=v\n # indented comment\n')
assert.deepEqual(Object.keys(env), ['K'])
assert.equal(env.K, 'v')
})
test('parseEnv strips one layer of matching quotes', () => {
const env = parseEnv(`Q1="hello world"\nQ2='a=b=c'\nQ3="$keep"\n`)
assert.equal(env.Q1, 'hello world')
assert.equal(env.Q2, 'a=b=c')
assert.equal(env.Q3, '$keep')
})
test('parseEnv keeps = signs inside the value', () => {
const env = parseEnv('URL=mysql://u:p@h:3306/db?x=1&y=2\n')
assert.equal(env.URL, 'mysql://u:p@h:3306/db?x=1&y=2')
})
test('parseEnv strips an optional leading export', () => {
const env = parseEnv('export DB_USER=root\n')
assert.equal(env.DB_USER, 'root')
})
test('parseEnv tolerates CRLF line endings', () => {
const env = parseEnv('A=1\r\nB=2\r\n')
assert.equal(env.A, '1')
assert.equal(env.B, '2')
})
test('parseEnv ignores lines without an = sign', () => {
const env = parseEnv('NOEQUALS\nK=v\n')
assert.deepEqual(Object.keys(env), ['K'])
})
test('parseEnv on empty / non-string input returns empty object', () => {
assert.deepEqual(parseEnv(''), {})
assert.deepEqual(parseEnv(undefined), {})
assert.deepEqual(parseEnv(null), {})
})