mirror of
https://github.com/denoland/deno.git
synced 2024-11-21 15:04:11 -05:00
fix: --env
flag confusing message on syntax error (#23915)
Enhanced warning message for --env flag with run and eval subcommands. The commit is specifically made to address issue #23674 by improving the warning messages that appear when using the --env flag with run or eval subcommands in the following scenarios: 1. Missing environment file. 2. Incorrect syntax in the environment file content. **Changes made** - Distinguishes between cases of missing environment file and wrong syntax in the environment file content. - Shows a concise warning message to convey the case/issue occurred. **Code changes & enhancements** - Implemented a match statement to handle different types of errors received while getting and parsing the file content to display a concise warning message, rather than simple error check and then displaying the same warning message for whatever the type of error is. - Updated the related existing tests to reflect the new warning messages. - Added two test cases to cover the wrong environment file content syntax with both run and eval subcommands. **Impact** The use of --env flag with both run/eval would be more user-friendly as it gives a precise description of what is not right when using incorrectly. If you could give it a look, @dsherret , I appreciate your feedback on these changes. --------- Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
This commit is contained in:
parent
8806eac634
commit
e44c538f37
9 changed files with 34 additions and 9 deletions
|
@ -857,13 +857,17 @@ impl CliOptions {
|
|||
};
|
||||
|
||||
if let Some(env_file_name) = &flags.env_file {
|
||||
if (from_filename(env_file_name)).is_err() {
|
||||
log::info!(
|
||||
"{} The `--env` flag was used, but the dotenv file '{}' was not found.",
|
||||
colors::yellow("Warning"),
|
||||
env_file_name
|
||||
);
|
||||
}
|
||||
match from_filename(env_file_name) {
|
||||
Ok(_) => (),
|
||||
Err(error) => {
|
||||
match error {
|
||||
dotenvy::Error::LineParse(line, index)=> log::info!("{} Parsing failed within the specified environment file: {} at index: {} of the value: {}",colors::yellow("Warning"), env_file_name, index, line),
|
||||
dotenvy::Error::Io(_)=> log::info!("{} The `--env` flag was used, but the environment file specified '{}' was not found.",colors::yellow("Warning"),env_file_name),
|
||||
dotenvy::Error::EnvVar(_)=>log::info!("{} One or more of the environment variables isn't present or not unicode within the specified environment file: {}",colors::yellow("Warning"),env_file_name),
|
||||
_ => log::info!("{} Unknown failure occurred with the specified environment file: {}", colors::yellow("Warning"), env_file_name),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let disable_deprecated_api_warning = flags.log_level
|
||||
|
|
4
tests/specs/eval/env_unparsable_file/__test__.jsonc
Normal file
4
tests/specs/eval/env_unparsable_file/__test__.jsonc
Normal file
|
@ -0,0 +1,4 @@
|
|||
{
|
||||
"args": "eval --env=../../../testdata/env_unparsable console.log(Deno.env.get(\"Another_FOO\"))",
|
||||
"output": "main.out"
|
||||
}
|
2
tests/specs/eval/env_unparsable_file/main.out
Normal file
2
tests/specs/eval/env_unparsable_file/main.out
Normal file
|
@ -0,0 +1,2 @@
|
|||
Warning Parsing failed within the specified environment file: ../../../testdata/env_unparsable at index: 3 of the value: c:\path
|
||||
undefined
|
4
tests/specs/run/env_unparsable_file/__test__.jsonc
Normal file
4
tests/specs/run/env_unparsable_file/__test__.jsonc
Normal file
|
@ -0,0 +1,4 @@
|
|||
{
|
||||
"args": "run --env=../../../testdata/env_unparsable --allow-env main.js",
|
||||
"output": "main.out"
|
||||
}
|
3
tests/specs/run/env_unparsable_file/main.js
Normal file
3
tests/specs/run/env_unparsable_file/main.js
Normal file
|
@ -0,0 +1,3 @@
|
|||
console.log(Deno.env.get("FOO"));
|
||||
console.log(Deno.env.get("ANOTHER_FOO"));
|
||||
console.log(Deno.env.get("MULTILINE"));
|
4
tests/specs/run/env_unparsable_file/main.out
Normal file
4
tests/specs/run/env_unparsable_file/main.out
Normal file
|
@ -0,0 +1,4 @@
|
|||
Warning Parsing failed within the specified environment file: ../../../testdata/env_unparsable at index: 3 of the value: c:\path
|
||||
valid
|
||||
undefined
|
||||
undefined
|
4
tests/testdata/env_unparsable
vendored
Normal file
4
tests/testdata/env_unparsable
vendored
Normal file
|
@ -0,0 +1,4 @@
|
|||
FOO=valid
|
||||
ANOTHER_FOO=c:\path
|
||||
MULTILINE="First Line
|
||||
Second Line"
|
2
tests/testdata/eval/env_file_missing.out
vendored
2
tests/testdata/eval/env_file_missing.out
vendored
|
@ -1,2 +1,2 @@
|
|||
Warning The `--env` flag was used, but the dotenv file 'missing' was not found.
|
||||
Warning The `--env` flag was used, but the environment file specified 'missing' was not found.
|
||||
undefined
|
||||
|
|
2
tests/testdata/run/env_file_missing.out
vendored
2
tests/testdata/run/env_file_missing.out
vendored
|
@ -1,4 +1,4 @@
|
|||
Warning The `--env` flag was used, but the dotenv file 'missing' was not found.
|
||||
Warning The `--env` flag was used, but the environment file specified 'missing' was not found.
|
||||
undefined
|
||||
undefined
|
||||
undefined
|
||||
|
|
Loading…
Reference in a new issue