From c41d4c91886ffeb0a5153d170c4344a18128ce54 Mon Sep 17 00:00:00 2001 From: Omer BenAmram Date: Wed, 31 Jul 2019 19:59:36 +0300 Subject: [PATCH] Support for variables with a literal dot in their name. (#25) * 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. --- dotenv/src/parse.rs | 41 +++++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/dotenv/src/parse.rs b/dotenv/src/parse.rs index fd80214..15e12f1 100644 --- a/dotenv/src/parse.rs +++ b/dotenv/src/parse.rs @@ -14,12 +14,12 @@ pub fn parse_line(line: &str, mut substitution_data: &mut HashMap[A-Za-z_][A-Za-z0-9_]*) # ...a key,... - = # ...then an equal sign,... - (?P.+?)? # ...and then its corresponding value. + \#.*| # A comment, or... + \s*| # ...an empty string, or... + (export\s+)? # ...(optionally preceded by "export")... + (?P[A-Za-z_][A-Za-z0-9_.]*) # ...a key,... + = # ...then an equal sign,... + (?P.+?)? # ...and then its corresponding value. )\s* ) [\r\n]* @@ -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( @@ -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"<><><>";