-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.test.ts
More file actions
154 lines (137 loc) · 4.1 KB
/
index.test.ts
File metadata and controls
154 lines (137 loc) · 4.1 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import * as assert from 'node:assert'
import fs from 'node:fs'
import os from 'node:os'
import path from 'node:path'
import { describe, test, afterAll } from 'vitest'
import { parse, stringify, load, loadSafe, populate, config } from '../../main/ts/index.js'
const randomId= () => Math.random().toString(36).slice(2)
const tempdir = (prefix: string = `temp-${randomId()}`): string => {
const dirpath = path.join(os.tmpdir(), prefix)
fs.mkdirSync(dirpath, { recursive: true })
return dirpath
}
const tempfile = (name?: string, data?: string | Buffer): string => {
const filepath = name
? path.join(tempdir(), name)
: path.join(os.tmpdir(), `temp-${randomId()}`)
if (data === undefined) fs.closeSync(fs.openSync(filepath, 'w'))
else fs.writeFileSync(filepath, data)
return filepath
}
describe('parse/stringify', () => {
test('works', () => {
const str = `SIMPLE=xyz123
# comment ###
NON_INTERPOLATED='raw text without variable interpolation'
MULTILINE = """
long text here, # not-comment
e.g. a private SSH key
"""
ENV=v1\nENV2=v2\r\n\n\r\n\t\t ENV3 = 'v"3' \n export ENV4="v\`4"
ENV5="v'5" # comment
ENV6=\`v'"6\`
ENV7=
ENV8=''
JSON={"foo": "b a r"}
JSONSTR='{"foo": "b a r"}'
`
const env = parse(str)
assert.deepEqual(env, {
SIMPLE: 'xyz123',
NON_INTERPOLATED: 'raw text without variable interpolation',
MULTILINE: 'long text here, # not-comment\ne.g. a private SSH key',
ENV: 'v1',
ENV2: 'v2',
ENV3: 'v"3',
ENV4: 'v`4',
ENV5: "v'5",
ENV6: `v'"6`,
ENV7: '',
ENV8: '',
JSON: '{"foo": "b a r"}',
JSONSTR: '{"foo": "b a r"}',
})
const nstr = stringify(env)
assert.equal(nstr, `SIMPLE=xyz123
NON_INTERPOLATED="raw text without variable interpolation"
MULTILINE="long text here, # not-comment\ne.g. a private SSH key"
ENV=v1
ENV2=v2
ENV3='v"3'
ENV4="v\`4"
ENV5="v'5"
ENV6=\`v'"6\`
ENV7=
ENV8=
JSON='{"foo": "b a r"}'
JSONSTR='{"foo": "b a r"}'`
)
})
test('stringify checks invalid (imbalanced quote) values', () => {
const V1 = "en_US\"'`\nBASH_ENV=$(id 1>&2)\nx=`"
const V2 = 'foo=\'bar\'\nbaz=\\`"qux\\`\"\"'
assert.throws(() => stringify({ V1 }))
assert.throws(() => stringify({ V2 }))
})
test('accepts buffer input', () => {
const str = 'FOO=BAR\r\nBAz=QUZ'
const env = {
FOO: 'BAR',
BAz: 'QUZ',
}
assert.deepEqual(parse(Buffer.from(str, 'utf8')), env)
assert.deepEqual(parse(Buffer.from(str, 'ascii')), env)
})
test('throws on invalid input', () => {
assert.throws(() => parse('BRO-KEN=xyz123'))
assert.throws(() => parse('BRO KEN=xyz123'))
assert.throws(() => parse('1BROKEN=xyz123'))
})
describe('load()', () => {
const file1 = tempfile('.env.1', 'ENV1=value1\nENV2=value2')
const file2 = tempfile('.env.2', 'ENV2=value222\nENV3=value3')
afterAll(() => {
fs.unlinkSync(file1)
fs.unlinkSync(file2)
})
test('loads env from files', () => {
const env = load(file1, file2)
assert.equal(env.ENV1, 'value1')
assert.equal(env.ENV2, 'value2')
assert.equal(env.ENV3, 'value3')
})
test('throws error on ENOENT', () => {
try {
load('./.env')
throw new Error('shouldnt have thrown')
} catch (e: any) {
assert.equal(e.code, 'ENOENT')
assert.equal(e.errno, -2)
}
})
})
describe('loadSafe()', () => {
const file1 = tempfile('.env.1', 'ENV1=value1\nENV2=value2')
const file2 = '.env.notexists'
afterAll(() => fs.unlinkSync(file1))
test('loads env from files', () => {
const env = loadSafe(file1, file2)
assert.equal(env.ENV1, 'value1')
assert.equal(env.ENV2, 'value2')
})
})
describe('config()', () => {
test('updates process.env', () => {
const file1 = tempfile('.env.1', 'ENV1=value1')
assert.equal(process.env.ENV1, undefined)
config(file1)
assert.equal(process.env.ENV1, 'value1')
delete process.env.ENV1
})
})
describe('populate()', () => {
test('populates env', () => {
assert.deepEqual(populate({FOO: 'BAR'}, { FOO: 'BAZ' }), { FOO: 'BAZ' })
})
})
})