Skip to content

Commit 849d3f4

Browse files
BUG: fix running a script with explicitly empty dependencies = [] (#1658)
* TST: add regression tests for running a script with implicitly or explicitly empty dependencies * BUG: fix running a script with explicitly empty `dependencies = []`
1 parent 33f37fc commit 849d3f4

File tree

3 files changed

+48
-18
lines changed

3 files changed

+48
-18
lines changed

changelog.d/1658.bugfix.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix running a script with explicitly empty ``dependencies = []``.

src/pipx/commands/run.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def run_script(
8080
use_cache: bool,
8181
) -> NoReturn:
8282
requirements = _get_requirements_from_script(content)
83-
if requirements is None:
83+
if not requirements:
8484
python_path = Path(python)
8585
else:
8686
# Note that the environment name is based on the identified

tests/test_run.py

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -193,29 +193,58 @@ def test_run_without_requirements(caplog, pipx_temp_env, tmp_path):
193193

194194

195195
@mock.patch("os.execvpe", new=execvpe_mock)
196-
def test_run_with_requirements(caplog, pipx_temp_env, tmp_path):
196+
@pytest.mark.parametrize(
197+
"script_text, expected_output",
198+
[
199+
pytest.param(
200+
"""
201+
# /// script
202+
# dependencies = []
203+
# ///
204+
from pathlib import Path
205+
Path({out!r}).write_text("explicit-empty")
206+
""",
207+
"explicit-empty",
208+
id="explicit-empty-dependencies",
209+
),
210+
pytest.param(
211+
"""
212+
# /// script
213+
# ///
214+
from pathlib import Path
215+
Path({out!r}).write_text("implicit-empty")
216+
""",
217+
"implicit-empty",
218+
id="implicit-empty-dependencies",
219+
),
220+
pytest.param(
221+
"""
222+
# /// script
223+
# dependencies = ["requests==2.31.0"]
224+
# ///
225+
226+
# Check requests can be imported
227+
import requests
228+
# Check dependencies of requests can be imported
229+
import certifi
230+
# Check the installed version
231+
from pathlib import Path
232+
Path({out!r}).write_text(requests.__version__)
233+
""",
234+
"2.31.0",
235+
id="non-empty-dependencies",
236+
),
237+
],
238+
)
239+
def test_run_with_requirements(script_text, expected_output, caplog, pipx_temp_env, tmp_path):
197240
script = tmp_path / "test.py"
198241
out = tmp_path / "output.txt"
199242
script.write_text(
200-
textwrap.dedent(
201-
f"""
202-
# /// script
203-
# dependencies = ["requests==2.31.0"]
204-
# ///
205-
206-
# Check requests can be imported
207-
import requests
208-
# Check dependencies of requests can be imported
209-
import certifi
210-
# Check the installed version
211-
from pathlib import Path
212-
Path({str(out)!r}).write_text(requests.__version__)
213-
"""
214-
).strip(),
243+
textwrap.dedent(script_text.format(out=str(out))).strip(),
215244
encoding="utf-8",
216245
)
217246
run_pipx_cli_exit(["run", script.as_uri()])
218-
assert out.read_text() == "2.31.0"
247+
assert out.read_text() == expected_output
219248

220249

221250
@mock.patch("os.execvpe", new=execvpe_mock)

0 commit comments

Comments
 (0)