Skip to content

Commit 5b0188f

Browse files
Flimmtheskumar
authored andcommitted
Fix issue with naive single quotes (theskumar#52)
* Make it clearer what --quote does * Add test * Parse single quotes
1 parent 9552db8 commit 5b0188f

4 files changed

Lines changed: 23 additions & 3 deletions

File tree

README.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ update your settings on remote server, handy isn't it!
126126
file in current working directory.
127127
-q, --quote [always|never|auto]
128128
Whether to quote or not the variable values.
129-
Default mode is always.
129+
Default mode is always. This does not affect
130+
parsing.
130131
--help Show this message and exit.
131132

132133
Commands:

dotenv/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
help="Location of the .env file, defaults to .env file in current working directory.")
1212
@click.option('-q', '--quote', default='always',
1313
type=click.Choice(['always', 'never', 'auto']),
14-
help="Whether to quote or not the variable values. Default mode is always.")
14+
help="Whether to quote or not the variable values. Default mode is always. This does not affect parsing.")
1515
@click.pass_context
1616
def cli(ctx, file, quote):
1717
'''This script is used to set, get or unset values from a .env file.'''

dotenv/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def parse_dotenv(dotenv_path):
103103
k, v = k.strip(), v.strip()
104104

105105
if len(v) > 0:
106-
quoted = v[0] == v[len(v) - 1] == '"'
106+
quoted = v[0] == v[len(v) - 1] in ['"', "'"]
107107

108108
if quoted:
109109
v = decode_escaped(v[1:-1])

tests/test_cli.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@ def test_key_value_without_quotes():
4646
sh.rm(dotenv_path)
4747

4848

49+
def test_value_with_quotes():
50+
with open(dotenv_path, 'w') as f:
51+
f.write('TEST="two words"\n')
52+
assert dotenv.get_key(dotenv_path, 'TEST') == 'two words'
53+
sh.rm(dotenv_path)
54+
55+
with open(dotenv_path, 'w') as f:
56+
f.write("TEST='two words'\n")
57+
assert dotenv.get_key(dotenv_path, 'TEST') == 'two words'
58+
sh.rm(dotenv_path)
59+
60+
4961
def test_unset():
5062
sh.touch(dotenv_path)
5163
success, key_to_set, value_to_set = dotenv.set_key(dotenv_path, 'HELLO', 'WORLD')
@@ -104,6 +116,13 @@ def test_get_key_with_interpolation(cli):
104116
dotenv.set_key(dotenv_path, 'FOO', '${HELLO}')
105117
dotenv.set_key(dotenv_path, 'BAR', 'CONCATENATED_${HELLO}_POSIX_VAR')
106118

119+
lines = list(open(dotenv_path, "r").readlines())
120+
assert lines == [
121+
'HELLO="WORLD"\n',
122+
'FOO="${HELLO}"\n',
123+
'BAR="CONCATENATED_${HELLO}_POSIX_VAR"\n',
124+
]
125+
107126
# test replace from variable in file
108127
stored_value = dotenv.get_key(dotenv_path, 'FOO')
109128
assert stored_value == 'WORLD'

0 commit comments

Comments
 (0)