Skip to content

Commit ea0604d

Browse files
Alexander Bakertheskumar
authored andcommitted
Add new line when updating key with "set_key" (Alternative to PR#103) (theskumar#123)
* added new line when updating value for a key * updated tests for test_set_key * updated tests to ignore whitespace at beginning and end of file
1 parent 7e73a89 commit ea0604d

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

dotenv/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def set_key(dotenv_path, key_to_set, value_to_set, quote_mode="always"):
143143
k, v = parse_line(line)
144144
if k == key_to_set:
145145
replaced = True
146-
line = line_out
146+
line = "{}\n".format(line_out)
147147
print(line, end='')
148148

149149
if not replaced:

tests/test_cli.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,19 @@ def test_get_key():
2424

2525

2626
def test_set_key(dotenv_file):
27-
success, key_to_set, value_to_set = dotenv.set_key(dotenv_path, 'HELLO', 'WORLD')
28-
success, key_to_set, value_to_set = dotenv.set_key(dotenv_path, 'foo', 'bar')
29-
dotenv.get_key(dotenv_path, 'HELLO') == 'WORLD'
27+
success, key_to_set, value_to_set = dotenv.set_key(dotenv_file, 'HELLO', 'WORLD')
28+
success, key_to_set, value_to_set = dotenv.set_key(dotenv_file, 'foo', 'bar')
29+
assert dotenv.get_key(dotenv_file, 'HELLO') == 'WORLD'
30+
31+
with open(dotenv_file, 'r') as fp:
32+
assert 'HELLO="WORLD"\nfoo="bar"' == fp.read().strip()
33+
34+
success, key_to_set, value_to_set = dotenv.set_key(dotenv_file, 'HELLO', 'WORLD 2')
35+
assert dotenv.get_key(dotenv_file, 'HELLO') == 'WORLD 2'
36+
assert dotenv.get_key(dotenv_file, 'foo') == 'bar'
3037

31-
success, key_to_set, value_to_set = dotenv.set_key(dotenv_path, 'HELLO', 'WORLD 2')
32-
dotenv.get_key(dotenv_path, 'HELLO') == 'WORLD 2'
33-
dotenv.get_key(dotenv_path, 'foo') == 'bar'
38+
with open(dotenv_file, 'r') as fp:
39+
assert 'HELLO="WORLD 2"\nfoo="bar"' == fp.read().strip()
3440

3541

3642
def test_list(cli, dotenv_file):

0 commit comments

Comments
 (0)