Skip to content

Commit

Permalink
Merge pull request #20 from pdm-project/fix/activate-pdm-plugin
Browse files Browse the repository at this point in the history
Do not activate pdm runner when not a pdm project
  • Loading branch information
frostming authored Feb 19, 2022
2 parents 792790d + 80dc928 commit 238f11e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
9 changes: 9 additions & 0 deletions tox_pdm/plugin3.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from .utils import (
clone_pdm_files,
detect_pdm_files,
get_env_lib_path,
inject_pdm_to_commands,
set_default_kwargs,
Expand Down Expand Up @@ -95,6 +96,8 @@ def acquire_package(config: config.Config, venv: VirtualEnv) -> py.path.local:
@hookimpl
def tox_package(session: session.Session, venv: VirtualEnv) -> Any:
clone_pdm_files(str(venv.path), str(venv.envconfig.config.toxinidir))
if not detect_pdm_files(venv.path):
return
if not hasattr(session, "package"):
session.package, session.dist = get_package(session, venv)
# Patch the install command to install to local __pypackages__ folder
Expand All @@ -109,6 +112,8 @@ def tox_package(session: session.Session, venv: VirtualEnv) -> Any:

@hookimpl
def tox_testenv_install_deps(venv: VirtualEnv, action: action.Action) -> Any:
if not detect_pdm_files(venv.path):
return
groups = venv.envconfig.groups or []
if not venv.envconfig.skip_install or groups:
action.setactivity("pdminstall", groups)
Expand Down Expand Up @@ -148,6 +153,8 @@ def tox_testenv_install_deps(venv: VirtualEnv, action: action.Action) -> Any:

@hookimpl
def tox_runtest_pre(venv: VirtualEnv) -> Any:
if not detect_pdm_files(venv.path):
return
inject_pdm_to_commands(
venv.envconfig.config.option.pdm, venv.path, venv.envconfig.commands_pre
)
Expand All @@ -161,6 +168,8 @@ def tox_runtest_pre(venv: VirtualEnv) -> Any:

@hookimpl
def tox_runenvreport(venv: VirtualEnv, action: action.Action):
if not detect_pdm_files(venv.path):
return
command = venv.envconfig.list_dependencies_command
for i, arg in enumerate(command):
if arg == "python":
Expand Down
2 changes: 1 addition & 1 deletion tox_pdm/plugin4.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def tox_add_option(parser: ArgumentParser) -> None:
def tox_register_tox_env(register: ToxEnvRegister) -> t.Optional[bool]:
register.add_run_env(PdmRunner)
register.add_package_env(PdmPackageEnv)
register.default_run_env = "pdm"
register.default_env_runner = "pdm"


class Pdm(Python):
Expand Down
12 changes: 12 additions & 0 deletions tox_pdm/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ def clone_pdm_files(env_path: str | Path, root: str | Path) -> None:
if not env_path.exists():
env_path.mkdir(parents=True)
root = Path(root)
if not root.joinpath("pyproject.toml").exists():
return
old_pyproject = toml.load(root.joinpath("pyproject.toml").open("r"))
if "name" in old_pyproject.get("project", {}):
del old_pyproject["project"]["name"]
Expand All @@ -28,6 +30,16 @@ def clone_pdm_files(env_path: str | Path, root: str | Path) -> None:
shutil.copy2(root.joinpath("pdm.lock"), env_path.joinpath("pdm.lock"))


def detect_pdm_files(root: str | Path) -> bool:
root = Path(root)
pyproject = root.joinpath("pyproject.toml")
if not pyproject.exists():
return False
with pyproject.open("r") as f:
toml_data = toml.load(f)
return "project" in toml_data


def set_default_kwargs(func_or_method, **kwargs):
"""Change the default value for keyword arguments."""
func = getattr(func_or_method, "__func__", func_or_method)
Expand Down

0 comments on commit 238f11e

Please sign in to comment.