|
13 | 13 | // limitations under the License.
|
14 | 14 |
|
15 | 15 | import assert from 'node:assert'
|
16 |
| -import { test, describe } from 'node:test' |
17 |
| -import { $, chalk } from '../build/index.js' |
18 |
| -import { echo, sleep, parseArgv } from '../build/goods.js' |
| 16 | +import { test, describe, after } from 'node:test' |
| 17 | +import { $, chalk, fs, tempfile } from '../build/index.js' |
| 18 | +import { echo, sleep, parseArgv, loadDotenv } from '../build/goods.js' |
19 | 19 |
|
20 | 20 | describe('goods', () => {
|
21 | 21 | function zx(script) {
|
@@ -173,4 +173,45 @@ describe('goods', () => {
|
173 | 173 | }
|
174 | 174 | )
|
175 | 175 | })
|
| 176 | + |
| 177 | + describe('loadDotenv()', () => { |
| 178 | + const env1 = tempfile( |
| 179 | + '.env', |
| 180 | + `FOO=BAR |
| 181 | + BAR=FOO+` |
| 182 | + ) |
| 183 | + const env2 = tempfile('.env.default', `BAR2=FOO2`) |
| 184 | + |
| 185 | + after(() => { |
| 186 | + fs.remove(env1) |
| 187 | + fs.remove(env2) |
| 188 | + }) |
| 189 | + |
| 190 | + test('handles multiple dotenv files', async () => { |
| 191 | + const env = loadDotenv(env1, env2) |
| 192 | + |
| 193 | + assert.equal((await $({ env })`echo $FOO`).stdout, 'BAR\n') |
| 194 | + assert.equal((await $({ env })`echo $BAR`).stdout, 'FOO+\n') |
| 195 | + assert.equal((await $({ env })`echo $BAR2`).stdout, 'FOO2\n') |
| 196 | + }) |
| 197 | + |
| 198 | + test('handles replace evn', async () => { |
| 199 | + const env = loadDotenv(env1) |
| 200 | + $.env = env |
| 201 | + assert.equal((await $`echo $FOO`).stdout, 'BAR\n') |
| 202 | + assert.equal((await $`echo $BAR`).stdout, 'FOO+\n') |
| 203 | + $.env = process.env |
| 204 | + }) |
| 205 | + |
| 206 | + test('handle error', async () => { |
| 207 | + try { |
| 208 | + loadDotenv('./.env') |
| 209 | + |
| 210 | + assert.throw() |
| 211 | + } catch (e) { |
| 212 | + assert.equal(e.code, 'ENOENT') |
| 213 | + assert.equal(e.errno, -2) |
| 214 | + } |
| 215 | + }) |
| 216 | + }) |
176 | 217 | })
|
0 commit comments