From b7961bec5821b25e4e363ea9d7d5456745a5b4b4 Mon Sep 17 00:00:00 2001 From: Ethan Paul <24588726+enpaul@users.noreply.github.com> Date: Mon, 28 Sep 2020 23:24:42 -0400 Subject: [PATCH] Add support for running in a non-poetry project Handle the error poetry raises when not in a poetry managed project and use that as a sign to skip further usage of the plugin in that environment --- tox_poetry_installer.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/tox_poetry_installer.py b/tox_poetry_installer.py index 9d5d713..f89f042 100644 --- a/tox_poetry_installer.py +++ b/tox_poetry_installer.py @@ -266,7 +266,23 @@ def tox_testenv_install_deps(venv: ToxVirtualEnv, action: ToxAction): ) return - poetry = PoetryFactory().create_poetry(venv.envconfig.config.toxinidir) + try: + poetry = PoetryFactory().create_poetry(venv.envconfig.config.toxinidir) + except RuntimeError as err: + # Support running the plugin when the current tox project does not use Poetry for its + # environment/dependency management. + # + # ``RuntimeError`` is dangerous to blindly catch because it can be (and in Poetry's case, + # is) raised in many different places for different purposes. This check of the error + # content, while crude and potentially fragile, will hopefully prevent ``RuntimeError``s + # not caused by this specific condition to be re-raised as genuine errors. This may need + # tuning in the future. + if "[tool.poetry] section not found" in str(err): + reporter.verbosity1( + f"{_REPORTER_PREFIX} project does not use Poetry for env management, skipping installation of locked dependencies" + ) + return + raise err reporter.verbosity1( f"{_REPORTER_PREFIX} loaded project pyproject.toml from {poetry.file}"