Allow spaces before = in dotenv

This commit is contained in:
Dan Halbert 2022-10-05 15:50:53 -04:00
parent 18cee2ee8b
commit 91079279da
4 changed files with 63 additions and 68 deletions

View File

@ -137,7 +137,7 @@ STATIC bool key_matches(file_arg *active_file, const char *key) {
if (character == '=' || character == '\n' || character == '#' || character == 0) {
// Rewind one so the value, if any, can be found.
seek_minus_one(active_file);
} else {
} else if (!unichar_isspace(character)) {
// We're followed by something else that is invalid syntax.
matches = false;
}

View File

@ -1,32 +1,23 @@
# No e0 value
# Key "notpresent" is not present
# comment preceded by spaces
e1=e1value
e2=e2value # value followed by a comment
e3='e3value'
e4='e4value' # quoted value followed by a comment
# e5 should be None
e5
# e6 should be the empty string
e6=
# e7 should be '#' (bash-like syntax processing)
e7=#
# e8 should be the empty string
e8=''
# e9 should be the empty string
e9= #
e10=e10_first
e10=e10_last
e11='abc#def'
# e12 should be 'abc#def'
e12=abc#def
e12='multi
plain_value=value
value_with_comment=value # value followed by a comment
quoted_value='value'
quoted_value_with_comment='value' # quoted value followed by a comment
should_be_none
should_be_empty_string=
should_be_hash=#
quoted_should_be_empty_string=''
duplicate_key=wrong
duplicate_key=right
value_with_hash=value#value
quoted_value_with_hash='value#value'
multi_line_value='multi
line'
e13=e13value
e14 #comment
e15 = e15value
# e16 should be '#'
e16=# #
# e17 should be 'def#hi'
e17='def'#hi
# e18 should be '#has a hash'
e18=#has a hash
space_before_key=value
space_before_value= value
space_before_hash_value= #value
space_after_key =value
space_after_key_before_value = value
quoted_then_comment='value'#comment
hash_with_spaces=#value value

View File

@ -2,22 +2,24 @@ import dotenv
FILE = __file__.rsplit(".", 1)[0] + ".env"
print("e0", dotenv.get_key(FILE, "e0"))
print("e1", dotenv.get_key(FILE, "e1"))
print("e2", dotenv.get_key(FILE, "e2"))
print("e3", dotenv.get_key(FILE, "e3"))
print("e4", dotenv.get_key(FILE, "e4"))
print("e5", dotenv.get_key(FILE, "e5"))
print("e6", dotenv.get_key(FILE, "e6"))
print("e7", dotenv.get_key(FILE, "e7"))
print("e8", dotenv.get_key(FILE, "e8"))
print("e9", dotenv.get_key(FILE, "e9"))
print("e10", dotenv.get_key(FILE, "e10"))
print("e11", dotenv.get_key(FILE, "e11"))
print("e12", dotenv.get_key(FILE, "e12"))
print("e13", dotenv.get_key(FILE, "e13"))
print("e14", dotenv.get_key(FILE, "e14"))
# print("e15", dotenv.get_key(FILE, "e15"))
print("e16", dotenv.get_key(FILE, "e16"))
print("e17", dotenv.get_key(FILE, "e17"))
print("e18", dotenv.get_key(FILE, "e18"))
print(f"notpresent={dotenv.get_key(FILE, 'notpresent')}")
print(f"plain_value={dotenv.get_key(FILE, 'plain_value')}")
print(f"value_with_comment={dotenv.get_key(FILE, 'value_with_comment')}")
print(f"quoted_value={dotenv.get_key(FILE, 'quoted_value')}")
print(f"quoted_value_with_comment={dotenv.get_key(FILE, 'quoted_value_with_comment')}")
print(f"should_be_none={dotenv.get_key(FILE, 'should_be_none')}")
print(f"should_be_empty_string={dotenv.get_key(FILE, 'should_be_empty_string')}")
print(f"should_be_hash={dotenv.get_key(FILE, 'should_be_hash')}")
print(f"quoted_should_be_empty_string={dotenv.get_key(FILE, 'quoted_should_be_empty_string')}")
print(f"duplicate_key={dotenv.get_key(FILE, 'duplicate_key')}")
### This is the a difference from CPython dotenv. The trailing #value is taken as a comment.
print(f"value_with_hash={dotenv.get_key(FILE, 'value_with_hash')}")
print(f"quoted_value_with_hash={dotenv.get_key(FILE, 'quoted_value_with_hash')}")
print(f"multi_line_value={dotenv.get_key(FILE, 'multi_line_value')}")
print(f"space_before_key={dotenv.get_key(FILE, 'space_before_key')}")
print(f"space_before_value={dotenv.get_key(FILE, 'space_before_value')}")
print(f"space_before_hash_value={dotenv.get_key(FILE, 'space_before_hash_value')}")
print(f"space_after_key={dotenv.get_key(FILE, 'space_after_key')}")
print(f"space_after_key_before_value={dotenv.get_key(FILE, 'space_after_key_before_value')}")
print(f"quoted_then_comment={dotenv.get_key(FILE, 'quoted_then_comment')}")
print(f"hash_with_spaces={dotenv.get_key(FILE, 'hash_with_spaces')}")

View File

@ -1,19 +1,21 @@
e0 None
e1 e1value
e2 e2value
e3 e3value
e4 e4value
e5 None
e6
e7 #
e8
e9 #
e10 e10_last
e11 abc#def
e12 multi
notpresent=None
plain_value=value
value_with_comment=value
quoted_value=value
quoted_value_with_comment=value
should_be_none=None
should_be_empty_string=
should_be_hash=#
quoted_should_be_empty_string=
duplicate_key=right
value_with_hash=value
quoted_value_with_hash=value#value
multi_line_value=multi
line
e13 e13value
e14 None
e16 #
e17 def
e18 #has a hash
space_before_key=value
space_before_value=value
space_before_hash_value=#value
space_after_key=value
space_after_key_before_value=value
quoted_then_comment=value
hash_with_spaces=#value value