-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose parse errors via
Dotenv::iter
.
Refactor `dotenv::dotenv::Iter` to expose parse errors for those interested. The parse errors are structured as opaque strings to hide the parsing implementation details (currently nom and nom's errors are the internal currency). This allows a consumer that cares about propagating errors in `.env` files to use something like: ``` let env = dotenv::from_filename(".env")?; let mut iter = env.iter(); while let Some((key, value)) = iter.try_next()? { if std::env::var(key).is_err() { std::env::set_var(key, value); } } ``` Fixes #4
- Loading branch information
Showing
8 changed files
with
105 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
mod fixtures; | ||
use fixtures::*; | ||
|
||
#[test] | ||
fn test_propagate_env_parse_errors() -> anyhow::Result<()> { | ||
let (_t, mut exps) = with_basic_dotenv()?; | ||
|
||
// This is an example of how a consumer that cares about invalid `.env` files can handle them using the | ||
// `Iter::try_next` API (see: https://github.com/arniu/dotenvs-rs/issues/4) | ||
let env = dotenv::from_filename(".env")?; | ||
let mut iter = env.iter(); | ||
while let Some((key, value)) = iter.try_next()? { | ||
let expected = exps.remove(key).unwrap(); | ||
assert_eq!(expected, value, "check {}", key); | ||
} | ||
assert!(exps.is_empty()); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use dotenv::Error; | ||
use std::collections::HashMap; | ||
use std::iter::{IntoIterator, Iterator}; | ||
|
||
const BAD_ENV: &str = r#" | ||
A=foo bar | ||
B="notenough | ||
C='toomany'' | ||
D=valid | ||
export NOT_SET | ||
E=valid | ||
"#; | ||
|
||
#[test] | ||
fn test_bad_env() -> anyhow::Result<()> { | ||
let env = dotenv::from_read(BAD_ENV.as_bytes())?; | ||
|
||
assert_eq!( | ||
vec![ | ||
("A", "foo bar".into()), | ||
("B", "\"notenough".into()), | ||
("C", "toomany".into()) | ||
] | ||
.into_iter() | ||
.collect::<HashMap<_, _>>(), | ||
env.iter().collect::<HashMap<_, _>>() | ||
); | ||
|
||
let mut iter = env.iter(); | ||
assert_eq!(Some(("A", "foo bar".into())), iter.try_next()?); | ||
assert_eq!(Some(("B", "\"notenough".into())), iter.try_next()?); | ||
assert_eq!(Some(("C", "toomany".into())), iter.try_next()?); | ||
|
||
// TODO: Use assert_matches! when it stabilizes: https://github.com/rust-lang/rust/issues/82775 | ||
match iter.try_next().unwrap_err() { | ||
Error::Parse(err) => assert_eq!( | ||
"Parsing Error: Error { input: \"'\\nD=valid\\nexport NOT_SET\\nE=valid\\n\", code: Tag }", | ||
err | ||
), | ||
err => panic!("Unexpected error variant: {err:?}", err = err), | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters