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

Add tests for dotenv-expand behavior #12140

Merged
merged 1 commit into from
Apr 23, 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
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