@@ -186,21 +186,16 @@ def test_run(tmp_path):
186186 with pushd (tmp_path ):
187187 (tmp_path / ".env" ).write_text ("a=b" )
188188
189- if IS_WINDOWS :
190- # On Windows, use environment variables directly with the Python interpreter
191- # Create a temporary batch file to source the environment and run Python
192- batch_path = tmp_path / "run_test.bat"
193- batch_path .write_text (
194- f"@echo off\n dotenv run { sys .executable } -c \" import os; print(os.environ['a'])\" "
195- )
196-
197- # Run the batch file directly
198- result = run_command (f"{ batch_path } " )
199- else :
200- printenv_cmd = ["dotenv" , "run" , "printenv" , "a" ]
201- result = run_command (printenv_cmd )
189+ printenv_cmd = [
190+ "dotenv" ,
191+ "run" ,
192+ "python" ,
193+ "-c" ,
194+ "import os; print(os.environ['a'])" ,
195+ ]
196+ result = run_command (printenv_cmd )
202197
203- assert result . strip () == "b"
198+ assert result == "b"
204199
205200
206201def test_run_with_existing_variable (tmp_path ):
@@ -209,24 +204,17 @@ def test_run_with_existing_variable(tmp_path):
209204 env = dict (os .environ )
210205 env .update ({"LANG" : "en_US.UTF-8" , "a" : "c" })
211206
212- if IS_WINDOWS :
213- printenv_cmd = [
214- "dotenv" ,
215- "run" ,
216- "cmd" ,
217- "/c" ,
218- "set" ,
219- "a" ,
220- ]
221- else :
222- printenv_cmd = ["dotenv" , "run" , "printenv" , "a" ]
207+ printenv_cmd = [
208+ "dotenv" ,
209+ "run" ,
210+ "--no-override" ,
211+ "python" ,
212+ "-c" ,
213+ "import os; print(os.environ['a'])" ,
214+ ]
223215
224216 result = run_command (printenv_cmd , env = env )
225- if IS_WINDOWS :
226- # Windows 'set' command includes variable name, extract just the value
227- assert result .strip ().endswith ("=b" )
228- else :
229- assert result == "b\n "
217+ assert result == "b"
230218
231219
232220def test_run_with_existing_variable_not_overridden (tmp_path ):
@@ -235,59 +223,50 @@ def test_run_with_existing_variable_not_overridden(tmp_path):
235223 env = dict (os .environ )
236224 env .update ({"LANG" : "en_US.UTF-8" , "a" : "c" })
237225
238- # Use appropriate command for the platform
239- if IS_WINDOWS :
240- printenv_cmd = [
241- "dotenv" ,
242- "run" ,
243- "--no-override" ,
244- "cmd" ,
245- "/c" ,
246- "set" ,
247- "a" ,
248- ]
249- else :
250- printenv_cmd = ["dotenv" , "run" , "--no-override" , "printenv" , "a" ]
226+ printenv_cmd = [
227+ "dotenv" ,
228+ "run" ,
229+ "--no-override" ,
230+ "python" ,
231+ "-c" ,
232+ "import os; print(os.environ['a'])" ,
233+ ]
251234
252235 result = run_command (printenv_cmd , env = env )
253-
254- if IS_WINDOWS :
255- # Windows 'set' command includes variable name, extract just the value
256- assert result .strip ().endswith ("=c" )
257- else :
258- assert result == "c\n "
236+ assert result == "c"
259237
260238
261239def test_run_with_none_value (tmp_path ):
262240 with pushd (tmp_path ):
263241 (tmp_path / ".env" ).write_text ("a=b\n c" )
264- # Use sys.executable to run the command via Python directly
265- if IS_WINDOWS :
266- printenv_cmd = [
267- sys .executable ,
268- "-m" ,
269- "dotenv" ,
270- "run" ,
271- "cmd" ,
272- "/c" ,
273- "set" ,
274- "a" ,
275- ]
276- else :
277- printenv_cmd = ["dotenv" , "run" , "printenv" , "a" ]
242+
243+ printenv_cmd = [
244+ "dotenv" ,
245+ "run" ,
246+ "--no-override" ,
247+ "python" ,
248+ "-c" ,
249+ "import os; print(os.environ['a'])" ,
250+ ]
278251
279252 result = run_command (printenv_cmd )
280- if IS_WINDOWS :
281- # Windows 'set' command includes variable name, extract just the value
282- assert result .strip ().endswith ("=b" )
283- else :
284- assert result == "b\n "
253+ assert result == "b"
285254
286255
287256def test_run_with_other_env (dotenv_path ):
288257 dotenv_path .write_text ("a=b" )
289258
290- result = run_command (["dotenv" , "--file" , dotenv_path , "run" , "printenv" , "a" ])
259+ printenv_cmd = [
260+ "dotenv" ,
261+ "--file" ,
262+ str (dotenv_path ),
263+ "run" ,
264+ "python" ,
265+ "-c" ,
266+ "import os; print(os.environ['a'])" ,
267+ ]
268+
269+ result = run_command (printenv_cmd )
291270
292271 assert result == "b\n "
293272
0 commit comments