Skip to content

Commit

Permalink
Add tests for dotenv-expand behavior (#12140)
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk authored Apr 23, 2020
1 parent 29f86d9 commit 0a3b64d
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 38 deletions.
8 changes: 6 additions & 2 deletions packages/next/lib/load-env-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ const packageJsonHasDep = (packageJsonPath: string, dep: string): boolean => {
return allPackages.some(pkg => pkg === dep)
}

let combinedEnv: Env | undefined = undefined

export function loadEnvConfig(dir: string, dev?: boolean): Env | false {
if (combinedEnv) return combinedEnv

const packageJson = findUp.sync('package.json', { cwd: dir })

// only do new env loading if dotenv isn't installed since we
Expand Down Expand Up @@ -61,9 +65,9 @@ export function loadEnvConfig(dir: string, dev?: boolean): Env | false {
'.env',
].filter(Boolean) as string[]

const combinedEnv: Env = {
combinedEnv = {
...(process.env as any),
}
} as Env

for (const envFile of dotenvFiles) {
// only load .env if the user provided has an env config file
Expand Down
5 changes: 4 additions & 1 deletion test/integration/env-config/app/.env
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ ENV_FILE_PRODUCTION_OVERRIDEOVERRIDE_TEST=env
ENV_FILE_PRODUCTION_LOCAL_OVERRIDEOVERRIDE_TEST=env
ENV_FILE_TEST_OVERRIDE_TEST=env
ENV_FILE_TEST_LOCAL_OVERRIDEOVERRIDE_TEST=env
NEXT_PUBLIC_TEST_DEST=another
NEXT_PUBLIC_TEST_DEST=another
ENV_FILE_EXPANDED=$ENV_FILE_KEY
ENV_FILE_EXPANDED_CONCAT=hello-${ENV_FILE_KEY}
ENV_FILE_EXPANDED_ESCAPED=\$ENV_FILE_KEY
3 changes: 3 additions & 0 deletions test/integration/env-config/app/pages/api/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ const variables = [
'LOCAL_TEST_ENV_FILE_KEY',
'ENV_FILE_TEST_OVERRIDE_TEST',
'ENV_FILE_TEST_LOCAL_OVERRIDEOVERRIDE_TEST',
'ENV_FILE_EXPANDED',
'ENV_FILE_EXPANDED_CONCAT',
'ENV_FILE_EXPANDED_ESCAPED',
]

const items = {}
Expand Down
3 changes: 3 additions & 0 deletions test/integration/env-config/app/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ const variables = [
'LOCAL_TEST_ENV_FILE_KEY',
'ENV_FILE_TEST_OVERRIDE_TEST',
'ENV_FILE_TEST_LOCAL_OVERRIDEOVERRIDE_TEST',
'ENV_FILE_EXPANDED',
'ENV_FILE_EXPANDED_CONCAT',
'ENV_FILE_EXPANDED_ESCAPED',
]

export async function getStaticProps() {
Expand Down
3 changes: 3 additions & 0 deletions test/integration/env-config/app/pages/some-ssg.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ const variables = [
'LOCAL_TEST_ENV_FILE_KEY',
'ENV_FILE_TEST_OVERRIDE_TEST',
'ENV_FILE_TEST_LOCAL_OVERRIDEOVERRIDE_TEST',
'ENV_FILE_EXPANDED',
'ENV_FILE_EXPANDED_CONCAT',
'ENV_FILE_EXPANDED_ESCAPED',
]

export async function getStaticProps() {
Expand Down
3 changes: 3 additions & 0 deletions test/integration/env-config/app/pages/some-ssp.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ const variables = [
'LOCAL_TEST_ENV_FILE_KEY',
'ENV_FILE_TEST_OVERRIDE_TEST',
'ENV_FILE_TEST_LOCAL_OVERRIDEOVERRIDE_TEST',
'ENV_FILE_EXPANDED',
'ENV_FILE_EXPANDED_CONCAT',
'ENV_FILE_EXPANDED_ESCAPED',
]

export async function getServerSideProps() {
Expand Down
35 changes: 0 additions & 35 deletions test/integration/env-config/app/pages/ssp.js

This file was deleted.

3 changes: 3 additions & 0 deletions test/integration/env-config/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ const runTests = (mode = 'dev') => {
expect(data.LOCAL_PRODUCTION_ENV_FILE_KEY).toBe(
isDev ? undefined : 'localproduction'
)
expect(data.ENV_FILE_EXPANDED).toBe('env')
expect(data.ENV_FILE_EXPANDED_CONCAT).toBe('hello-env')
expect(data.ENV_FILE_EXPANDED_ESCAPED).toBe('$ENV_FILE_KEY')
}

it('should have process environment override .env', async () => {
Expand Down

0 comments on commit 0a3b64d

Please sign in to comment.