Skip to content

Commit

Permalink
Support for variables with a literal dot in their name. (#25)
Browse files Browse the repository at this point in the history
* allow dot

* actually support dots

* added a test to check that an error is raised when using a dot as a first character of a key.
  • Loading branch information
omerbenamram authored and ZoeyR committed Jul 31, 2019
1 parent 98b0ca9 commit c41d4c9
Showing 1 changed file with 35 additions and 6 deletions.
41 changes: 35 additions & 6 deletions dotenv/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ pub fn parse_line(line: &str, mut substitution_data: &mut HashMap<String, Option
^(
\s*
(
\#.*| # A comment, or...
\s*| # ...an empty string, or...
(export\s+)? # ...(optionally preceded by "export")...
(?P<key>[A-Za-z_][A-Za-z0-9_]*) # ...a key,...
= # ...then an equal sign,...
(?P<value>.+?)? # ...and then its corresponding value.
\#.*| # A comment, or...
\s*| # ...an empty string, or...
(export\s+)? # ...(optionally preceded by "export")...
(?P<key>[A-Za-z_][A-Za-z0-9_.]*) # ...a key,...
= # ...then an equal sign,...
(?P<value>.+?)? # ...and then its corresponding value.
)\s*
)
[\r\n]*
Expand Down Expand Up @@ -396,6 +396,19 @@ mod variable_substitution_tests {
);
}

#[test]
fn with_dot() {
assert_parsed_string(
r#"
KEY.Value=VALUE
"#,
vec![
("KEY.Value", "VALUE"),
],
);
}


#[test]
fn recursive_substitution() {
assert_parsed_string(
Expand Down Expand Up @@ -515,6 +528,22 @@ mod error_tests {
}
}

#[test]
fn should_not_allow_dot_as_first_character_of_key() {
let wrong_key_value = ".Key=VALUE";

let parsed_values: Vec<_> = Iter::new(wrong_key_value.as_bytes()).collect();

assert_eq!(parsed_values.len(), 1);

if let Err(LineParse(second_value, index)) = &parsed_values[0] {
assert_eq!(second_value, wrong_key_value);
assert_eq!(*index, 0)
} else {
assert!(false, "Expected the second value not to be parsed")
}
}

#[test]
fn should_not_parse_illegal_format() {
let wrong_format = r"<><><>";
Expand Down

0 comments on commit c41d4c9

Please sign in to comment.