Skip to content

Commit bc567e6

Browse files
author
Saurabh Kumar
committed
fix: make the return value of set_key and unset_key consistent
1 parent bdff4ca commit bc567e6

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

dotenv.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def set_key(dotenv_path, key_to_set, value_to_set):
4949
value_to_set = str(value_to_set).strip("'").strip('"')
5050
if not os.path.exists(dotenv_path):
5151
warnings.warn("can't write to %s - it doesn't exist." % dotenv_path)
52-
return None
52+
return None, key_to_set, value_to_set
5353
dotenv_as_dict = OrderedDict(parse_dotenv(dotenv_path))
5454
dotenv_as_dict[key_to_set] = value_to_set
5555
success = flatten_and_write(dotenv_path, dotenv_as_dict)
@@ -66,14 +66,13 @@ def unset_key(dotenv_path, key_to_unset):
6666
key_to_unset = str(key_to_unset)
6767
if not os.path.exists(dotenv_path):
6868
warnings.warn("can't delete from %s - it doesn't exist." % dotenv_path)
69-
return None
69+
return None, key_to_unset
7070
dotenv_as_dict = OrderedDict(parse_dotenv(dotenv_path))
7171
if key_to_unset in dotenv_as_dict:
7272
dotenv_as_dict.pop(key_to_unset, None)
7373
else:
74-
warnings.warn(
75-
"key %s not removed from %s - key doesn't exist." % (key_to_unset, dotenv_path))
76-
return None
74+
warnings.warn("key %s not removed from %s - key doesn't exist." % (key_to_unset, dotenv_path))
75+
return None, key_to_unset
7776
success = flatten_and_write(dotenv_path, dotenv_as_dict)
7877
return success, key_to_unset
7978

tests/test_cli.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,27 @@
1010
dotenv_path = join(here, '.env')
1111

1212

13-
def test_read_write():
13+
def test_get_key():
1414
sh.touch(dotenv_path)
1515
success, key_to_set, value_to_set = dotenv.set_key(dotenv_path, 'HELLO', 'WORLD')
1616
stored_value = dotenv.get_key(dotenv_path, 'HELLO')
1717
assert stored_value == 'WORLD'
1818
sh.rm(dotenv_path)
19+
assert dotenv.get_key(dotenv_path, 'HELLO') is None
20+
success, key_to_set, value_to_set = dotenv.set_key(dotenv_path, 'HELLO', 'WORLD')
21+
assert success is None
22+
23+
24+
def test_unset():
25+
sh.touch(dotenv_path)
26+
success, key_to_set, value_to_set = dotenv.set_key(dotenv_path, 'HELLO', 'WORLD')
27+
stored_value = dotenv.get_key(dotenv_path, 'HELLO')
28+
assert stored_value == 'WORLD'
29+
success, key_to_unset = dotenv.unset_key(dotenv_path, 'HELLO')
30+
assert dotenv.get_key(dotenv_path, 'HELLO') is None
31+
sh.rm(dotenv_path)
32+
success, key_to_unset = dotenv.unset_key(dotenv_path, 'HELLO')
33+
assert success is None
1934

2035

2136
def test_console_script():

0 commit comments

Comments
 (0)