Skip to content

Commit ab268f0

Browse files
author
Saurabh Kumar
committed
Add test for key/value stripping
closes theskumar#29
1 parent 04a343a commit ab268f0

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

README.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,11 @@ would not have been possible without the support of these `awesome people <https
212212
Changelog
213213
=========
214214

215+
dev
216+
----------
217+
- Handle escaped charaters and newlines in quoted values
218+
- Remove any spaces around unquoted key/value
219+
215220
0.5.1
216221
----------
217222
- Fix `find_dotenv` - it now start search from the file where this function is called from.

dotenv/main.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# -*- coding: utf-8 -*-
2+
from __future__ import absolute_import
3+
24
import os
35
import sys
46
import warnings
@@ -89,8 +91,10 @@ def parse_dotenv(dotenv_path):
8991
if not line or line.startswith('#') or '=' not in line:
9092
continue
9193
k, v = line.split('=', 1)
92-
k = k.strip()
93-
v = v.strip()
94+
95+
# Remove any leading and trailing spaces in key, value
96+
k, v = k.strip(), v.strip()
97+
9498
if len(v) > 0:
9599
quoted = v[0] == v[len(v) - 1] == '"'
96100

tests/test_cli.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@ def test_get_key():
2222
assert success is None
2323

2424

25+
def test_key_value_without_quotes():
26+
with open(dotenv_path, 'w') as f:
27+
f.write("TEST = value \n")
28+
assert dotenv.get_key(dotenv_path, 'TEST') == "value"
29+
sh.rm(dotenv_path)
30+
31+
with open(dotenv_path, 'w') as f:
32+
f.write('TEST = " with spaces " \n')
33+
assert dotenv.get_key(dotenv_path, 'TEST') == " with spaces "
34+
sh.rm(dotenv_path)
35+
36+
2537
def test_unset():
2638
sh.touch(dotenv_path)
2739
success, key_to_set, value_to_set = dotenv.set_key(dotenv_path, 'HELLO', 'WORLD')

0 commit comments

Comments
 (0)