Skip to content

Commit 17e059e

Browse files
Relay return code as exit code when running script (#6824)
Co-authored-by: Randy Döring <[email protected]>
1 parent 4962622 commit 17e059e

File tree

7 files changed

+77
-1
lines changed

7 files changed

+77
-1
lines changed

src/poetry/console/commands/run.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def run_script(self, script: str | dict[str, str], args: str) -> int:
5858
"import sys; "
5959
"from importlib import import_module; "
6060
f"sys.argv = {args!r}; {src_in_sys_path}"
61-
f"import_module('{module}').{callable_}()"
61+
f"sys.exit(import_module('{module}').{callable_}())"
6262
]
6363

6464
return self.env.execute(*cmd)

tests/console/commands/test_run.py

+41
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import annotations
22

3+
import subprocess
4+
35
from typing import TYPE_CHECKING
46

57
import pytest
@@ -12,9 +14,12 @@
1214
from cleo.testers.command_tester import CommandTester
1315
from pytest_mock import MockerFixture
1416

17+
from poetry.poetry import Poetry
1518
from poetry.utils.env import MockEnv
1619
from poetry.utils.env import VirtualEnv
1720
from tests.types import CommandTesterFactory
21+
from tests.types import FixtureDirGetter
22+
from tests.types import ProjectFactory
1823

1924

2025
@pytest.fixture
@@ -27,6 +32,19 @@ def patches(mocker: MockerFixture, env: MockEnv) -> None:
2732
mocker.patch("poetry.utils.env.EnvManager.get", return_value=env)
2833

2934

35+
@pytest.fixture
36+
def poetry_with_scripts(
37+
project_factory: ProjectFactory, fixture_dir: FixtureDirGetter
38+
) -> Poetry:
39+
source = fixture_dir("scripts")
40+
41+
return project_factory(
42+
name="scripts",
43+
pyproject_content=(source / "pyproject.toml").read_text(encoding="utf-8"),
44+
source=source,
45+
)
46+
47+
3048
def test_run_passes_all_args(app_tester: ApplicationTester, env: MockEnv):
3149
app_tester.execute("run python -V")
3250
assert [["python", "-V"]] == env.executed
@@ -105,3 +123,26 @@ def test_run_console_scripts_of_editable_dependencies_on_windows(
105123
# We prove that the CMD script executed successfully by verifying the exit code
106124
# matches what we wrote in the script
107125
assert tester.execute("quix") == 123
126+
127+
128+
def test_run_script_exit_code(
129+
poetry_with_scripts: Poetry,
130+
command_tester_factory: CommandTesterFactory,
131+
tmp_venv: VirtualEnv,
132+
mocker: MockerFixture,
133+
) -> None:
134+
mocker.patch(
135+
"os.execvpe",
136+
lambda file, args, env: subprocess.call([file] + args[1:], env=env),
137+
)
138+
install_tester = command_tester_factory(
139+
"install",
140+
poetry=poetry_with_scripts,
141+
environment=tmp_venv,
142+
)
143+
assert install_tester.execute() == 0
144+
tester = command_tester_factory(
145+
"run", poetry=poetry_with_scripts, environment=tmp_venv
146+
)
147+
assert tester.execute("exit-code") == 42
148+
assert tester.execute("return-code") == 42

tests/fixtures/scripts/README.md

Whitespace-only changes.

tests/fixtures/scripts/pyproject.toml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[tool.poetry]
2+
name = "scripts"
3+
version = "0.1.0"
4+
description = ""
5+
authors = ["Your Name <[email protected]>"]
6+
readme = "README.md"
7+
8+
[tool.poetry.dependencies]
9+
python = "^3.7"
10+
11+
[tool.poetry.scripts]
12+
exit-code = "scripts.exit_code:main"
13+
return-code = "scripts.return_code:main"
14+
15+
[build-system]
16+
requires = ["poetry-core"]
17+
build-backend = "poetry.core.masonry.api"

tests/fixtures/scripts/scripts/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from __future__ import annotations
2+
3+
4+
def main() -> None:
5+
raise SystemExit(42)
6+
7+
8+
if __name__ == "__main__":
9+
main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from __future__ import annotations
2+
3+
4+
def main() -> int:
5+
return 42
6+
7+
8+
if __name__ == "__main__":
9+
raise SystemExit(main())

0 commit comments

Comments
 (0)