Skip to content

Commit 24dfe1d

Browse files
committed
Remove the use of sh in tests
1 parent 09d7cee commit 24dfe1d

File tree

5 files changed

+92
-67
lines changed

5 files changed

+92
-67
lines changed

requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ click
33
ipython
44
pytest-cov
55
pytest>=3.9
6-
sh>=2
76
tox
87
wheel
98
ruff

tests/test_cli.py

Lines changed: 59 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import os
22
import subprocess
3-
import sys
43
from pathlib import Path
54
from typing import Optional, Sequence
65

@@ -10,9 +9,6 @@
109
from dotenv.cli import cli as dotenv_cli
1110
from dotenv.version import __version__
1211

13-
if sys.platform != "win32":
14-
import sh
15-
1612

1713
def invoke_sub(args: Sequence[str]) -> subprocess.CompletedProcess:
1814
"""
@@ -192,67 +188,93 @@ def test_set_no_file(cli):
192188
assert "Missing argument" in result.output
193189

194190

195-
@pytest.mark.skipif(sys.platform == "win32", reason="sh module doesn't support Windows")
196191
def test_get_default_path(tmp_path):
197-
with sh.pushd(tmp_path):
198-
(tmp_path / ".env").write_text("a=b")
192+
(tmp_path / ".env").write_text("A=x")
199193

200-
result = sh.dotenv("get", "a")
194+
result = subprocess.run(
195+
["dotenv", "get", "A"],
196+
capture_output=True,
197+
check=True,
198+
cwd=tmp_path,
199+
text=True,
200+
)
201201

202-
assert result == "b\n"
202+
assert result.stdout == "x\n"
203203

204204

205-
@pytest.mark.skipif(sys.platform == "win32", reason="sh module doesn't support Windows")
206205
def test_run(tmp_path):
207-
with sh.pushd(tmp_path):
208-
(tmp_path / ".env").write_text("a=b")
206+
(tmp_path / ".env").write_text("A=x")
209207

210-
result = sh.dotenv("run", "printenv", "a")
208+
result = subprocess.run(
209+
["dotenv", "run", "printenv", "A"],
210+
capture_output=True,
211+
check=True,
212+
cwd=tmp_path,
213+
text=True,
214+
)
211215

212-
assert result == "b\n"
216+
assert result.stdout == "x\n"
213217

214218

215-
@pytest.mark.skipif(sys.platform == "win32", reason="sh module doesn't support Windows")
216219
def test_run_with_existing_variable(tmp_path):
217-
with sh.pushd(tmp_path):
218-
(tmp_path / ".env").write_text("a=b")
219-
env = dict(os.environ)
220-
env.update({"LANG": "en_US.UTF-8", "a": "c"})
220+
(tmp_path / ".env").write_text("A=x")
221+
env = dict(os.environ)
222+
env.update({"LANG": "en_US.UTF-8", "A": "y"})
221223

222-
result = sh.dotenv("run", "printenv", "a", _env=env)
224+
result = subprocess.run(
225+
["dotenv", "run", "printenv", "A"],
226+
capture_output=True,
227+
check=True,
228+
cwd=tmp_path,
229+
text=True,
230+
env=env,
231+
)
223232

224-
assert result == "b\n"
233+
assert result.stdout == "x\n"
225234

226235

227-
@pytest.mark.skipif(sys.platform == "win32", reason="sh module doesn't support Windows")
228236
def test_run_with_existing_variable_not_overridden(tmp_path):
229-
with sh.pushd(tmp_path):
230-
(tmp_path / ".env").write_text("a=b")
231-
env = dict(os.environ)
232-
env.update({"LANG": "en_US.UTF-8", "a": "c"})
237+
(tmp_path / ".env").write_text("A=x")
238+
env = dict(os.environ)
239+
env.update({"LANG": "en_US.UTF-8", "A": "C"})
233240

234-
result = sh.dotenv("run", "--no-override", "printenv", "a", _env=env)
241+
result = subprocess.run(
242+
["dotenv", "run", "--no-override", "printenv", "A"],
243+
capture_output=True,
244+
check=True,
245+
cwd=tmp_path,
246+
text=True,
247+
env=env,
248+
)
235249

236-
assert result == "c\n"
250+
assert result.stdout == "C\n"
237251

238252

239-
@pytest.mark.skipif(sys.platform == "win32", reason="sh module doesn't support Windows")
240253
def test_run_with_none_value(tmp_path):
241-
with sh.pushd(tmp_path):
242-
(tmp_path / ".env").write_text("a=b\nc")
254+
(tmp_path / ".env").write_text("A=x\nc")
243255

244-
result = sh.dotenv("run", "printenv", "a")
256+
result = subprocess.run(
257+
["dotenv", "run", "printenv", "A"],
258+
capture_output=True,
259+
check=True,
260+
cwd=tmp_path,
261+
text=True,
262+
)
245263

246-
assert result == "b\n"
264+
assert result.stdout == "x\n"
247265

248266

249-
@pytest.mark.skipif(sys.platform == "win32", reason="sh module doesn't support Windows")
250267
def test_run_with_other_env(dotenv_path):
251-
dotenv_path.write_text("a=b")
268+
dotenv_path.write_text("A=x")
252269

253-
result = sh.dotenv("--file", dotenv_path, "run", "printenv", "a")
270+
result = subprocess.run(
271+
["dotenv", "--file", dotenv_path, "run", "printenv", "A"],
272+
capture_output=True,
273+
check=True,
274+
text=True,
275+
)
254276

255-
assert result == "b\n"
277+
assert result.stdout == "x\n"
256278

257279

258280
def test_run_without_cmd(cli):

tests/test_main.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import logging
33
import os
44
import stat
5+
import subprocess
56
import sys
67
import textwrap
78
from unittest import mock
@@ -10,9 +11,6 @@
1011

1112
import dotenv
1213

13-
if sys.platform != "win32":
14-
import sh
15-
1614

1715
def test_set_key_no_file(tmp_path):
1816
nx_path = tmp_path / "nx"
@@ -483,7 +481,6 @@ def test_load_dotenv_file_stream(dotenv_path):
483481
assert os.environ == {"a": "b"}
484482

485483

486-
@pytest.mark.skipif(sys.platform == "win32", reason="sh module doesn't support Windows")
487484
def test_load_dotenv_in_current_dir(tmp_path):
488485
dotenv_path = tmp_path / ".env"
489486
dotenv_path.write_bytes(b"a=b")
@@ -499,9 +496,14 @@ def test_load_dotenv_in_current_dir(tmp_path):
499496
)
500497
os.chdir(tmp_path)
501498

502-
result = sh.Command(sys.executable)(code_path)
499+
result = subprocess.run(
500+
[sys.executable, str(code_path)],
501+
capture_output=True,
502+
text=True,
503+
check=True,
504+
)
503505

504-
assert result == "b\n"
506+
assert result.stdout == "b\n"
505507

506508

507509
def test_dotenv_values_file(dotenv_path):

tests/test_zip_imports.py

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
import os
2+
import posixpath
3+
import subprocess
24
import sys
35
import textwrap
46
from typing import List
57
from unittest import mock
68
from zipfile import ZipFile
79

8-
import pytest
9-
10-
if sys.platform != "win32":
11-
import sh
12-
1310

1411
def walk_to_root(path: str):
1512
last_dir = None
1613
current_dir = path
1714
while last_dir != current_dir:
1815
yield current_dir
19-
(parent_dir, _) = os.path.split(current_dir)
16+
parent_dir = posixpath.dirname(current_dir)
2017
last_dir, current_dir = current_dir, parent_dir
2118

2219

@@ -32,12 +29,11 @@ def setup_zipfile(path, files: List[FileToAdd]):
3229
with ZipFile(zip_file_path, "w") as zipfile:
3330
for f in files:
3431
zipfile.writestr(data=f.content, zinfo_or_arcname=f.path)
35-
for dirname in walk_to_root(os.path.dirname(f.path)):
32+
for dirname in walk_to_root(posixpath.dirname(f.path)):
3633
if dirname not in dirs_init_py_added_to:
37-
print(os.path.join(dirname, "__init__.py"))
38-
zipfile.writestr(
39-
data="", zinfo_or_arcname=os.path.join(dirname, "__init__.py")
40-
)
34+
init_path = posixpath.join(dirname, "__init__.py")
35+
print(f"setup_zipfile: {init_path}")
36+
zipfile.writestr(data="", zinfo_or_arcname=init_path)
4137
dirs_init_py_added_to.add(dirname)
4238
return zip_file_path
4339

@@ -65,7 +61,6 @@ def test_load_dotenv_gracefully_handles_zip_imports_when_no_env_file(tmp_path):
6561
import child1.child2.test # noqa
6662

6763

68-
@pytest.mark.skipif(sys.platform == "win32", reason="sh module doesn't support Windows")
6964
def test_load_dotenv_outside_zip_file_when_called_in_zipfile(tmp_path):
7065
zip_file_path = setup_zipfile(
7166
tmp_path,
@@ -83,24 +78,32 @@ def test_load_dotenv_outside_zip_file_when_called_in_zipfile(tmp_path):
8378
],
8479
)
8580
dotenv_path = tmp_path / ".env"
86-
dotenv_path.write_bytes(b"a=b")
81+
dotenv_path.write_bytes(b"A=x")
8782
code_path = tmp_path / "code.py"
8883
code_path.write_text(
8984
textwrap.dedent(
9085
f"""
91-
import os
92-
import sys
86+
import os
87+
import sys
9388
94-
sys.path.append("{zip_file_path}")
89+
sys.path.append({str(zip_file_path)!r})
9590
96-
import child1.child2.test
91+
import child1.child2.test
9792
98-
print(os.environ['a'])
99-
"""
93+
print(os.environ['A'])
94+
"""
10095
)
10196
)
102-
os.chdir(str(tmp_path))
10397

104-
result = sh.Command(sys.executable)(code_path)
98+
result = subprocess.run(
99+
[sys.executable, str(code_path)],
100+
capture_output=True,
101+
check=True,
102+
cwd=tmp_path,
103+
text=True,
104+
env={
105+
k: v for k, v in os.environ.items() if k.upper() != "A"
106+
}, # env without 'A'
107+
)
105108

106-
assert result == "b\n"
109+
assert result.stdout == "x\n"

tox.ini

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ python =
1515
deps =
1616
pytest
1717
pytest-cov
18-
sh >= 2.0.2, <3
1918
click
2019
py{310,311,312,313,314,314t,pypy3}: ipython
2120
commands = pytest --cov --cov-report=term-missing {posargs}

0 commit comments

Comments
 (0)