From cff6e280c77afb0bc42a10348eeef5360db8f361 Mon Sep 17 00:00:00 2001 From: Bhuwan Pandit Date: Sun, 17 Nov 2024 22:49:35 +0000 Subject: feat(cli): support multiple env file argument (#26527) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes #26425 ## Overview This PR adds support for specifying multiple environment files as arguments when using the Deno CLI. Subsequent files override pre-existing variables defined in previous files. If the same variable is defined in the environment and in the file, the value from the environment takes precedence. ## Example Usage ```bash deno run --allow-env --env-file --env-file=".env.one" --env-file=".env.two" script.ts ``` --------- Co-authored-by: Bartek IwaƄczuk --- tests/integration/run_tests.rs | 10 ---------- tests/specs/run/env_file/__test__.jsonc | 20 ++++++++++++++++++++ tests/specs/run/env_file/env | 4 ++++ tests/specs/run/env_file/env_file.out | 4 ++++ tests/specs/run/env_file/env_file.ts | 3 +++ tests/specs/run/env_file/env_file_missing.out | 4 ++++ tests/specs/run/env_file/env_one | 2 ++ tests/specs/run/env_file/env_two | 1 + tests/specs/run/env_file/env_unparseable | 4 ++++ tests/specs/run/env_file/env_unparseable.out | 4 ++++ tests/specs/run/env_file/multiple_env_file.out | 4 ++++ tests/specs/run/env_unparsable_file/__test__.jsonc | 4 ---- tests/specs/run/env_unparsable_file/main.js | 3 --- tests/specs/run/env_unparsable_file/main.out | 4 ---- tests/testdata/env_unparsable | 4 ---- tests/testdata/run/env_file.out | 4 ---- tests/testdata/run/env_file.ts | 3 --- tests/testdata/run/env_file_missing.out | 4 ---- 18 files changed, 50 insertions(+), 36 deletions(-) create mode 100644 tests/specs/run/env_file/__test__.jsonc create mode 100644 tests/specs/run/env_file/env create mode 100644 tests/specs/run/env_file/env_file.out create mode 100644 tests/specs/run/env_file/env_file.ts create mode 100644 tests/specs/run/env_file/env_file_missing.out create mode 100644 tests/specs/run/env_file/env_one create mode 100644 tests/specs/run/env_file/env_two create mode 100644 tests/specs/run/env_file/env_unparseable create mode 100644 tests/specs/run/env_file/env_unparseable.out create mode 100644 tests/specs/run/env_file/multiple_env_file.out delete mode 100644 tests/specs/run/env_unparsable_file/__test__.jsonc delete mode 100644 tests/specs/run/env_unparsable_file/main.js delete mode 100644 tests/specs/run/env_unparsable_file/main.out delete mode 100644 tests/testdata/env_unparsable delete mode 100644 tests/testdata/run/env_file.out delete mode 100644 tests/testdata/run/env_file.ts delete mode 100644 tests/testdata/run/env_file_missing.out (limited to 'tests') diff --git a/tests/integration/run_tests.rs b/tests/integration/run_tests.rs index 549b88bac..c97b700c5 100644 --- a/tests/integration/run_tests.rs +++ b/tests/integration/run_tests.rs @@ -418,16 +418,6 @@ fn permissions_cache() { }); } -itest!(env_file { - args: "run --env=env --allow-env run/env_file.ts", - output: "run/env_file.out", -}); - -itest!(env_file_missing { - args: "run --env=missing --allow-env run/env_file.ts", - output: "run/env_file_missing.out", -}); - itest!(lock_write_fetch { args: "run --quiet --allow-import --allow-read --allow-write --allow-env --allow-run run/lock_write_fetch/main.ts", diff --git a/tests/specs/run/env_file/__test__.jsonc b/tests/specs/run/env_file/__test__.jsonc new file mode 100644 index 000000000..642062169 --- /dev/null +++ b/tests/specs/run/env_file/__test__.jsonc @@ -0,0 +1,20 @@ +{ + "tests": { + "basic": { + "args": "run --env=./env --allow-env env_file.ts", + "output": "env_file.out" + }, + "missing": { + "args": "run --env=./missing --allow-env env_file.ts", + "output": "env_file_missing.out" + }, + "multiple": { + "args": "run --env=./env --env=./env_one --env=./env_two --allow-env env_file.ts", + "output": "multiple_env_file.out" + }, + "unparseable": { + "args": "run --env=./env_unparseable --allow-env env_file.ts", + "output": "env_unparseable.out" + } + } +} diff --git a/tests/specs/run/env_file/env b/tests/specs/run/env_file/env new file mode 100644 index 000000000..c41732d30 --- /dev/null +++ b/tests/specs/run/env_file/env @@ -0,0 +1,4 @@ +FOO=BAR +ANOTHER_FOO=ANOTHER_${FOO} +MULTILINE="First Line +Second Line" \ No newline at end of file diff --git a/tests/specs/run/env_file/env_file.out b/tests/specs/run/env_file/env_file.out new file mode 100644 index 000000000..54a0bf25d --- /dev/null +++ b/tests/specs/run/env_file/env_file.out @@ -0,0 +1,4 @@ +BAR +ANOTHER_BAR +First Line +Second Line diff --git a/tests/specs/run/env_file/env_file.ts b/tests/specs/run/env_file/env_file.ts new file mode 100644 index 000000000..48488ce72 --- /dev/null +++ b/tests/specs/run/env_file/env_file.ts @@ -0,0 +1,3 @@ +console.log(Deno.env.get("FOO")); +console.log(Deno.env.get("ANOTHER_FOO")); +console.log(Deno.env.get("MULTILINE")); diff --git a/tests/specs/run/env_file/env_file_missing.out b/tests/specs/run/env_file/env_file_missing.out new file mode 100644 index 000000000..34b2bf810 --- /dev/null +++ b/tests/specs/run/env_file/env_file_missing.out @@ -0,0 +1,4 @@ +Warning The `--env-file` flag was used, but the environment file specified './missing' was not found. +undefined +undefined +undefined diff --git a/tests/specs/run/env_file/env_one b/tests/specs/run/env_file/env_one new file mode 100644 index 000000000..c26038a67 --- /dev/null +++ b/tests/specs/run/env_file/env_one @@ -0,0 +1,2 @@ +FOO=BARBAR +ANOTHER_FOO=OVERRIDEN_BY_ENV_ONE diff --git a/tests/specs/run/env_file/env_two b/tests/specs/run/env_file/env_two new file mode 100644 index 000000000..fe8392c3a --- /dev/null +++ b/tests/specs/run/env_file/env_two @@ -0,0 +1 @@ +FOO=OVERRIDEN_BY_ENV_TWO diff --git a/tests/specs/run/env_file/env_unparseable b/tests/specs/run/env_file/env_unparseable new file mode 100644 index 000000000..5542b80bc --- /dev/null +++ b/tests/specs/run/env_file/env_unparseable @@ -0,0 +1,4 @@ +FOO=valid +ANOTHER_FOO=c:\path +MULTILINE="First Line +Second Line" \ No newline at end of file diff --git a/tests/specs/run/env_file/env_unparseable.out b/tests/specs/run/env_file/env_unparseable.out new file mode 100644 index 000000000..0a88d164e --- /dev/null +++ b/tests/specs/run/env_file/env_unparseable.out @@ -0,0 +1,4 @@ +Warning Parsing failed within the specified environment file: ./env_unparseable at index: 3 of the value: c:\path +valid +undefined +undefined diff --git a/tests/specs/run/env_file/multiple_env_file.out b/tests/specs/run/env_file/multiple_env_file.out new file mode 100644 index 000000000..3fa97d599 --- /dev/null +++ b/tests/specs/run/env_file/multiple_env_file.out @@ -0,0 +1,4 @@ +OVERRIDEN_BY_ENV_TWO +OVERRIDEN_BY_ENV_ONE +First Line +Second Line diff --git a/tests/specs/run/env_unparsable_file/__test__.jsonc b/tests/specs/run/env_unparsable_file/__test__.jsonc deleted file mode 100644 index bed150635..000000000 --- a/tests/specs/run/env_unparsable_file/__test__.jsonc +++ /dev/null @@ -1,4 +0,0 @@ -{ - "args": "run --env=../../../testdata/env_unparsable --allow-env main.js", - "output": "main.out" -} diff --git a/tests/specs/run/env_unparsable_file/main.js b/tests/specs/run/env_unparsable_file/main.js deleted file mode 100644 index 48488ce72..000000000 --- a/tests/specs/run/env_unparsable_file/main.js +++ /dev/null @@ -1,3 +0,0 @@ -console.log(Deno.env.get("FOO")); -console.log(Deno.env.get("ANOTHER_FOO")); -console.log(Deno.env.get("MULTILINE")); diff --git a/tests/specs/run/env_unparsable_file/main.out b/tests/specs/run/env_unparsable_file/main.out deleted file mode 100644 index a19ff4dd6..000000000 --- a/tests/specs/run/env_unparsable_file/main.out +++ /dev/null @@ -1,4 +0,0 @@ -Warning Parsing failed within the specified environment file: ../../../testdata/env_unparsable at index: 3 of the value: c:\path -valid -undefined -undefined diff --git a/tests/testdata/env_unparsable b/tests/testdata/env_unparsable deleted file mode 100644 index 5542b80bc..000000000 --- a/tests/testdata/env_unparsable +++ /dev/null @@ -1,4 +0,0 @@ -FOO=valid -ANOTHER_FOO=c:\path -MULTILINE="First Line -Second Line" \ No newline at end of file diff --git a/tests/testdata/run/env_file.out b/tests/testdata/run/env_file.out deleted file mode 100644 index 54a0bf25d..000000000 --- a/tests/testdata/run/env_file.out +++ /dev/null @@ -1,4 +0,0 @@ -BAR -ANOTHER_BAR -First Line -Second Line diff --git a/tests/testdata/run/env_file.ts b/tests/testdata/run/env_file.ts deleted file mode 100644 index 48488ce72..000000000 --- a/tests/testdata/run/env_file.ts +++ /dev/null @@ -1,3 +0,0 @@ -console.log(Deno.env.get("FOO")); -console.log(Deno.env.get("ANOTHER_FOO")); -console.log(Deno.env.get("MULTILINE")); diff --git a/tests/testdata/run/env_file_missing.out b/tests/testdata/run/env_file_missing.out deleted file mode 100644 index f50c1789e..000000000 --- a/tests/testdata/run/env_file_missing.out +++ /dev/null @@ -1,4 +0,0 @@ -Warning The `--env-file` flag was used, but the environment file specified 'missing' was not found. -undefined -undefined -undefined -- cgit v1.2.3