11import os
2-
3- import pytest
42import sh
3+ from pathlib import Path
54from typing import Optional
5+
6+ import pytest
7+
68import dotenv
79from dotenv .cli import cli as dotenv_cli
810from dotenv .version import __version__
2325 ("export" , "x='a b c'" , '''export x='a b c'\n ''' ),
2426 )
2527)
26- def test_list (cli , dotenv_file , format : Optional [str ], content : str , expected : str ):
27- with open (dotenv_file , "w" ) as f :
28- f .write (content + '\n ' )
28+ def test_list (cli , dotenv_path , format : Optional [str ], content : str , expected : str ):
29+ dotenv_path .write_text (content + '\n ' )
2930
30- args = ['--file' , dotenv_file , 'list' ]
31+ args = ['--file' , dotenv_path , 'list' ]
3132 if format is not None :
3233 args .extend (['--format' , format ])
3334
@@ -56,17 +57,16 @@ def test_list_no_file(cli):
5657 assert (result .exit_code , result .output ) == (1 , "" )
5758
5859
59- def test_get_existing_value (cli , dotenv_file ):
60- with open (dotenv_file , "w" ) as f :
61- f .write ("a=b" )
60+ def test_get_existing_value (cli , dotenv_path ):
61+ dotenv_path .write_text ("a=b" )
6262
63- result = cli .invoke (dotenv_cli , ['--file' , dotenv_file , 'get' , 'a' ])
63+ result = cli .invoke (dotenv_cli , ['--file' , dotenv_path , 'get' , 'a' ])
6464
6565 assert (result .exit_code , result .output ) == (0 , "b\n " )
6666
6767
68- def test_get_non_existent_value (cli , dotenv_file ):
69- result = cli .invoke (dotenv_cli , ['--file' , dotenv_file , 'get' , 'a' ])
68+ def test_get_non_existent_value (cli , dotenv_path ):
69+ result = cli .invoke (dotenv_cli , ['--file' , dotenv_path , 'get' , 'a' ])
7070
7171 assert (result .exit_code , result .output ) == (1 , "" )
7272
@@ -85,21 +85,20 @@ def test_get_not_a_file(cli):
8585 assert "Error opening env file" in result .output
8686
8787
88- def test_unset_existing_value (cli , dotenv_file ):
89- with open (dotenv_file , "w" ) as f :
90- f .write ("a=b" )
88+ def test_unset_existing_value (cli , dotenv_path ):
89+ dotenv_path .write_text ("a=b" )
9190
92- result = cli .invoke (dotenv_cli , ['--file' , dotenv_file , 'unset' , 'a' ])
91+ result = cli .invoke (dotenv_cli , ['--file' , dotenv_path , 'unset' , 'a' ])
9392
9493 assert (result .exit_code , result .output ) == (0 , "Successfully removed a\n " )
95- assert open ( dotenv_file , "r" ). read () == ""
94+ assert dotenv_path . read_text () == ""
9695
9796
98- def test_unset_non_existent_value (cli , dotenv_file ):
99- result = cli .invoke (dotenv_cli , ['--file' , dotenv_file , 'unset' , 'a' ])
97+ def test_unset_non_existent_value (cli , dotenv_path ):
98+ result = cli .invoke (dotenv_cli , ['--file' , dotenv_path , 'unset' , 'a' ])
10099
101100 assert (result .exit_code , result .output ) == (1 , "" )
102- assert open ( dotenv_file , "r" ). read () == ""
101+ assert dotenv_path . read_text () == ""
103102
104103
105104@pytest .mark .parametrize (
@@ -112,31 +111,31 @@ def test_unset_non_existent_value(cli, dotenv_file):
112111 ("auto" , "a" , "$" , "a='$'\n " ),
113112 )
114113)
115- def test_set_quote_options (cli , dotenv_file , quote_mode , variable , value , expected ):
114+ def test_set_quote_options (cli , dotenv_path , quote_mode , variable , value , expected ):
116115 result = cli .invoke (
117116 dotenv_cli ,
118- ["--file" , dotenv_file , "--export" , "false" , "--quote" , quote_mode , "set" , variable , value ]
117+ ["--file" , dotenv_path , "--export" , "false" , "--quote" , quote_mode , "set" , variable , value ]
119118 )
120119
121120 assert (result .exit_code , result .output ) == (0 , "{}={}\n " .format (variable , value ))
122- assert open ( dotenv_file , "r" ). read () == expected
121+ assert dotenv_path . read_text () == expected
123122
124123
125124@pytest .mark .parametrize (
126- "dotenv_file ,export_mode,variable,value,expected" ,
125+ "dotenv_path ,export_mode,variable,value,expected" ,
127126 (
128- (".nx_file" , "true" , "a" , "x" , "export a='x'\n " ),
129- (".nx_file" , "false" , "a" , "x" , "a='x'\n " ),
127+ (Path ( ".nx_file" ) , "true" , "a" , "x" , "export a='x'\n " ),
128+ (Path ( ".nx_file" ) , "false" , "a" , "x" , "a='x'\n " ),
130129 )
131130)
132- def test_set_export (cli , dotenv_file , export_mode , variable , value , expected ):
131+ def test_set_export (cli , dotenv_path , export_mode , variable , value , expected ):
133132 result = cli .invoke (
134133 dotenv_cli ,
135- ["--file" , dotenv_file , "--quote" , "always" , "--export" , export_mode , "set" , variable , value ]
134+ ["--file" , dotenv_path , "--quote" , "always" , "--export" , export_mode , "set" , variable , value ]
136135 )
137136
138137 assert (result .exit_code , result .output ) == (0 , "{}={}\n " .format (variable , value ))
139- assert open ( dotenv_file , "r" ). read () == expected
138+ assert dotenv_path . read_text () == expected
140139
141140
142141def test_set_non_existent_file (cli ):
@@ -153,31 +152,26 @@ def test_set_no_file(cli):
153152
154153
155154def test_get_default_path (tmp_path ):
156- with sh .pushd (str (tmp_path )):
157- with open (str (tmp_path / ".env" ), "w" ) as f :
158- f .write ("a=b" )
155+ with sh .pushd (tmp_path ):
156+ (tmp_path / ".env" ).write_text ("a=b" )
159157
160158 result = sh .dotenv ("get" , "a" )
161159
162160 assert result == "b\n "
163161
164162
165163def test_run (tmp_path ):
166- with sh .pushd (str (tmp_path )):
167- dotenv_file = str (tmp_path / ".env" )
168- with open (dotenv_file , "w" ) as f :
169- f .write ("a=b" )
164+ with sh .pushd (tmp_path ):
165+ (tmp_path / ".env" ).write_text ("a=b" )
170166
171167 result = sh .dotenv ("run" , "printenv" , "a" )
172168
173169 assert result == "b\n "
174170
175171
176172def test_run_with_existing_variable (tmp_path ):
177- with sh .pushd (str (tmp_path )):
178- dotenv_file = str (tmp_path / ".env" )
179- with open (dotenv_file , "w" ) as f :
180- f .write ("a=b" )
173+ with sh .pushd (tmp_path ):
174+ (tmp_path / ".env" ).write_text ("a=b" )
181175 env = dict (os .environ )
182176 env .update ({"LANG" : "en_US.UTF-8" , "a" : "c" })
183177
@@ -187,10 +181,8 @@ def test_run_with_existing_variable(tmp_path):
187181
188182
189183def test_run_with_existing_variable_not_overridden (tmp_path ):
190- with sh .pushd (str (tmp_path )):
191- dotenv_file = str (tmp_path / ".env" )
192- with open (dotenv_file , "w" ) as f :
193- f .write ("a=b" )
184+ with sh .pushd (tmp_path ):
185+ (tmp_path / ".env" ).write_text ("a=b" )
194186 env = dict (os .environ )
195187 env .update ({"LANG" : "en_US.UTF-8" , "a" : "c" })
196188
@@ -200,21 +192,18 @@ def test_run_with_existing_variable_not_overridden(tmp_path):
200192
201193
202194def test_run_with_none_value (tmp_path ):
203- with sh .pushd (str (tmp_path )):
204- dotenv_file = str (tmp_path / ".env" )
205- with open (dotenv_file , "w" ) as f :
206- f .write ("a=b\n c" )
195+ with sh .pushd (tmp_path ):
196+ (tmp_path / ".env" ).write_text ("a=b\n c" )
207197
208198 result = sh .dotenv ("run" , "printenv" , "a" )
209199
210200 assert result == "b\n "
211201
212202
213- def test_run_with_other_env (dotenv_file ):
214- with open (dotenv_file , "w" ) as f :
215- f .write ("a=b" )
203+ def test_run_with_other_env (dotenv_path ):
204+ dotenv_path .write_text ("a=b" )
216205
217- result = sh .dotenv ("--file" , dotenv_file , "run" , "printenv" , "a" )
206+ result = sh .dotenv ("--file" , dotenv_path , "run" , "printenv" , "a" )
218207
219208 assert result == "b\n "
220209
0 commit comments