Skip to content

Commit

Permalink
refactor: split core function of loadDotEnvFiles for better testing (#…
Browse files Browse the repository at this point in the history
…3396)

Co-authored-by: ehmicky <[email protected]>
  • Loading branch information
tinfoil-knight and ehmicky authored Sep 24, 2021
1 parent 6e7e4ea commit d929073
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/utils/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ const getEnvSourceName = (source) => {
// dot-env files and the process itself, and injects into `process.env`.
const injectEnvVariables = async ({ env, site }) => {
const environment = new Map(Object.entries(env))
const dotEnvFiles = await loadDotEnvFiles({ projectDir: site.root, warn })
const dotEnvFiles = await loadDotEnvFiles({ projectDir: site.root })

dotEnvFiles.forEach(({ file, env: fileEnv }) => {
Object.keys(fileEnv).forEach((key) => {
Expand Down
22 changes: 18 additions & 4 deletions src/utils/dot-env.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,20 @@ const dotenv = require('dotenv')

const { isFileAsync, readFileAsync } = require('../lib/fs')

const loadDotEnvFiles = async function ({ projectDir, warn }) {
const { warn } = require('./command-helpers')

const loadDotEnvFiles = async function ({ projectDir }) {
const response = await tryLoadDotEnvFiles({ projectDir })

const filesWithWarning = response.filter((el) => el.warning)
filesWithWarning.forEach((el) => {
warn(el.warning)
})

return response.filter((el) => el.file && el.env)
}

const tryLoadDotEnvFiles = async ({ projectDir }) => {
const dotenvFiles = ['.env', '.env.development']
const results = await Promise.all(
dotenvFiles.map(async (file) => {
Expand All @@ -15,8 +28,9 @@ const loadDotEnvFiles = async function ({ projectDir, warn }) {
return
}
} catch (error) {
warn(`Failed reading env variables from file: ${filepath}: ${error.message}`)
return
return {
warning: `Failed reading env variables from file: ${filepath}: ${error.message}`,
}
}
const content = await readFileAsync(filepath)
const env = dotenv.parse(content)
Expand All @@ -27,4 +41,4 @@ const loadDotEnvFiles = async function ({ projectDir, warn }) {
return results.filter(Boolean)
}

module.exports = { loadDotEnvFiles }
module.exports = { loadDotEnvFiles, tryLoadDotEnvFiles }
17 changes: 7 additions & 10 deletions src/utils/dot-env.test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
const process = require('process')

const test = require('ava')
const sinon = require('sinon')

const { withSiteBuilder } = require('../../tests/utils/site-builder')

const { loadDotEnvFiles } = require('./dot-env')
const { tryLoadDotEnvFiles } = require('./dot-env')

const warn = sinon.stub()

test('should return an object with empty array for a site with no .env file', async (t) => {
test('should return an empty array for a site with no .env file', async (t) => {
await withSiteBuilder('site-without-env-file', async (builder) => {
await builder.buildAsync()

const results = await loadDotEnvFiles({ projectDir: builder.directory, warn })
const results = await tryLoadDotEnvFiles({ projectDir: builder.directory })
t.deepEqual(results, [])
})
})
Expand All @@ -27,7 +24,7 @@ test('should read env vars from .env file', async (t) => {
})
await builder.buildAsync()

const results = await loadDotEnvFiles({ projectDir: builder.directory, warn })
const results = await tryLoadDotEnvFiles({ projectDir: builder.directory })
t.deepEqual(results, [{ file: '.env', env: { TEST: 'FROM_ENV' } }])
})
})
Expand All @@ -41,7 +38,7 @@ test('should read env vars from .env.development file', async (t) => {
})
await builder.buildAsync()

const results = await loadDotEnvFiles({ projectDir: builder.directory, warn })
const results = await tryLoadDotEnvFiles({ projectDir: builder.directory })
t.deepEqual(results, [{ file: '.env.development', env: { TEST: 'FROM_DEVELOPMENT_ENV' } }])
})
})
Expand All @@ -60,7 +57,7 @@ test('should read from both .env.development and .env', async (t) => {
})
await builder.buildAsync()

const results = await loadDotEnvFiles({ projectDir: builder.directory, warn })
const results = await tryLoadDotEnvFiles({ projectDir: builder.directory })
t.deepEqual(results, [
{ file: '.env', env: { ONE: 'FROM_ENV', TWO: 'FROM_ENV' } },
{ file: '.env.development', env: { ONE: 'FROM_DEVELOPMENT_ENV', THREE: 'FROM_DEVELOPMENT_ENV' } },
Expand All @@ -76,7 +73,7 @@ test('should handle empty .env file', async (t) => {

await builder.buildAsync()

const results = await loadDotEnvFiles({ projectDir: builder.directory, warn })
const results = await tryLoadDotEnvFiles({ projectDir: builder.directory })
t.deepEqual(results, [{ file: '.env', env: {} }])
})
})

1 comment on commit d929073

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📊 Benchmark results

Package size: 352 MB

Please sign in to comment.