11import os
2- import sys
32from pathlib import Path
43from typing import Optional
54
98from dotenv .cli import cli as dotenv_cli
109from dotenv .version import __version__
1110
12- from tests .utils import pushd , run_command , IS_WINDOWS
11+ from tests .utils import pushd , run_command
1312
1413
1514@pytest .mark .parametrize (
@@ -186,21 +185,16 @@ def test_run(tmp_path):
186185 with pushd (tmp_path ):
187186 (tmp_path / ".env" ).write_text ("a=b" )
188187
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 )
188+ printenv_cmd = [
189+ "dotenv" ,
190+ "run" ,
191+ "python" ,
192+ "-c" ,
193+ "import os; print(os.environ['a'])" ,
194+ ]
195+ result = run_command (printenv_cmd )
202196
203- assert result . strip () == "b"
197+ assert result == "b\n "
204198
205199
206200def test_run_with_existing_variable (tmp_path ):
@@ -209,24 +203,17 @@ def test_run_with_existing_variable(tmp_path):
209203 env = dict (os .environ )
210204 env .update ({"LANG" : "en_US.UTF-8" , "a" : "c" })
211205
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" ]
206+ printenv_cmd = [
207+ "dotenv" ,
208+ "run" ,
209+ "--no-override" ,
210+ "python" ,
211+ "-c" ,
212+ "import os; print(os.environ['a'])" ,
213+ ]
223214
224215 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 "
216+ assert result == "c\n "
230217
231218
232219def test_run_with_existing_variable_not_overridden (tmp_path ):
@@ -235,59 +222,50 @@ def test_run_with_existing_variable_not_overridden(tmp_path):
235222 env = dict (os .environ )
236223 env .update ({"LANG" : "en_US.UTF-8" , "a" : "c" })
237224
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" ]
225+ printenv_cmd = [
226+ "dotenv" ,
227+ "run" ,
228+ "--no-override" ,
229+ "python" ,
230+ "-c" ,
231+ "import os; print(os.environ['a'])" ,
232+ ]
251233
252234 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 "
235+ assert result == "c\n "
259236
260237
261238def test_run_with_none_value (tmp_path ):
262239 with pushd (tmp_path ):
263240 (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" ]
241+
242+ printenv_cmd = [
243+ "dotenv" ,
244+ "run" ,
245+ "--no-override" ,
246+ "python" ,
247+ "-c" ,
248+ "import os; print(os.environ['a'])" ,
249+ ]
278250
279251 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 "
252+ assert result == "b\n "
285253
286254
287255def test_run_with_other_env (dotenv_path ):
288256 dotenv_path .write_text ("a=b" )
289257
290- result = run_command (["dotenv" , "--file" , dotenv_path , "run" , "printenv" , "a" ])
258+ printenv_cmd = [
259+ "dotenv" ,
260+ "--file" ,
261+ str (dotenv_path ),
262+ "run" ,
263+ "python" ,
264+ "-c" ,
265+ "import os; print(os.environ['a'])" ,
266+ ]
267+
268+ result = run_command (printenv_cmd )
291269
292270 assert result == "b\n "
293271
0 commit comments