@@ -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\n c=d" , "a" , "e" , (True , "a" , "e" ), 'a="e"\n c=d' ),
45+ ("a=b\n c=d\n e=f" , "c" , "g" , (True , "c" , "g" ), 'a=b\n c="g"\n e=f' ),
46+ ("a=b\n " , "c" , "d" , (True , "c" , "d" ), 'a=b\n c="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\n foo="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\n foo=bar\n c=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\n foo="baz"\n c=d'
58+ assert open (dotenv_file , "r" ).read () == after
9059 mock_warning .assert_not_called ()
9160
9261
0 commit comments