-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
src: add
process.loadEnvFile
programmatic API
- Loading branch information
Showing
7 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
BASIC=basic |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const fixtures = require('../../test/common/fixtures'); | ||
const path = require('node:path'); | ||
const assert = require('node:assert'); | ||
const { describe, it } = require('node:test'); | ||
|
||
const validEnvFilePath = fixtures.path('dotenv/valid.env'); | ||
const missingEnvFile = fixtures.path('dotenv/non-existent-file.env'); | ||
|
||
describe('process.loadEnvFile()', () => { | ||
|
||
it('supports passing path', async () => { | ||
const code = ` | ||
process.loadEnvFile(${JSON.stringify(validEnvFilePath)}); | ||
const assert = require('assert'); | ||
assert.strictEqual(process.env.BASIC, 'basic'); | ||
`.trim(); | ||
const child = await common.spawnPromisified( | ||
process.execPath, | ||
[ '--eval', code ], | ||
{ cwd: __dirname }, | ||
); | ||
assert.strictEqual(child.stderr, ''); | ||
assert.strictEqual(child.code, 0); | ||
}); | ||
|
||
it('supports not-passing a path', async () => { | ||
// Uses `../fixtures/dotenv/.env` file. | ||
const code = ` | ||
process.loadEnvFile(); | ||
const assert = require('assert'); | ||
assert.strictEqual(process.env.BASIC, 'basic'); | ||
`.trim(); | ||
const child = await common.spawnPromisified( | ||
process.execPath, | ||
[ '--eval', code ], | ||
{ cwd: path.join(__dirname, '../fixtures/dotenv') }, | ||
); | ||
assert.strictEqual(child.stderr, ''); | ||
assert.strictEqual(child.code, 0); | ||
}); | ||
|
||
it('should handle non-existent files', async () => { | ||
const code = ` | ||
process.loadEnvFile(${JSON.stringify(missingEnvFile)}); | ||
`.trim(); | ||
const child = await common.spawnPromisified( | ||
process.execPath, | ||
[ '--eval', code ], | ||
{ cwd: __dirname }, | ||
); | ||
assert.strictEqual(child.stderr, ''); | ||
assert.strictEqual(child.code, 0); | ||
}); | ||
|
||
it('should check for permissions', async () => { | ||
const code = ` | ||
process.loadEnvFile(${JSON.stringify(missingEnvFile)}); | ||
`.trim(); | ||
const child = await common.spawnPromisified( | ||
process.execPath, | ||
[ '--eval', code, '--experimental-permission' ], | ||
{ cwd: __dirname }, | ||
); | ||
assert.match(child.stderr, /Error: Access to this API has been restricted/); | ||
assert.match(child.stderr, /code: 'ERR_ACCESS_DENIED'/); | ||
assert.match(child.stderr, /permission: 'FileSystemRead'/); | ||
assert.strictEqual(child.code, 1); | ||
}); | ||
}); |