Skip to content

Commit

Permalink
Align the tests with issue #4 better.
Browse files Browse the repository at this point in the history
  • Loading branch information
jsirois committed Nov 4, 2023
1 parent 192c49d commit 88ffef4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
5 changes: 4 additions & 1 deletion tests/test-dotenv-try-next.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ mod fixtures;
use fixtures::*;

#[test]
fn test_sample() -> anyhow::Result<()> {
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()? {
Expand Down
35 changes: 21 additions & 14 deletions tests/test-sample-bad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,39 @@ use dotenv::Error;
use std::collections::HashMap;
use std::iter::{IntoIterator, Iterator};

const BAD_KEY: &str = "
export BASIC=basic
BAD KEY=value
";
const BAD_ENV: &str = r#"
A=foo bar
B="notenough
C='toomany''
D=valid
export NOT_SET
E=valid
"#;

#[test]
fn test_bad_key_space() -> anyhow::Result<()> {
let env = dotenv::from_read(BAD_KEY.as_bytes()).unwrap();
fn test_bad_env() -> anyhow::Result<()> {
let env = dotenv::from_read(BAD_ENV.as_bytes())?;

assert_eq!(
vec![("BASIC", "basic".to_string())]
.into_iter()
.collect::<HashMap<_, _>>(),
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(("BASIC", "basic".to_string())),
iter.try_next().unwrap()
);
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: \"KEY=value\\n\", code: Char }",
"Parsing Error: Error { input: \"'\\nD=valid\\nexport NOT_SET\\nE=valid\\n\", code: Tag }",
err
),
err => panic!("Unexpected error variant: {err:?}", err = err),
Expand Down

0 comments on commit 88ffef4

Please sign in to comment.