Commit 17e059e3 by rc-mattschwager Committed by GitHub

Relay return code as exit code when running script (#6824)

Co-authored-by: Randy Döring <30527984+radoering@users.noreply.github.com>
parent 4962622e
...@@ -58,7 +58,7 @@ class RunCommand(EnvCommand): ...@@ -58,7 +58,7 @@ class RunCommand(EnvCommand):
"import sys; " "import sys; "
"from importlib import import_module; " "from importlib import import_module; "
f"sys.argv = {args!r}; {src_in_sys_path}" f"sys.argv = {args!r}; {src_in_sys_path}"
f"import_module('{module}').{callable_}()" f"sys.exit(import_module('{module}').{callable_}())"
] ]
return self.env.execute(*cmd) return self.env.execute(*cmd)
from __future__ import annotations from __future__ import annotations
import subprocess
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
import pytest import pytest
...@@ -12,9 +14,12 @@ if TYPE_CHECKING: ...@@ -12,9 +14,12 @@ if TYPE_CHECKING:
from cleo.testers.command_tester import CommandTester from cleo.testers.command_tester import CommandTester
from pytest_mock import MockerFixture from pytest_mock import MockerFixture
from poetry.poetry import Poetry
from poetry.utils.env import MockEnv from poetry.utils.env import MockEnv
from poetry.utils.env import VirtualEnv from poetry.utils.env import VirtualEnv
from tests.types import CommandTesterFactory from tests.types import CommandTesterFactory
from tests.types import FixtureDirGetter
from tests.types import ProjectFactory
@pytest.fixture @pytest.fixture
...@@ -27,6 +32,19 @@ def patches(mocker: MockerFixture, env: MockEnv) -> None: ...@@ -27,6 +32,19 @@ def patches(mocker: MockerFixture, env: MockEnv) -> None:
mocker.patch("poetry.utils.env.EnvManager.get", return_value=env) mocker.patch("poetry.utils.env.EnvManager.get", return_value=env)
@pytest.fixture
def poetry_with_scripts(
project_factory: ProjectFactory, fixture_dir: FixtureDirGetter
) -> Poetry:
source = fixture_dir("scripts")
return project_factory(
name="scripts",
pyproject_content=(source / "pyproject.toml").read_text(encoding="utf-8"),
source=source,
)
def test_run_passes_all_args(app_tester: ApplicationTester, env: MockEnv): def test_run_passes_all_args(app_tester: ApplicationTester, env: MockEnv):
app_tester.execute("run python -V") app_tester.execute("run python -V")
assert [["python", "-V"]] == env.executed assert [["python", "-V"]] == env.executed
...@@ -105,3 +123,26 @@ def test_run_console_scripts_of_editable_dependencies_on_windows( ...@@ -105,3 +123,26 @@ def test_run_console_scripts_of_editable_dependencies_on_windows(
# We prove that the CMD script executed successfully by verifying the exit code # We prove that the CMD script executed successfully by verifying the exit code
# matches what we wrote in the script # matches what we wrote in the script
assert tester.execute("quix") == 123 assert tester.execute("quix") == 123
def test_run_script_exit_code(
poetry_with_scripts: Poetry,
command_tester_factory: CommandTesterFactory,
tmp_venv: VirtualEnv,
mocker: MockerFixture,
) -> None:
mocker.patch(
"os.execvpe",
lambda file, args, env: subprocess.call([file] + args[1:], env=env),
)
install_tester = command_tester_factory(
"install",
poetry=poetry_with_scripts,
environment=tmp_venv,
)
assert install_tester.execute() == 0
tester = command_tester_factory(
"run", poetry=poetry_with_scripts, environment=tmp_venv
)
assert tester.execute("exit-code") == 42
assert tester.execute("return-code") == 42
[tool.poetry]
name = "scripts"
version = "0.1.0"
description = ""
authors = ["Your Name <you@example.com>"]
readme = "README.md"
[tool.poetry.dependencies]
python = "^3.7"
[tool.poetry.scripts]
exit-code = "scripts.exit_code:main"
return-code = "scripts.return_code:main"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
from __future__ import annotations
def main() -> None:
raise SystemExit(42)
if __name__ == "__main__":
main()
from __future__ import annotations
def main() -> int:
return 42
if __name__ == "__main__":
raise SystemExit(main())
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment