Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed version restore behaviour when executing 'poetry run' commands on Linux with poetry >= 1.0.0b2 #6

Merged
merged 1 commit into from
Dec 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions poetry_dynamic_versioning/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ def __init__(
self,
patched_poetry_console_main: bool = False,
patched_poetry_create: bool = False,
patched_poetry_command_run: bool = False,
original_version: str = None,
version: Tuple[Version, str] = None,
) -> None:
self.patched_poetry_console_main = patched_poetry_console_main
self.patched_poetry_create = patched_poetry_create
self.patched_poetry_command_run = patched_poetry_command_run
self.original_version = original_version
self.version = version
self.original_import_func = builtins.__import__
Expand Down Expand Up @@ -142,6 +144,26 @@ def alt_poetry_create(cls, *args, **kwargs):
sys.modules["poetry.poetry"] = poetry_factory_module


def _patch_poetry_command_run() -> None:
poetry_command_run_module = _state.original_import_func(
"poetry.console.commands.run", fromlist=["RunCommand"]
)
original_poetry_command_run = poetry_command_run_module.RunCommand.handle

@functools.wraps(original_poetry_command_run)
def alt_poetry_command_run(self, *args, **kwargs):
# As of version 1.0.0b2, on Linux, the `poetry run` command
# uses `os.execvp` function instead of spawning a new process.
# This prevents the atexit `deactivte` hook to be invoked.
# For this reason, we immediately call `deactivate` before
# actually executing the run command.
deactivate()
mtkennerly marked this conversation as resolved.
Show resolved Hide resolved
return original_poetry_command_run(self, *args, **kwargs)

poetry_command_run_module.RunCommand.handle = alt_poetry_command_run
sys.modules["poetry.console.commands.run"] = poetry_command_run_module


def _patch_poetry_console_main(module, name, fromlist):
if name == "poetry.console" and fromlist:
console = module
Expand All @@ -161,6 +183,11 @@ def alt_poetry_console_main(*args, **kwargs):
if not _state.patched_poetry_create:
_patch_poetry_create()
_state.patched_poetry_create = True

if not _state.patched_poetry_command_run:
_patch_poetry_command_run()
_state.patched_poetry_command_run = True

original_console_main(*args, **kwargs)

console.main = alt_poetry_console_main
Expand Down
7 changes: 7 additions & 0 deletions tests/integration.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ function test_keep_pyproject_modifications {
cat $dummy/pyproject.toml | grep $package
}

function test_poetry_run {
$do_poetry run echo "Testing 'poetry run'"
# Make sure original version number is restored
# also when executing a 'poetry run' command
cat $dummy/pyproject.toml | grep "version = \"0.0.999\""
}

function run_test {
cd $dummy
git checkout -- $dummy
Expand Down