Skip to content

Commit 64de2f9

Browse files
committed
Remove the use of sh in tests
1 parent 09d7cee commit 64de2f9

File tree

3 files changed

+77
-58
lines changed

3 files changed

+77
-58
lines changed

.github/workflows/test.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ name: Run Tests
22

33
on:
44
push:
5-
branches:
6-
- main
75
pull_request:
86

97
concurrency:

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=B")
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 == "B\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=B")
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 == "B\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=B")
221+
env = dict(os.environ)
222+
env.update({"LANG": "en_US.UTF-8", "A": "C"})
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 == "B\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=B")
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=B\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 == "B\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=B")
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 == "B\n"
256278

257279

258280
def test_run_without_cmd(cli):

tests/test_zip_imports.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
1-
import os
1+
import posixpath
2+
import subprocess
23
import sys
34
import textwrap
45
from typing import List
56
from unittest import mock
67
from zipfile import ZipFile
78

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

1410
def walk_to_root(path: str):
1511
last_dir = None
1612
current_dir = path
1713
while last_dir != current_dir:
1814
yield current_dir
19-
(parent_dir, _) = os.path.split(current_dir)
15+
parent_dir = posixpath.dirname(current_dir)
2016
last_dir, current_dir = current_dir, parent_dir
2117

2218

@@ -32,12 +28,11 @@ def setup_zipfile(path, files: List[FileToAdd]):
3228
with ZipFile(zip_file_path, "w") as zipfile:
3329
for f in files:
3430
zipfile.writestr(data=f.content, zinfo_or_arcname=f.path)
35-
for dirname in walk_to_root(os.path.dirname(f.path)):
31+
for dirname in walk_to_root(posixpath.dirname(f.path)):
3632
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-
)
33+
init_path = posixpath.join(dirname, "__init__.py")
34+
print(f"setup_zipfile: {init_path}")
35+
zipfile.writestr(data="", zinfo_or_arcname=init_path)
4136
dirs_init_py_added_to.add(dirname)
4237
return zip_file_path
4338

@@ -65,7 +60,6 @@ def test_load_dotenv_gracefully_handles_zip_imports_when_no_env_file(tmp_path):
6560
import child1.child2.test # noqa
6661

6762

68-
@pytest.mark.skipif(sys.platform == "win32", reason="sh module doesn't support Windows")
6963
def test_load_dotenv_outside_zip_file_when_called_in_zipfile(tmp_path):
7064
zip_file_path = setup_zipfile(
7165
tmp_path,
@@ -83,24 +77,29 @@ def test_load_dotenv_outside_zip_file_when_called_in_zipfile(tmp_path):
8377
],
8478
)
8579
dotenv_path = tmp_path / ".env"
86-
dotenv_path.write_bytes(b"a=b")
80+
dotenv_path.write_bytes(b"A=B")
8781
code_path = tmp_path / "code.py"
8882
code_path.write_text(
8983
textwrap.dedent(
9084
f"""
9185
import os
9286
import sys
9387
94-
sys.path.append("{zip_file_path}")
88+
sys.path.append({str(zip_file_path)!r})
9589
9690
import child1.child2.test
9791
98-
print(os.environ['a'])
92+
print(os.environ['A'])
9993
"""
10094
)
10195
)
102-
os.chdir(str(tmp_path))
10396

104-
result = sh.Command(sys.executable)(code_path)
97+
result = subprocess.run(
98+
[sys.executable, str(code_path)],
99+
capture_output=True,
100+
check=True,
101+
cwd=tmp_path,
102+
text=True,
103+
)
105104

106-
assert result == "b\n"
105+
assert result.stdout == "B\n"

0 commit comments

Comments
 (0)