Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(#133) Add option allowEmptyValues #134

Merged
merged 6 commits into from
May 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ Use the following properties to configure your instance.

* **path** (`'./.env'`) - The path to your environment variables.
* **safe** (`false`) - If true, load '.env.example' to verify the '.env' variables are all set. Can also be a string to a different file.
* **allowEmptyValues** (`false`) - Whether to allow empty strings in safe mode. If false, will throw an error if any env variables are empty (but only if safe mode is enabled).
* **systemvars** (`false`) - Set to true if you would rather load all system variables as well (useful for CI purposes).
* **silent** (`false`) - If true, all warnings will be suppressed.
* **expand** (`false`) - Allows your variables to be "expanded" for reusability within your `.env` file.
Expand All @@ -106,6 +107,7 @@ module.exports = {
new Dotenv({
path: './some.other.env', // load this now instead of the ones in '.env'
safe: true, // load '.env.example' to verify the '.env' variables are all set. Can also be a string to a different file.
allowEmptyValues: true, // allow empty variables (e.g. `FOO=`) (treat it as empty string, rather than missing)
systemvars: true, // load all the predefined 'process.env' variables which will trump anything local per dotenv specs.
silent: true, // hide any errors
defaults: false // load '.env.defaults' as the default values if empty.
Expand Down
8 changes: 6 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,18 @@ class Dotenv {
}

gatherVariables () {
const { safe } = this.config
const { safe, allowEmptyValues } = this.config
const vars = this.initializeVars()

const { env, blueprint } = this.getEnvs()

Object.keys(blueprint).map(key => {
const value = Object.prototype.hasOwnProperty.call(vars, key) ? vars[key] : env[key]
if (!value && safe) {

const isMissing = typeof value === 'undefined' || value === null ||
(!allowEmptyValues && value === '')

if (safe && isMissing) {
throw new Error(`Missing environment variable: ${key}`)
} else {
vars[key] = value
Expand Down
1 change: 0 additions & 1 deletion test/envs/.missingone
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
TEST=
TEST2=Hello
2 changes: 2 additions & 0 deletions test/envs/.oneempty
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
TEST=
TEST2=Hello
2 changes: 2 additions & 0 deletions test/envs/.oneempty.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
TEST=
TEST2=
25 changes: 23 additions & 2 deletions test/main.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const envEmpty = path.resolve(__dirname, './envs/.empty')
const envEmptyExample = path.resolve(__dirname, './envs/.empty.example')
const envSimple = path.resolve(__dirname, './envs/.simple')
const envSimpleExample = path.resolve(__dirname, './envs/.simple.example')
const envOneEmpty = path.resolve(__dirname, './envs/.oneempty')
const envOneEmptyExample = path.resolve(__dirname, './envs/.oneempty.example')
const envMissingOne = path.resolve(__dirname, './envs/.missingone')
const envMissingOneExample = path.resolve(__dirname, './envs/.missingone.example')
const envSystemvars = path.resolve(__dirname, './envs/.systemvars')
Expand All @@ -28,7 +30,8 @@ const buildExpectation = (obj) => Object.keys(obj).reduce((all, key) => {
const envDefJson = buildExpectation({ TEST: 'hi' })
const envEmptyJson = buildExpectation({})
const envSimpleJson = buildExpectation({ TEST: 'testing' })
const envMissingOneJson = buildExpectation({ TEST: '', TEST2: 'Hello' })
const envOneEmptyJson = buildExpectation({ TEST: '', TEST2: 'Hello' })
const envMissingOneJson = buildExpectation({ TEST2: 'Hello' })
const envDefaultsJson = buildExpectation({ TEST: 'hi', TEST2: 'hidefault' })
const envDefaultsJson2 = buildExpectation({ TEST: 'hi', TEST2: 'youcanseethis' })

Expand Down Expand Up @@ -208,12 +211,30 @@ function runTests (Obj, name) {
})
})

describe('Empty variables', () => {
it('Should load fine (not-safe)', () => {
envTest({ path: envOneEmpty }).should.deep.equal(envOneEmptyJson)
})

it('Should fail on safe mode', () => {
function errorTest () {
envTest({ path: envOneEmpty, safe: envOneEmptyExample })
}

errorTest.should.throw('Missing environment variable')
})

it('Should succeed in safe mode if allowEmptyValues is true', () => {
envTest({ path: envOneEmpty, safe: envOneEmptyExample, allowEmptyValues: true }).should.deep.equal(envOneEmptyJson)
})
})

describe('Missing a variable', () => {
it('Should load fine (not-safe)', () => {
envTest({ path: envMissingOne }).should.deep.equal(envMissingOneJson)
})

it('Should fail on safe mode', () => {
it('Should fail on safe mode (if allowEmptyValues is false)', () => {
function errorTest () {
envTest({ path: envMissingOne, safe: envMissingOneExample })
}
Expand Down