Skip to content

Commit bd9f2c0

Browse files
committed
Remove warning when last line is empty
This also simplifies tests and adds test cases for `set_key`.
1 parent 2f58bcc commit bd9f2c0

File tree

4 files changed

+38
-48
lines changed

4 files changed

+38
-48
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
1616

1717
- Fix escaping of quoted values written by `set_key` (#236 by [@bbc2]).
1818
- Fix `dotenv run` crashing on environment variables without values (#237 by [@yannham]).
19+
- Remove warning when last line is empty (#238 by [@bbc2]).
1920

2021
## [0.11.0] - 2020-02-07
2122

src/dotenv/parser.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,13 @@ def parse_binding(reader):
197197
reader.set_mark()
198198
try:
199199
reader.read_regex(_multiline_whitespace)
200+
if not reader.has_next():
201+
return Binding(
202+
key=None,
203+
value=None,
204+
original=reader.get_marked(),
205+
error=False,
206+
)
200207
reader.read_regex(_export)
201208
key = parse_key(reader)
202209
reader.read_regex(_whitespace)

tests/test_main.py

Lines changed: 17 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -30,63 +30,32 @@ def test_set_key_no_file(tmp_path):
3030

3131

3232
@pytest.mark.parametrize(
33-
"key,value,expected,content",
33+
"before,key,value,expected,after",
3434
[
35-
("a", "", (True, "a", ""), 'a=""\n'),
36-
("a", "b", (True, "a", "b"), 'a="b"\n'),
37-
("a", "'b'", (True, "a", "b"), 'a="b"\n'),
38-
("a", "\"b\"", (True, "a", "b"), 'a="b"\n'),
39-
("a", "b'c", (True, "a", "b'c"), 'a="b\'c"\n'),
40-
("a", "b\"c", (True, "a", "b\"c"), 'a="b\\\"c"\n'),
35+
("", "a", "", (True, "a", ""), 'a=""\n'),
36+
("", "a", "b", (True, "a", "b"), 'a="b"\n'),
37+
("", "a", "'b'", (True, "a", "b"), 'a="b"\n'),
38+
("", "a", "\"b\"", (True, "a", "b"), 'a="b"\n'),
39+
("", "a", "b'c", (True, "a", "b'c"), 'a="b\'c"\n'),
40+
("", "a", "b\"c", (True, "a", "b\"c"), 'a="b\\\"c"\n'),
41+
("a=b", "a", "c", (True, "a", "c"), 'a="c"\n'),
42+
("a=b\n", "a", "c", (True, "a", "c"), 'a="c"\n'),
43+
("a=b\n\n", "a", "c", (True, "a", "c"), 'a="c"\n\n'),
44+
("a=b\nc=d", "a", "e", (True, "a", "e"), 'a="e"\nc=d'),
45+
("a=b\nc=d\ne=f", "c", "g", (True, "c", "g"), 'a=b\nc="g"\ne=f'),
46+
("a=b\n", "c", "d", (True, "c", "d"), 'a=b\nc="d"\n'),
4147
],
4248
)
43-
def test_set_key_new(dotenv_file, key, value, expected, content):
49+
def test_set_key(dotenv_file, before, key, value, expected, after):
4450
logger = logging.getLogger("dotenv.main")
51+
with open(dotenv_file, "w") as f:
52+
f.write(before)
4553

4654
with mock.patch.object(logger, "warning") as mock_warning:
4755
result = dotenv.set_key(dotenv_file, key, value)
4856

4957
assert result == expected
50-
assert open(dotenv_file, "r").read() == content
51-
mock_warning.assert_not_called()
52-
53-
54-
def test_set_key_new_with_other_values(dotenv_file):
55-
logger = logging.getLogger("dotenv.main")
56-
with open(dotenv_file, "w") as f:
57-
f.write("a=b\n")
58-
59-
with mock.patch.object(logger, "warning") as mock_warning:
60-
result = dotenv.set_key(dotenv_file, "foo", "bar")
61-
62-
assert result == (True, "foo", "bar")
63-
assert open(dotenv_file, "r").read() == 'a=b\nfoo="bar"\n'
64-
mock_warning.assert_not_called()
65-
66-
67-
def test_set_key_existing(dotenv_file):
68-
logger = logging.getLogger("dotenv.main")
69-
with open(dotenv_file, "w") as f:
70-
f.write("foo=bar")
71-
72-
with mock.patch.object(logger, "warning") as mock_warning:
73-
result = dotenv.set_key(dotenv_file, "foo", "baz")
74-
75-
assert result == (True, "foo", "baz")
76-
assert open(dotenv_file, "r").read() == 'foo="baz"\n'
77-
mock_warning.assert_not_called()
78-
79-
80-
def test_set_key_existing_with_other_values(dotenv_file):
81-
logger = logging.getLogger("dotenv.main")
82-
with open(dotenv_file, "w") as f:
83-
f.write("a=b\nfoo=bar\nc=d")
84-
85-
with mock.patch.object(logger, "warning") as mock_warning:
86-
result = dotenv.set_key(dotenv_file, "foo", "baz")
87-
88-
assert result == (True, "foo", "baz")
89-
assert open(dotenv_file, "r").read() == 'a=b\nfoo="baz"\nc=d'
58+
assert open(dotenv_file, "r").read() == after
9059
mock_warning.assert_not_called()
9160

9261

tests/test_parser.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,19 @@
8686
Binding(key=u"b", value=u'c', original=Original(string=u"b=c", line=2), error=False),
8787
]
8888
),
89+
(
90+
u"\n\n",
91+
[
92+
Binding(key=None, value=None, original=Original(string=u"\n\n", line=1), error=False),
93+
]
94+
),
95+
(
96+
u"a=b\n\n",
97+
[
98+
Binding(key=u"a", value=u"b", original=Original(string=u"a=b\n", line=1), error=False),
99+
Binding(key=None, value=None, original=Original(string=u"\n", line=2), error=False),
100+
]
101+
),
89102
(
90103
u'a=b\n\nc=d',
91104
[

0 commit comments

Comments
 (0)