From fc730578bbcdc610dc624027e05ef62f9178cb31 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Thu, 23 Apr 2020 10:36:48 -0500 Subject: [PATCH] Add tests for dotenv-expand behavior --- packages/next/lib/load-env-config.ts | 8 +++-- test/integration/env-config/app/.env | 5 ++- .../env-config/app/pages/api/all.js | 3 ++ .../integration/env-config/app/pages/index.js | 3 ++ .../env-config/app/pages/some-ssg.js | 3 ++ .../env-config/app/pages/some-ssp.js | 3 ++ test/integration/env-config/app/pages/ssp.js | 35 ------------------- .../integration/env-config/test/index.test.js | 3 ++ 8 files changed, 25 insertions(+), 38 deletions(-) delete mode 100644 test/integration/env-config/app/pages/ssp.js diff --git a/packages/next/lib/load-env-config.ts b/packages/next/lib/load-env-config.ts index 9400ef7d07e65..7023099eab377 100644 --- a/packages/next/lib/load-env-config.ts +++ b/packages/next/lib/load-env-config.ts @@ -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 @@ -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 diff --git a/test/integration/env-config/app/.env b/test/integration/env-config/app/.env index edeb6e1217c31..ea9c02f8cce67 100644 --- a/test/integration/env-config/app/.env +++ b/test/integration/env-config/app/.env @@ -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 \ No newline at end of file +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 \ No newline at end of file diff --git a/test/integration/env-config/app/pages/api/all.js b/test/integration/env-config/app/pages/api/all.js index f52313245ac34..a80843c7720f9 100644 --- a/test/integration/env-config/app/pages/api/all.js +++ b/test/integration/env-config/app/pages/api/all.js @@ -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 = {} diff --git a/test/integration/env-config/app/pages/index.js b/test/integration/env-config/app/pages/index.js index 137c507e90755..f087c9c363fe4 100644 --- a/test/integration/env-config/app/pages/index.js +++ b/test/integration/env-config/app/pages/index.js @@ -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() { diff --git a/test/integration/env-config/app/pages/some-ssg.js b/test/integration/env-config/app/pages/some-ssg.js index 137c507e90755..f087c9c363fe4 100644 --- a/test/integration/env-config/app/pages/some-ssg.js +++ b/test/integration/env-config/app/pages/some-ssg.js @@ -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() { diff --git a/test/integration/env-config/app/pages/some-ssp.js b/test/integration/env-config/app/pages/some-ssp.js index e314d452aeb27..03e75f4f7d889 100644 --- a/test/integration/env-config/app/pages/some-ssp.js +++ b/test/integration/env-config/app/pages/some-ssp.js @@ -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() { diff --git a/test/integration/env-config/app/pages/ssp.js b/test/integration/env-config/app/pages/ssp.js deleted file mode 100644 index 79ac4b006bc86..0000000000000 --- a/test/integration/env-config/app/pages/ssp.js +++ /dev/null @@ -1,35 +0,0 @@ -const variables = [ - 'PROCESS_ENV_KEY', - 'ENV_FILE_KEY', - 'LOCAL_ENV_FILE_KEY', - 'ENV_FILE_LOCAL_OVERRIDE_TEST', - 'PRODUCTION_ENV_FILE_KEY', - 'LOCAL_PRODUCTION_ENV_FILE_KEY', - 'DEVELOPMENT_ENV_FILE_KEY', - 'LOCAL_DEVELOPMENT_ENV_FILE_KEY', - 'ENV_FILE_DEVELOPMENT_OVERRIDE_TEST', - 'ENV_FILE_DEVELOPMENT_LOCAL_OVERRIDEOVERRIDE_TEST', - 'ENV_FILE_PRODUCTION_OVERRIDEOVERRIDE_TEST', - 'ENV_FILE_PRODUCTION_LOCAL_OVERRIDEOVERRIDE_TEST', - 'TEST_ENV_FILE_KEY', - 'LOCAL_TEST_ENV_FILE_KEY', - 'ENV_FILE_TEST_OVERRIDE_TEST', - 'ENV_FILE_TEST_LOCAL_OVERRIDEOVERRIDE_TEST', -] - -export async function getServerSideProps() { - const items = {} - - variables.forEach(variable => { - if (process.env[variable]) { - items[variable] = process.env[variable] - } - }) - return { - // Do not pass any sensitive values here as they will - // be made PUBLICLY available in `pageProps` - props: { env: items }, - } -} - -export default ({ env }) =>

{JSON.stringify(env)}

diff --git a/test/integration/env-config/test/index.test.js b/test/integration/env-config/test/index.test.js index 3231cf7ee59e4..de0f1145d6840 100644 --- a/test/integration/env-config/test/index.test.js +++ b/test/integration/env-config/test/index.test.js @@ -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 () => {