|
1 | 1 | import * as assert from 'node:assert' |
2 | | -import { describe, it } from 'vitest' |
3 | | -import { parse, stringify } from '../../main/ts/index.js' |
| 2 | +import fs from 'node:fs' |
| 3 | +import os from 'node:os' |
| 4 | +import path from 'node:path' |
| 5 | +import { describe, test, afterAll } from 'vitest' |
| 6 | +import { parse, stringify, load, loadSafe, config } from '../../main/ts/index.js' |
| 7 | + |
| 8 | +const randomId= () => Math.random().toString(36).slice(2) |
| 9 | +const tempdir = (prefix: string = `temp-${randomId()}`): string => { |
| 10 | + const dirpath = path.join(os.tmpdir(), prefix) |
| 11 | + fs.mkdirSync(dirpath, { recursive: true }) |
| 12 | + return dirpath |
| 13 | +} |
| 14 | +const tempfile = (name?: string, data?: string | Buffer): string => { |
| 15 | + const filepath = name |
| 16 | + ? path.join(tempdir(), name) |
| 17 | + : path.join(os.tmpdir(), `temp-${randomId()}`) |
| 18 | + if (data === undefined) fs.closeSync(fs.openSync(filepath, 'w')) |
| 19 | + else fs.writeFileSync(filepath, data) |
| 20 | + return filepath |
| 21 | +} |
4 | 22 |
|
5 | 23 | describe('parse/stringify', () => { |
6 | | - it('works', () => { |
| 24 | + test('works', () => { |
7 | 25 | const str = `SIMPLE=xyz123 |
8 | 26 | # comment ### |
9 | 27 | NON_INTERPOLATED='raw text without variable interpolation' |
@@ -53,9 +71,59 @@ JSONSTR='{"foo": "b a r"}'` |
53 | 71 | ) |
54 | 72 | }) |
55 | 73 |
|
56 | | - it('throws on invalid input', () => { |
| 74 | + test('throws on invalid input', () => { |
57 | 75 | assert.throws(() => parse('BRO-KEN=xyz123')) |
58 | 76 | assert.throws(() => parse('BRO KEN=xyz123')) |
59 | 77 | assert.throws(() => parse('1BROKEN=xyz123')) |
60 | 78 | }) |
| 79 | + |
| 80 | + describe('load()', () => { |
| 81 | + const file1 = tempfile('.env.1', 'ENV1=value1\nENV2=value2') |
| 82 | + const file2 = tempfile('.env.2', 'ENV2=value222\nENV3=value3') |
| 83 | + afterAll(() => { |
| 84 | + fs.unlinkSync(file1) |
| 85 | + fs.unlinkSync(file2) |
| 86 | + }) |
| 87 | + |
| 88 | + test('loads env from files', () => { |
| 89 | + const env = load(file1, file2) |
| 90 | + assert.equal(env.ENV1, 'value1') |
| 91 | + assert.equal(env.ENV2, 'value2') |
| 92 | + assert.equal(env.ENV3, 'value3') |
| 93 | + }) |
| 94 | + |
| 95 | + test('throws error on ENOENT', () => { |
| 96 | + try { |
| 97 | + load('./.env') |
| 98 | + throw new Error('shouldnt have thrown') |
| 99 | + } catch (e: any) { |
| 100 | + assert.equal(e.code, 'ENOENT') |
| 101 | + assert.equal(e.errno, -2) |
| 102 | + } |
| 103 | + }) |
| 104 | + }) |
| 105 | + |
| 106 | + describe('loadSafe()', () => { |
| 107 | + const file1 = tempfile('.env.1', 'ENV1=value1\nENV2=value2') |
| 108 | + const file2 = '.env.notexists' |
| 109 | + |
| 110 | + afterAll(() => fs.unlinkSync(file1)) |
| 111 | + |
| 112 | + test('loads env from files', () => { |
| 113 | + const env = loadSafe(file1, file2) |
| 114 | + assert.equal(env.ENV1, 'value1') |
| 115 | + assert.equal(env.ENV2, 'value2') |
| 116 | + }) |
| 117 | + }) |
| 118 | + |
| 119 | + describe('config()', () => { |
| 120 | + test('updates process.env', () => { |
| 121 | + const file1 = tempfile('.env.1', 'ENV1=value1') |
| 122 | + |
| 123 | + assert.equal(process.env.ENV1, undefined) |
| 124 | + config(file1) |
| 125 | + assert.equal(process.env.ENV1, 'value1') |
| 126 | + delete process.env.ENV1 |
| 127 | + }) |
| 128 | + }) |
61 | 129 | }) |
0 commit comments